Image_Chatbot / app.py
jpjp9292's picture
Update app.py
6974b92 verified
import streamlit as st
import google.generativeai as genai
import numpy as np
import PIL.Image
# Configure the Generative AI API
genai.configure(api_key="Api_key")
# Define the function that handles the model's response
def ImageChat(image, prompt):
# Load model
model = genai.GenerativeModel("gemini-1.5-flash")
# Check if the image is a numpy array and convert to PIL format if necessary
if isinstance(image, np.ndarray):
img = PIL.Image.fromarray(image)
else:
img = PIL.Image.open(image)
# Generate a response from the model
response = model.generate_content([prompt, img])
return response.text
# Set up the Streamlit interface
st.title("Image Chat")
st.write("Upload an image and enter a prompt to get a response.")
# File uploader for image input
uploaded_image = st.file_uploader("Upload an Image", type=["jpg", "jpeg", "png"])
# Text input for prompt
prompt = st.text_input("Enter your prompt:")
# When both an image and a prompt are provided, generate a response
if uploaded_image and prompt:
# Convert the uploaded image to a numpy array
image = np.array(PIL.Image.open(uploaded_image))
# Generate and display the response
response = ImageChat(image, prompt)
st.write("Response:", response)