Update app.py
Browse files1223567891011121314151617181920212223242526272829303132333435363738394041424345645 unmodified lines5253549596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132555657import streamlit as stfrom pytube import YouTubefrom youtube_transcript_api import YouTubeTranscriptApifrom youtube_transcript_api.formatters import SRTFormatterfrom phi.agent import Agentfrom phi.model.google import Geminifrom phi.tools.duckduckgo import DuckDuckGofrom google.generativeai import upload_file, get_fileimport google.generativeai as genai
import timefrom pathlib import Pathfrom dotenv import load_dotenvimport osimport tempfile
# Load environment variablesload_dotenv()API_KEY = os.getenv("GOOGLE_API_KEY")if API_KEY: genai.configure(api_key=API_KEY)
# Page configurationst.set_page_config( page_title="YouTube Video Summarizer", page_icon="🎥", layout="wide")
st.title("Phidata YouTube Video Summarizer Agent 🎥🎤🖬")st.header("Powered by Gemini 2.0 Flash Exp")
@st.cache_resourcedef initialize_agent(): return Agent( name="YouTube Video Summarizer", model=Gemini(id="gemini-2.0-flash-exp"), tools=[DuckDuckGo()], markdown=True, )
# Initialize the agentmultimodal_Agent = initialize_agent()
# Function to get captions using youtube-transcript-apidef get_youtube_captions(youtube_url):45 unmodified lines else: st.error(captions)
# User query input user_query = st.text_area( "What insights are you seeking from the video?", placeholder="Ask anything about the video content. The AI agent will analyze and gather additional context if needed.", help="Provide specific questions or insights you want from the video." )
if st.button("🔍 Analyze Video", key="analyze_video_button"): if not user_query: st.warning("Please enter a question or insight to analyze the video.") else: try: with st.spinner("Analyzing video and gathering insights..."): # Upload and process the transcript (captions) processed_captions = upload_file(captions) while processed_captions.state.name == "PROCESSING": time.sleep(1) processed_captions = get_file(processed_captions.name)
# Prompt generation for analysis analysis_prompt = ( f""" Analyze the content of the uploaded YouTube video. Respond to the following query using video insights and supplementary web research: {user_query} Provide a detailed, user-friendly, and actionable response. """ )
# AI agent processing response = multimodal_Agent.run(analysis_prompt, videos=[processed_captions])
# Display the result st.subheader("Analysis Result") st.markdown(response.content)
except Exception as error: st.error(f"An error occurred during analysis: {error}") except Exception as e: st.error(f"An error occurred: {e}")else:app.py-78