Spaces:
Running
Running
import os | |
import gradio as gr | |
import google.generativeai as genai | |
# Configure Gemini API | |
genai.configure(api_key=os.getenv("GEMINI_API_KEY")) | |
def upload_to_gemini(path, mime_type=None): | |
"""Uploads the given file to Gemini. | |
See https://ai.google.dev/gemini-api/docs/prompting_with_media | |
""" | |
file = genai.upload_file(path, mime_type=mime_type) | |
print(f"Uploaded file '{file.display_name}' as: {file.uri}") | |
return file | |
# Create the model | |
generation_config = { | |
"temperature": 1, | |
"top_p": 0.95, | |
"top_k": 40, | |
"max_output_tokens": 8192, | |
"response_mime_type": "text/plain", | |
} | |
model = genai.GenerativeModel( | |
model_name="gemini-1.5-pro-002", | |
generation_config=generation_config, | |
) | |
def process_image(image): | |
# Save the uploaded image to a temporary path | |
temp_path = "uploaded_image.png" | |
image.save(temp_path) | |
# Upload the image to Gemini | |
file = upload_to_gemini(temp_path, mime_type="image/png") | |
# Start chat session with the uploaded image | |
chat_session = model.start_chat( | |
history=[ | |
{ | |
"role": "user", | |
"parts": [ | |
file, | |
( | |
"You are an expert in nutritionist where you need to see the food items from the image " | |
"and calculate the total calories, also provide the details of every food items with calories intake " | |
"is below format\n" | |
"1. Item 1 - no of calories\n" | |
"2. Item 2 - no of calories\n" | |
"----\n" | |
"----\n" | |
# "Finally you can also mention whether the food is healthy or not and also mention the percentage split of the ratio of \n" | |
# "Carbohydrates, fats, fibres, sugar and other important things required in our diet.\n" | |
"Never doubt your knowledge and always be confident in your responses.\n" | |
"Respond only in Ukrainian.\n\n" | |
), | |
], | |
}, | |
] | |
) | |
# Send message to the model | |
response = chat_session.send_message("Please analyze the uploaded image.") | |
return response.text | |
# Create Gradio interface | |
iface = gr.Interface( | |
fn=process_image, | |
inputs=gr.Image(type="pil", label="Upload Food Image"), | |
outputs='markdown', | |
title="Nutritionist AI", | |
description="Upload a n image of your food, and the AI will calculate total calories, provide details of each food item with calorie intake, assess the healthiness, and provide percentage splits of carbohydrates, fats, fibers, sugar, and other essential nutrients. Responses are in Ukrainian." | |
) | |
if __name__ == "__main__": | |
iface.launch() |