poultaapp / app.py
poulta55's picture
Update app.py
d573a53 verified
# Load all environment variables
from dotenv import load_dotenv
load_dotenv()
# Import required packages and libraries
import streamlit as st # Streamlit to create a web application
import os
import google.generativeai as genai # The primary LLM model that resides inside genAI library
from PIL import Image
# Fetch the API key that is defined inside .env
google_api_key = os.getenv("GOOGLE_API_KEY")
if google_api_key is None:
st.error("API key not found. Please check your .env file.")
st.stop()
# Configure genAI library with the fetched API key
genai.configure(api_key=google_api_key)
# Function that loads Gemini Model and returns the response
def get_model_response(query, user_image):
# Create a GenerativeModel instance with the 'gemini-pro-vision' model
model = genai.GenerativeModel('gemini-pro-vision')
# Check if a query is provided and generate content accordingly
if query != "":
model_response = model.generate_content([query, user_image])
else:
model_response = model.generate_content(user_image)
# Extract and return the text from the model response
return model_response.text
# Set page configuration
st.set_page_config(page_title="Visualize Image-Text")
st.header("Poulta App Image Analyses")
# Add logo
logo_image_url = "https://poulta.com/assets/site-images/poulta-logo.png"
st.image(logo_image_url, width=200) # Adjust width as needed
# User input for query
user_input = st.text_input("What do you want to know about the image? ", key="user_input")
# File uploader for image
uploaded_file = st.file_uploader("Pick your image...", type=["jpg", "jpeg", "png"])
image = ""
if uploaded_file is not None:
image = Image.open(uploaded_file)
st.image(image, caption="Uploaded Image.", use_column_width=True)
# Button to trigger image analysis
submit = st.button("See the magic ✨")
if submit:
# Perform model analysis and display results
model_response = get_model_response(user_input, image)
st.subheader("Results here πŸ‘‡πŸ»")
st.write(model_response)