Update app.py
Browse files
app.py
CHANGED
|
@@ -7,13 +7,13 @@ from PIL import Image
|
|
| 7 |
openai.api_key = os.getenv('OPENAI_API_KEY').strip()
|
| 8 |
nutrition_prompt = os.getenv('NUTRITION_PROMPT').strip()
|
| 9 |
|
| 10 |
-
# Function to call GPT-4o-mini
|
| 11 |
def get_nutritional_info(description):
|
| 12 |
prompt = f"{nutrition_prompt}: {description}"
|
| 13 |
|
| 14 |
try:
|
| 15 |
response = openai.ChatCompletion.create(
|
| 16 |
-
model="gpt-4o-mini", #
|
| 17 |
messages=[
|
| 18 |
{"role": "system", "content": "You are a nutrition expert."},
|
| 19 |
{"role": "user", "content": prompt}
|
|
@@ -28,20 +28,29 @@ def get_nutritional_info(description):
|
|
| 28 |
# Gradio interface function
|
| 29 |
def analyze_meal(image, description):
|
| 30 |
if image is not None:
|
| 31 |
-
#
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
elif description:
|
| 34 |
# Use the description to get nutritional information
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
| 37 |
else:
|
| 38 |
-
return "
|
| 39 |
|
| 40 |
-
# Gradio app layout
|
| 41 |
inputs = [
|
| 42 |
-
gr.Image(label="Upload your meal (Take a bite out of that picture!)", type="pil",
|
| 43 |
-
gr.Textbox(label="Describe your meal if image recognition fails", lines=2, placeholder="e.g., a plate of upma with coconut chutney")
|
| 44 |
-
)
|
| 45 |
]
|
| 46 |
|
| 47 |
outputs = gr.Textbox(label="Nutritional Breakdown (Let's see what’s on your plate!)")
|
|
|
|
| 7 |
openai.api_key = os.getenv('OPENAI_API_KEY').strip()
|
| 8 |
nutrition_prompt = os.getenv('NUTRITION_PROMPT').strip()
|
| 9 |
|
| 10 |
+
# Function to call GPT-4o-mini to get nutritional information from text description
|
| 11 |
def get_nutritional_info(description):
|
| 12 |
prompt = f"{nutrition_prompt}: {description}"
|
| 13 |
|
| 14 |
try:
|
| 15 |
response = openai.ChatCompletion.create(
|
| 16 |
+
model="gpt-4o-mini", # Ensure this is the correct model identifier
|
| 17 |
messages=[
|
| 18 |
{"role": "system", "content": "You are a nutrition expert."},
|
| 19 |
{"role": "user", "content": prompt}
|
|
|
|
| 28 |
# Gradio interface function
|
| 29 |
def analyze_meal(image, description):
|
| 30 |
if image is not None:
|
| 31 |
+
# Handling if image is given but no text description is provided
|
| 32 |
+
if not description:
|
| 33 |
+
return "Please describe your meal in the text box below the image upload."
|
| 34 |
+
try:
|
| 35 |
+
# Process the image if necessary or use the description
|
| 36 |
+
result = get_nutritional_info(description)
|
| 37 |
+
return result
|
| 38 |
+
except Exception as e:
|
| 39 |
+
return f"Error processing image or description: {str(e)}"
|
| 40 |
elif description:
|
| 41 |
# Use the description to get nutritional information
|
| 42 |
+
try:
|
| 43 |
+
result = get_nutritional_info(description)
|
| 44 |
+
return result
|
| 45 |
+
except Exception as e:
|
| 46 |
+
return f"Error processing description: {str(e)}"
|
| 47 |
else:
|
| 48 |
+
return "Please upload an image and provide a description of the meal."
|
| 49 |
|
| 50 |
+
# Gradio app layout
|
| 51 |
inputs = [
|
| 52 |
+
gr.Image(label="Upload your meal (Take a bite out of that picture!)", type="pil"),
|
| 53 |
+
gr.Textbox(label="Describe your meal if image recognition fails", lines=2, placeholder="e.g., a plate of upma with coconut chutney")
|
|
|
|
| 54 |
]
|
| 55 |
|
| 56 |
outputs = gr.Textbox(label="Nutritional Breakdown (Let's see what’s on your plate!)")
|