from flask import Flask, render_template, request 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 = """
{{ error }}
{% endif %} {% if transcription %}{{ transcription }}
{% endif %} """ @app.route('/') def index(): return 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)