Elalimy commited on
Commit
d3ebe2a
·
verified ·
1 Parent(s): 994336e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -13
app.py CHANGED
@@ -4,6 +4,7 @@ import requests
4
  from moviepy.editor import VideoFileClip
5
  from pydub import AudioSegment
6
  from pydub.utils import make_chunks
 
7
 
8
  app = Flask(__name__)
9
 
@@ -52,19 +53,29 @@ def extract_audio(video_path):
52
  raise RuntimeError(f"Error extracting audio: {e}")
53
  return audio_path
54
 
55
- def transcribe_audio_chunk(audio_chunk_path):
56
- try:
57
- with open(audio_chunk_path, "rb") as audio_chunk:
58
- headers = {"Authorization": f"Bearer {API_TOKEN}"}
59
- response = requests.post(API_URL, headers=headers, files={"file": audio_chunk})
60
-
61
- if response.status_code != 200:
62
- raise RuntimeError(f"Error during transcription: {response.text}")
63
-
64
- result = response.json()
65
- return result.get("text", "")
66
- except Exception as e:
67
- raise RuntimeError(f"Error during transcription: {e}")
 
 
 
 
 
 
 
 
 
 
68
 
69
  def split_and_transcribe_audio(audio_path):
70
  if not os.path.exists(audio_path):
 
4
  from moviepy.editor import VideoFileClip
5
  from pydub import AudioSegment
6
  from pydub.utils import make_chunks
7
+ import time
8
 
9
  app = Flask(__name__)
10
 
 
53
  raise RuntimeError(f"Error extracting audio: {e}")
54
  return audio_path
55
 
56
+ def transcribe_audio_chunk(audio_chunk_path, retries=5, wait_time=60):
57
+ headers = {"Authorization": f"Bearer {API_TOKEN}"}
58
+
59
+ for attempt in range(retries):
60
+ try:
61
+ with open(audio_chunk_path, "rb") as audio_chunk:
62
+ response = requests.post(API_URL, headers=headers, files={"file": audio_chunk})
63
+
64
+ if response.status_code == 200:
65
+ result = response.json()
66
+ return result.get("text", "")
67
+ elif response.status_code == 503 and 'estimated_time' in response.json():
68
+ # If the model is loading, wait and retry
69
+ wait = response.json()['estimated_time']
70
+ time.sleep(wait)
71
+ else:
72
+ response.raise_for_status()
73
+ except Exception as e:
74
+ if attempt == retries - 1:
75
+ raise RuntimeError(f"Error during transcription after {retries} attempts: {e}")
76
+ else:
77
+ time.sleep(wait_time)
78
+ return ""
79
 
80
  def split_and_transcribe_audio(audio_path):
81
  if not os.path.exists(audio_path):