Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -15,7 +15,8 @@ def download_video(url):
|
|
15 |
stream = yt.streams.filter(progressive=True, file_extension='mp4').first()
|
16 |
if not stream:
|
17 |
raise ValueError("No suitable video stream found.")
|
18 |
-
|
|
|
19 |
else:
|
20 |
# For direct MP4 links, download file
|
21 |
import requests
|
@@ -25,11 +26,13 @@ def download_video(url):
|
|
25 |
with open(local_filename, 'wb') as f:
|
26 |
for chunk in r.iter_content(chunk_size=8192):
|
27 |
f.write(chunk)
|
28 |
-
|
|
|
29 |
except Exception as e:
|
30 |
raise RuntimeError(f"Failed to download video: {e}")
|
31 |
|
32 |
|
|
|
33 |
def extract_audio(video_path):
|
34 |
clip = VideoFileClip(video_path)
|
35 |
audio_path = "temp_audio.wav"
|
@@ -51,16 +54,20 @@ def classify_accent(audio_path):
|
|
51 |
return predicted_accent, f"{confidence:.2f}%"
|
52 |
|
53 |
def process_video(url):
|
|
|
|
|
54 |
try:
|
55 |
video_path = download_video(url)
|
56 |
audio_path = extract_audio(video_path)
|
57 |
accent, confidence = classify_accent(audio_path)
|
|
|
|
|
|
|
58 |
finally:
|
59 |
-
# Cleanup temp files if they exist
|
60 |
for f in [video_path, audio_path]:
|
61 |
-
if os.path.exists(f):
|
62 |
os.remove(f)
|
63 |
-
|
64 |
|
65 |
# Gradio interface
|
66 |
iface = gr.Interface(
|
|
|
15 |
stream = yt.streams.filter(progressive=True, file_extension='mp4').first()
|
16 |
if not stream:
|
17 |
raise ValueError("No suitable video stream found.")
|
18 |
+
video_path = stream.download() # Store the download path
|
19 |
+
return video_path
|
20 |
else:
|
21 |
# For direct MP4 links, download file
|
22 |
import requests
|
|
|
26 |
with open(local_filename, 'wb') as f:
|
27 |
for chunk in r.iter_content(chunk_size=8192):
|
28 |
f.write(chunk)
|
29 |
+
video_path = local_filename # Store the download path
|
30 |
+
return video_path
|
31 |
except Exception as e:
|
32 |
raise RuntimeError(f"Failed to download video: {e}")
|
33 |
|
34 |
|
35 |
+
|
36 |
def extract_audio(video_path):
|
37 |
clip = VideoFileClip(video_path)
|
38 |
audio_path = "temp_audio.wav"
|
|
|
54 |
return predicted_accent, f"{confidence:.2f}%"
|
55 |
|
56 |
def process_video(url):
|
57 |
+
video_path = None
|
58 |
+
audio_path = None
|
59 |
try:
|
60 |
video_path = download_video(url)
|
61 |
audio_path = extract_audio(video_path)
|
62 |
accent, confidence = classify_accent(audio_path)
|
63 |
+
return accent, confidence
|
64 |
+
except Exception as e:
|
65 |
+
return f"Error: {e}", ""
|
66 |
finally:
|
|
|
67 |
for f in [video_path, audio_path]:
|
68 |
+
if f and os.path.exists(f):
|
69 |
os.remove(f)
|
70 |
+
|
71 |
|
72 |
# Gradio interface
|
73 |
iface = gr.Interface(
|