Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import google.generativeai as genai
|
3 |
+
from PIL import Image
|
4 |
+
import io
|
5 |
+
|
6 |
+
st.set_page_config(page_title="Gemini AI Chat", layout="wide")
|
7 |
+
|
8 |
+
st.title("🤖 Gemini AI Chat Interface")
|
9 |
+
st.markdown("Chat with Google's Gemini AI models. Supports both text and image inputs.")
|
10 |
+
|
11 |
+
# Sidebar for settings
|
12 |
+
with st.sidebar:
|
13 |
+
api_key = st.text_input("Enter Google AI API Key", type="password")
|
14 |
+
model = st.selectbox(
|
15 |
+
"Select Model",
|
16 |
+
[
|
17 |
+
"gemini-1.5-pro",
|
18 |
+
"gemini-1.5-pro-vision-latest",
|
19 |
+
"gemini-1.0-pro",
|
20 |
+
"gemini-1.0-pro-vision-latest"
|
21 |
+
]
|
22 |
+
)
|
23 |
+
|
24 |
+
temperature = st.slider("Temperature", 0.0, 1.0, 0.7)
|
25 |
+
max_tokens = st.slider("Max Tokens", 1, 2048, 1000)
|
26 |
+
system_prompt = st.text_area("System Prompt (Optional)")
|
27 |
+
|
28 |
+
# Initialize session state for chat history
|
29 |
+
if "messages" not in st.session_state:
|
30 |
+
st.session_state.messages = []
|
31 |
+
|
32 |
+
# Display chat history
|
33 |
+
for message in st.session_state.messages:
|
34 |
+
with st.chat_message(message["role"]):
|
35 |
+
st.markdown(message["content"])
|
36 |
+
|
37 |
+
# File uploader for images
|
38 |
+
uploaded_file = st.file_uploader("Upload an image (optional)", type=["jpg", "jpeg", "png"])
|
39 |
+
uploaded_image = None
|
40 |
+
if uploaded_file is not None:
|
41 |
+
uploaded_image = Image.open(uploaded_file).convert('RGB')
|
42 |
+
st.image(uploaded_image, caption="Uploaded Image", use_column_width=True)
|
43 |
+
|
44 |
+
# Chat input
|
45 |
+
user_input = st.chat_input("Type your message here...")
|
46 |
+
|
47 |
+
if user_input and api_key:
|
48 |
+
try:
|
49 |
+
# Configure the API
|
50 |
+
genai.configure(api_key=api_key)
|
51 |
+
|
52 |
+
# Add user message to chat history
|
53 |
+
st.session_state.messages.append({"role": "user", "content": user_input})
|
54 |
+
with st.chat_message("user"):
|
55 |
+
st.markdown(user_input)
|
56 |
+
|
57 |
+
# Prepare the model and content
|
58 |
+
model_instance = genai.GenerativeModel(model_name=model)
|
59 |
+
|
60 |
+
content = [{"text": user_input}]
|
61 |
+
if uploaded_image:
|
62 |
+
content.append({"inline_data": {"mime_type": "image/jpeg", "data": uploaded_image}})
|
63 |
+
|
64 |
+
# Generate response
|
65 |
+
response = model_instance.generate_content(
|
66 |
+
content,
|
67 |
+
generation_config=genai.types.GenerationConfig(
|
68 |
+
temperature=temperature,
|
69 |
+
max_output_tokens=max_tokens
|
70 |
+
)
|
71 |
+
)
|
72 |
+
|
73 |
+
# Display assistant response
|
74 |
+
with st.chat_message("assistant"):
|
75 |
+
st.markdown(response.text)
|
76 |
+
|
77 |
+
# Add assistant response to chat history
|
78 |
+
st.session_state.messages.append({"role": "assistant", "content": response.text})
|
79 |
+
|
80 |
+
except Exception as e:
|
81 |
+
st.error(f"Error: {str(e)}")
|
82 |
+
|
83 |
+
elif not api_key and user_input:
|
84 |
+
st.warning("Please enter your API key in the sidebar first.")
|
85 |
+
|
86 |
+
# Instructions in the sidebar
|
87 |
+
with st.sidebar:
|
88 |
+
st.markdown("""
|
89 |
+
## 📝 Instructions:
|
90 |
+
1. Enter your Google AI API key
|
91 |
+
2. Select a model
|
92 |
+
3. Adjust temperature and max tokens if needed
|
93 |
+
4. Optional: Set a system prompt
|
94 |
+
5. Upload an image (optional)
|
95 |
+
6. Type your message and press Enter
|
96 |
+
""")
|