Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -11,92 +11,35 @@ from openai import OpenAI
|
|
11 |
openai_api_key = os.getenv("OPENAI_API_KEY")
|
12 |
openai = OpenAI(api_key=openai_api_key)
|
13 |
|
14 |
-
# def download_audio(youtube_url):
|
15 |
-
# try:
|
16 |
-
# output_template = "/tmp/downloaded_audio.%(ext)s"
|
17 |
-
|
18 |
-
# # Remove any old files
|
19 |
-
# for f in glob.glob("/tmp/downloaded_audio.*"):
|
20 |
-
# os.remove(f)
|
21 |
-
|
22 |
-
# command = [
|
23 |
-
# "yt-dlp", "-f", "bestaudio",
|
24 |
-
# "--extract-audio", "--audio-format", "mp3",
|
25 |
-
# "--audio-quality", "0",
|
26 |
-
# "-o", output_template,
|
27 |
-
# youtube_url
|
28 |
-
# ]
|
29 |
-
|
30 |
-
# result = subprocess.run(command, capture_output=True, text=True)
|
31 |
-
# print("stdout:\n", result.stdout)
|
32 |
-
# print("stderr:\n", result.stderr)
|
33 |
-
|
34 |
-
# if result.returncode != 0:
|
35 |
-
# raise RuntimeError(f"yt-dlp failed: {result.stderr}")
|
36 |
-
|
37 |
-
# files = glob.glob("/tmp/downloaded_audio.*")
|
38 |
-
# if not files:
|
39 |
-
# raise FileNotFoundError("No audio file downloaded.")
|
40 |
-
|
41 |
-
# return files[0]
|
42 |
-
# except Exception as e:
|
43 |
-
# raise RuntimeError(f"Download error: {e}")
|
44 |
-
|
45 |
-
from pytube import YouTube
|
46 |
-
|
47 |
-
def clean_youtube_url(url):
|
48 |
-
match = re.search(r"(?:v=|shorts/)([a-zA-Z0-9_-]{11})", url)
|
49 |
-
video_id = match.group(1) if match else None
|
50 |
-
return f"https://www.youtube.com/watch?v={video_id}" if video_id else None
|
51 |
-
|
52 |
def download_audio(youtube_url):
|
53 |
try:
|
54 |
-
print(f"βΆοΈ Original URL: {youtube_url}")
|
55 |
output_template = "/tmp/downloaded_audio.%(ext)s"
|
56 |
|
57 |
-
#
|
58 |
for f in glob.glob("/tmp/downloaded_audio.*"):
|
59 |
os.remove(f)
|
60 |
|
61 |
-
# β
Try yt-dlp first
|
62 |
command = [
|
63 |
-
"yt-dlp",
|
64 |
-
"-f", "bestaudio",
|
65 |
"--extract-audio", "--audio-format", "mp3",
|
66 |
"--audio-quality", "0",
|
67 |
"-o", output_template,
|
68 |
youtube_url
|
69 |
]
|
70 |
-
|
71 |
result = subprocess.run(command, capture_output=True, text=True)
|
72 |
-
print("
|
73 |
-
print("
|
74 |
-
|
75 |
-
if result.returncode
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
# π Fallback: try pytube with cleaned URL
|
82 |
-
print("π yt-dlp failed. Trying pytube...")
|
83 |
-
clean_url = clean_youtube_url(youtube_url)
|
84 |
-
if not clean_url:
|
85 |
-
raise ValueError("Unable to extract video ID for pytube fallback.")
|
86 |
-
|
87 |
-
print(f"π§½ Cleaned URL for pytube: {clean_url}")
|
88 |
-
yt = YouTube(clean_url)
|
89 |
-
stream = yt.streams.filter(only_audio=True).first()
|
90 |
-
if not stream:
|
91 |
-
raise ValueError("No audio stream found via pytube.")
|
92 |
-
|
93 |
-
output_path = "/tmp/fallback_audio.mp4"
|
94 |
-
stream.download(filename=output_path)
|
95 |
-
print("β
pytube download success.")
|
96 |
-
return output_path
|
97 |
|
|
|
98 |
except Exception as e:
|
99 |
-
print("β Final Download error:", e)
|
100 |
raise RuntimeError(f"Download error: {e}")
|
101 |
|
102 |
def transcribe_audio(file_path):
|
|
|
11 |
openai_api_key = os.getenv("OPENAI_API_KEY")
|
12 |
openai = OpenAI(api_key=openai_api_key)
|
13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
def download_audio(youtube_url):
|
15 |
try:
|
|
|
16 |
output_template = "/tmp/downloaded_audio.%(ext)s"
|
17 |
|
18 |
+
# Remove any old files
|
19 |
for f in glob.glob("/tmp/downloaded_audio.*"):
|
20 |
os.remove(f)
|
21 |
|
|
|
22 |
command = [
|
23 |
+
"yt-dlp", "-f", "bestaudio",
|
|
|
24 |
"--extract-audio", "--audio-format", "mp3",
|
25 |
"--audio-quality", "0",
|
26 |
"-o", output_template,
|
27 |
youtube_url
|
28 |
]
|
29 |
+
|
30 |
result = subprocess.run(command, capture_output=True, text=True)
|
31 |
+
print("stdout:\n", result.stdout)
|
32 |
+
print("stderr:\n", result.stderr)
|
33 |
+
|
34 |
+
if result.returncode != 0:
|
35 |
+
raise RuntimeError(f"yt-dlp failed: {result.stderr}")
|
36 |
+
|
37 |
+
files = glob.glob("/tmp/downloaded_audio.*")
|
38 |
+
if not files:
|
39 |
+
raise FileNotFoundError("No audio file downloaded.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
+
return files[0]
|
42 |
except Exception as e:
|
|
|
43 |
raise RuntimeError(f"Download error: {e}")
|
44 |
|
45 |
def transcribe_audio(file_path):
|