Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from speechbrain.inference import EncoderClassifier
|
| 3 |
import torch
|
|
|
|
| 4 |
import subprocess
|
| 5 |
import os
|
| 6 |
import uuid
|
|
@@ -14,16 +15,86 @@ def get_model():
|
|
| 14 |
model = EncoderClassifier.from_hparams("Jzuluaga/accent-id-commonaccent_ecapa")
|
| 15 |
return model
|
| 16 |
|
| 17 |
-
def
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
}
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
def extract_audio(video_file):
|
| 29 |
audio_path = f"audio_{uuid.uuid4()}.wav"
|
|
@@ -35,6 +106,7 @@ def extract_audio(video_file):
|
|
| 35 |
subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
| 36 |
return audio_path
|
| 37 |
|
|
|
|
| 38 |
def classify_accent(input_file_or_url):
|
| 39 |
model = get_model()
|
| 40 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from speechbrain.inference import EncoderClassifier
|
| 3 |
import torch
|
| 4 |
+
import requests
|
| 5 |
import subprocess
|
| 6 |
import os
|
| 7 |
import uuid
|
|
|
|
| 15 |
model = EncoderClassifier.from_hparams("Jzuluaga/accent-id-commonaccent_ecapa")
|
| 16 |
return model
|
| 17 |
|
| 18 |
+
def extract_id_from_url(url):
|
| 19 |
+
urlRet = url.split("/")[-1]
|
| 20 |
+
if '?' in urlRet:
|
| 21 |
+
urlRet = urlRet.split("?")[0]
|
| 22 |
+
return urlRet
|
| 23 |
+
|
| 24 |
+
def fetch_loom_download_url(id):
|
| 25 |
+
response = requests.post(url=f"https://www.loom.com/api/campaigns/sessions/{id}/transcoded-url")
|
| 26 |
+
if response.status_code == 200:
|
| 27 |
+
return response.json()["url"]
|
| 28 |
+
else:
|
| 29 |
+
print("Error while retrieving response: ", response.status_code)
|
| 30 |
+
exit
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def download_loom_video(url, filename):
|
| 35 |
+
headers = {
|
| 36 |
+
"User-Agent": "Mozilla/5.0"
|
| 37 |
}
|
| 38 |
+
|
| 39 |
+
try:
|
| 40 |
+
with requests.get(url, headers=headers, stream=True) as response:
|
| 41 |
+
response.raise_for_status()
|
| 42 |
+
|
| 43 |
+
with open(filename, "wb") as f:
|
| 44 |
+
for chunk in response.iter_content(chunk_size=8192):
|
| 45 |
+
if chunk:
|
| 46 |
+
f.write(chunk)
|
| 47 |
+
|
| 48 |
+
print(f"Downloaded video to {filename}")
|
| 49 |
+
return filename
|
| 50 |
+
|
| 51 |
+
except requests.exceptions.RequestException as e:
|
| 52 |
+
print(f"Failed to download Loom video: {e}")
|
| 53 |
+
return None
|
| 54 |
+
|
| 55 |
+
def download_direct_mp4(url, filename):
|
| 56 |
+
try:
|
| 57 |
+
response = requests.get(url, stream=True)
|
| 58 |
+
response.raise_for_status()
|
| 59 |
+
with open(filename, "wb") as f:
|
| 60 |
+
for chunk in response.iter_content(chunk_size=8192):
|
| 61 |
+
if chunk:
|
| 62 |
+
f.write(chunk)
|
| 63 |
+
return filename
|
| 64 |
+
except Exception as e:
|
| 65 |
+
print(f"Error downloading direct mp4: {e}")
|
| 66 |
+
return None
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def download_video_from_url(url):
|
| 70 |
+
if "loom.com" in url:
|
| 71 |
+
video_id = extract_id_from_url(url)
|
| 72 |
+
print(video_id)
|
| 73 |
+
direct_url = fetch_loom_download_url(video_id)
|
| 74 |
+
print(direct_url)
|
| 75 |
+
filename = f"LoomVideo_{video_id}.mp4"
|
| 76 |
+
success = download_loom_video(direct_url, filename)
|
| 77 |
+
print(success)
|
| 78 |
+
return filename if success else None
|
| 79 |
+
|
| 80 |
+
elif url.endswith(".mp4"):
|
| 81 |
+
filename = f"video_{uuid.uuid4()}.mp4"
|
| 82 |
+
result = download_direct_mp4(url, filename)
|
| 83 |
+
return result
|
| 84 |
+
|
| 85 |
+
else:
|
| 86 |
+
# fallback to yt_dlp for youtube, vimeo, etc.
|
| 87 |
+
out_path = f"video_{uuid.uuid4()}.mp4"
|
| 88 |
+
ydl_opts = {
|
| 89 |
+
'format': 'bestaudio/best',
|
| 90 |
+
'outtmpl': out_path,
|
| 91 |
+
'quiet': True,
|
| 92 |
+
}
|
| 93 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 94 |
+
ydl.download([url])
|
| 95 |
+
return out_path
|
| 96 |
+
|
| 97 |
+
|
| 98 |
|
| 99 |
def extract_audio(video_file):
|
| 100 |
audio_path = f"audio_{uuid.uuid4()}.wav"
|
|
|
|
| 106 |
subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
| 107 |
return audio_path
|
| 108 |
|
| 109 |
+
|
| 110 |
def classify_accent(input_file_or_url):
|
| 111 |
model = get_model()
|
| 112 |
|