Spaces:
Sleeping
Sleeping
Update src/tools/accent_tool.py
Browse files- src/tools/accent_tool.py +30 -5
src/tools/accent_tool.py
CHANGED
@@ -1,4 +1,7 @@
|
|
1 |
-
import os
|
|
|
|
|
|
|
2 |
from pydub import AudioSegment
|
3 |
import whisper
|
4 |
from speechbrain.pretrained.interfaces import foreign_class
|
@@ -11,7 +14,7 @@ class AccentAnalyzerTool:
|
|
11 |
pymodule_file="custom_interface.py",
|
12 |
classname="CustomEncoderWav2vec2Classifier"
|
13 |
)
|
14 |
-
self.last_transcript = None
|
15 |
|
16 |
def log(self, msg):
|
17 |
print(f"[AccentAnalyzerTool] {msg}")
|
@@ -20,11 +23,33 @@ class AccentAnalyzerTool:
|
|
20 |
try:
|
21 |
self.log("Downloading video...")
|
22 |
tmp_dir = "tmp"
|
|
|
|
|
|
|
23 |
os.makedirs(tmp_dir, exist_ok=True)
|
|
|
24 |
video_path = os.path.join(tmp_dir, "video.mp4")
|
25 |
-
|
|
|
|
|
|
|
26 |
with open(video_path, "wb") as f:
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
self.log("Extracting audio...")
|
30 |
audio_path = os.path.join(tmp_dir, "audio.wav")
|
@@ -44,7 +69,7 @@ class AccentAnalyzerTool:
|
|
44 |
f"with **{confidence}% confidence**.\n\n"
|
45 |
f"**Transcript of the audio:**\n\n *{transcript.strip(' ')}*"
|
46 |
)
|
47 |
-
|
48 |
shutil.rmtree(tmp_dir, ignore_errors=True)
|
49 |
return summary
|
50 |
|
|
|
1 |
+
import os
|
2 |
+
import requests
|
3 |
+
import shutil
|
4 |
+
import subprocess
|
5 |
from pydub import AudioSegment
|
6 |
import whisper
|
7 |
from speechbrain.pretrained.interfaces import foreign_class
|
|
|
14 |
pymodule_file="custom_interface.py",
|
15 |
classname="CustomEncoderWav2vec2Classifier"
|
16 |
)
|
17 |
+
self.last_transcript = None
|
18 |
|
19 |
def log(self, msg):
|
20 |
print(f"[AccentAnalyzerTool] {msg}")
|
|
|
23 |
try:
|
24 |
self.log("Downloading video...")
|
25 |
tmp_dir = "tmp"
|
26 |
+
# Clean up tmp folder if exists
|
27 |
+
if os.path.exists(tmp_dir):
|
28 |
+
shutil.rmtree(tmp_dir)
|
29 |
os.makedirs(tmp_dir, exist_ok=True)
|
30 |
+
|
31 |
video_path = os.path.join(tmp_dir, "video.mp4")
|
32 |
+
|
33 |
+
headers = {"User-Agent": "Mozilla/5.0"}
|
34 |
+
r = requests.get(url, headers=headers, stream=True)
|
35 |
+
r.raise_for_status()
|
36 |
with open(video_path, "wb") as f:
|
37 |
+
for chunk in r.iter_content(chunk_size=8192):
|
38 |
+
if chunk:
|
39 |
+
f.write(chunk)
|
40 |
+
|
41 |
+
file_size = os.path.getsize(video_path)
|
42 |
+
self.log(f"Downloaded video size: {file_size} bytes")
|
43 |
+
if file_size < 1000:
|
44 |
+
raise ValueError("Downloaded video file is too small or invalid")
|
45 |
+
|
46 |
+
# Debug with ffprobe to check video validity
|
47 |
+
ffprobe_cmd = ["ffprobe", "-v", "error", "-show_format", "-show_streams", video_path]
|
48 |
+
try:
|
49 |
+
output = subprocess.check_output(ffprobe_cmd, stderr=subprocess.STDOUT).decode()
|
50 |
+
self.log(f"ffprobe output:\n{output}")
|
51 |
+
except subprocess.CalledProcessError as e:
|
52 |
+
self.log(f"ffprobe error:\n{e.output.decode()}")
|
53 |
|
54 |
self.log("Extracting audio...")
|
55 |
audio_path = os.path.join(tmp_dir, "audio.wav")
|
|
|
69 |
f"with **{confidence}% confidence**.\n\n"
|
70 |
f"**Transcript of the audio:**\n\n *{transcript.strip(' ')}*"
|
71 |
)
|
72 |
+
|
73 |
shutil.rmtree(tmp_dir, ignore_errors=True)
|
74 |
return summary
|
75 |
|