|
# 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) |
|
|