MrSimple07 commited on
Commit
3f00908
·
verified ·
1 Parent(s): ac4fda2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -13
app.py CHANGED
@@ -10,17 +10,27 @@ ELEVENLABS_API_KEY = os.environ.get("ELEVENLABS_API_KEY", None)
10
 
11
  def download_video_from_url(url):
12
  try:
13
- filename = f"downloaded_video_{uuid.uuid4().hex[:8]}.mp4"
 
14
  ydl_opts = {
15
  'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/mp4',
16
  'outtmpl': filename,
17
  'quiet': True,
 
 
 
 
 
18
  }
19
 
20
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
21
  ydl.download([url])
22
 
23
- return filename, None
 
 
 
 
24
  except Exception as e:
25
  return None, f"Error downloading video: {str(e)}"
26
 
@@ -32,7 +42,8 @@ def extract_audio(video_path, output_format="mp3"):
32
 
33
  try:
34
  video = VideoFileClip(video_path)
35
- video.audio.write_audiofile(output_path, verbose=False, logger=None)
 
36
  video.close()
37
  return output_path, f"Audio extracted successfully"
38
  except Exception as e:
@@ -42,6 +53,7 @@ def save_transcription(transcription):
42
  if "error" in transcription:
43
  return None, transcription["error"]
44
 
 
45
  transcript_filename = f"transcription_{uuid.uuid4().hex[:8]}.txt"
46
 
47
  try:
@@ -68,11 +80,16 @@ def process_video_file(video_file, output_format, api_key, model_id):
68
  def process_video_url(video_url, output_format, api_key, model_id):
69
  if not video_url.strip():
70
  return None, "Please enter a video URL", None, "No URL provided"
 
 
71
  video_path, error = download_video_from_url(video_url)
72
  if error:
73
  return None, error, None, "Video download failed, cannot transcribe"
74
 
 
75
  audio_path, message = extract_audio(video_path, output_format)
 
 
76
  if video_path and os.path.exists(video_path):
77
  try:
78
  os.remove(video_path)
@@ -94,26 +111,24 @@ def transcribe_audio(audio_file, api_key, model_id="scribe_v1"):
94
  headers = {
95
  "xi-api-key": api_key
96
  }
97
- files = {
98
- "file": open(audio_file, "rb"),
99
- "model_id": (None, model_id)
100
- }
101
 
102
  try:
103
- response = requests.post(url, headers=headers, files=files)
104
- response.raise_for_status()
105
- result = response.json()
 
 
 
 
 
106
  return result
107
  except requests.exceptions.RequestException as e:
108
  return {"error": f"API request failed: {str(e)}"}
109
  except json.JSONDecodeError:
110
  return {"error": "Failed to parse API response"}
111
- finally:
112
- files["file"].close()
113
 
114
  with gr.Blocks(title="Video to Audio to Transcription") as app:
115
  gr.Markdown("# Video => Audio => Transcription")
116
- gr.Markdown("Upload a video or provide a URL to extract audio and transcribe it using ElevenLabs' API.")
117
 
118
  api_key = gr.Textbox(
119
  placeholder="Enter your ElevenLabs API key",
 
10
 
11
  def download_video_from_url(url):
12
  try:
13
+ filename = f"downloaded_video_{uuid.uuid4().hex[:8]}.mp4"
14
+
15
  ydl_opts = {
16
  'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/mp4',
17
  'outtmpl': filename,
18
  'quiet': True,
19
+ 'no_warnings': True,
20
+ 'ignoreerrors': True,
21
+ 'cookiesfrombrowser': ('chrome',),
22
+ 'nocheckcertificate': True,
23
+ 'age_limit': 99,
24
  }
25
 
26
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
27
  ydl.download([url])
28
 
29
+ if os.path.exists(filename) and os.path.getsize(filename) > 0:
30
+ return filename, None
31
+ else:
32
+ return None, "Failed to download video. Try providing a direct video URL instead of YouTube."
33
+
34
  except Exception as e:
35
  return None, f"Error downloading video: {str(e)}"
36
 
 
42
 
43
  try:
44
  video = VideoFileClip(video_path)
45
+ # Remove the verbose parameter that's causing issues
46
+ video.audio.write_audiofile(output_path, logger=None)
47
  video.close()
48
  return output_path, f"Audio extracted successfully"
49
  except Exception as e:
 
53
  if "error" in transcription:
54
  return None, transcription["error"]
55
 
56
+ # Create a filename for the transcription
57
  transcript_filename = f"transcription_{uuid.uuid4().hex[:8]}.txt"
58
 
59
  try:
 
80
  def process_video_url(video_url, output_format, api_key, model_id):
81
  if not video_url.strip():
82
  return None, "Please enter a video URL", None, "No URL provided"
83
+
84
+ # Download the video
85
  video_path, error = download_video_from_url(video_url)
86
  if error:
87
  return None, error, None, "Video download failed, cannot transcribe"
88
 
89
+ # Extract audio from the downloaded video
90
  audio_path, message = extract_audio(video_path, output_format)
91
+
92
+ # Clean up the downloaded video
93
  if video_path and os.path.exists(video_path):
94
  try:
95
  os.remove(video_path)
 
111
  headers = {
112
  "xi-api-key": api_key
113
  }
 
 
 
 
114
 
115
  try:
116
+ with open(audio_file, "rb") as f:
117
+ files = {
118
+ "file": f,
119
+ "model_id": (None, model_id)
120
+ }
121
+ response = requests.post(url, headers=headers, files=files)
122
+ response.raise_for_status()
123
+ result = response.json()
124
  return result
125
  except requests.exceptions.RequestException as e:
126
  return {"error": f"API request failed: {str(e)}"}
127
  except json.JSONDecodeError:
128
  return {"error": "Failed to parse API response"}
 
 
129
 
130
  with gr.Blocks(title="Video to Audio to Transcription") as app:
131
  gr.Markdown("# Video => Audio => Transcription")
 
132
 
133
  api_key = gr.Textbox(
134
  placeholder="Enter your ElevenLabs API key",