chido10's picture
Update app.py
0efacbb verified
### HeartFuel NutriGuide APP
from dotenv import load_dotenv
load_dotenv() ## load all the environment variables
import streamlit as st
import os
import google.generativeai as genai
from PIL import Image
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
## Function to load Google Gemini Pro Vision API And get response
def get_gemini_repsonse(input,image,prompt):
model=genai.GenerativeModel('gemini-pro-vision')
response=model.generate_content([input,image[0],prompt])
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")
##initialize our streamlit app
# Set the page title
st.set_page_config(page_title="HeartFuel NutriGuide")
# Colorful header
st.markdown("<h1 style='color: #009933;'>HeartFuel NutriGuide</h1>", unsafe_allow_html=True)
# Colorful text
st.markdown("<span style='color:#0066cc;'>**Regulating food intake for CVD Risk patients**</span><br>by <span style='color:#ff3399;'>***Chidozie Louis Uzoegwu***</span>", unsafe_allow_html=True)
input=st.text_input("Ask any Question about this food: ",key="input")
uploaded_file = st.file_uploader("Choose an 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)
submit=st.button("Calculate Nutrition Profile")
input_prompt="""
As a nutritionist specialist in cardiovascular health, your task is to analyze the nutritional content of the food items in the provided image. For each item, list the following nutrients in a table:
Food Item
Calories
Omega-3 (mg)
Fiber (g)
Antioxidants
Potassium (mg)
Magnesium (mg)
After listing the nutrients, provide a summary of the overall impact of these nutrients on heart health. If any items are found to be less than ideal, suggest alternative heart-healthy options.
"""
## If submit button is clicked
if submit:
image_data=input_image_setup(uploaded_file)
response=get_gemini_repsonse(input_prompt,image_data,input)
st.markdown("**<h3 style='color: blue;'>RESULT:</h3>**", unsafe_allow_html=True)
st.write(response)