EladSpamson commited on
Commit
697c7eb
·
verified ·
1 Parent(s): 965be1d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -17
app.py CHANGED
@@ -9,7 +9,7 @@ from flask import Flask, request, jsonify
9
  from transformers import WhisperProcessor, WhisperForConditionalGeneration
10
 
11
  ###############################################################################
12
- # 1) Configure environment to avoid permission issues & set up model
13
  ###############################################################################
14
  os.environ["HF_HOME"] = "/tmp/hf_cache"
15
  os.environ["TRANSFORMERS_CACHE"] = "/tmp/hf_cache"
@@ -18,7 +18,6 @@ os.environ["XDG_CACHE_HOME"] = "/tmp"
18
 
19
  app = Flask(__name__)
20
 
21
- # Example: your custom Hebrew model
22
  model_id = "ivrit-ai/whisper-large-v3-turbo"
23
  processor = WhisperProcessor.from_pretrained(model_id)
24
  model = WhisperForConditionalGeneration.from_pretrained(model_id)
@@ -35,26 +34,26 @@ WEBHOOK_URL = "https://hook.eu1.make.com/86zogci73u394k2uqpulp5yjjwgm8b9x"
35
  ###############################################################################
36
  # 2) Background transcription function
37
  ###############################################################################
38
- def transcribe_in_background(audio_url):
39
  """
40
  Called by a background thread. Downloads & transcribes audio,
41
  then sends results to your Make.com webhook.
42
  """
43
  try:
44
- # Download audio
45
  r = requests.get(audio_url)
46
  audio_path = "/tmp/temp_audio.wav"
47
  with open(audio_path, "wb") as f:
48
  f.write(r.content)
49
 
50
- # Load with librosa
51
  waveform, sr = librosa.load(audio_path, sr=16000)
52
 
53
- # Optional limit ~1 hour
54
  max_sec = 3600
55
  waveform = waveform[: sr * max_sec]
56
 
57
- # Split audio into 25-second chunks
58
  chunk_sec = 25
59
  chunk_size = sr * chunk_sec
60
  chunks = [waveform[i : i + chunk_size] for i in range(0, len(waveform), chunk_size)]
@@ -73,39 +72,56 @@ def transcribe_in_background(audio_url):
73
  transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)[0]
74
  partial_text += transcription + "\n"
75
 
76
- # Post final transcription back to Make.com
77
- payload = {"Transcription": partial_text.strip()}
 
 
 
 
 
78
  requests.post(WEBHOOK_URL, json=payload)
79
 
80
  except Exception as e:
81
  # In case of errors, notify the webhook
82
- error_payload = {"error": str(e)}
 
 
 
 
 
83
  requests.post(WEBHOOK_URL, json=error_payload)
84
 
85
-
86
  ###############################################################################
87
- # 3) Flask route: returns immediately, does the heavy lifting in a thread
88
  ###############################################################################
89
  @app.route("/transcribe", methods=["POST"])
90
  def transcribe_endpoint():
 
91
  data = request.get_json()
92
  audio_url = data.get("audio_url")
93
  if not audio_url:
94
  return jsonify({"error": "Missing 'audio_url' in request"}), 400
95
 
96
- # Spawn a thread to handle transcription & webhook
97
- thread = threading.Thread(target=transcribe_in_background, args=(audio_url,))
 
 
 
 
 
 
 
 
98
  thread.start()
99
 
100
- # Immediately return a JSON response to Make.com
101
  return jsonify({
102
  "status": "Received. Transcription in progress.",
103
  "note": "Results will be sent via webhook once done."
104
  }), 202
105
 
106
-
107
  ###############################################################################
108
- # 4) Run app if local, else Hugging Face will use gunicorn.
109
  ###############################################################################
110
  if __name__ == "__main__":
111
  app.run(host="0.0.0.0", port=7860)
 
9
  from transformers import WhisperProcessor, WhisperForConditionalGeneration
10
 
11
  ###############################################################################
12
+ # 1) Configure environment & set up model
13
  ###############################################################################
14
  os.environ["HF_HOME"] = "/tmp/hf_cache"
15
  os.environ["TRANSFORMERS_CACHE"] = "/tmp/hf_cache"
 
18
 
19
  app = Flask(__name__)
20
 
 
21
  model_id = "ivrit-ai/whisper-large-v3-turbo"
22
  processor = WhisperProcessor.from_pretrained(model_id)
23
  model = WhisperForConditionalGeneration.from_pretrained(model_id)
 
34
  ###############################################################################
35
  # 2) Background transcription function
36
  ###############################################################################
37
+ def transcribe_in_background(audio_url, file_id, company, user):
38
  """
39
  Called by a background thread. Downloads & transcribes audio,
40
  then sends results to your Make.com webhook.
41
  """
42
  try:
43
+ # 1) Download the audio
44
  r = requests.get(audio_url)
45
  audio_path = "/tmp/temp_audio.wav"
46
  with open(audio_path, "wb") as f:
47
  f.write(r.content)
48
 
49
+ # 2) Load with librosa
50
  waveform, sr = librosa.load(audio_path, sr=16000)
51
 
52
+ # Optional: limit ~1 hour
53
  max_sec = 3600
54
  waveform = waveform[: sr * max_sec]
55
 
56
+ # 3) Split audio into 25-second chunks
57
  chunk_sec = 25
58
  chunk_size = sr * chunk_sec
59
  chunks = [waveform[i : i + chunk_size] for i in range(0, len(waveform), chunk_size)]
 
72
  transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)[0]
73
  partial_text += transcription + "\n"
74
 
75
+ # 4) Post final transcription back to Make.com, including the extra fields
76
+ payload = {
77
+ "Transcription": partial_text.strip(),
78
+ "fileId": file_id,
79
+ "company": company,
80
+ "user": user
81
+ }
82
  requests.post(WEBHOOK_URL, json=payload)
83
 
84
  except Exception as e:
85
  # In case of errors, notify the webhook
86
+ error_payload = {
87
+ "error": str(e),
88
+ "fileId": file_id,
89
+ "company": company,
90
+ "user": user
91
+ }
92
  requests.post(WEBHOOK_URL, json=error_payload)
93
 
 
94
  ###############################################################################
95
+ # 3) Flask route: returns immediately, transcribes in a separate thread
96
  ###############################################################################
97
  @app.route("/transcribe", methods=["POST"])
98
  def transcribe_endpoint():
99
+ # 1) Get JSON data from request
100
  data = request.get_json()
101
  audio_url = data.get("audio_url")
102
  if not audio_url:
103
  return jsonify({"error": "Missing 'audio_url' in request"}), 400
104
 
105
+ # 2) Read custom headers (fileId, company, user)
106
+ file_id = request.headers.get("fileId", "")
107
+ company = request.headers.get("company", "")
108
+ user = request.headers.get("user", "")
109
+
110
+ # 3) Spawn a thread to handle transcription
111
+ thread = threading.Thread(
112
+ target=transcribe_in_background,
113
+ args=(audio_url, file_id, company, user)
114
+ )
115
  thread.start()
116
 
117
+ # 4) Immediately return a JSON response
118
  return jsonify({
119
  "status": "Received. Transcription in progress.",
120
  "note": "Results will be sent via webhook once done."
121
  }), 202
122
 
 
123
  ###############################################################################
124
+ # 4) Run app if local; on HF Spaces, gunicorn is used
125
  ###############################################################################
126
  if __name__ == "__main__":
127
  app.run(host="0.0.0.0", port=7860)