Manasa1 commited on
Commit
6df9adf
Β·
verified Β·
1 Parent(s): f0afab6

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +110 -0
app.py ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from phi.agent import Agent
3
+ from phi.model.google import Gemini
4
+ from phi.tools.duckduckgo import DuckDuckGo
5
+ from google.generativeai import upload_file, get_file
6
+ import google.generativeai as genai
7
+ import time
8
+ from pathlib import Path
9
+ import tempfile
10
+ from dotenv import load_dotenv
11
+ load_dotenv()
12
+ import os
13
+
14
+ API_KEY = os.getenv("GOOGLE_API_KEY")
15
+ if API_KEY:
16
+ genai.configure(api_key=API_KEY)
17
+
18
+ # Page configuration
19
+ st.set_page_config(
20
+ page_title="Multimodal AI Agent- Video Summarizer",
21
+ page_icon="πŸŽ₯",
22
+ layout="wide"
23
+ )
24
+
25
+ st.title("Phidata Video AI Summarizer Agent πŸŽ₯πŸŽ€πŸ–¬")
26
+ st.header("Powered by Gemini 2.0 Flash Exp")
27
+
28
+ @st.cache_resource
29
+ def initialize_agent():
30
+ return Agent(
31
+ name="Video AI Summarizer",
32
+ model=Gemini(id="gemini-2.0-flash-exp"),
33
+ tools=[DuckDuckGo()],
34
+ markdown=True,
35
+ )
36
+
37
+ ## Initialize the agent
38
+ multimodal_Agent = initialize_agent()
39
+
40
+ # File uploader
41
+ video_file = st.file_uploader(
42
+ "Upload a video file", type=['mp4', 'mov', 'avi'], help="Upload a video for AI analysis"
43
+ )
44
+
45
+ if video_file:
46
+ with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as temp_video:
47
+ temp_video.write(video_file.read())
48
+ video_path = temp_video.name
49
+
50
+ st.video(video_path, format="video/mp4", start_time=0)
51
+
52
+ user_query = st.text_area(
53
+ "What insights are you seeking from the video?",
54
+ value="Generate detailed student-style notes from this video, including key points, definitions, and important concepts.",
55
+ help="Provide specific questions or insights you want from the video."
56
+ )
57
+
58
+ if st.button("πŸ” Analyze Video", key="analyze_video_button"):
59
+ if not user_query:
60
+ st.warning("Please enter a question or insight to analyze the video.")
61
+ else:
62
+ try:
63
+ with st.spinner("Processing video and creating detailed notes..."):
64
+ # Upload and process video file
65
+ processed_video = upload_file(video_path)
66
+ while processed_video.state.name == "PROCESSING":
67
+ time.sleep(1)
68
+ processed_video = get_file(processed_video.name)
69
+
70
+ # Enhanced prompt for detailed student notes
71
+ analysis_prompt = (
72
+ f"""
73
+ Analyze the uploaded video thoroughly and create EXTREMELY DETAILED STUDENT NOTES.
74
+
75
+ Your notes should:
76
+ - Include a clear title and organized sections with headings
77
+ - Capture ALL key information, definitions, and concepts
78
+ - Use bullet points for clarity and organization
79
+ - Highlight important points, dates, names, and formulas
80
+ - Include examples to explain difficult concepts
81
+ - Provide comprehensive coverage like a diligent student would create
82
+
83
+ Additional context/question from user: {user_query}
84
+
85
+ Format the notes in a clean, organized manner with proper headings, subheadings, and emphasis on key points.
86
+ """
87
+ )
88
+
89
+ # AI agent processing
90
+ response = multimodal_Agent.run(analysis_prompt, videos=[processed_video])
91
+
92
+ # Display the result
93
+ st.subheader("πŸ“ Detailed Notes")
94
+ st.markdown(response.content)
95
+
96
+ # Simple download option
97
+ st.download_button(
98
+ label="Download Notes",
99
+ data=response.content,
100
+ file_name="video_notes.md",
101
+ mime="text/markdown",
102
+ )
103
+
104
+ except Exception as error:
105
+ st.error(f"An error occurred during analysis: {error}")
106
+ finally:
107
+ # Clean up temporary video file
108
+ Path(video_path).unlink(missing_ok=True)
109
+ else:
110
+ st.info("Upload a video file to begin analysis.")