File size: 2,634 Bytes
3b89013 b0083be 3b89013 56d8b8a 3b89013 7dbf5c0 56d8b8a 7489fea 3b89013 7dbf5c0 3b89013 b0083be 3b89013 56d8b8a 3b89013 56d8b8a 3b89013 56d8b8a 3b89013 56d8b8a 3b89013 56d8b8a 3b89013 |
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 63 64 65 66 67 68 69 |
import gradio as gr
import openai
import os
from PIL import Image
# Set up the OpenAI API key and prompt from environment variables (secrets)
openai.api_key = os.getenv('OPENAI_API_KEY').strip()
nutrition_prompt = os.getenv('NUTRITION_PROMPT').strip()
# Function to call GPT-4o-mini to get nutritional information from text description
def get_nutritional_info(description):
prompt = f"{nutrition_prompt}: {description}"
try:
response = openai.ChatCompletion.create(
model="gpt-4o-mini", # Ensure this is the correct model identifier
messages=[
{"role": "system", "content": "You are a nutrition expert."},
{"role": "user", "content": prompt}
],
max_tokens=300,
temperature=0.7
)
return response.choices[0].message['content'].strip()
except Exception as e:
return f"Oops! Something went wrong with nutritional breakdown: {str(e)}"
# Gradio interface function
def analyze_meal(image, description):
if image is not None:
# Handling if image is given but no text description is provided
if not description:
return "Please describe your meal in the text box below the image upload."
try:
# Process the image if necessary or use the description
result = get_nutritional_info(description)
return result
except Exception as e:
return f"Error processing image or description: {str(e)}"
elif description:
# Use the description to get nutritional information
try:
result = get_nutritional_info(description)
return result
except Exception as e:
return f"Error processing description: {str(e)}"
else:
return "Please upload an image and provide a description of the meal."
# Gradio app layout
inputs = [
gr.Image(label="Upload your meal (Take a bite out of that picture!)", type="pil"),
gr.Textbox(label="Describe your meal if image recognition fails", lines=2, placeholder="e.g., a plate of upma with coconut chutney")
]
outputs = gr.Textbox(label="Nutritional Breakdown (Let's see what’s on your plate!)")
# Launch the Gradio app with a fun and playful description
app = gr.Interface(
fn=analyze_meal,
inputs=inputs,
outputs=outputs,
title="NOMP NOMP: Nutrition on My Plate",
description="👋 Welcome to NOMP NOMP! Nomp nomp... what's on your plate? Upload a picture or tell us about your meal, and we'll break it down nutritionally for you!"
)
if __name__ == "__main__":
app.launch()
|