Spaces:
Running
on
Zero
Running
on
Zero
artificialguybr
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -25,14 +25,15 @@ print("Model successfully loaded.")
|
|
25 |
def generate_unique_filename(extension):
|
26 |
return f"{uuid.uuid4()}{extension}"
|
27 |
|
28 |
-
def
|
29 |
-
|
30 |
-
os.
|
31 |
-
|
|
|
32 |
|
33 |
def download_youtube_audio(url):
|
34 |
print(f"Downloading audio from YouTube: {url}")
|
35 |
-
output_path = generate_unique_filename(
|
36 |
ydl_opts = {
|
37 |
'format': 'bestaudio/best',
|
38 |
'postprocessors': [{
|
@@ -41,41 +42,36 @@ def download_youtube_audio(url):
|
|
41 |
}],
|
42 |
'outtmpl': output_path
|
43 |
}
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
return None
|
55 |
-
except Exception as e:
|
56 |
-
print(f"Error downloading YouTube audio: {e}")
|
57 |
-
return None
|
58 |
|
59 |
@spaces.GPU(duration=60)
|
60 |
def transcribe_audio(file_path):
|
61 |
print(f"Starting transcription of file: {file_path}")
|
62 |
-
|
63 |
-
print(f"Error: File {file_path} does not exist.")
|
64 |
-
return None
|
65 |
-
|
66 |
if file_path.endswith(('.mp4', '.avi', '.mov', '.flv')):
|
67 |
print("Video file detected. Extracting audio...")
|
68 |
try:
|
69 |
video = mp.VideoFileClip(file_path)
|
70 |
-
|
71 |
-
video.audio.write_audiofile(
|
72 |
-
|
73 |
-
file_path = audio_path
|
74 |
except Exception as e:
|
75 |
print(f"Error extracting audio from video: {e}")
|
76 |
-
|
77 |
-
|
78 |
-
|
|
|
|
|
|
|
79 |
command = [
|
80 |
"insanely-fast-whisper",
|
81 |
"--file-name", file_path,
|
@@ -94,32 +90,34 @@ def transcribe_audio(file_path):
|
|
94 |
print(f"Error running insanely-fast-whisper: {e}")
|
95 |
print(f"Standard output: {e.stdout}")
|
96 |
print(f"Error output: {e.stderr}")
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
try:
|
102 |
with open(output_file, "r") as f:
|
103 |
transcription = json.load(f)
|
104 |
except json.JSONDecodeError as e:
|
105 |
print(f"Error decoding JSON: {e}")
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
if "text" in transcription:
|
111 |
result = transcription["text"]
|
112 |
else:
|
113 |
result = " ".join([chunk["text"] for chunk in transcription.get("chunks", [])])
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
|
|
|
|
118 |
return result
|
119 |
|
120 |
@spaces.GPU(duration=60)
|
121 |
def generate_summary_stream(transcription):
|
122 |
print("Starting summary generation...")
|
|
|
|
|
123 |
detected_language = langdetect.detect(transcription)
|
124 |
|
125 |
prompt = f"""Summarize the following video transcription in 150-300 words.
|
@@ -130,35 +128,44 @@ def generate_summary_stream(transcription):
|
|
130 |
|
131 |
response, history = model.chat(tokenizer, prompt, history=[])
|
132 |
print(f"Final summary generated: {response[:100]}...")
|
|
|
133 |
return response
|
134 |
|
135 |
def process_youtube(url):
|
136 |
if not url:
|
|
|
137 |
return "Please enter a YouTube URL.", None
|
|
|
|
|
138 |
try:
|
139 |
audio_file = download_youtube_audio(url)
|
140 |
-
if audio_file
|
141 |
-
|
|
|
|
|
|
|
142 |
transcription = transcribe_audio(audio_file)
|
143 |
-
|
144 |
-
return "Error transcribing audio.", None
|
145 |
return transcription, None
|
146 |
except Exception as e:
|
|
|
147 |
return f"Processing error: {str(e)}", None
|
148 |
finally:
|
149 |
-
|
150 |
-
|
151 |
|
152 |
def process_uploaded_video(video_path):
|
|
|
153 |
try:
|
|
|
154 |
transcription = transcribe_audio(video_path)
|
155 |
-
|
156 |
-
return "Error transcribing video.", None
|
157 |
return transcription, None
|
158 |
except Exception as e:
|
|
|
159 |
return f"Processing error: {str(e)}", None
|
160 |
finally:
|
161 |
-
|
162 |
|
163 |
print("Setting up Gradio interface...")
|
164 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
@@ -201,7 +208,9 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
201 |
def process_video_and_update(video):
|
202 |
if video is None:
|
203 |
return "No video uploaded.", "Please upload a video."
|
|
|
204 |
transcription, _ = process_uploaded_video(video)
|
|
|
205 |
return transcription or "Transcription error", ""
|
206 |
|
207 |
video_button.click(process_video_and_update, inputs=[video_input], outputs=[transcription_output, summary_output])
|
|
|
25 |
def generate_unique_filename(extension):
|
26 |
return f"{uuid.uuid4()}{extension}"
|
27 |
|
28 |
+
def cleanup_files(*files):
|
29 |
+
for file in files:
|
30 |
+
if file and os.path.exists(file):
|
31 |
+
os.remove(file)
|
32 |
+
print(f"Removed file: {file}")
|
33 |
|
34 |
def download_youtube_audio(url):
|
35 |
print(f"Downloading audio from YouTube: {url}")
|
36 |
+
output_path = generate_unique_filename(".wav")
|
37 |
ydl_opts = {
|
38 |
'format': 'bestaudio/best',
|
39 |
'postprocessors': [{
|
|
|
42 |
}],
|
43 |
'outtmpl': output_path
|
44 |
}
|
45 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
46 |
+
ydl.download([url])
|
47 |
+
|
48 |
+
if os.path.exists(output_path):
|
49 |
+
print(f"Audio download completed. File saved at: {output_path}")
|
50 |
+
print(f"File size: {os.path.getsize(output_path)} bytes")
|
51 |
+
else:
|
52 |
+
print(f"Error: File {output_path} not found after download.")
|
53 |
+
|
54 |
+
return output_path
|
|
|
|
|
|
|
|
|
55 |
|
56 |
@spaces.GPU(duration=60)
|
57 |
def transcribe_audio(file_path):
|
58 |
print(f"Starting transcription of file: {file_path}")
|
59 |
+
temp_audio = None
|
|
|
|
|
|
|
60 |
if file_path.endswith(('.mp4', '.avi', '.mov', '.flv')):
|
61 |
print("Video file detected. Extracting audio...")
|
62 |
try:
|
63 |
video = mp.VideoFileClip(file_path)
|
64 |
+
temp_audio = generate_unique_filename(".wav")
|
65 |
+
video.audio.write_audiofile(temp_audio)
|
66 |
+
file_path = temp_audio
|
|
|
67 |
except Exception as e:
|
68 |
print(f"Error extracting audio from video: {e}")
|
69 |
+
raise
|
70 |
+
|
71 |
+
print(f"Does the file exist? {os.path.exists(file_path)}")
|
72 |
+
print(f"File size: {os.path.getsize(file_path) if os.path.exists(file_path) else 'N/A'} bytes")
|
73 |
+
|
74 |
+
output_file = generate_unique_filename(".json")
|
75 |
command = [
|
76 |
"insanely-fast-whisper",
|
77 |
"--file-name", file_path,
|
|
|
90 |
print(f"Error running insanely-fast-whisper: {e}")
|
91 |
print(f"Standard output: {e.stdout}")
|
92 |
print(f"Error output: {e.stderr}")
|
93 |
+
raise
|
94 |
+
|
95 |
+
print(f"Reading transcription file: {output_file}")
|
|
|
96 |
try:
|
97 |
with open(output_file, "r") as f:
|
98 |
transcription = json.load(f)
|
99 |
except json.JSONDecodeError as e:
|
100 |
print(f"Error decoding JSON: {e}")
|
101 |
+
print(f"File content: {open(output_file, 'r').read()}")
|
102 |
+
raise
|
103 |
+
|
|
|
104 |
if "text" in transcription:
|
105 |
result = transcription["text"]
|
106 |
else:
|
107 |
result = " ".join([chunk["text"] for chunk in transcription.get("chunks", [])])
|
108 |
+
|
109 |
+
print("Transcription completed.")
|
110 |
+
|
111 |
+
# Cleanup
|
112 |
+
cleanup_files(temp_audio, output_file)
|
113 |
+
|
114 |
return result
|
115 |
|
116 |
@spaces.GPU(duration=60)
|
117 |
def generate_summary_stream(transcription):
|
118 |
print("Starting summary generation...")
|
119 |
+
print(f"Transcription length: {len(transcription)} characters")
|
120 |
+
|
121 |
detected_language = langdetect.detect(transcription)
|
122 |
|
123 |
prompt = f"""Summarize the following video transcription in 150-300 words.
|
|
|
128 |
|
129 |
response, history = model.chat(tokenizer, prompt, history=[])
|
130 |
print(f"Final summary generated: {response[:100]}...")
|
131 |
+
print("Summary generation completed.")
|
132 |
return response
|
133 |
|
134 |
def process_youtube(url):
|
135 |
if not url:
|
136 |
+
print("YouTube URL not provided.")
|
137 |
return "Please enter a YouTube URL.", None
|
138 |
+
print(f"Processing YouTube URL: {url}")
|
139 |
+
|
140 |
try:
|
141 |
audio_file = download_youtube_audio(url)
|
142 |
+
if not os.path.exists(audio_file):
|
143 |
+
raise FileNotFoundError(f"File {audio_file} does not exist after download.")
|
144 |
+
|
145 |
+
print(f"Audio file found: {audio_file}")
|
146 |
+
print("Starting transcription...")
|
147 |
transcription = transcribe_audio(audio_file)
|
148 |
+
print(f"Transcription completed. Length: {len(transcription)} characters")
|
|
|
149 |
return transcription, None
|
150 |
except Exception as e:
|
151 |
+
print(f"Error processing YouTube: {e}")
|
152 |
return f"Processing error: {str(e)}", None
|
153 |
finally:
|
154 |
+
cleanup_files(audio_file)
|
155 |
+
print(f"Directory content after processing: {os.listdir('.')}")
|
156 |
|
157 |
def process_uploaded_video(video_path):
|
158 |
+
print(f"Processing uploaded video: {video_path}")
|
159 |
try:
|
160 |
+
print("Starting transcription...")
|
161 |
transcription = transcribe_audio(video_path)
|
162 |
+
print(f"Transcription completed. Length: {len(transcription)} characters")
|
|
|
163 |
return transcription, None
|
164 |
except Exception as e:
|
165 |
+
print(f"Error processing video: {e}")
|
166 |
return f"Processing error: {str(e)}", None
|
167 |
finally:
|
168 |
+
cleanup_files(video_path)
|
169 |
|
170 |
print("Setting up Gradio interface...")
|
171 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
|
208 |
def process_video_and_update(video):
|
209 |
if video is None:
|
210 |
return "No video uploaded.", "Please upload a video."
|
211 |
+
print(f"Video received: {video}")
|
212 |
transcription, _ = process_uploaded_video(video)
|
213 |
+
print(f"Returned transcription: {transcription[:100] if transcription else 'No transcription generated'}...")
|
214 |
return transcription or "Transcription error", ""
|
215 |
|
216 |
video_button.click(process_video_and_update, inputs=[video_input], outputs=[transcription_output, summary_output])
|