-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
68 lines (57 loc) · 2.3 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import streamlit as st
from langchain_community.embeddings import OpenAIEmbeddings
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
from dotenv import load_dotenv
from langchain_core.messages import HumanMessage
from langchain_google_genai import ChatGoogleGenerativeAI
import base64, httpx
from PIL import Image
import io
import os
from Prompts import prompt
# Load environment variables from .env file
load_dotenv()
gemini_api_key = os.getenv("GEMINI_API_KEY")
if "GOOGLE_API_KEY" not in os.environ:
os.environ["GOOGLE_API_KEY"] = gemini_api_key
model = ChatGoogleGenerativeAI(model="gemini-1.5-flash") # Initialize the model
# Display title and instructions in the Streamlit app
st.set_page_config("Sagar's Food Classification App")
st.title("Food Classification App")
st.write("Upload an image, and the app will classify if the food is healthy or unhealthy.")
# Sidebar for file uploader with padding
with st.sidebar:
st.markdown("<div style='padding: 10px;'></div>", unsafe_allow_html=True)
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
# Main area for displaying image and classification results
if uploaded_file is not None:
# Display uploaded image on the right side
image = Image.open(uploaded_file)
col1, col2 = st.columns([3, 2]) # Adjust column widths (col2 is wider)
with col1:
st.image(image, caption='Uploaded Image', use_container_width=True)
# Convert image to byte stream for processing
img_bytes = io.BytesIO()
image.save(img_bytes, format='PNG')
img_data = img_bytes.getvalue()
image_data = base64.b64encode(img_data).decode("utf-8")
# Create a message with the image
message = HumanMessage(
content=[
{"type": "text", "text": prompt.foodClassificationPrompts},
{
"type": "image_url",
"image_url": {"url": f"data:image/jpeg;base64,{image_data}"},
},
]
)
# Process the image and get classification response
with st.spinner("Classifying..."):
response = model.invoke([message])
# Display the response in the main area (right side)
with col2:
st.write("Food Classification:")
st.write(response.content)
else:
st.sidebar.write("Please upload an image.")