dindizz commited on
Commit
b0083be
·
verified ·
1 Parent(s): 65fd744

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -9
app.py CHANGED
@@ -4,16 +4,16 @@ 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.ChatCompletion.create(
16
- model="gpt-3.5-turbo", # Using GPT-3.5 turbo model
17
  messages=[
18
  {"role": "system", "content": "You are a nutrition expert."},
19
  {"role": "user", "content": prompt}
@@ -23,14 +23,13 @@ def get_nutritional_info(description):
23
  )
24
  return response.choices[0].message['content'].strip()
25
  except Exception as e:
26
- return f"Oops! Something went wrong: {str(e)}"
27
 
28
  # Gradio interface function
29
  def analyze_meal(image, description):
30
  if image is not None:
31
- # Placeholder for image recognition (to be implemented or integrated later)
32
- detected_dish = "Yum! Looks like a delicious dish, but image analysis is not ready yet. Please describe your meal below!"
33
- return detected_dish
34
  elif description:
35
  # Use the description to get nutritional information
36
  result = get_nutritional_info(description)
@@ -40,7 +39,7 @@ def analyze_meal(image, description):
40
 
41
  # Gradio app layout
42
  inputs = [
43
- gr.Image(label="Upload your meal (Take a bite out of that picture!)", type="pil"),
44
  gr.Textbox(label="Describe your meal if image recognition fails", lines=2, placeholder="e.g., a plate of upma with coconut chutney"),
45
  ]
46
 
 
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').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", # Assuming GPT-4o-mini is the text model you're using
17
  messages=[
18
  {"role": "system", "content": "You are a nutrition expert."},
19
  {"role": "user", "content": prompt}
 
23
  )
24
  return response.choices[0].message['content'].strip()
25
  except Exception as e:
26
+ return f"Oops! Something went wrong with nutritional breakdown: {str(e)}"
27
 
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)
 
39
 
40
  # Gradio app layout
41
  inputs = [
42
+ gr.Image(label="Upload your meal (Take a bite out of that picture!)", type="pil", optional=True),
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