|
|
|
import os |
|
import tempfile |
|
from flask import request, jsonify |
|
from transformers import pipeline |
|
import torch |
|
import traceback |
|
|
|
|
|
|
|
cache_dir = os.environ.get("HF_HOME", "/tmp/.cache") |
|
os.makedirs(cache_dir, exist_ok=True) |
|
|
|
|
|
print("Loading openai/whisper-tiny model via transformers pipeline...") |
|
|
|
|
|
device = "cuda:0" if torch.cuda.is_available() else "cpu" |
|
|
|
|
|
model = pipeline( |
|
"automatic-speech-recognition", |
|
model="openai/whisper-tiny", |
|
device=device, |
|
model_kwargs={"cache_dir": cache_dir} |
|
) |
|
|
|
print("Whisper model loaded.") |
|
|
|
def handle_transcribe(): |
|
try: |
|
|
|
if 'audio' not in request.files: |
|
print("Error: 'audio' key not in request.files") |
|
return jsonify({'error': 'No audio file part in the request'}), 400 |
|
|
|
file = request.files['audio'] |
|
|
|
if file.filename == '': |
|
print("Error: No selected file") |
|
return jsonify({'error': 'No selected file'}), 400 |
|
|
|
|
|
with tempfile.NamedTemporaryFile(delete=True, suffix=".webm") as temp_audio: |
|
file.save(temp_audio.name) |
|
|
|
print(f"Transcribing file: {temp_audio.name} with openai/whisper-tiny pipeline for Hindi.") |
|
|
|
|
|
|
|
result = model( |
|
temp_audio.name, |
|
generate_kwargs={"language": "hindi", "task": "transcribe"} |
|
) |
|
|
|
transcribed_text = result.get('text', '') |
|
|
|
print("Transcription successful.") |
|
return jsonify({'text': transcribed_text}) |
|
|
|
except Exception as e: |
|
|
|
print("❌ Error in handle_transcribe():") |
|
traceback.print_exc() |
|
return jsonify({'error': f"An unexpected error occurred during transcription: {str(e)}"}), 500 |
|
|