Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,36 +6,42 @@ import gradio as gr
|
|
| 6 |
openai.api_key = os.getenv('OPENAI_API_KEY')
|
| 7 |
|
| 8 |
def generate_health_advice(health_goals, dietary_preferences, dietary_restrictions, nutritional_interests):
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
]
|
| 16 |
-
|
| 17 |
-
|
|
|
|
| 18 |
model="gpt-3.5-turbo",
|
| 19 |
-
messages=
|
| 20 |
)
|
| 21 |
-
|
| 22 |
-
# Extract the text from the response object
|
| 23 |
-
advice = response.choices[0].message['content']
|
| 24 |
-
return advice
|
| 25 |
|
| 26 |
-
|
|
|
|
|
|
|
| 27 |
iface = gr.Interface(
|
| 28 |
fn=generate_health_advice,
|
| 29 |
inputs=[
|
| 30 |
-
gr.Textbox(label="Health Goals", placeholder="Enter your health goals
|
| 31 |
-
gr.Textbox(label="Dietary Preferences", placeholder="Enter your dietary preferences
|
| 32 |
-
gr.Textbox(label="Dietary Restrictions", placeholder="List any dietary restrictions
|
| 33 |
-
gr.Textbox(label="Nutritional Interests", placeholder="Enter your nutritional interests
|
| 34 |
],
|
| 35 |
outputs=gr.Text(label="Personalized Health Advice"),
|
| 36 |
title="Personalized Health and Nutrition Advisor",
|
| 37 |
description="Get personalized health advice based on your goals, preferences, and restrictions. Input your information below and receive guidance."
|
| 38 |
)
|
| 39 |
|
| 40 |
-
#
|
| 41 |
-
iface.launch()
|
|
|
|
| 6 |
openai.api_key = os.getenv('OPENAI_API_KEY')
|
| 7 |
|
| 8 |
def generate_health_advice(health_goals, dietary_preferences, dietary_restrictions, nutritional_interests):
|
| 9 |
+
# System and user messages for the chat completion API
|
| 10 |
+
system_message = {"role": "system", "content": "You are a helpful assistant, providing health and nutrition advice."}
|
| 11 |
+
health_goals_message = {"role": "user", "content": f"My health goals are: {health_goals}."}
|
| 12 |
+
dietary_preferences_message = {"role": "user", "content": f"My dietary preferences are: {dietary_preferences}."}
|
| 13 |
+
dietary_restrictions_message = {"role": "user", "content": f"I have the following dietary restrictions: {dietary_restrictions}."}
|
| 14 |
+
nutritional_interests_message = {"role": "user", "content": f"I'm interested in learning more about: {nutritional_interests}."}
|
| 15 |
+
|
| 16 |
+
messages = [
|
| 17 |
+
system_message,
|
| 18 |
+
health_goals_message,
|
| 19 |
+
dietary_preferences_message,
|
| 20 |
+
dietary_restrictions_message,
|
| 21 |
+
nutritional_interests_message
|
| 22 |
]
|
| 23 |
+
|
| 24 |
+
# Corrected syntax for generating chat completions
|
| 25 |
+
response = client.chat.completions.create(
|
| 26 |
model="gpt-3.5-turbo",
|
| 27 |
+
messages=messages
|
| 28 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
return response.choices[0].message.content
|
| 31 |
+
|
| 32 |
+
# Gradio interface setup
|
| 33 |
iface = gr.Interface(
|
| 34 |
fn=generate_health_advice,
|
| 35 |
inputs=[
|
| 36 |
+
gr.Textbox(label="Health Goals", placeholder="Enter your health goals...", lines=2),
|
| 37 |
+
gr.Textbox(label="Dietary Preferences", placeholder="Enter your dietary preferences...", lines=2),
|
| 38 |
+
gr.Textbox(label="Dietary Restrictions", placeholder="List any dietary restrictions...", lines=2),
|
| 39 |
+
gr.Textbox(label="Nutritional Interests", placeholder="Enter your nutritional interests...", lines=2)
|
| 40 |
],
|
| 41 |
outputs=gr.Text(label="Personalized Health Advice"),
|
| 42 |
title="Personalized Health and Nutrition Advisor",
|
| 43 |
description="Get personalized health advice based on your goals, preferences, and restrictions. Input your information below and receive guidance."
|
| 44 |
)
|
| 45 |
|
| 46 |
+
# Launch the interface with sharing enabled
|
| 47 |
+
iface.launch(share=True, debug=True)
|