File size: 1,914 Bytes
9d1fc29 071b65a 9d1fc29 071b65a 9d1fc29 071b65a | 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 | import streamlit as st
import os
from PIL import Image
import google.generativeai as genai
genai.configure(api_key="AIzaSyCUIDWVCkslMJi4azNcPDJveuyqZyhnJMg")
# Load the Gemini model
model = genai.GenerativeModel('gemini-1.5-flash')
# Function to get response from Gemini AI
def get_gemini_response(input_text, image, user_prompt):
response = model.generate_content([input_text, image[0], user_prompt])
return response.text
# Function to process uploaded image
def input_image_details(uploaded_file):
if uploaded_file is not None:
bytes_data = uploaded_file.getvalue() # Read the file into bytes
image_parts = [
{
"mime_type": uploaded_file.type, # Get the mime type of the uploaded file
"data": bytes_data
}
]
return image_parts
else:
raise FileNotFoundError("No file uploaded")
# Streamlit UI
st.set_page_config(page_title='Food Analysis')
st.header('Food Analysis')
# User input
input_text = st.text_input("Input Prompt:", key="input")
# File uploader
uploaded_file = st.file_uploader("Choose an image of the food...", type=["jpg", "jpeg", "png"])
if uploaded_file is not None:
img = Image.open(uploaded_file)
st.image(img, caption="Uploaded Image", use_column_width=True)
# AI Prompt
input_prompt = (
"You are an expert chef who has worked for more than 30 years. "
"We will upload an image of food, and you will have to answer any questions based on the uploaded image in 20 words."
)
# Submit button
submit = st.button("SUBMIT")
if submit:
if uploaded_file is not None:
image_data = input_image_details(uploaded_file)
response = get_gemini_response(input_prompt, image_data, input_text)
st.subheader("The Response is:")
st.write(response)
else:
st.warning("Please upload an image before submitting.")
|