Spaces:
Sleeping
Sleeping
Create app.py
#1
by
TaJ001
- opened
app.py
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import os
|
3 |
+
import google.generativeai as genai
|
4 |
+
from dotenv import load_dotenv
|
5 |
+
load_dotenv() #Load all env variables
|
6 |
+
from PIL import Image
|
7 |
+
|
8 |
+
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
9 |
+
|
10 |
+
# Backend Function to load Gemini Pro Vision and get response
|
11 |
+
def get_gemini_repsonse(input, image, prompt):
|
12 |
+
model = genai.GenerativeModel('gemini-pro-vision')
|
13 |
+
response = model.generate_content([input, image[0], prompt])
|
14 |
+
return response.text
|
15 |
+
|
16 |
+
|
17 |
+
def input_image_setup(uploaded_file):
|
18 |
+
# Check if a file has been uploaded
|
19 |
+
if uploaded_file is not None:
|
20 |
+
# Read the file into bytes
|
21 |
+
bytes_data = uploaded_file.getvalue()
|
22 |
+
|
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 |
+
|
36 |
+
st.set_page_config(page_title="Cal Advisor App")
|
37 |
+
|
38 |
+
st.header("Cal Advisor App")
|
39 |
+
input = st.text_input("Input Prompt: ", key="input")
|
40 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
41 |
+
image = ""
|
42 |
+
if uploaded_file is not None:
|
43 |
+
image = Image.open(uploaded_file)
|
44 |
+
st.image(image, caption="Uploaded Image.", use_column_width=True)
|
45 |
+
|
46 |
+
submit = st.button("Tell me the total calories")
|
47 |
+
|
48 |
+
input_prompt = """
|
49 |
+
You are an expert in nutritionist where you need to see the food items from the image
|
50 |
+
and calculate the total calories, also provide the details of every food items with calories intake
|
51 |
+
is below format
|
52 |
+
1. Item 1 - no of calories
|
53 |
+
2. Item 2 - no of calories
|
54 |
+
----
|
55 |
+
----
|
56 |
+
"""
|
57 |
+
|
58 |
+
# If submit button is clicked
|
59 |
+
if submit:
|
60 |
+
image_data = input_image_setup(uploaded_file)
|
61 |
+
response = get_gemini_repsonse(input_prompt, image_data, input)
|
62 |
+
st.subheader("The Response is")
|
63 |
+
st.write(response)
|