File size: 1,832 Bytes
d445a93
c080f5c
d445a93
c080f5c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d445a93
c080f5c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d445a93
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# app_combined.py

from flask import Flask, render_template, request, render_template_string
from speechbrain.pretrained import WhisperASR

app = Flask(__name__)

asr_model = WhisperASR.from_hparams(
    source="speechbrain/asr-whisper-large-v2-commonvoice-fa",
    savedir="pretrained_models/asr-whisper-large-v2-commonvoice-fa"
)

html_template = """
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Speech Transcription</title>
</head>
<body>
    <h1>Speech Transcription</h1>
    <form action="/transcribe" method="post" enctype="multipart/form-data">
        <label for="audio_file">Select an audio file:</label>
        <input type="file" name="audio_file" accept=".wav, .mp3">
        <button type="submit">Transcribe</button>
    </form>

    {% if error %}
        <p style="color: red;">{{ error }}</p>
    {% endif %}

    {% if transcription %}
        <h2>Transcription Result:</h2>
        <p>{{ transcription }}</p>
    {% endif %}
</body>
</html>
"""

@app.route('/')
def index():
    return render_template_string(html_template)

@app.route('/transcribe', methods=['POST'])
def transcribe():
    if 'audio_file' not in request.files:
        return render_template_string(html_template, error='No file part')

    audio_file = request.files['audio_file']

    if audio_file.filename == '':
        return render_template_string(html_template, error='No selected file')

    try:
        transcription = asr_model.transcribe_file(audio_file)
        return render_template_string(html_template, transcription=transcription)

    except Exception as e:
        return render_template_string(html_template, error=f'Error during transcription: {str(e)}')

if __name__ == '__main__':
    app.run(debug=True)