Docfile commited on
Commit
9ee01a9
·
verified ·
1 Parent(s): f4e0bf4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -74
app.py CHANGED
@@ -1,82 +1,63 @@
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 time
7
- from pathlib import Path
8
- import tempfile
9
 
 
10
  st.set_page_config(
11
- page_title="Multimodal AI Agent",
12
- page_icon="🧬",
13
  layout="wide"
14
  )
15
 
16
- st.title("Multimodal AI Agent 🧬")
17
-
18
- # Initialize single agent with both capabilities
19
- @st.cache_resource
20
- def initialize_agent():
21
- return Agent(
22
- name="Multimodal Analyst",
23
- model=Gemini(id="gemini-2.0-flash-exp"),
24
- tools=[DuckDuckGo()],
25
- markdown=True,
26
- )
27
-
28
- agent = initialize_agent()
29
-
30
- # File uploader
31
- uploaded_file = st.file_uploader("Upload a video file", type=['mp4', 'mov', 'avi'])
32
-
33
- if uploaded_file:
34
- with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as tmp_file:
35
- tmp_file.write(uploaded_file.read())
36
- video_path = tmp_file.name
37
-
38
- st.video(video_path)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
 
40
- user_prompt = st.text_area(
41
- "What would you like to know?",
42
- placeholder="Ask any question related to the video - the AI Agent will analyze it and search the web if needed",
43
- help="You can ask questions about the video content and get relevant information from the web"
44
- )
45
-
46
- if st.button("Analyze & Research"):
47
- if not user_prompt:
48
- st.warning("Please enter your question.")
49
- else:
50
- try:
51
- with st.spinner("Processing video and researching..."):
52
- video_file = upload_file(video_path)
53
- while video_file.state.name == "PROCESSING":
54
- time.sleep(2)
55
- video_file = get_file(video_file.name)
56
-
57
- prompt = f"""
58
- First analyze this video and then answer the following question using both
59
- the video analysis and web research: {user_prompt}
60
-
61
- Provide a comprehensive response focusing on practical, actionable information.
62
- """
63
-
64
- result = agent.run(prompt, videos=[video_file])
65
-
66
- st.subheader("Result")
67
- st.markdown(result.content)
68
-
69
- except Exception as e:
70
- st.error(f"An error occurred: {str(e)}")
71
- finally:
72
- Path(video_path).unlink(missing_ok=True)
73
- else:
74
- st.info("Please upload a video to begin analysis.")
75
-
76
- st.markdown("""
77
- <style>
78
- .stTextArea textarea {
79
- height: 100px;
80
- }
81
- </style>
82
- """, unsafe_allow_html=True)
 
1
  import streamlit as st
2
+ import google.generativeai as genai
3
+ import os
 
 
4
  import time
 
 
5
 
6
+ # Setup page config
7
  st.set_page_config(
8
+ page_title="Gemini Streaming Demo",
9
+ page_icon="🤖",
10
  layout="wide"
11
  )
12
 
13
+ # Create a title
14
+ st.title("🤖 Gemini API Streaming Demo")
15
+
16
+ # Configure the Gemini API with environment variable
17
+ genai.configure(api_key=os.environ['GOOGLE_API_KEY'])
18
+
19
+ # Initialize the model
20
+ model = genai.GenerativeModel('gemini-1.5-flash')
21
+
22
+ # Create input text area
23
+ user_input = st.text_area("Enter your prompt:", height=100)
24
+
25
+ # Create a button to generate content
26
+ if st.button("Generate", type="primary"):
27
+ if user_input:
28
+ # Create a placeholder for the streaming text
29
+ response_placeholder = st.empty()
30
+
31
+ # Initialize full_response
32
+ full_response = ""
33
+
34
+ # Generate streaming response
35
+ try:
36
+ response = model.generate_content(user_input, stream=True)
37
+
38
+ # Stream the response
39
+ for chunk in response:
40
+ if chunk.text:
41
+ full_response += chunk.text
42
+ # Update the placeholder with the full response so far
43
+ response_placeholder.markdown(full_response + "▌")
44
+ time.sleep(0.05) # Add a small delay for better visualization
45
+
46
+ # Final update without the cursor
47
+ response_placeholder.markdown(full_response)
48
+
49
+ except Exception as e:
50
+ st.error(f"An error occurred: {str(e)}")
51
+ else:
52
+ st.warning("Please enter a prompt first.")
53
+
54
+ # Add instructions in the sidebar
55
+ with st.sidebar:
56
+ st.markdown("""
57
+ ### Instructions
58
+ 1. Type your prompt in the text area
59
+ 2. Click 'Generate' to see the streaming response
60
 
61
+ ### About
62
+ This demo shows how to use Gemini's streaming capabilities to generate text responses in real-time.
63
+ """)