mathias1 commited on
Commit
1d4dac3
·
verified ·
1 Parent(s): 1f8dab3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -16
app.py CHANGED
@@ -2,28 +2,34 @@ import openai
2
  import os
3
  import gradio as gr
4
 
5
- # Ensure OPENAI_API_KEY is set in your environment variables
6
  openai.api_key = os.getenv('OPENAI_API_KEY')
7
 
8
  def generate_health_advice(health_goals, dietary_preferences, dietary_restrictions, nutritional_interests):
9
- prompt = f"You are a helpful assistant, providing health and nutrition advice.\nMy health goals are: {health_goals}.\nMy dietary preferences are: {dietary_preferences}.\nI have the following dietary restrictions: {dietary_restrictions}.\nI'm interested in learning more about: {nutritional_interests}."
 
 
 
 
 
 
 
10
 
11
- try:
12
- response = openai.ChatCompletion.create(
13
- model="gpt-3.5-turbo",
14
- messages=[
15
- {"role": "system", "content": "You are a helpful assistant, providing health and nutrition advice."},
16
- {"role": "user", "content": f"My health goals are: {health_goals}."},
17
- {"role": "user", "content": f"My dietary preferences are: {dietary_preferences}."},
18
- {"role": "user", "content": f"I have the following dietary restrictions: {dietary_restrictions}."},
19
- {"role": "user", "content": f"I'm interested in learning more about: {nutritional_interests}."}
20
- ]
21
- )
22
- except Exception as e:
23
- return str(e)
24
 
25
- return response.choices[0].message['content']
 
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()