Update app.py
Browse files123456734 unmodified lines4243444345464746484950511 unmodified line535455545556575859606162636456575859606166676869707172737475626364656667686970714 unmodified lines767778868779808182838490919285868788899091927 unmodified lines1001011021061071031041051061071081091101111121101111131141151161171181191201181211221231241 unmodified line126127128126129128130131132import 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 YouTube video captions using pytube# Function to get captions using youtube-transcript-apidef get_youtube_captions(youtube_url): """ Retrieves YouTube video captions using pytube. Retrieves YouTube video captions using youtube-transcript-api. Parameters: - youtube_url: The URL of the YouTube video.1 unmodified line Returns: - The captions of the video in SRT format, or a message if captions are not available. """ yt = YouTube(youtube_url)
# List available caption tracks caption_tracks = yt.caption_tracks available_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 select language = 'en' # Change this to any available language from the list video_id = youtube_url.split("v=")[-1].split("&")[0] try: # Fetch transcript for the video transcript = YouTubeTranscriptApi.get_transcript(video_id) # Check if the chosen language exists in the video if language in yt.captions: caption = yt.captions[language] return caption.generate_srt_captions() # Return captions in SRT format else: return f"Captions not available in the selected language ({language})." else: return "No captions available for this video."
# Format the transcript in SRT format formatter = SRTFormatter() formatted_transcript = formatter.format_transcript(transcript) return formatted_transcript except Exception as e: return f"Error: {str(e)}"
# YouTube video URL inputyoutube_url = st.text_input(4 unmodified lines
if youtube_url: try: with st.spinner("Fetching captions from the YouTube video..."): # Get captions for the video with st.spinner("Fetching and processing the YouTube video..."): # Get captions using youtube-transcript-api captions = get_youtube_captions(youtube_url)
# Check if captions are available if "Error" not in captions: # Display captions st.subheader("Video Captions (SRT format)") st.text(captions) # Display captions st.subheader("Video Captions:") st.text(captions) else: st.error(captions)
# User query input user_query = st.text_area(7 unmodified lines st.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 captions 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 following YouTube video captions. Respond to the following query using the video captions and supplementary web research: 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, video_captions=captions) response = multimodal_Agent.run(analysis_prompt, videos=[processed_captions])
# Display the result st.subheader("Analysis Result")1 unmodified line
except 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}") st.error(f"An error occurred: {e}")else: st.info("Enter a YouTube video link to begin analysis.")app.py+37-35