Update app.py
Browse files
app.py
CHANGED
|
@@ -1,25 +1,38 @@
|
|
| 1 |
-
#
|
| 2 |
from dotenv import load_dotenv
|
| 3 |
load_dotenv()
|
| 4 |
|
| 5 |
-
#Import required packages and libraries
|
| 6 |
-
import streamlit as st
|
| 7 |
import os
|
| 8 |
-
import google.generativeai as genai
|
| 9 |
from PIL import Image
|
| 10 |
|
| 11 |
-
#Fetch the API key that is defined inside .env
|
| 12 |
-
|
| 13 |
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
def get_model_response(query, user_image):
|
|
|
|
| 16 |
model = genai.GenerativeModel('gemini-pro-vision')
|
|
|
|
|
|
|
| 17 |
if query != "":
|
| 18 |
model_response = model.generate_content([query, user_image])
|
| 19 |
else:
|
| 20 |
model_response = model.generate_content(user_image)
|
|
|
|
|
|
|
| 21 |
return model_response.text
|
| 22 |
|
|
|
|
| 23 |
st.set_page_config(page_title="Visualize Image-Text")
|
| 24 |
st.header("Poulta App Image Analyses")
|
| 25 |
|
|
@@ -27,16 +40,27 @@ st.header("Poulta App Image Analyses")
|
|
| 27 |
logo_image_url = "https://poulta.com/assets/site-images/poulta-logo.png"
|
| 28 |
st.image(logo_image_url, width=200) # Adjust width as needed
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
image=""
|
| 34 |
if uploaded_file is not None:
|
| 35 |
image = Image.open(uploaded_file)
|
| 36 |
st.image(image, caption="Uploaded Image.", use_column_width=True)
|
| 37 |
|
| 38 |
-
|
|
|
|
| 39 |
if submit:
|
| 40 |
-
|
|
|
|
| 41 |
st.subheader("Results here 👇🏻")
|
| 42 |
-
st.write(model_response)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Load all environment variables
|
| 2 |
from dotenv import load_dotenv
|
| 3 |
load_dotenv()
|
| 4 |
|
| 5 |
+
# Import required packages and libraries
|
| 6 |
+
import streamlit as st # Streamlit to create a web application
|
| 7 |
import os
|
| 8 |
+
import google.generativeai as genai # The primary LLM model that resides inside genAI library
|
| 9 |
from PIL import Image
|
| 10 |
|
| 11 |
+
# Fetch the API key that is defined inside .env
|
| 12 |
+
google_api_key = os.getenv("GOOGLE_API_KEY")
|
| 13 |
|
| 14 |
+
if google_api_key is None:
|
| 15 |
+
st.error("API key not found. Please check your .env file.")
|
| 16 |
+
st.stop()
|
| 17 |
+
|
| 18 |
+
# Configure genAI library with the fetched API key
|
| 19 |
+
genai.configure(api_key=google_api_key)
|
| 20 |
+
|
| 21 |
+
# Function that loads Gemini Model and returns the response
|
| 22 |
def get_model_response(query, user_image):
|
| 23 |
+
# Create a GenerativeModel instance with the 'gemini-pro-vision' model
|
| 24 |
model = genai.GenerativeModel('gemini-pro-vision')
|
| 25 |
+
|
| 26 |
+
# Check if a query is provided and generate content accordingly
|
| 27 |
if query != "":
|
| 28 |
model_response = model.generate_content([query, user_image])
|
| 29 |
else:
|
| 30 |
model_response = model.generate_content(user_image)
|
| 31 |
+
|
| 32 |
+
# Extract and return the text from the model response
|
| 33 |
return model_response.text
|
| 34 |
|
| 35 |
+
# Set page configuration
|
| 36 |
st.set_page_config(page_title="Visualize Image-Text")
|
| 37 |
st.header("Poulta App Image Analyses")
|
| 38 |
|
|
|
|
| 40 |
logo_image_url = "https://poulta.com/assets/site-images/poulta-logo.png"
|
| 41 |
st.image(logo_image_url, width=200) # Adjust width as needed
|
| 42 |
|
| 43 |
+
# User input for query
|
| 44 |
+
user_input = st.text_input("What do you want to know about the image? ", key="user_input")
|
| 45 |
+
|
| 46 |
+
# File uploader for image
|
| 47 |
+
uploaded_file = st.file_uploader("Pick your image...", type=["jpg", "jpeg", "png", "pdf"])
|
| 48 |
|
| 49 |
+
image = ""
|
| 50 |
if uploaded_file is not None:
|
| 51 |
image = Image.open(uploaded_file)
|
| 52 |
st.image(image, caption="Uploaded Image.", use_column_width=True)
|
| 53 |
|
| 54 |
+
# Button to trigger image analysis
|
| 55 |
+
submit = st.button("See the magic ✨")
|
| 56 |
if submit:
|
| 57 |
+
# Perform model analysis and display results
|
| 58 |
+
model_response = get_model_response(user_input, image)
|
| 59 |
st.subheader("Results here 👇🏻")
|
| 60 |
+
st.write(model_response)
|
| 61 |
+
|
| 62 |
+
# Add some markdown text for instructions or information
|
| 63 |
+
st.markdown("### Instructions:")
|
| 64 |
+
st.markdown("1. Enter a query about the image.")
|
| 65 |
+
st.markdown("2. Upload an image.")
|
| 66 |
+
st.markdown("3. Click the 'See the magic ✨' button to get results.")
|