Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,103 +1,83 @@
|
|
| 1 |
import os
|
| 2 |
import subprocess
|
| 3 |
import sys
|
| 4 |
-
import requests
|
| 5 |
-
import gradio as gr
|
| 6 |
-
from urllib.parse import urlparse
|
| 7 |
-
from moviepy.editor import VideoFileClip
|
| 8 |
-
from transformers import pipeline
|
| 9 |
-
import yt_dlp as youtube_dl
|
| 10 |
-
|
| 11 |
-
# Ensure required packages are installed
|
| 12 |
-
def install_package(package):
|
| 13 |
-
try:
|
| 14 |
-
__import__(package)
|
| 15 |
-
except ImportError:
|
| 16 |
-
print(f"Installing {package}...")
|
| 17 |
-
subprocess.check_call([sys.executable, "-m", "pip", "install", package])
|
| 18 |
-
__import__(package)
|
| 19 |
-
|
| 20 |
-
install_package("torch")
|
| 21 |
-
install_package("tensorflow")
|
| 22 |
-
install_package("transformers")
|
| 23 |
-
install_package("gradio")
|
| 24 |
-
install_package("yt-dlp")
|
| 25 |
-
install_package("moviepy")
|
| 26 |
|
| 27 |
-
#
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
ext = os.path.splitext(urlparse(video_url).path)[1]
|
| 36 |
-
if ext == "":
|
| 37 |
-
raise ValueError("Cannot determine file extension.")
|
| 38 |
-
filename_with_ext = filename + ext
|
| 39 |
-
print(f"Downloading: {video_url}")
|
| 40 |
-
resp = requests.get(video_url, stream=True)
|
| 41 |
-
resp.raise_for_status()
|
| 42 |
-
with open(filename_with_ext, "wb") as f:
|
| 43 |
-
for chunk in resp.iter_content(chunk_size=8192):
|
| 44 |
-
f.write(chunk)
|
| 45 |
-
return filename_with_ext
|
| 46 |
|
| 47 |
-
def
|
|
|
|
|
|
|
|
|
|
| 48 |
ydl_opts = {
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
}
|
| 54 |
-
|
| 55 |
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
|
| 56 |
ydl.download([video_url])
|
| 57 |
return filename
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
|
|
|
| 71 |
classifier = pipeline("audio-classification", model=model_name)
|
| 72 |
results = classifier(audio_file)
|
| 73 |
if results:
|
| 74 |
top = results[0]
|
| 75 |
-
return f"Accent: {top['label']}
|
| 76 |
-
|
|
|
|
| 77 |
|
| 78 |
-
|
| 79 |
-
def accent_classifier(video_url: str) -> str:
|
| 80 |
-
tmp_video, tmp_audio = None, None
|
| 81 |
try:
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
result = classify_accent(
|
| 85 |
except Exception as e:
|
| 86 |
-
result = f"Error: {e}"
|
| 87 |
finally:
|
| 88 |
-
if
|
| 89 |
-
os.remove(
|
| 90 |
-
if
|
| 91 |
-
os.remove(
|
| 92 |
return result
|
| 93 |
|
| 94 |
-
# Gradio UI setup
|
| 95 |
iface = gr.Interface(
|
| 96 |
fn=accent_classifier,
|
| 97 |
-
inputs=gr.Textbox(label="Video URL", placeholder="Enter a
|
| 98 |
outputs="text",
|
| 99 |
-
title="
|
| 100 |
-
description="
|
| 101 |
)
|
| 102 |
|
| 103 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import os
|
| 2 |
import subprocess
|
| 3 |
import sys
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
# Ensure required libraries are installed
|
| 6 |
+
try:
|
| 7 |
+
import yt_dlp as youtube_dl
|
| 8 |
+
except ImportError:
|
| 9 |
+
subprocess.check_call([sys.executable, "-m", "pip", "install", "yt-dlp"])
|
| 10 |
+
import yt_dlp as youtube_dl
|
| 11 |
|
| 12 |
+
import gradio as gr
|
| 13 |
+
from transformers import pipeline
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
def download_video(video_url, filename="downloaded_video.mp4"):
|
| 16 |
+
"""
|
| 17 |
+
Download the video file using yt_dlp with cookies.txt for authentication.
|
| 18 |
+
"""
|
| 19 |
ydl_opts = {
|
| 20 |
+
'format': 'best',
|
| 21 |
+
'outtmpl': filename,
|
| 22 |
+
'noplaylist': True,
|
| 23 |
+
'quiet': True,
|
| 24 |
+
'cookiefile': 'cookies.txt',
|
| 25 |
+
'user_agent': (
|
| 26 |
+
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
|
| 27 |
+
'AppleWebKit/537.36 (KHTML, like Gecko) '
|
| 28 |
+
'Chrome/115.0.0.0 Safari/537.36'
|
| 29 |
+
),
|
| 30 |
}
|
| 31 |
+
|
| 32 |
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
|
| 33 |
ydl.download([video_url])
|
| 34 |
return filename
|
| 35 |
|
| 36 |
+
def extract_audio(video_filename, audio_filename="extracted_audio.wav"):
|
| 37 |
+
command = [
|
| 38 |
+
'ffmpeg',
|
| 39 |
+
'-y',
|
| 40 |
+
'-i', video_filename,
|
| 41 |
+
'-vn',
|
| 42 |
+
'-acodec', 'pcm_s16le',
|
| 43 |
+
'-ar', '44100',
|
| 44 |
+
'-ac', '2',
|
| 45 |
+
audio_filename
|
| 46 |
+
]
|
| 47 |
+
subprocess.run(command, check=True)
|
| 48 |
+
return audio_filename
|
| 49 |
|
| 50 |
+
def classify_accent(audio_file, model_name="your-hf-accent-model"):
|
| 51 |
+
if model_name == "your-hf-accent-model":
|
| 52 |
+
return "Accent: Demo Accent\nConfidence: 100.00%"
|
| 53 |
classifier = pipeline("audio-classification", model=model_name)
|
| 54 |
results = classifier(audio_file)
|
| 55 |
if results:
|
| 56 |
top = results[0]
|
| 57 |
+
return f"Accent: {top['label']}\nConfidence: {top['score'] * 100:.2f}%"
|
| 58 |
+
else:
|
| 59 |
+
return "No classification result."
|
| 60 |
|
| 61 |
+
def accent_classifier(video_url):
|
|
|
|
|
|
|
| 62 |
try:
|
| 63 |
+
video_filename = download_video(video_url)
|
| 64 |
+
audio_filename = extract_audio(video_filename)
|
| 65 |
+
result = classify_accent(audio_filename)
|
| 66 |
except Exception as e:
|
| 67 |
+
result = f"Error occurred: {e}"
|
| 68 |
finally:
|
| 69 |
+
if os.path.exists("downloaded_video.mp4"):
|
| 70 |
+
os.remove("downloaded_video.mp4")
|
| 71 |
+
if os.path.exists("extracted_audio.wav"):
|
| 72 |
+
os.remove("extracted_audio.wav")
|
| 73 |
return result
|
| 74 |
|
|
|
|
| 75 |
iface = gr.Interface(
|
| 76 |
fn=accent_classifier,
|
| 77 |
+
inputs=gr.Textbox(label="Video URL", placeholder="Enter a public YouTube, Vimeo, or Dailymotion link"),
|
| 78 |
outputs="text",
|
| 79 |
+
title="Accent Classifier",
|
| 80 |
+
description="Downloads a video, extracts audio, and classifies the accent using a Hugging Face model."
|
| 81 |
)
|
| 82 |
|
| 83 |
if __name__ == "__main__":
|