dindizz commited on
Commit
56d8b8a
·
verified ·
1 Parent(s): 72bc1e8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -11
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 (or GPT-3.5/4) 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", # Assuming GPT-4o-mini is the model you're using (could be gpt-3.5-turbo, etc.)
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
- # Since GPT-4o-mini does not support image input, we ask the user to describe the meal
32
- return "Yum! Looks like a delicious dish, but image analysis is not supported. Please describe your meal below!"
 
 
 
 
 
 
 
33
  elif description:
34
  # Use the description to get nutritional information
35
- result = get_nutritional_info(description)
36
- return result
 
 
 
37
  else:
38
- return "Nomp Nomp! Please upload an image or provide a description of the meal."
39
 
40
- # Gradio app layout (swapped the positions of image upload and text description)
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!)")