Safwanahmad619's picture
Update app.py
d9d4dec verified
import gradio as gr
import anthropic
import os
# Function to call Claude AI API and get a personalized meal plan
def get_meal_plan(fasting_sugar, pre_meal_sugar, post_meal_sugar, dietary_preferences):
ani_sa_api = os.getenv('claude_api') # Replace with your actual API key or environment variable
client = anthropic.Anthropic(api_key=ani_sa_api)
# Define the prompt to send to Claude AI
prompt = (
f"My fasting sugar level is {fasting_sugar} mg/dL, "
f"my pre-meal sugar level is {pre_meal_sugar} mg/dL, "
f"and my post-meal sugar level is {post_meal_sugar} mg/dL. "
f"My dietary preferences are {dietary_preferences}. "
"Please provide a personalized meal plan that can help me manage my blood sugar levels effectively."
)
# Call Claude AI API
message = client.messages.create(
model="claude-3-5-sonnet-20240620",
max_tokens=250,
temperature=0.7,
system="You are a world-class nutritionist who specializes in diabetes management.",
messages=[
{
"role": "user",
"content": prompt
}
]
)
raw_context = message.content
meal_plan = raw_context[0].text
return meal_plan
# Gradio interface
def generate_meal_plan(fasting_sugar, pre_meal_sugar, post_meal_sugar, dietary_preferences):
meal_plan = get_meal_plan(fasting_sugar, pre_meal_sugar, post_meal_sugar, dietary_preferences)
return meal_plan
with gr.Blocks() as demo:
gr.Markdown("# Glucose Guardian")
gr.Markdown("**Glucose Guardian** is a personalized meal planning tool designed specifically for diabetic patients. By entering your sugar levels and dietary preferences, Glucose Guardian generates meal plans that are tailored to help you manage your blood sugar levels effectively.")
with gr.Row():
fasting_sugar = gr.Number(label="Fasting Sugar Levels (mg/dL)", value=0)
pre_meal_sugar = gr.Number(label="Pre-Meal Sugar Levels (mg/dL)", value=0)
post_meal_sugar = gr.Number(label="Post-Meal Sugar Levels (mg/dL)", value=0)
dietary_preferences = gr.Textbox(label="Dietary Preferences (e.g., vegetarian, low-carb)")
meal_plan_output = gr.Markdown()
generate_button = gr.Button("Generate Meal Plan")
generate_button.click(fn=generate_meal_plan, inputs=[fasting_sugar, pre_meal_sugar, post_meal_sugar, dietary_preferences], outputs=meal_plan_output)
demo.launch()