Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -7,7 +7,6 @@ import json
|
|
7 |
from threading import Thread
|
8 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
9 |
import spaces
|
10 |
-
import moviepy.editor as mp
|
11 |
import time
|
12 |
import langdetect
|
13 |
import uuid
|
@@ -46,35 +45,20 @@ def download_youtube_audio(url):
|
|
46 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
47 |
ydl.download([url])
|
48 |
|
49 |
-
# Check if the file was renamed to .wav.wav
|
50 |
if os.path.exists(output_path + ".wav"):
|
51 |
os.rename(output_path + ".wav", output_path)
|
52 |
|
53 |
-
if os.path.exists(output_path):
|
54 |
-
print(f"Audio download completed. File saved at: {output_path}")
|
55 |
-
print(f"File size: {os.path.getsize(output_path)} bytes")
|
56 |
-
else:
|
57 |
-
print(f"Error: File {output_path} not found after download.")
|
58 |
-
|
59 |
return output_path
|
60 |
|
61 |
-
|
62 |
def transcribe_audio(file_path):
|
63 |
print(f"Starting transcription of file: {file_path}")
|
64 |
temp_audio = None
|
65 |
if file_path.endswith(('.mp4', '.avi', '.mov', '.flv')):
|
66 |
-
print("Video file detected. Extracting audio...")
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
file_path = temp_audio
|
72 |
-
except Exception as e:
|
73 |
-
print(f"Error extracting audio from video: {e}")
|
74 |
-
raise
|
75 |
-
|
76 |
-
print(f"Does the file exist? {os.path.exists(file_path)}")
|
77 |
-
print(f"File size: {os.path.getsize(file_path) if os.path.exists(file_path) else 'N/A'} bytes")
|
78 |
|
79 |
output_file = generate_unique_filename(".json")
|
80 |
command = [
|
@@ -86,143 +70,60 @@ def transcribe_audio(file_path):
|
|
86 |
"--timestamp", "chunk",
|
87 |
"--transcript-path", output_file
|
88 |
]
|
89 |
-
|
90 |
-
try:
|
91 |
-
result = subprocess.run(command, check=True, capture_output=True, text=True)
|
92 |
-
print(f"Standard output: {result.stdout}")
|
93 |
-
print(f"Error output: {result.stderr}")
|
94 |
-
except subprocess.CalledProcessError as e:
|
95 |
-
print(f"Error running insanely-fast-whisper: {e}")
|
96 |
-
print(f"Standard output: {e.stdout}")
|
97 |
-
print(f"Error output: {e.stderr}")
|
98 |
-
raise
|
99 |
-
|
100 |
-
print(f"Reading transcription file: {output_file}")
|
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 |
-
print(f"File content: {open(output_file, 'r').read()}")
|
107 |
-
raise
|
108 |
|
109 |
-
|
110 |
-
|
111 |
-
else:
|
112 |
-
result = " ".join([chunk["text"] for chunk in transcription.get("chunks", [])])
|
113 |
|
114 |
-
|
115 |
|
116 |
-
# Cleanup
|
117 |
cleanup_files(output_file)
|
118 |
if temp_audio:
|
119 |
cleanup_files(temp_audio)
|
120 |
|
121 |
return result
|
122 |
|
123 |
-
@spaces.GPU(duration=90)
|
124 |
def generate_summary_stream(transcription):
|
125 |
-
print("Starting summary generation...")
|
126 |
-
print(f"Transcription length: {len(transcription)} characters")
|
127 |
-
|
128 |
detected_language = langdetect.detect(transcription)
|
129 |
-
|
130 |
-
prompt = f"""Summarize the following video transcription in 150-300 words.
|
131 |
-
The summary should be in the same language as the transcription, which is detected as {detected_language}.
|
132 |
-
Please ensure that the summary captures the main points and key ideas of the transcription:
|
133 |
-
|
134 |
{transcription[:300000]}..."""
|
135 |
|
136 |
response, history = model.chat(tokenizer, prompt, history=[])
|
137 |
-
print(f"Final summary generated: {response[:100]}...")
|
138 |
-
print("Summary generation completed.")
|
139 |
return response
|
140 |
|
141 |
def process_youtube(url):
|
142 |
if not url:
|
143 |
-
print("YouTube URL not provided.")
|
144 |
return "Please enter a YouTube URL.", None
|
145 |
-
|
146 |
-
|
147 |
-
audio_file
|
148 |
-
|
149 |
-
audio_file = download_youtube_audio(url)
|
150 |
-
if not os.path.exists(audio_file):
|
151 |
-
raise FileNotFoundError(f"File {audio_file} does not exist after download.")
|
152 |
-
|
153 |
-
print(f"Audio file found: {audio_file}")
|
154 |
-
print("Starting transcription...")
|
155 |
-
transcription = transcribe_audio(audio_file)
|
156 |
-
print(f"Transcription completed. Length: {len(transcription)} characters")
|
157 |
-
return transcription, None
|
158 |
-
except Exception as e:
|
159 |
-
print(f"Error processing YouTube: {e}")
|
160 |
-
return f"Processing error: {str(e)}", None
|
161 |
-
finally:
|
162 |
-
if audio_file and os.path.exists(audio_file):
|
163 |
-
cleanup_files(audio_file)
|
164 |
-
print(f"Directory content after processing: {os.listdir('.')}")
|
165 |
|
166 |
def process_uploaded_video(video_path):
|
167 |
-
|
168 |
-
|
169 |
-
print("Starting transcription...")
|
170 |
-
transcription = transcribe_audio(video_path)
|
171 |
-
print(f"Transcription completed. Length: {len(transcription)} characters")
|
172 |
-
return transcription, None
|
173 |
-
except Exception as e:
|
174 |
-
print(f"Error processing video: {e}")
|
175 |
-
return f"Processing error: {str(e)}", None
|
176 |
|
177 |
-
print("Setting up Gradio interface...")
|
178 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
179 |
-
gr.Markdown(
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
Upload a video or provide a YouTube link to get a transcription and AI-generated summary. HF Zero GPU has a usage time limit. So if you want to run longer videos I recommend you clone the space. Remove @Spaces.gpu from the code and run it locally on your GPU!
|
184 |
-
"""
|
185 |
-
)
|
186 |
|
187 |
with gr.Tabs():
|
188 |
with gr.TabItem("📤 Video Upload"):
|
189 |
-
video_input = gr.Video(
|
190 |
-
video_button = gr.Button("🚀 Process Video"
|
191 |
|
192 |
with gr.TabItem("🔗 YouTube Link"):
|
193 |
-
url_input = gr.Textbox(
|
194 |
-
url_button = gr.Button("🚀 Process URL"
|
195 |
-
|
196 |
-
with gr.Row():
|
197 |
-
with gr.Column():
|
198 |
-
transcription_output = gr.Textbox(label="📝 Transcription", lines=10, show_copy_button=True)
|
199 |
-
with gr.Column():
|
200 |
-
summary_output = gr.Textbox(label="📊 Summary", lines=10, show_copy_button=True)
|
201 |
|
202 |
-
|
|
|
|
|
203 |
|
204 |
-
|
205 |
-
"""
|
206 |
-
### How to use:
|
207 |
-
1. Upload a video or paste a YouTube link.
|
208 |
-
2. Click 'Process' to get the transcription.
|
209 |
-
3. Click 'Generate Summary' to get a summary of the content.
|
210 |
-
|
211 |
-
*Note: Processing may take a few minutes depending on the video length.*
|
212 |
-
"""
|
213 |
-
)
|
214 |
-
|
215 |
-
def process_video_and_update(video):
|
216 |
-
if video is None:
|
217 |
-
return "No video uploaded.", "Please upload a video."
|
218 |
-
print(f"Video received: {video}")
|
219 |
-
transcription, _ = process_uploaded_video(video)
|
220 |
-
print(f"Returned transcription: {transcription[:100] if transcription else 'No transcription generated'}...")
|
221 |
-
return transcription or "Transcription error", ""
|
222 |
-
|
223 |
-
video_button.click(process_video_and_update, inputs=[video_input], outputs=[transcription_output, summary_output])
|
224 |
url_button.click(process_youtube, inputs=[url_input], outputs=[transcription_output, summary_output])
|
225 |
summary_button.click(generate_summary_stream, inputs=[transcription_output], outputs=[summary_output])
|
226 |
|
227 |
-
|
228 |
-
demo.launch()
|
|
|
7 |
from threading import Thread
|
8 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
9 |
import spaces
|
|
|
10 |
import time
|
11 |
import langdetect
|
12 |
import uuid
|
|
|
45 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
46 |
ydl.download([url])
|
47 |
|
|
|
48 |
if os.path.exists(output_path + ".wav"):
|
49 |
os.rename(output_path + ".wav", output_path)
|
50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
return output_path
|
52 |
|
|
|
53 |
def transcribe_audio(file_path):
|
54 |
print(f"Starting transcription of file: {file_path}")
|
55 |
temp_audio = None
|
56 |
if file_path.endswith(('.mp4', '.avi', '.mov', '.flv')):
|
57 |
+
print("Video file detected. Extracting audio using ffmpeg...")
|
58 |
+
temp_audio = generate_unique_filename(".wav")
|
59 |
+
command = ["ffmpeg", "-i", file_path, "-q:a", "0", "-map", "a", temp_audio]
|
60 |
+
subprocess.run(command, check=True)
|
61 |
+
file_path = temp_audio
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
output_file = generate_unique_filename(".json")
|
64 |
command = [
|
|
|
70 |
"--timestamp", "chunk",
|
71 |
"--transcript-path", output_file
|
72 |
]
|
73 |
+
subprocess.run(command, check=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
|
75 |
+
with open(output_file, "r") as f:
|
76 |
+
transcription = json.load(f)
|
|
|
|
|
77 |
|
78 |
+
result = transcription.get("text", " ".join([chunk["text"] for chunk in transcription.get("chunks", [])]))
|
79 |
|
|
|
80 |
cleanup_files(output_file)
|
81 |
if temp_audio:
|
82 |
cleanup_files(temp_audio)
|
83 |
|
84 |
return result
|
85 |
|
|
|
86 |
def generate_summary_stream(transcription):
|
|
|
|
|
|
|
87 |
detected_language = langdetect.detect(transcription)
|
88 |
+
prompt = f"""Summarize the following video transcription in 150-300 words in {detected_language}:
|
|
|
|
|
|
|
|
|
89 |
{transcription[:300000]}..."""
|
90 |
|
91 |
response, history = model.chat(tokenizer, prompt, history=[])
|
|
|
|
|
92 |
return response
|
93 |
|
94 |
def process_youtube(url):
|
95 |
if not url:
|
|
|
96 |
return "Please enter a YouTube URL.", None
|
97 |
+
audio_file = download_youtube_audio(url)
|
98 |
+
transcription = transcribe_audio(audio_file)
|
99 |
+
cleanup_files(audio_file)
|
100 |
+
return transcription, None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
|
102 |
def process_uploaded_video(video_path):
|
103 |
+
transcription = transcribe_audio(video_path)
|
104 |
+
return transcription, None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
|
|
|
106 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
107 |
+
gr.Markdown("""
|
108 |
+
# 🎥 Video Transcription and Smart Summary
|
109 |
+
Upload a video or provide a YouTube link to get a transcription and AI-generated summary.
|
110 |
+
""")
|
|
|
|
|
|
|
111 |
|
112 |
with gr.Tabs():
|
113 |
with gr.TabItem("📤 Video Upload"):
|
114 |
+
video_input = gr.Video()
|
115 |
+
video_button = gr.Button("🚀 Process Video")
|
116 |
|
117 |
with gr.TabItem("🔗 YouTube Link"):
|
118 |
+
url_input = gr.Textbox(placeholder="https://www.youtube.com/watch?v=...")
|
119 |
+
url_button = gr.Button("🚀 Process URL")
|
|
|
|
|
|
|
|
|
|
|
|
|
120 |
|
121 |
+
transcription_output = gr.Textbox(label="📝 Transcription", lines=10, show_copy_button=True)
|
122 |
+
summary_output = gr.Textbox(label="📊 Summary", lines=10, show_copy_button=True)
|
123 |
+
summary_button = gr.Button("📝 Generate Summary")
|
124 |
|
125 |
+
video_button.click(process_uploaded_video, inputs=[video_input], outputs=[transcription_output, summary_output])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
126 |
url_button.click(process_youtube, inputs=[url_input], outputs=[transcription_output, summary_output])
|
127 |
summary_button.click(generate_summary_stream, inputs=[transcription_output], outputs=[summary_output])
|
128 |
|
129 |
+
demo.launch()
|
|