Update app.py
Browse files
app.py
CHANGED
@@ -1,82 +1,63 @@
|
|
1 |
import streamlit as st
|
2 |
-
|
3 |
-
|
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="
|
12 |
-
page_icon="
|
13 |
layout="wide"
|
14 |
)
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
-
|
41 |
-
|
42 |
-
|
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 |
+
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|