EladSpamson commited on
Commit
84b9b91
·
verified ·
1 Parent(s): 6d007f8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -39
app.py CHANGED
@@ -2,8 +2,12 @@ from flask import Flask, request, jsonify
2
  import requests
3
  import torch
4
  import librosa
 
5
  from transformers import WhisperProcessor, WhisperForConditionalGeneration
6
 
 
 
 
7
  app = Flask(__name__)
8
 
9
  model_id = "openai/whisper-base"
@@ -15,42 +19,4 @@ 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
- audio_path = "/tmp/temp_audio.wav"
21
- with open(audio_path, "wb") as f:
22
- f.write(response.content)
23
-
24
- waveform, sr = librosa.load(audio_path, sr=16000)
25
- max_duration_sec = 3600
26
- waveform = waveform[:sr * max_duration_sec]
27
-
28
- chunk_duration_sec = 25
29
- chunk_size = sr * chunk_duration_sec
30
- chunks = [waveform[i:i + chunk_size] for i in range(0, len(waveform), chunk_size)]
31
-
32
- partial_text = ""
33
- for chunk in chunks:
34
- inputs = processor(chunk, sampling_rate=16000, return_tensors="pt", padding=True)
35
- input_features = inputs.input_features.to(device)
36
-
37
- with torch.no_grad():
38
- predicted_ids = model.generate(input_features, forced_decoder_ids=forced_decoder_ids)
39
-
40
- transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)[0]
41
- partial_text += transcription + "\n"
42
-
43
- return partial_text.strip()
44
-
45
- @app.route('/transcribe', methods=['POST'])
46
- def transcribe_endpoint():
47
- data = request.get_json()
48
- audio_url = data.get('audio_url')
49
- if not audio_url:
50
- return jsonify({"error": "Missing 'audio_url' in request"}), 400
51
-
52
- transcription = transcribe_audio(audio_url)
53
- return jsonify({"transcription": transcription})
54
-
55
- if __name__ == '__main__':
56
- app.run(host="0.0.0.0", port=7860)
 
2
  import requests
3
  import torch
4
  import librosa
5
+ import os
6
  from transformers import WhisperProcessor, WhisperForConditionalGeneration
7
 
8
+ # Set Hugging Face cache directory explicitly
9
+ os.environ["HF_HOME"] = "/tmp/hf_cache"
10
+
11
  app = Flask(__name__)
12
 
13
  model_id = "openai/whisper-base"
 
19
 
20
  forced_decoder_ids = processor.get_decoder_prompt_ids(language="he", task="transcribe")
21
 
22
+ # rest of the code remains unchanged...