Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -4,7 +4,6 @@ import requests
|
|
4 |
import threading
|
5 |
import torch
|
6 |
import librosa
|
7 |
-
#import psutil # Not needed for concurrency gating, only for CPU usage checks
|
8 |
|
9 |
from flask import Flask, request, jsonify
|
10 |
from transformers import WhisperProcessor, WhisperForConditionalGeneration
|
@@ -26,6 +25,7 @@ forced_decoder_ids = processor.get_decoder_prompt_ids(language="he", task="trans
|
|
26 |
|
27 |
WEBHOOK_URL = "https://hook.eu1.make.com/86zogci73u394k2uqpulp5yjjwgm8b9x"
|
28 |
|
|
|
29 |
def transcribe_in_background(audio_url, file_id, company, user, file_name):
|
30 |
global concurrent_requests
|
31 |
try:
|
@@ -35,14 +35,14 @@ def transcribe_in_background(audio_url, file_id, company, user, file_name):
|
|
35 |
with open(audio_path, "wb") as f:
|
36 |
f.write(r.content)
|
37 |
|
38 |
-
# Load & limit to 1 hour
|
39 |
waveform, sr = librosa.load(audio_path, sr=16000)
|
40 |
max_sec = 3600
|
41 |
waveform = waveform[: sr * max_sec]
|
42 |
|
43 |
call_duration = int(len(waveform) / sr)
|
44 |
|
45 |
-
#
|
46 |
chunk_sec = 25
|
47 |
chunk_size = sr * chunk_sec
|
48 |
chunks = [waveform[i : i + chunk_size] for i in range(0, len(waveform), chunk_size)]
|
@@ -53,11 +53,14 @@ def transcribe_in_background(audio_url, file_id, company, user, file_name):
|
|
53 |
input_features = inputs.input_features.to(device)
|
54 |
|
55 |
with torch.no_grad():
|
56 |
-
predicted_ids = model.generate(
|
|
|
|
|
|
|
57 |
transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)[0]
|
58 |
partial_text += transcription + "\n"
|
59 |
|
60 |
-
#
|
61 |
payload = {
|
62 |
"Transcription": partial_text.strip(),
|
63 |
"callDuration": call_duration,
|
@@ -79,31 +82,35 @@ def transcribe_in_background(audio_url, file_id, company, user, file_name):
|
|
79 |
requests.post(WEBHOOK_URL, json=error_payload)
|
80 |
|
81 |
finally:
|
82 |
-
# Decrement concurrency
|
83 |
with concurrent_requests_lock:
|
84 |
concurrent_requests -= 1
|
85 |
|
|
|
86 |
@app.route("/transcribe", methods=["POST"])
|
87 |
def transcribe_endpoint():
|
88 |
global concurrent_requests
|
89 |
|
90 |
-
#
|
91 |
with concurrent_requests_lock:
|
92 |
if concurrent_requests >= 1:
|
93 |
-
# Return
|
94 |
-
return jsonify({
|
|
|
|
|
|
|
|
|
95 |
concurrent_requests += 1
|
96 |
|
97 |
-
# Parse JSON
|
98 |
data = request.get_json()
|
99 |
audio_url = data.get("audio_url")
|
100 |
if not audio_url:
|
101 |
-
#
|
102 |
with concurrent_requests_lock:
|
103 |
concurrent_requests -= 1
|
104 |
return jsonify({"error": "Missing 'audio_url' in request"}), 400
|
105 |
|
106 |
-
# Read
|
107 |
file_id = request.headers.get("fileId", "")
|
108 |
company = request.headers.get("company", "")
|
109 |
user = request.headers.get("user", "")
|
@@ -116,11 +123,11 @@ def transcribe_endpoint():
|
|
116 |
)
|
117 |
thread.start()
|
118 |
|
119 |
-
# Return immediately
|
120 |
return jsonify({
|
121 |
"status": "Received. Transcription in progress.",
|
122 |
"note": "Results will be sent via webhook once done."
|
123 |
}), 202
|
124 |
|
|
|
125 |
if __name__ == "__main__":
|
126 |
app.run(host="0.0.0.0", port=7860)
|
|
|
4 |
import threading
|
5 |
import torch
|
6 |
import librosa
|
|
|
7 |
|
8 |
from flask import Flask, request, jsonify
|
9 |
from transformers import WhisperProcessor, WhisperForConditionalGeneration
|
|
|
25 |
|
26 |
WEBHOOK_URL = "https://hook.eu1.make.com/86zogci73u394k2uqpulp5yjjwgm8b9x"
|
27 |
|
28 |
+
|
29 |
def transcribe_in_background(audio_url, file_id, company, user, file_name):
|
30 |
global concurrent_requests
|
31 |
try:
|
|
|
35 |
with open(audio_path, "wb") as f:
|
36 |
f.write(r.content)
|
37 |
|
38 |
+
# Load audio & limit to 1 hour
|
39 |
waveform, sr = librosa.load(audio_path, sr=16000)
|
40 |
max_sec = 3600
|
41 |
waveform = waveform[: sr * max_sec]
|
42 |
|
43 |
call_duration = int(len(waveform) / sr)
|
44 |
|
45 |
+
# Transcribe in 25-second chunks
|
46 |
chunk_sec = 25
|
47 |
chunk_size = sr * chunk_sec
|
48 |
chunks = [waveform[i : i + chunk_size] for i in range(0, len(waveform), chunk_size)]
|
|
|
53 |
input_features = inputs.input_features.to(device)
|
54 |
|
55 |
with torch.no_grad():
|
56 |
+
predicted_ids = model.generate(
|
57 |
+
input_features,
|
58 |
+
forced_decoder_ids=forced_decoder_ids
|
59 |
+
)
|
60 |
transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)[0]
|
61 |
partial_text += transcription + "\n"
|
62 |
|
63 |
+
# Send result to webhook
|
64 |
payload = {
|
65 |
"Transcription": partial_text.strip(),
|
66 |
"callDuration": call_duration,
|
|
|
82 |
requests.post(WEBHOOK_URL, json=error_payload)
|
83 |
|
84 |
finally:
|
85 |
+
# Decrement concurrency count
|
86 |
with concurrent_requests_lock:
|
87 |
concurrent_requests -= 1
|
88 |
|
89 |
+
|
90 |
@app.route("/transcribe", methods=["POST"])
|
91 |
def transcribe_endpoint():
|
92 |
global concurrent_requests
|
93 |
|
94 |
+
# We only allow ONE job at a time:
|
95 |
with concurrent_requests_lock:
|
96 |
if concurrent_requests >= 1:
|
97 |
+
# Return a 200 (OK) and a JSON message
|
98 |
+
return jsonify({
|
99 |
+
"message": "Server is already processing another job, please try again later."
|
100 |
+
}), 200
|
101 |
+
|
102 |
+
# If it's free, occupy the slot
|
103 |
concurrent_requests += 1
|
104 |
|
|
|
105 |
data = request.get_json()
|
106 |
audio_url = data.get("audio_url")
|
107 |
if not audio_url:
|
108 |
+
# If missing the audio_url, free the slot we claimed
|
109 |
with concurrent_requests_lock:
|
110 |
concurrent_requests -= 1
|
111 |
return jsonify({"error": "Missing 'audio_url' in request"}), 400
|
112 |
|
113 |
+
# Read headers
|
114 |
file_id = request.headers.get("fileId", "")
|
115 |
company = request.headers.get("company", "")
|
116 |
user = request.headers.get("user", "")
|
|
|
123 |
)
|
124 |
thread.start()
|
125 |
|
|
|
126 |
return jsonify({
|
127 |
"status": "Received. Transcription in progress.",
|
128 |
"note": "Results will be sent via webhook once done."
|
129 |
}), 202
|
130 |
|
131 |
+
|
132 |
if __name__ == "__main__":
|
133 |
app.run(host="0.0.0.0", port=7860)
|