Update app.py
Browse files39 unmodified lines404142434445464748495051525354555657585960616263646566676869707172734 unmodified lines7879808182838485868788897 unmodified lines9798991001011021031041051061071081091101111121131141151161171181191201211 unmodified line12312412512612712812913013113239 unmodified lines# Initialize the agentmultimodal_Agent = initialize_agent()# Function to download YouTube video using pytubedef download_youtube_video(youtube_url):"""Downloads a YouTube video using pytube.Parameters:- youtube_url: The URL of the YouTube video.Returns:- The path to the downloaded video file."""# Initialize YouTube object using pytubeyt = YouTube(youtube_url)# Get the highest resolution stream availablevideo_stream = yt.streams.get_highest_resolution()# Temporary file for the videowith tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as temp_video:temp_video_path = temp_video.nametry:# Download the video to the temporary filevideo_stream.download(output_path=temp_video_path)return temp_video_path # Return the video file pathexcept Exception as e:raise RuntimeError(f"An error occurred while downloading the video: {str(e)}")# YouTube video URL inputyoutube_url = st.text_input(4 unmodified linesif youtube_url:try:with st.spinner("Downloading and processing the YouTube video..."):# Download video using pytubevideo_path = download_youtube_video(youtube_url)# Display videost.video(video_path, format="video/mp4", start_time=0)# 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 video fileprocessed_video = upload_file(video_path)while processed_video.state.name == "PROCESSING":time.sleep(1)processed_video = get_file(processed_video.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_video])# Display the resultst.subheader("Analysis Result")1 unmodified lineexcept Exception as error:st.error(f"An error occurred during analysis: {error}")finally:# Clean up temporary video filePath(video_path).unlink(missing_ok=True)except Exception as e:st.error(f"An error occurred while downloading the video: {e}")else:st.info("Enter a YouTube video link to begin analysis.")39 unmodified lines404142434445464748495051525354555657585960616263644 unmodified lines697071727374757677787980817 unmodified lines89909192939495969798991001011021031041051061071 unmodified line10911011111211311411511639 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.Returns:- The captions of the video in SRT format, or a message if captions are not available."""yt = YouTube(youtube_url)# Check if captions are available in English (you can modify this language)if 'en' in yt.captions:caption = yt.captions['en']return caption.generate_srt_captions() # Return captions in SRT formatelse: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.")
app.py+23-39