Update app.py
Browse files
app.py
CHANGED
|
@@ -2,41 +2,55 @@
|
|
| 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
|
| 9 |
from PIL import Image
|
| 10 |
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
#Fetch the API key that is defined inside .env
|
| 15 |
genai.configure(api_key=GOOGLE_API_KEY)
|
| 16 |
|
| 17 |
-
#Function that loads Gemini Model and returns the response
|
| 18 |
def get_model_response(query, user_image):
|
| 19 |
model = genai.GenerativeModel('gemini-pro-vision')
|
| 20 |
if query != "":
|
| 21 |
model_response = model.generate_content([query, user_image])
|
| 22 |
else:
|
| 23 |
model_response = model.generate_content(user_image)
|
| 24 |
-
return model_response
|
| 25 |
|
| 26 |
-
#Set up Streamlit application to visualize results
|
| 27 |
st.set_page_config(page_title="Visualize Image-Text")
|
| 28 |
st.header("Poulta App Image Analyses")
|
| 29 |
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
uploaded_file = st.file_uploader("Pick your image ...", type=["jpg", "jpeg", "png"])
|
| 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 |
-
submit=st.button("See the magic ✨")
|
| 39 |
if submit:
|
| 40 |
-
model_response=get_model_response(user_input, image)
|
| 41 |
st.subheader("Results here 👇🏻")
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 generativeai as genai
|
| 9 |
from PIL import Image
|
| 10 |
|
| 11 |
+
GOOGLE_API_KEY = "AIzaSyBcOP9DmVuhp3tzmv-y5WJioaAGW1PQouQ"
|
| 12 |
|
| 13 |
+
# Fetch the API key that is defined inside .env
|
|
|
|
|
|
|
| 14 |
genai.configure(api_key=GOOGLE_API_KEY)
|
| 15 |
|
| 16 |
+
# Function that loads Gemini Model and returns the response
|
| 17 |
def get_model_response(query, user_image):
|
| 18 |
model = genai.GenerativeModel('gemini-pro-vision')
|
| 19 |
if query != "":
|
| 20 |
model_response = model.generate_content([query, user_image])
|
| 21 |
else:
|
| 22 |
model_response = model.generate_content(user_image)
|
| 23 |
+
return model_response
|
| 24 |
|
| 25 |
+
# Set up Streamlit application to visualize results
|
| 26 |
st.set_page_config(page_title="Visualize Image-Text")
|
| 27 |
st.header("Poulta App Image Analyses")
|
| 28 |
|
| 29 |
+
# Add logo
|
| 30 |
+
logo_image_url = "https://poulta.com/assets/site-images/poulta-logo.png"
|
| 31 |
+
st.image(logo_image_url, width=200) # Adjust width as needed
|
| 32 |
+
|
| 33 |
+
user_input = st.text_input("What do you want to know about the image? ", key="user_input")
|
| 34 |
uploaded_file = st.file_uploader("Pick your image ...", type=["jpg", "jpeg", "png"])
|
| 35 |
|
| 36 |
+
image = ""
|
| 37 |
if uploaded_file is not None:
|
| 38 |
image = Image.open(uploaded_file)
|
| 39 |
st.image(image, caption="Uploaded Image.", use_column_width=True)
|
| 40 |
|
| 41 |
+
submit = st.button("See the magic ✨")
|
| 42 |
if submit:
|
| 43 |
+
model_response = get_model_response(user_input, image)
|
| 44 |
st.subheader("Results here 👇🏻")
|
| 45 |
+
|
| 46 |
+
# Check if response has prompt_feedback
|
| 47 |
+
if hasattr(model_response, 'prompt_feedback'):
|
| 48 |
+
print("Prompt Feedback:", model_response.prompt_feedback)
|
| 49 |
+
|
| 50 |
+
if model_response.prompt_feedback == "blocked":
|
| 51 |
+
st.write("The prompt was blocked. Try a different input.")
|
| 52 |
+
else:
|
| 53 |
+
st.write("No candidates were returned.")
|
| 54 |
+
else:
|
| 55 |
+
# Display the results if available
|
| 56 |
+
st.write(model_response.text)
|