Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import google.generativeai as genai
|
| 3 |
+
import numpy as np
|
| 4 |
+
import PIL.Image
|
| 5 |
+
|
| 6 |
+
# Configure the Generative AI API
|
| 7 |
+
genai.configure(api_key="AIzaSyAs726sBS7iWEO7ql2LFbvCxJ_bzbpDbiI")
|
| 8 |
+
|
| 9 |
+
# Define the function that handles the model's response
|
| 10 |
+
def ImageChat(image, prompt):
|
| 11 |
+
# Load model
|
| 12 |
+
model = genai.GenerativeModel("gemini-1.5-flash")
|
| 13 |
+
|
| 14 |
+
# Check if the image is a numpy array and convert to PIL format if necessary
|
| 15 |
+
if isinstance(image, np.ndarray):
|
| 16 |
+
img = PIL.Image.fromarray(image)
|
| 17 |
+
else:
|
| 18 |
+
img = PIL.Image.open(image)
|
| 19 |
+
|
| 20 |
+
# Generate a response from the model
|
| 21 |
+
response = model.generate_content([prompt, img])
|
| 22 |
+
|
| 23 |
+
return response.text
|
| 24 |
+
|
| 25 |
+
# Set up the Streamlit interface
|
| 26 |
+
st.title("Image Chat")
|
| 27 |
+
st.write("Upload an image and enter a prompt to get a response.")
|
| 28 |
+
|
| 29 |
+
# File uploader for image input
|
| 30 |
+
uploaded_image = st.file_uploader("Upload an Image", type=["jpg", "jpeg", "png"])
|
| 31 |
+
|
| 32 |
+
# Text input for prompt
|
| 33 |
+
prompt = st.text_input("Enter your prompt:")
|
| 34 |
+
|
| 35 |
+
# When both an image and a prompt are provided, generate a response
|
| 36 |
+
if uploaded_image and prompt:
|
| 37 |
+
# Convert the uploaded image to a numpy array
|
| 38 |
+
image = np.array(PIL.Image.open(uploaded_image))
|
| 39 |
+
|
| 40 |
+
# Generate and display the response
|
| 41 |
+
response = ImageChat(image, prompt)
|
| 42 |
+
st.write("Response:", response)
|