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.")