Update app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,9 @@ from faster_whisper import WhisperModel
|
|
3 |
import torch
|
4 |
import io
|
5 |
import time
|
|
|
|
|
|
|
6 |
|
7 |
app = Flask(__name__)
|
8 |
|
@@ -15,31 +18,91 @@ print(f"Using device: {device} with compute_type: {compute_type}")
|
|
15 |
beamsize = 2
|
16 |
wmodel = WhisperModel("guillaumekln/faster-whisper-small", device=device, compute_type=compute_type)
|
17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
@app.route("/whisper_transcribe", methods=["POST"])
|
19 |
def whisper_transcribe():
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
if __name__ == "__main__":
|
45 |
app.run(host="0.0.0.0", debug=True, port=7860, threaded=True)
|
|
|
3 |
import torch
|
4 |
import io
|
5 |
import time
|
6 |
+
from threading import Lock
|
7 |
+
from queue import Queue
|
8 |
+
import datetime
|
9 |
|
10 |
app = Flask(__name__)
|
11 |
|
|
|
18 |
beamsize = 2
|
19 |
wmodel = WhisperModel("guillaumekln/faster-whisper-small", device=device, compute_type=compute_type)
|
20 |
|
21 |
+
# Server status tracking
|
22 |
+
active_requests = 0
|
23 |
+
request_queue = Queue()
|
24 |
+
status_lock = Lock()
|
25 |
+
MAX_CONCURRENT_REQUESTS = 2 # Adjust based on your server capacity
|
26 |
+
|
27 |
+
@app.route("/health", methods=["GET"])
|
28 |
+
def health_check():
|
29 |
+
"""Endpoint to check if API is running"""
|
30 |
+
return jsonify({
|
31 |
+
'status': 'API is running',
|
32 |
+
'timestamp': datetime.datetime.now().isoformat(),
|
33 |
+
'device': device,
|
34 |
+
'compute_type': compute_type
|
35 |
+
})
|
36 |
+
|
37 |
+
@app.route("/status/busy", methods=["GET"])
|
38 |
+
def server_busy():
|
39 |
+
"""Endpoint to check if server is busy"""
|
40 |
+
with status_lock:
|
41 |
+
is_busy = active_requests >= MAX_CONCURRENT_REQUESTS
|
42 |
+
return jsonify({
|
43 |
+
'is_busy': is_busy,
|
44 |
+
'active_requests': active_requests,
|
45 |
+
'max_capacity': MAX_CONCURRENT_REQUESTS,
|
46 |
+
'queue_size': request_queue.qsize()
|
47 |
+
})
|
48 |
+
|
49 |
+
@app.route("/status/queue", methods=["GET"])
|
50 |
+
def queue_status():
|
51 |
+
"""Endpoint to get current queue size"""
|
52 |
+
return jsonify({
|
53 |
+
'queue_size': request_queue.qsize(),
|
54 |
+
'active_requests': active_requests
|
55 |
+
})
|
56 |
+
|
57 |
@app.route("/whisper_transcribe", methods=["POST"])
|
58 |
def whisper_transcribe():
|
59 |
+
global active_requests
|
60 |
+
|
61 |
+
# Check if server is at capacity
|
62 |
+
with status_lock:
|
63 |
+
if active_requests >= MAX_CONCURRENT_REQUESTS:
|
64 |
+
request_queue.put(datetime.datetime.now())
|
65 |
+
return jsonify({
|
66 |
+
'status': 'Server busy',
|
67 |
+
'message': f'Currently processing {active_requests} requests',
|
68 |
+
'queue_position': request_queue.qsize()
|
69 |
+
}), 503
|
70 |
+
|
71 |
+
active_requests += 1
|
72 |
+
|
73 |
+
try:
|
74 |
+
if 'audio' not in request.files:
|
75 |
+
return jsonify({'error': 'No file provided'}), 400
|
76 |
|
77 |
+
audio_file = request.files['audio']
|
78 |
+
allowed_extensions = {'mp3', 'wav', 'ogg', 'm4a'}
|
79 |
+
if not (audio_file and audio_file.filename.lower().split('.')[-1] in allowed_extensions):
|
80 |
+
return jsonify({'error': 'Invalid file format'}), 400
|
81 |
|
82 |
+
print(f"Transcribing audio on {device} (Active requests: {active_requests})")
|
83 |
+
audio_bytes = audio_file.read()
|
84 |
+
audio_file = io.BytesIO(audio_bytes)
|
85 |
|
86 |
+
try:
|
87 |
+
segments, info = wmodel.transcribe(audio_file, beam_size=beamsize)
|
88 |
+
text = ''
|
89 |
+
starttime = time.time()
|
90 |
+
for segment in segments:
|
91 |
+
text += segment.text
|
92 |
+
print(f"Time to transcribe: {time.time() - starttime} seconds")
|
93 |
+
return jsonify({'transcription': text})
|
94 |
+
except Exception as e:
|
95 |
+
print(f"Transcription error: {str(e)}")
|
96 |
+
return jsonify({'error': 'Transcription failed'}), 500
|
97 |
+
finally:
|
98 |
+
with status_lock:
|
99 |
+
active_requests -= 1
|
100 |
+
# Remove oldest queued request if any
|
101 |
+
if not request_queue.empty():
|
102 |
+
try:
|
103 |
+
request_queue.get_nowait()
|
104 |
+
except:
|
105 |
+
pass
|
106 |
|
107 |
if __name__ == "__main__":
|
108 |
app.run(host="0.0.0.0", debug=True, port=7860, threaded=True)
|