iDrops's picture
Update app.py
1a9903c verified
import streamlit as st
import google.generativeai as genai
from PIL import Image
import os
genai.configure(api_key = os.getenv('GOOGLE_API_KEY'))
model=genai.GenerativeModel('gemini-1.5-pro-latest')
input_prompt = """
As a doctor specialized in healthcare, analyze the base contents of the food, if there are allergens warn about the risks and potential harm of the food, then recommend healthy food and exercies to balance the diet. Make the answer simple, concise, and friendly.
Output structure:
Start and end with (```)
"""
def generate_response(input_prompt,image):
response = model.generate_content([input_prompt,image[0]])
# print(response.text)
return response.text
def input_image_setup(uploaded_file):
# Check if a file has been uploaded
if uploaded_file is not None:
# Read the file into bytes
bytes_data = uploaded_file.getvalue()
image_parts = [
{
"mime_type": uploaded_file.type, # Get the mime type of the uploaded file
"data": bytes_data
}
]
return image_parts
else:
raise FileNotFoundError("No file uploaded")
st.title("Allergen analysis and Recommendation🍔")
st.text("Upload food image here:")
upload_file = st.file_uploader('',type=['jpg','jpeg','png'])
if upload_file is not None:
image = Image.open(upload_file)
st.image(image, caption="Uploaded Image", use_column_width=True)
submit = st.button('Analyze Contents')
if submit:
image_data = input_image_setup(uploaded_file=upload_file)
with st.spinner("Analyzing the contents..."):
response = generate_response(input_prompt, image_data)
st.subheader("OUTPUT:")
st.markdown(response)