Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +64 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import google.generativeai as genai
|
3 |
+
from PIL import Image
|
4 |
+
import os
|
5 |
+
|
6 |
+
# Loading the environment variables
|
7 |
+
from dotenv import load_dotenv
|
8 |
+
load_dotenv()
|
9 |
+
|
10 |
+
# Configuring the API key...
|
11 |
+
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
12 |
+
|
13 |
+
def get_gemini_response(input_prompt, image):
|
14 |
+
model = genai.GenerativeModel("gemini-pro-vision")
|
15 |
+
response = model.generate_content([input_prompt, image[0]])
|
16 |
+
return response.text
|
17 |
+
|
18 |
+
def input_image_setup(uploaded_file):
|
19 |
+
# Check if a file has been uploaded
|
20 |
+
if uploaded_file is not None:
|
21 |
+
# Read the file into bytes
|
22 |
+
bytes_data = uploaded_file.getvalue()
|
23 |
+
image_parts = [
|
24 |
+
{
|
25 |
+
"mime_type": uploaded_file.type, # Get the mime type of the uploaded file
|
26 |
+
"data": bytes_data
|
27 |
+
}
|
28 |
+
]
|
29 |
+
return image_parts
|
30 |
+
else:
|
31 |
+
raise FileNotFoundError("No file uploaded")
|
32 |
+
|
33 |
+
|
34 |
+
##initialize our streamlit app
|
35 |
+
st.set_page_config(page_title="Gemini Health App")
|
36 |
+
|
37 |
+
st.header("Gemini Health App")
|
38 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
39 |
+
image=""
|
40 |
+
if uploaded_file is not None:
|
41 |
+
image = Image.open(uploaded_file)
|
42 |
+
st.image(image, caption="Uploaded Image.", use_column_width=True)
|
43 |
+
|
44 |
+
submit=st.button("Tell me the total calories")
|
45 |
+
|
46 |
+
input_prompt="""
|
47 |
+
You are an expert in nutritionist where you need to see the food items from the image
|
48 |
+
and calculate the total calories, also provide the details of every food items with calories intake
|
49 |
+
in the below format
|
50 |
+
|
51 |
+
1. Item 1 - no of calories
|
52 |
+
2. Item 2 - no of calories
|
53 |
+
----
|
54 |
+
----
|
55 |
+
Finally you can also mention whether the food item is healthy or not and also mention the percentage split of the ratio of the carbohydrates, fats, fibers, sugar and other important nutrients that are required in a diet.
|
56 |
+
"""
|
57 |
+
|
58 |
+
## If submit button is clicked
|
59 |
+
if submit:
|
60 |
+
image_data=input_image_setup(uploaded_file)
|
61 |
+
response=get_gemini_response(input_prompt, image_data)
|
62 |
+
st.header("The Response is")
|
63 |
+
st.write(response)
|
64 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
google-generativeai
|
3 |
+
python-dotenv
|