Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,25 +1,31 @@
|
|
1 |
-
import
|
2 |
import torch
|
3 |
import librosa
|
4 |
from transformers import WhisperProcessor, WhisperForConditionalGeneration
|
|
|
5 |
|
6 |
-
|
|
|
|
|
7 |
processor = WhisperProcessor.from_pretrained(model_id)
|
8 |
model = WhisperForConditionalGeneration.from_pretrained(model_id)
|
9 |
|
10 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
11 |
model.to(device)
|
12 |
|
13 |
-
# Force Hebrew transcription
|
14 |
forced_decoder_ids = processor.get_decoder_prompt_ids(language="he", task="transcribe")
|
15 |
|
16 |
-
def transcribe_audio(
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
20 |
|
21 |
-
|
22 |
-
chunk_size = sr *
|
23 |
chunks = [waveform[i:i + chunk_size] for i in range(0, len(waveform), chunk_size)]
|
24 |
|
25 |
partial_text = ""
|
@@ -30,23 +36,24 @@ def transcribe_audio(audio_file):
|
|
30 |
with torch.no_grad():
|
31 |
predicted_ids = model.generate(
|
32 |
input_features,
|
33 |
-
max_new_tokens=444,
|
34 |
forced_decoder_ids=forced_decoder_ids
|
35 |
)
|
36 |
|
37 |
-
|
38 |
-
partial_text +=
|
39 |
-
|
40 |
-
return partial_text # no yield, just final result
|
41 |
|
42 |
-
|
43 |
-
gr.Markdown("## Exceedea Transcription")
|
44 |
|
45 |
-
|
46 |
-
|
|
|
|
|
|
|
|
|
47 |
|
48 |
-
|
49 |
|
50 |
-
|
51 |
|
52 |
-
|
|
|
|
1 |
+
import requests
|
2 |
import torch
|
3 |
import librosa
|
4 |
from transformers import WhisperProcessor, WhisperForConditionalGeneration
|
5 |
+
from flask import Flask, request, jsonify
|
6 |
|
7 |
+
app = Flask(__name__)
|
8 |
+
|
9 |
+
model_id = "openai/whisper-large-v3"
|
10 |
processor = WhisperProcessor.from_pretrained(model_id)
|
11 |
model = WhisperForConditionalGeneration.from_pretrained(model_id)
|
12 |
|
13 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
14 |
model.to(device)
|
15 |
|
|
|
16 |
forced_decoder_ids = processor.get_decoder_prompt_ids(language="he", task="transcribe")
|
17 |
|
18 |
+
def transcribe_audio(audio_url):
|
19 |
+
response = requests.get(audio_url)
|
20 |
+
with open("temp_audio.wav", "wb") as f:
|
21 |
+
f.write(response.content)
|
22 |
+
|
23 |
+
waveform, sr = librosa.load("temp_audio.wav", sr=16000)
|
24 |
+
max_duration_sec = 3600
|
25 |
+
waveform = waveform[:sr * max_duration_sec]
|
26 |
|
27 |
+
chunk_duration_sec = 25
|
28 |
+
chunk_size = sr * chunk_duration_sec
|
29 |
chunks = [waveform[i:i + chunk_size] for i in range(0, len(waveform), chunk_size)]
|
30 |
|
31 |
partial_text = ""
|
|
|
36 |
with torch.no_grad():
|
37 |
predicted_ids = model.generate(
|
38 |
input_features,
|
|
|
39 |
forced_decoder_ids=forced_decoder_ids
|
40 |
)
|
41 |
|
42 |
+
transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)[0]
|
43 |
+
partial_text += transcription + "\n"
|
|
|
|
|
44 |
|
45 |
+
return partial_text.strip()
|
|
|
46 |
|
47 |
+
@app.route('/transcribe', methods=['POST'])
|
48 |
+
def transcribe_endpoint():
|
49 |
+
data = request.get_json()
|
50 |
+
audio_url = data.get('audio_url')
|
51 |
+
if not audio_url:
|
52 |
+
return jsonify({"error": "Missing 'audio_url' in request"}), 400
|
53 |
|
54 |
+
transcription = transcribe_audio(audio_url)
|
55 |
|
56 |
+
return jsonify({"transcription": transcription})
|
57 |
|
58 |
+
if __name__ == '__main__':
|
59 |
+
app.run(host="0.0.0.0", port=8080)
|