Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import openai
|
| 3 |
+
import os
|
| 4 |
+
from PIL import Image
|
| 5 |
+
|
| 6 |
+
# Set up the OpenAI API key and prompt from environment variables (secrets)
|
| 7 |
+
openai.api_key = os.getenv('OPENAI_API_KEY')
|
| 8 |
+
nutrition_prompt = os.getenv('NUTRITION_PROMPT')
|
| 9 |
+
|
| 10 |
+
# Function to call OpenAI API and get nutritional information
|
| 11 |
+
def get_nutritional_info(description):
|
| 12 |
+
prompt = f"{nutrition_prompt}: {description}"
|
| 13 |
+
|
| 14 |
+
try:
|
| 15 |
+
response = openai.Completion.create(
|
| 16 |
+
engine="text-davinci-003", # You can also use 'gpt-3.5-turbo' or 'gpt-4'
|
| 17 |
+
prompt=prompt,
|
| 18 |
+
max_tokens=300,
|
| 19 |
+
temperature=0.7
|
| 20 |
+
)
|
| 21 |
+
return response.choices[0].text.strip()
|
| 22 |
+
except Exception as e:
|
| 23 |
+
return f"Oops! Something went wrong: {str(e)}"
|
| 24 |
+
|
| 25 |
+
# Gradio interface function
|
| 26 |
+
def analyze_meal(image, description):
|
| 27 |
+
if image is not None:
|
| 28 |
+
# Placeholder for image recognition (to be implemented or integrated later)
|
| 29 |
+
detected_dish = "Yum! Looks like a delicious dish, but image analysis is not ready yet. Please describe your meal below!"
|
| 30 |
+
return detected_dish
|
| 31 |
+
elif description:
|
| 32 |
+
# Use the description to get nutritional information
|
| 33 |
+
result = get_nutritional_info(description)
|
| 34 |
+
return result
|
| 35 |
+
else:
|
| 36 |
+
return "Nomp Nomp! Please upload an image or provide a description of the meal."
|
| 37 |
+
|
| 38 |
+
# Gradio app layout
|
| 39 |
+
inputs = [
|
| 40 |
+
gr.Image(label="Upload your meal (Take a bite out of that picture!)", type="pil", optional=True),
|
| 41 |
+
gr.Textbox(label="Describe your meal if image recognition fails", lines=2, placeholder="e.g., a plate of upma with coconut chutney"),
|
| 42 |
+
]
|
| 43 |
+
|
| 44 |
+
outputs = gr.Textbox(label="Nutritional Breakdown (Let's see what’s on your plate!)")
|
| 45 |
+
|
| 46 |
+
# Launch the Gradio app with a fun and playful description
|
| 47 |
+
app = gr.Interface(
|
| 48 |
+
fn=analyze_meal,
|
| 49 |
+
inputs=inputs,
|
| 50 |
+
outputs=outputs,
|
| 51 |
+
title="NOMP NOMP: Nutrition on My Plate",
|
| 52 |
+
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!"
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
if __name__ == "__main__":
|
| 56 |
+
app.launch()
|