Upload whisper_server (3).py
Browse files- whisper_server (3).py +65 -0
whisper_server (3).py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import os
|
| 3 |
+
import tempfile
|
| 4 |
+
from flask import request, jsonify
|
| 5 |
+
from transformers import pipeline
|
| 6 |
+
import torch
|
| 7 |
+
import traceback
|
| 8 |
+
|
| 9 |
+
# Define a writable directory for the model cache.
|
| 10 |
+
# This now respects the HF_HOME environment variable set in the Dockerfile.
|
| 11 |
+
cache_dir = os.environ.get("HF_HOME", "/tmp/.cache")
|
| 12 |
+
os.makedirs(cache_dir, exist_ok=True)
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
print("Loading openai/whisper-tiny model via transformers pipeline...")
|
| 16 |
+
|
| 17 |
+
# Determine device
|
| 18 |
+
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
| 19 |
+
|
| 20 |
+
# Initialize the ASR pipeline with the lightweight model
|
| 21 |
+
model = pipeline(
|
| 22 |
+
"automatic-speech-recognition",
|
| 23 |
+
model="openai/whisper-tiny",
|
| 24 |
+
device=device,
|
| 25 |
+
model_kwargs={"cache_dir": cache_dir}
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
print("Whisper model loaded.")
|
| 29 |
+
|
| 30 |
+
def handle_transcribe():
|
| 31 |
+
try:
|
| 32 |
+
# Step 1: Validate request - looking for 'audio' key from frontend
|
| 33 |
+
if 'audio' not in request.files:
|
| 34 |
+
print("Error: 'audio' key not in request.files")
|
| 35 |
+
return jsonify({'error': 'No audio file part in the request'}), 400
|
| 36 |
+
|
| 37 |
+
file = request.files['audio']
|
| 38 |
+
|
| 39 |
+
if file.filename == '':
|
| 40 |
+
print("Error: No selected file")
|
| 41 |
+
return jsonify({'error': 'No selected file'}), 400
|
| 42 |
+
|
| 43 |
+
# Step 2: Use a temporary file to save the upload
|
| 44 |
+
with tempfile.NamedTemporaryFile(delete=True, suffix=".webm") as temp_audio:
|
| 45 |
+
file.save(temp_audio.name)
|
| 46 |
+
|
| 47 |
+
print(f"Transcribing file: {temp_audio.name} with openai/whisper-tiny pipeline for Hindi.")
|
| 48 |
+
|
| 49 |
+
# Step 3: Transcribe using the pipeline with language-specific configuration
|
| 50 |
+
# This tells Whisper to process the audio as Hindi.
|
| 51 |
+
result = model(
|
| 52 |
+
temp_audio.name,
|
| 53 |
+
generate_kwargs={"language": "hindi", "task": "transcribe"}
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
transcribed_text = result.get('text', '')
|
| 57 |
+
|
| 58 |
+
print("Transcription successful.")
|
| 59 |
+
return jsonify({'text': transcribed_text})
|
| 60 |
+
|
| 61 |
+
except Exception as e:
|
| 62 |
+
# Step 4: Robust error logging
|
| 63 |
+
print("❌ Error in handle_transcribe():")
|
| 64 |
+
traceback.print_exc()
|
| 65 |
+
return jsonify({'error': f"An unexpected error occurred during transcription: {str(e)}"}), 500
|