Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,28 +2,34 @@ import openai
|
|
| 2 |
import os
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
-
#
|
| 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 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
)
|
| 22 |
-
except Exception as e:
|
| 23 |
-
return str(e)
|
| 24 |
|
| 25 |
-
|
|
|
|
| 26 |
|
|
|
|
| 27 |
iface = gr.Interface(
|
| 28 |
fn=generate_health_advice,
|
| 29 |
inputs=[
|
|
@@ -37,4 +43,5 @@ iface = gr.Interface(
|
|
| 37 |
description="Get personalized health advice based on your goals, preferences, and restrictions. Input your information below and receive guidance."
|
| 38 |
)
|
| 39 |
|
|
|
|
| 40 |
iface.launch()
|
|
|
|
| 2 |
import os
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
+
# Set the OpenAI API key using an environment variable
|
| 6 |
openai.api_key = os.getenv('OPENAI_API_KEY')
|
| 7 |
|
| 8 |
def generate_health_advice(health_goals, dietary_preferences, dietary_restrictions, nutritional_interests):
|
| 9 |
+
# Construct the prompt for the completion request
|
| 10 |
+
prompt_text = f"""
|
| 11 |
+
You are a helpful assistant, providing health and nutrition advice.
|
| 12 |
+
My health goals are: {health_goals}.
|
| 13 |
+
My dietary preferences are: {dietary_preferences}.
|
| 14 |
+
I have the following dietary restrictions: {dietary_restrictions}.
|
| 15 |
+
I'm interested in learning more about: {nutritional_interests}.
|
| 16 |
+
"""
|
| 17 |
|
| 18 |
+
# Make the completion request
|
| 19 |
+
response = openai.Completion.create(
|
| 20 |
+
model="text-davinci-003", # Or another model of your choice
|
| 21 |
+
prompt=prompt_text,
|
| 22 |
+
temperature=0.7,
|
| 23 |
+
max_tokens=150,
|
| 24 |
+
top_p=1.0,
|
| 25 |
+
frequency_penalty=0.0,
|
| 26 |
+
presence_penalty=0.0
|
| 27 |
+
)
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
# Return the generated text
|
| 30 |
+
return response.choices[0].text.strip()
|
| 31 |
|
| 32 |
+
# Gradio interface setup
|
| 33 |
iface = gr.Interface(
|
| 34 |
fn=generate_health_advice,
|
| 35 |
inputs=[
|
|
|
|
| 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 Gradio interface
|
| 47 |
iface.launch()
|