SoSa123456
commited on
Commit
•
c080f5c
1
Parent(s):
d3012a5
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
|
3 |
+
from flask import Flask, render_template, request
|
4 |
+
from speechbrain.pretrained import WhisperASR
|
5 |
+
|
6 |
+
app = Flask(__name__)
|
7 |
+
|
8 |
+
asr_model = WhisperASR.from_hparams(
|
9 |
+
source="speechbrain/asr-whisper-large-v2-commonvoice-fa",
|
10 |
+
savedir="pretrained_models/asr-whisper-large-v2-commonvoice-fa"
|
11 |
+
)
|
12 |
+
|
13 |
+
html_template = """
|
14 |
+
<!DOCTYPE html>
|
15 |
+
<html lang="en">
|
16 |
+
<head>
|
17 |
+
<meta charset="UTF-8">
|
18 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
19 |
+
<title>Speech Transcription</title>
|
20 |
+
</head>
|
21 |
+
<body>
|
22 |
+
<h1>Speech Transcription</h1>
|
23 |
+
<form action="/transcribe" method="post" enctype="multipart/form-data">
|
24 |
+
<label for="audio_file">Select an audio file:</label>
|
25 |
+
<input type="file" name="audio_file" accept=".wav, .mp3">
|
26 |
+
<button type="submit">Transcribe</button>
|
27 |
+
</form>
|
28 |
+
|
29 |
+
{% if error %}
|
30 |
+
<p style="color: red;">{{ error }}</p>
|
31 |
+
{% endif %}
|
32 |
+
|
33 |
+
{% if transcription %}
|
34 |
+
<h2>Transcription Result:</h2>
|
35 |
+
<p>{{ transcription }}</p>
|
36 |
+
{% endif %}
|
37 |
+
</body>
|
38 |
+
</html>
|
39 |
+
"""
|
40 |
+
|
41 |
+
@app.route('/')
|
42 |
+
def index():
|
43 |
+
return html_template
|
44 |
+
|
45 |
+
@app.route('/transcribe', methods=['POST'])
|
46 |
+
def transcribe():
|
47 |
+
if 'audio_file' not in request.files:
|
48 |
+
return render_template_string(html_template, error='No file part')
|
49 |
+
|
50 |
+
audio_file = request.files['audio_file']
|
51 |
+
|
52 |
+
if audio_file.filename == '':
|
53 |
+
return render_template_string(html_template, error='No selected file')
|
54 |
+
|
55 |
+
try:
|
56 |
+
transcription = asr_model.transcribe_file(audio_file)
|
57 |
+
return render_template_string(html_template, transcription=transcription)
|
58 |
+
|
59 |
+
except Exception as e:
|
60 |
+
return render_template_string(html_template, error=f'Error during transcription: {str(e)}')
|
61 |
+
|
62 |
+
if __name__ == '__main__':
|
63 |
+
app.run(debug=True)
|