Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from dotenv import load_dotenv
|
3 |
+
import streamlit as st
|
4 |
+
import google.generativeai as genai
|
5 |
+
from PIL import Image
|
6 |
+
|
7 |
+
## load the environment variables
|
8 |
+
load_dotenv()
|
9 |
+
|
10 |
+
## configure the google with apikey
|
11 |
+
genai.configure(api_key=os.getenv('GOOGLE_API_KEY'))
|
12 |
+
|
13 |
+
#to get response from geminipro
|
14 |
+
def get_response_gemini(input_prompt:str,image:dict):
|
15 |
+
model=genai.GenerativeModel('gemini-pro-vision')
|
16 |
+
response=model.generate_content([image,input_prompt])
|
17 |
+
return response.text
|
18 |
+
|
19 |
+
#to get the image data
|
20 |
+
def input_image_details(uploaded_file):
|
21 |
+
if uploaded_file is not None:
|
22 |
+
#Read the file into bytes
|
23 |
+
bytes_data=uploaded_file.getvalue()
|
24 |
+
|
25 |
+
image_parts=[
|
26 |
+
{
|
27 |
+
'mime_type':uploaded_file.type,
|
28 |
+
'data': bytes_data
|
29 |
+
}
|
30 |
+
]
|
31 |
+
return image_parts[0]
|
32 |
+
else:
|
33 |
+
raise FileNotFoundError('No file uploaded')
|
34 |
+
|
35 |
+
|
36 |
+
# To read the input_prompt
|
37 |
+
prompt=["""
|
38 |
+
You are a Calories Advisor app that helps users identify ingredients in a photo and provides calorie counts for
|
39 |
+
those ingredients.
|
40 |
+
|
41 |
+
Inputs:
|
42 |
+
|
43 |
+
Photo: Users will upload a photo containing food items.
|
44 |
+
|
45 |
+
Output:
|
46 |
+
you will tell what the are the food items in the uploaded photo with calories value in form of bullet points and you
|
47 |
+
should output a summary of the identified ingredients,
|
48 |
+
whether it is nutritious or not based on the calories
|
49 |
+
also give the ratio of carbohydrates,fibers,cholesterol and others information present in tht photo
|
50 |
+
|
51 |
+
"""]
|
52 |
+
|
53 |
+
## Streamlit app
|
54 |
+
st.set_page_config(page_title="Calorie Advisor App")
|
55 |
+
st.header('Calorie advisor app using Gemini Pro Vision')
|
56 |
+
uploaded_file=st.file_uploader('Upload photo: ',type=['jpg','jpeg','png'])
|
57 |
+
|
58 |
+
# Display the uploaded image
|
59 |
+
image=''
|
60 |
+
if uploaded_file is not None:
|
61 |
+
image = Image.open(uploaded_file)
|
62 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
63 |
+
|
64 |
+
submit=st.button('Tell me about this food')
|
65 |
+
|
66 |
+
if submit:
|
67 |
+
if uploaded_file is not None:
|
68 |
+
image_data=input_image_details(uploaded_file)
|
69 |
+
response=get_response_gemini(image_data,prompt[0])
|
70 |
+
st.header('The response is: ')
|
71 |
+
st.write(response)
|
72 |
+
else:
|
73 |
+
st.subheader('Please upload the photo')
|