Spaces:
Running
Running
louiecerv
commited on
Commit
·
58c5e3e
1
Parent(s):
c47c623
Save
Browse files- app.py +67 -0
- requirements.txt +2 -0
app.py
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from PIL import Image
|
3 |
+
import google.generativeai as genai
|
4 |
+
import os
|
5 |
+
|
6 |
+
|
7 |
+
MODEL_ID = "gemini-2.0-flash-exp"
|
8 |
+
api_key = os.getenv("GEMINI_API_KEY")
|
9 |
+
|
10 |
+
# Initialize chat history
|
11 |
+
if "messages" not in st.session_state:
|
12 |
+
st.session_state.messages = []
|
13 |
+
|
14 |
+
# Function to reset chat history
|
15 |
+
def reset_chat():
|
16 |
+
st.session_state.messages = []
|
17 |
+
|
18 |
+
# Streamlit app
|
19 |
+
st.title("Gemini Image Chat")
|
20 |
+
|
21 |
+
model_id=MODEL_ID
|
22 |
+
genai.configure(api_key=api_key)
|
23 |
+
model = genai.GenerativeModel(model_id)
|
24 |
+
chat = model.start_chat()
|
25 |
+
|
26 |
+
# File uploader
|
27 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
28 |
+
|
29 |
+
if uploaded_file is not None:
|
30 |
+
# Display the uploaded image
|
31 |
+
image = Image.open(uploaded_file)
|
32 |
+
st.image(image, caption="Uploaded Image.", use_container_width=True)
|
33 |
+
|
34 |
+
# Reset chat history when a new image is uploaded
|
35 |
+
reset_chat()
|
36 |
+
|
37 |
+
# Text input for user prompt
|
38 |
+
user_input = st.text_input("Enter your prompt:")
|
39 |
+
|
40 |
+
# Send button
|
41 |
+
if st.button("Send"):
|
42 |
+
if user_input:
|
43 |
+
# Add user message to chat history
|
44 |
+
st.session_state.messages.append({"role": "user", "content": user_input})
|
45 |
+
|
46 |
+
# Display chat history
|
47 |
+
for message in st.session_state.messages:
|
48 |
+
with st.chat_message(message["role"]):
|
49 |
+
st.markdown(message["content"])
|
50 |
+
|
51 |
+
|
52 |
+
img_file = genai.upload_file(uploaded_file, mime_type="image/jpeg")
|
53 |
+
|
54 |
+
# Send image and prompt to Gemini API
|
55 |
+
response = model.generate_content(
|
56 |
+
[
|
57 |
+
user_input,
|
58 |
+
img_file
|
59 |
+
]
|
60 |
+
)
|
61 |
+
|
62 |
+
# Add Gemini response to chat history
|
63 |
+
st.session_state.messages.append({"role": "assistant", "content": response.text})
|
64 |
+
|
65 |
+
# Display Gemini response
|
66 |
+
with st.chat_message("assistant"):
|
67 |
+
st.markdown(response.text)
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
google-generativeai
|