EswarP's picture
Create app.py
9852d62 verified
import os
from dotenv import load_dotenv
import streamlit as st
import google.generativeai as genai
from PIL import Image
## load the environment variables
load_dotenv()
## configure the google with apikey
genai.configure(api_key=os.getenv('GOOGLE_API_KEY'))
#to get response from geminipro
def get_response_gemini(input_prompt:str,image:dict):
model=genai.GenerativeModel('gemini-pro-vision')
response=model.generate_content([image,input_prompt])
return response.text
#to get the image data
def input_image_details(uploaded_file):
if uploaded_file is not None:
#Read the file into bytes
bytes_data=uploaded_file.getvalue()
image_parts=[
{
'mime_type':uploaded_file.type,
'data': bytes_data
}
]
return image_parts[0]
else:
raise FileNotFoundError('No file uploaded')
# To read the input_prompt
prompt=["""
You are a Calories Advisor app that helps users identify ingredients in a photo and provides calorie counts for
those ingredients.
Inputs:
Photo: Users will upload a photo containing food items.
Output:
you will tell what the are the food items in the uploaded photo with calories value in form of bullet points and you
should output a summary of the identified ingredients,
whether it is nutritious or not based on the calories
also give the ratio of carbohydrates,fibers,cholesterol and others information present in tht photo
"""]
## Streamlit app
st.set_page_config(page_title="Calorie Advisor App")
st.header('Calorie advisor app using Gemini Pro Vision')
uploaded_file=st.file_uploader('Upload photo: ',type=['jpg','jpeg','png'])
# Display the uploaded image
image=''
if uploaded_file is not None:
image = Image.open(uploaded_file)
st.image(image, caption="Uploaded Image", use_column_width=True)
submit=st.button('Tell me about this food')
if submit:
if uploaded_file is not None:
image_data=input_image_details(uploaded_file)
response=get_response_gemini(image_data,prompt[0])
st.header('The response is: ')
st.write(response)
else:
st.subheader('Please upload the photo')