norhan12 commited on
Commit
384c17b
·
verified ·
1 Parent(s): aecc9b7

Update process_interview.py

Browse files
Files changed (1) hide show
  1. process_interview.py +37 -9
process_interview.py CHANGED
@@ -177,16 +177,31 @@ def extract_prosodic_features(audio_path: str, start_ms: int, end_ms: int) -> Di
177
  }
178
 
179
 
 
 
 
 
 
 
 
180
  def transcribe(audio_path: str) -> Dict:
181
  try:
 
182
  with open(audio_path, 'rb') as f:
183
  upload_response = requests.post(
184
  "https://api.assemblyai.com/v2/upload",
185
  headers={"authorization": ASSEMBLYAI_KEY},
186
  data=f
187
  )
188
- audio_url = upload_response.json()['upload_url']
 
 
 
 
 
 
189
 
 
190
  transcript_response = requests.post(
191
  "https://api.assemblyai.com/v2/transcript",
192
  headers={"authorization": ASSEMBLYAI_KEY},
@@ -196,25 +211,38 @@ def transcribe(audio_path: str) -> Dict:
196
  "filter_profanity": True
197
  }
198
  )
199
- transcript_id = transcript_response.json()['id']
200
 
 
 
 
 
 
 
 
 
 
 
201
  while True:
202
- result = requests.get(
203
  f"https://api.assemblyai.com/v2/transcript/{transcript_id}",
204
  headers={"authorization": ASSEMBLYAI_KEY}
205
- ).json()
206
-
 
 
 
 
 
207
  if result['status'] == 'completed':
208
  return result
209
  elif result['status'] == 'error':
210
- raise Exception(result['error'])
211
 
212
  time.sleep(5)
 
213
  except Exception as e:
214
  logger.error(f"Transcription failed: {str(e)}")
215
- raise
216
-
217
-
218
  def process_utterance(utterance, full_audio, wav_file):
219
  try:
220
  start = utterance['start']
 
177
  }
178
 
179
 
180
+ import requests
181
+ import time
182
+ import logging
183
+ from typing import Dict
184
+
185
+ logger = logging.getLogger(__name__)
186
+
187
  def transcribe(audio_path: str) -> Dict:
188
  try:
189
+ # 1. رفع الملف
190
  with open(audio_path, 'rb') as f:
191
  upload_response = requests.post(
192
  "https://api.assemblyai.com/v2/upload",
193
  headers={"authorization": ASSEMBLYAI_KEY},
194
  data=f
195
  )
196
+
197
+ if upload_response.status_code != 200:
198
+ raise Exception(f"فشل رفع الملف: {upload_response.status_code} - {upload_response.text}")
199
+
200
+ audio_url = upload_response.json().get('upload_url')
201
+ if not audio_url:
202
+ raise Exception(f"لم يتم الحصول على رابط الملف: {upload_response.json()}")
203
 
204
+ # 2. بدء التحويل (Transcription)
205
  transcript_response = requests.post(
206
  "https://api.assemblyai.com/v2/transcript",
207
  headers={"authorization": ASSEMBLYAI_KEY},
 
211
  "filter_profanity": True
212
  }
213
  )
 
214
 
215
+ if transcript_response.status_code != 200:
216
+ raise Exception(f"فشل طلب التحويل: {transcript_response.status_code} - {transcript_response.text}")
217
+
218
+ resp_json = transcript_response.json()
219
+ transcript_id = resp_json.get('id')
220
+
221
+ if not transcript_id:
222
+ raise Exception(f"AssemblyAI لم ترجع ID. الرد كان: {resp_json}")
223
+
224
+ # 3. متابعة الحالة
225
  while True:
226
+ result_response = requests.get(
227
  f"https://api.assemblyai.com/v2/transcript/{transcript_id}",
228
  headers={"authorization": ASSEMBLYAI_KEY}
229
+ )
230
+
231
+ if result_response.status_code != 200:
232
+ raise Exception(f"خطأ أثناء جلب النتيجة: {result_response.status_code} - {result_response.text}")
233
+
234
+ result = result_response.json()
235
+
236
  if result['status'] == 'completed':
237
  return result
238
  elif result['status'] == 'error':
239
+ raise Exception(f"خطأ من الخدمة: {result.get('error', 'غير معروف')}")
240
 
241
  time.sleep(5)
242
+
243
  except Exception as e:
244
  logger.error(f"Transcription failed: {str(e)}")
245
+ raise e
 
 
246
  def process_utterance(utterance, full_audio, wav_file):
247
  try:
248
  start = utterance['start']