| import gradio as gr |
| import openai |
| import os |
| from PIL import Image |
|
|
| |
| openai.api_key = os.getenv('OPENAI_API_KEY').strip() |
| nutrition_prompt = os.getenv('NUTRITION_PROMPT').strip() |
|
|
| |
| def get_nutritional_info(description): |
| prompt = f"{nutrition_prompt}: {description}" |
| |
| try: |
| response = openai.ChatCompletion.create( |
| model="gpt-4o-mini", |
| 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)}" |
|
|
| |
| def analyze_meal(image, description): |
| if image is not None: |
| |
| if not description: |
| return "Please describe your meal in the text box below the image upload." |
| try: |
| |
| result = get_nutritional_info(description) |
| return result |
| except Exception as e: |
| return f"Error processing image or description: {str(e)}" |
| elif description: |
| |
| 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." |
|
|
| |
| 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!)") |
|
|
| |
| 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() |
|
|