Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import os
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import google.generativeai as genai
|
| 5 |
+
|
| 6 |
+
genai.configure(api_key="API KEy")
|
| 7 |
+
|
| 8 |
+
# Load the Gemini model
|
| 9 |
+
model = genai.GenerativeModel('gemini-1.5-flash')
|
| 10 |
+
|
| 11 |
+
# Function to get response from Gemini AI
|
| 12 |
+
def get_gemini_response(input_text, image, user_prompt):
|
| 13 |
+
response = model.generate_content([input_text, image[0], user_prompt])
|
| 14 |
+
return response.text
|
| 15 |
+
|
| 16 |
+
# Function to process uploaded image
|
| 17 |
+
def input_image_details(uploaded_file):
|
| 18 |
+
if uploaded_file is not None:
|
| 19 |
+
bytes_data = uploaded_file.getvalue() # Read the file into bytes
|
| 20 |
+
image_parts = [
|
| 21 |
+
{
|
| 22 |
+
"mime_type": uploaded_file.type, # Get the mime type of the uploaded file
|
| 23 |
+
"data": bytes_data
|
| 24 |
+
}
|
| 25 |
+
]
|
| 26 |
+
return image_parts
|
| 27 |
+
else:
|
| 28 |
+
raise FileNotFoundError("No file uploaded")
|
| 29 |
+
|
| 30 |
+
# Streamlit UI
|
| 31 |
+
st.set_page_config(page_title='Food Analysis')
|
| 32 |
+
st.header('Food Analysis')
|
| 33 |
+
|
| 34 |
+
# User input
|
| 35 |
+
input_text = st.text_input("Input Prompt:", key="input")
|
| 36 |
+
|
| 37 |
+
# File uploader
|
| 38 |
+
uploaded_file = st.file_uploader("Choose an image of the food...", type=["jpg", "jpeg", "png"])
|
| 39 |
+
|
| 40 |
+
if uploaded_file is not None:
|
| 41 |
+
img = Image.open(uploaded_file)
|
| 42 |
+
st.image(img, caption="Uploaded Image", use_column_width=True)
|
| 43 |
+
|
| 44 |
+
# AI Prompt
|
| 45 |
+
input_prompt = (
|
| 46 |
+
"You are an expert microbiologist who has worked for more than 30 years. "
|
| 47 |
+
"We will upload an image of food, and you will have to answer any questions based on the uploaded image in 20 words."
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
# Submit button
|
| 51 |
+
submit = st.button("SUBMIT")
|
| 52 |
+
|
| 53 |
+
if submit:
|
| 54 |
+
if uploaded_file is not None:
|
| 55 |
+
image_data = input_image_details(uploaded_file)
|
| 56 |
+
response = get_gemini_response(input_prompt, image_data, input_text)
|
| 57 |
+
|
| 58 |
+
st.subheader("The Response is:")
|
| 59 |
+
st.write(response)
|
| 60 |
+
else:
|
| 61 |
+
st.warning("Please upload an image before submitting.")
|