Update app.py
Browse files1234534 unmodified lines404142434445464748491 unmodified line515253545556575859606162636465666768697071727374757677784 unmodified lines838485868788899091929394957 unmodified lines1031041051061071081091101111121131141151161171181191201211 unmodified line123124125126127128129130import streamlit as stfrom pytube import YouTubefrom phi.agent import Agentfrom phi.model.google import Geminifrom phi.tools.duckduckgo import DuckDuckGo34 unmodified lines# Initialize the agentmultimodal_Agent = initialize_agent()# Function to get YouTube video captions using pytubedef get_youtube_captions(youtube_url):"""Retrieves YouTube video captions using pytube.Parameters:- youtube_url: The URL of the YouTube video.1 unmodified lineReturns:- The captions of the video in SRT format, or a message if captions are not available."""yt = YouTube(youtube_url)# List available caption trackscaption_tracks = yt.caption_tracksavailable_languages = [track.language_code for track in caption_tracks]if available_languages:st.write(f"Available caption languages: {', '.join(available_languages)}")# You can set a fallback language or prompt the user to selectlanguage = 'en' # Change this to any available language from the list# Check if the chosen language exists in the videoif language in yt.captions:caption = yt.captions[language]return caption.generate_srt_captions() # Return captions in SRT formatelse:return f"Captions not available in the selected language ({language})."else:return "No captions available for this video."# YouTube video URL inputyoutube_url = st.text_input(4 unmodified linesif youtube_url:try:with st.spinner("Fetching captions from the YouTube video..."):# Get captions for the videocaptions = get_youtube_captions(youtube_url)# Display captionsst.subheader("Video Captions (SRT format)")st.text(captions)# User query inputuser_query = st.text_area(7 unmodified linesst.warning("Please enter a question or insight to analyze the video.")else:try:with st.spinner("Analyzing captions and gathering insights..."):# Generate the analysis prompt using captionsanalysis_prompt = (f"""Analyze the content of the following YouTube video captions.Respond to the following query using the video captions and supplementary web research:{user_query}Provide a detailed, user-friendly, and actionable response.""")# AI agent processingresponse = multimodal_Agent.run(analysis_prompt, video_captions=captions)# Display the resultst.subheader("Analysis Result")1 unmodified lineexcept Exception as error:st.error(f"An error occurred during analysis: {error}")except Exception as e:st.error(f"An error occurred while fetching captions: {e}")else:st.info("Enter a YouTube video link to begin analysis.")123456734 unmodified lines424344454647484950511 unmodified line535455565758596061626364656667686970714 unmodified lines76777879808182838485868788899091927 unmodified lines1001011021031041051061071081091101111121131141151161171181191201211221231241 unmodified line126127128129130131132import 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 DuckDuckGo34 unmodified lines# Initialize the agentmultimodal_Agent = initialize_agent()# Function to get captions using youtube-transcript-apidef get_youtube_captions(youtube_url):"""Retrieves YouTube video captions using youtube-transcript-api.Parameters:- youtube_url: The URL of the YouTube video.1 unmodified lineReturns:- The captions of the video in SRT format, or a message if captions are not available."""video_id = youtube_url.split("v=")[-1].split("&")[0]try:# Fetch transcript for the videotranscript = YouTubeTranscriptApi.get_transcript(video_id)# Format the transcript in SRT formatformatter = SRTFormatter()formatted_transcript = formatter.format_transcript(transcript)return formatted_transcriptexcept Exception as e:return f"Error: {str(e)}"# YouTube video URL inputyoutube_url = st.text_input(4 unmodified linesif youtube_url:try:with st.spinner("Fetching and processing the YouTube video..."):# Get captions using youtube-transcript-apicaptions = get_youtube_captions(youtube_url)# Check if captions are availableif "Error" not in captions:# Display captionsst.subheader("Video Captions:")st.text(captions)else:st.error(captions)# User query inputuser_query = st.text_area(7 unmodified linesst.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 analysisanalysis_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 processingresponse = multimodal_Agent.run(analysis_prompt, videos=[processed_captions])# Display the resultst.subheader("Analysis Result")1 unmodified lineexcept Exception as error:st.error(f"An error occurred during analysis: {error}")except Exception as e:st.error(f"An error occurred: {e}")else:st.info("Enter a YouTube video link to begin analysis.")
app.py+37-35