| import streamlit as st |
| import os |
| from PIL import Image |
| import google.generativeai as genai |
|
|
| genai.configure(api_key="AIzaSyCUIDWVCkslMJi4azNcPDJveuyqZyhnJMg") |
|
|
| |
| model = genai.GenerativeModel('gemini-1.5-flash') |
|
|
| |
| def get_gemini_response(input_text, image, user_prompt): |
| response = model.generate_content([input_text, image[0], user_prompt]) |
| return response.text |
|
|
| |
| def input_image_details(uploaded_file): |
| if uploaded_file is not None: |
| bytes_data = uploaded_file.getvalue() |
| image_parts = [ |
| { |
| "mime_type": uploaded_file.type, |
| "data": bytes_data |
| } |
| ] |
| return image_parts |
| else: |
| raise FileNotFoundError("No file uploaded") |
|
|
| |
| st.set_page_config(page_title='Food Analysis') |
| st.header('Food Analysis') |
|
|
| |
| input_text = st.text_input("Input Prompt:", key="input") |
|
|
| |
| 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) |
|
|
| |
| 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 = 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.") |
| |