CoolFace
Apppublic

JaganathC/Smart_Video_To_Text_Conv

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
app.py157 linesDownload Raw Back to root
1import streamlit as st2from phi.agent import Agent3from phi.model.google import Gemini4from phi.tools.duckduckgo import DuckDuckGo5from google.generativeai import upload_file, get_file6import google.generativeai as genai7import time8from pathlib import Path9import tempfile10from dotenv import load_dotenv11import os12from phi.model.groq import Groq13from phi.tools.youtube_tools import YouTubeTools14 15# Load environment variables16load_dotenv()17 18# Configure API keys19API_KEY = os.getenv("GOOGLE_API_KEY")20groq_api_key = os.getenv("GROQ_API_KEY")21if API_KEY:22    genai.configure(api_key=API_KEY)23 24# Page configuration25st.set_page_config(26    page_title="Multimodal AI Applications",27    page_icon="๐ŸŒ",28    layout="wide"29)30 31# Tabs for the two applications32tab1, tab2 = st.tabs(["Video Summarizer", "YouTube Video Analyzer"])33 34# Tab 1: Video Summarizer with Gemini35with tab1:36    st.title("Phidata Video AI Summarizer Agent ๐ŸŽฅ๐ŸŽค๐Ÿ–ฌ")37    st.header("Powered by Gemini 2.0 Flash Exp")38 39    @st.cache_resource40    def initialize_agent():41        return Agent(42            name="Video AI Summarizer",43            model=Gemini(id="gemini-2.0-flash-exp"),44            tools=[DuckDuckGo()],45            markdown=True,46        )47 48    multimodal_Agent = initialize_agent()49 50    video_file = st.file_uploader(51        "Upload a video file", 52        type=['mp4'], 53        help="Upload a video for AI analysis"54    )55 56    if video_file:57        with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as temp_video:58            temp_video.write(video_file.read())59            video_path = temp_video.name60 61        st.video(video_path, format="video/mp4", start_time=0)62 63        user_query = st.text_area(64            "What insights are you seeking from the video?",65            placeholder="Ask anything about the video content. The AI agent will analyze and gather additional context if needed. This AI agent can search the internet.",66            help="Provide specific questions or insights you want from the video."67        )68 69        if st.button("๐Ÿ” Analyze Video", key="analyze_video_button"):70            if not user_query:71                st.warning("Please enter a question or insight to analyze the video.")72            else:73                try:74                    with st.spinner("Processing video and gathering insights..."):75                        processed_video = upload_file(video_path)76 77                        while processed_video.state.name == "PROCESSING":78                            time.sleep(1)79                            processed_video = get_file(processed_video.name)80 81                        analysis_prompt = (82                            f"""83                            Analyze the uploaded video for content and context. Provide a detailed summary. Search the internet for similar topics discussed in the uploaded video.84                            Respond to the following query using video insights and supplementary web research: {user_query}85                            Provide a detailed, user-friendly, and actionable response. 86                            """87                        )88 89                        response = multimodal_Agent.run(analysis_prompt, videos=[processed_video])90 91                        st.subheader("Analysis Result")92                        st.markdown(response.content)93 94                except Exception as error:95                    st.error(f"An error occurred during analysis: {error}")96 97                finally:98                    Path(video_path).unlink(missing_ok=True)99    else:100        st.info("Upload a video file to begin analysis.")101 102# Tab 2: YouTube Video Analyzer with Groq103with tab2:104    st.title("YouTube Video Analyzer")105    st.header("Analyze Youtube video using video link ๐ŸŽฌ ๐Ÿงท")106 107    try:108        youtube_agent = Agent(109            model=Groq(id="llama3-8b-8192", api_key=groq_api_key),110            tools=[YouTubeTools(), DuckDuckGo()],111            show_tool_calls=True,112            get_video_captions=True,113            get_video_data = True,114            description="You are a YouTube agent. Obtain the captions of a YouTube video, analyze its content and context, provide detailed summaries with key points, conduct web research on similar topics, and answer user questions.",115        )116    except Exception as e:117        st.error(f"Error initializing the agent: {e}")118        st.stop()119 120    video_url = st.text_input("Enter YouTube Video URL:", "")121    user_query = st.text_area("Enter your question about the video (optional):", "")122 123    if st.button("โœจ Analyze the Video", key="Analyze_Video_Button"):124        if video_url:125            with st.spinner("Analyzing..."):126                try:127                    prompt_prefix = """Analyze the YouTube video provided by the URL. Provide a detailed summary of the video, including key points. 128                    Structure the output as follows:129                    **Detailed Summary:**130                    [Detailed summary of the video content]131                    **Key Points:**132                    * [Key point 1]133                    * [Key point 2]134                    * [Key point 3]135                    ... (and so on)136                    Conduct web research on similar topics discussed in the video to provide additional context.137                    """138 139                    if user_query:140                        prompt = f"""{prompt_prefix}141                                 Respond to the following query using video insights and supplementary web research:142                                          {user_query}143                                 Provide a detailed, user-friendly, and actionable response.144                                 Video URL: {video_url}"""145                    else:146                        prompt = f"""{prompt_prefix}147                                        Video URL: {video_url}"""148 149                    output = youtube_agent.run(prompt)150 151                    st.markdown(output.content)152 153                except Exception as e:154                    st.error(f"Error analyzing the video: {e}")155        else:156            st.warning("Please enter a YouTube video URL.")157