Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,153 +1,88 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
from phi.agent import Agent
|
3 |
-
from phi.model.google import Gemini
|
4 |
-
from phi.tools.duckduckgo import DuckDuckGo
|
5 |
-
from google.generativeai import upload_file, get_file
|
6 |
-
import google.generativeai as genai
|
7 |
-
import time
|
8 |
-
from pathlib import Path
|
9 |
-
import tempfile
|
10 |
-
from dotenv import load_dotenv
|
11 |
import os
|
12 |
-
|
13 |
-
from
|
|
|
|
|
|
|
|
|
14 |
|
15 |
-
|
16 |
-
|
|
|
|
|
|
|
|
|
17 |
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
30 |
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
height: 50px;
|
39 |
-
font-size: 18px;
|
40 |
-
font-weight: bold;
|
41 |
-
background: rgba(255, 255, 255, 0.2);
|
42 |
-
border-radius: 12px;
|
43 |
-
border: 2px solid rgba(255, 255, 255, 0.5);
|
44 |
-
box-shadow: 0px 4px 10px rgba(0, 0, 0, 0.2);
|
45 |
-
}
|
46 |
-
.stTextInput>div>div>input, .stTextArea>div>textarea {
|
47 |
-
background: rgba(255, 255, 255, 0.1);
|
48 |
-
border-radius: 8px;
|
49 |
-
border: 1px solid rgba(255, 255, 255, 0.3);
|
50 |
-
color: white;
|
51 |
-
padding: 10px;
|
52 |
-
}
|
53 |
-
</style>
|
54 |
-
""",
|
55 |
-
unsafe_allow_html=True
|
56 |
-
)
|
57 |
|
58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
-
|
61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
-
|
64 |
-
|
|
|
|
|
65 |
|
66 |
-
|
67 |
-
|
68 |
-
st.subheader("Phidata Video AI Summarizer Agent 🎥")
|
69 |
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
name="Video AI Summarizer",
|
74 |
-
model=Gemini(id="gemini-2.0-flash-exp"),
|
75 |
-
tools=[DuckDuckGo()],
|
76 |
-
markdown=True,
|
77 |
-
)
|
78 |
|
79 |
-
|
|
|
|
|
80 |
|
81 |
-
|
|
|
82 |
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
video_path = temp_video.name
|
87 |
-
|
88 |
-
st.video(video_path, format="video/mp4", start_time=0)
|
89 |
-
|
90 |
-
user_query = st.text_area("What insights are you seeking from the video?", "")
|
91 |
-
|
92 |
-
if st.button("🚀 Analyze Video", key="analyze_video_button"):
|
93 |
-
if not user_query:
|
94 |
-
st.warning("Please enter a question or insight to analyze the video.")
|
95 |
-
else:
|
96 |
-
try:
|
97 |
-
with st.spinner("Processing video..."):
|
98 |
-
processed_video = upload_file(video_path)
|
99 |
-
while processed_video.state.name == "PROCESSING":
|
100 |
-
time.sleep(1)
|
101 |
-
processed_video = get_file(processed_video.name)
|
102 |
-
|
103 |
-
prompt = f"""
|
104 |
-
Analyze the uploaded video and provide a summary.
|
105 |
-
Respond to: {user_query}
|
106 |
-
"""
|
107 |
-
response = multimodal_Agent.run(prompt, videos=[processed_video])
|
108 |
-
st.subheader("Analysis Result")
|
109 |
-
st.markdown(response.content)
|
110 |
-
except Exception as error:
|
111 |
-
st.error(f"Error: {error}")
|
112 |
-
finally:
|
113 |
-
Path(video_path).unlink(missing_ok=True)
|
114 |
-
else:
|
115 |
-
st.info("Upload a video file to begin analysis.")
|
116 |
-
|
117 |
-
# Tab 2: YouTube Video Analyzer with Groq
|
118 |
-
with tab2:
|
119 |
-
st.subheader("YouTube Video Analyzer 🎬")
|
120 |
-
|
121 |
-
try:
|
122 |
-
youtube_agent = Agent(
|
123 |
-
model=Groq(id="llama3-8b-8192", api_key=groq_api_key),
|
124 |
-
tools=[YouTubeTools(), DuckDuckGo()],
|
125 |
-
show_tool_calls=True,
|
126 |
-
get_video_captions=True,
|
127 |
-
get_video_data=True,
|
128 |
-
description="Analyze YouTube videos for content, key points, and web research.",
|
129 |
-
)
|
130 |
-
except Exception as e:
|
131 |
-
st.error(f"Error initializing the agent: {e}")
|
132 |
-
st.stop()
|
133 |
|
134 |
-
|
135 |
-
|
136 |
|
137 |
-
|
138 |
-
if video_url:
|
139 |
-
with st.spinner("Analyzing..."):
|
140 |
-
try:
|
141 |
-
prompt = f"""
|
142 |
-
Analyze the YouTube video.
|
143 |
-
Provide a detailed summary with key points.
|
144 |
-
{f'Respond to: {user_query}' if user_query else ''}
|
145 |
-
Video URL: {video_url}
|
146 |
-
"""
|
147 |
-
output = youtube_agent.run(prompt)
|
148 |
-
st.subheader("Analysis Result")
|
149 |
-
st.markdown(output.content)
|
150 |
-
except Exception as e:
|
151 |
-
st.error(f"Error: {e}")
|
152 |
-
else:
|
153 |
-
st.warning("Please enter a YouTube video URL.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
+
import gradio as gr
|
3 |
+
from moviepy.video.io.VideoFileClip import VideoFileClip
|
4 |
+
from pydub import AudioSegment
|
5 |
+
import whisper
|
6 |
+
from transformers import pipeline, MarianMTModel, MarianTokenizer
|
7 |
+
import yt_dlp as youtube_dl
|
8 |
|
9 |
+
def download_youtube_video(url):
|
10 |
+
try:
|
11 |
+
os.system(f"yt-dlp -o video.mp4 {url}")
|
12 |
+
return "video.mp4"
|
13 |
+
except Exception as e:
|
14 |
+
return str(e)
|
15 |
|
16 |
+
def extract_audio(video_path):
|
17 |
+
try:
|
18 |
+
audio = AudioSegment.from_file(video_path)
|
19 |
+
audio.export("extracted_audio.mp3", format="mp3")
|
20 |
+
return "extracted_audio.mp3"
|
21 |
+
except Exception as e:
|
22 |
+
return str(e)
|
23 |
|
24 |
+
def transcribe_audio(audio_path):
|
25 |
+
try:
|
26 |
+
model = whisper.load_model("base")
|
27 |
+
result = model.transcribe(audio_path)
|
28 |
+
return result['text']
|
29 |
+
except Exception as e:
|
30 |
+
return str(e)
|
31 |
|
32 |
+
def summarize_text(text):
|
33 |
+
try:
|
34 |
+
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
35 |
+
summary = summarizer(text, max_length=150, min_length=30, do_sample=False)
|
36 |
+
return summary[0]['summary_text']
|
37 |
+
except Exception as e:
|
38 |
+
return str(e)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
+
def translate_text(text, tgt_lang="es"):
|
41 |
+
try:
|
42 |
+
model_name = f"Helsinki-NLP/opus-mt-en-{tgt_lang}"
|
43 |
+
tokenizer = MarianTokenizer.from_pretrained(model_name)
|
44 |
+
model = MarianMTModel.from_pretrained(model_name)
|
45 |
+
translated = model.generate(**tokenizer(text, return_tensors="pt", padding=True))
|
46 |
+
return tokenizer.decode(translated[0], skip_special_tokens=True)
|
47 |
+
except Exception as e:
|
48 |
+
return str(e)
|
49 |
|
50 |
+
def process_video(video_file, youtube_url):
|
51 |
+
if youtube_url:
|
52 |
+
video_path = download_youtube_video(youtube_url)
|
53 |
+
elif video_file:
|
54 |
+
video_path = video_file.name
|
55 |
+
else:
|
56 |
+
return "No valid input provided."
|
57 |
+
|
58 |
+
audio_path = extract_audio(video_path)
|
59 |
+
transcription = transcribe_audio(audio_path)
|
60 |
+
return transcription
|
61 |
|
62 |
+
def summarize_and_translate(text, lang):
|
63 |
+
summary = summarize_text(text)
|
64 |
+
translation = translate_text(summary, lang)
|
65 |
+
return summary, translation
|
66 |
|
67 |
+
with gr.Blocks() as app:
|
68 |
+
gr.Markdown("# 🎥 Smart Video-to-Text Summarization App")
|
|
|
69 |
|
70 |
+
with gr.Row():
|
71 |
+
video_upload = gr.File(label="Upload Video File", type="file")
|
72 |
+
youtube_link = gr.Textbox(label="YouTube URL")
|
|
|
|
|
|
|
|
|
|
|
73 |
|
74 |
+
process_button = gr.Button("🚀 Process Video")
|
75 |
+
transcription_output = gr.Textbox(label="Transcription", interactive=False)
|
76 |
+
process_button.click(process_video, inputs=[video_upload, youtube_link], outputs=transcription_output)
|
77 |
|
78 |
+
summarize_button = gr.Button("📝 Summarize Text")
|
79 |
+
summary_output = gr.Textbox(label="Summary", interactive=False)
|
80 |
|
81 |
+
translate_button = gr.Button("🌍 Translate Summary")
|
82 |
+
language_dropdown = gr.Dropdown(choices=["es", "fr", "de", "zh"], label="Select Translation Language")
|
83 |
+
translated_output = gr.Textbox(label="Translated Summary", interactive=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
|
85 |
+
summarize_button.click(summarize_text, inputs=transcription_output, outputs=summary_output)
|
86 |
+
translate_button.click(summarize_and_translate, inputs=[transcription_output, language_dropdown], outputs=[summary_output, translated_output])
|
87 |
|
88 |
+
app.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|