Delete whisper_server.py
Browse files- whisper_server.py +0 -61
whisper_server.py
DELETED
|
@@ -1,61 +0,0 @@
|
|
| 1 |
-
|
| 2 |
-
import os
|
| 3 |
-
import tempfile
|
| 4 |
-
from flask import request, jsonify
|
| 5 |
-
from transformers import pipeline
|
| 6 |
-
import torch
|
| 7 |
-
|
| 8 |
-
# Define a writable directory for the model cache
|
| 9 |
-
cache_dir = os.path.join(os.getenv("XDG_CACHE_HOME", "/tmp/.cache"), "huggingface_models")
|
| 10 |
-
os.makedirs(cache_dir, exist_ok=True)
|
| 11 |
-
|
| 12 |
-
print("Loading collabora/whisper-tiny-hindi model via transformers pipeline...")
|
| 13 |
-
|
| 14 |
-
# Determine device
|
| 15 |
-
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
| 16 |
-
|
| 17 |
-
# Initialize the ASR pipeline with the specified model
|
| 18 |
-
# Using the transformers pipeline is the correct way to load custom models from the Hub.
|
| 19 |
-
model = pipeline(
|
| 20 |
-
"automatic-speech-recognition",
|
| 21 |
-
model="collabora/whisper-tiny-hindi",
|
| 22 |
-
device=device,
|
| 23 |
-
model_kwargs={"cache_dir": cache_dir}
|
| 24 |
-
)
|
| 25 |
-
|
| 26 |
-
print("Whisper model loaded.")
|
| 27 |
-
|
| 28 |
-
def handle_transcribe():
|
| 29 |
-
if 'file' not in request.files:
|
| 30 |
-
return jsonify({'error': 'No file part in the request'}), 400
|
| 31 |
-
|
| 32 |
-
file = request.files['file']
|
| 33 |
-
|
| 34 |
-
if file.filename == '':
|
| 35 |
-
return jsonify({'error': 'No selected file'}), 400
|
| 36 |
-
|
| 37 |
-
if file:
|
| 38 |
-
# Use a temporary file to save the upload
|
| 39 |
-
with tempfile.NamedTemporaryFile(delete=True, suffix=".webm") as temp_audio:
|
| 40 |
-
file.save(temp_audio.name)
|
| 41 |
-
|
| 42 |
-
try:
|
| 43 |
-
print(f"Transcribing file: {temp_audio.name} with collabora/whisper-tiny-hindi pipeline")
|
| 44 |
-
|
| 45 |
-
# The pipeline expects a file path and handles the processing.
|
| 46 |
-
result = model(temp_audio.name)
|
| 47 |
-
|
| 48 |
-
transcribed_text = result.get('text', '')
|
| 49 |
-
|
| 50 |
-
print("Transcription successful.")
|
| 51 |
-
return jsonify({'text': transcribed_text})
|
| 52 |
-
except Exception as e:
|
| 53 |
-
print(f"Error during transcription: {e}")
|
| 54 |
-
# Provide a more specific error if possible
|
| 55 |
-
error_message = f"An unexpected error occurred during transcription: {str(e)}"
|
| 56 |
-
if "out of memory" in str(e).lower():
|
| 57 |
-
error_message = "The model ran out of memory. Please try a smaller audio file or check server resources."
|
| 58 |
-
|
| 59 |
-
return jsonify({'error': error_message}), 500
|
| 60 |
-
|
| 61 |
-
return jsonify({'error': 'File processing failed'}), 500
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|