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