File size: 2,194 Bytes
3077af4
8f44423
 
48f946e
8f44423
c202ca9
5335d95
 
7ddba72
 
 
 
 
 
 
 
 
 
 
 
 
48f946e
7ddba72
 
 
48f946e
7ddba72
1d4dac3
5335d95
7ddba72
 
 
5335d95
 
 
7ddba72
 
 
 
5335d95
 
 
 
 
 
7ddba72
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from openai import OpenAI
import os
import gradio as gr

# Set the OpenAI API key using an environment variable
client = OpenAI(api_key=os.environ['mkey'])

def generate_health_advice(health_goals, dietary_preferences, dietary_restrictions, nutritional_interests):
    # System and user messages for the chat completion API
    system_message = {"role": "system", "content": "You are a helpful assistant, providing health and nutrition advice."}
    health_goals_message = {"role": "user", "content": f"My health goals are: {health_goals}."}
    dietary_preferences_message = {"role": "user", "content": f"My dietary preferences are: {dietary_preferences}."}
    dietary_restrictions_message = {"role": "user", "content": f"I have the following dietary restrictions: {dietary_restrictions}."}
    nutritional_interests_message = {"role": "user", "content": f"I'm interested in learning more about: {nutritional_interests}."}

    messages = [
        system_message,
        health_goals_message,
        dietary_preferences_message,
        dietary_restrictions_message,
        nutritional_interests_message
    ]

    # Corrected syntax for generating chat completions
    response = client.chat.completions.create(
        model="gpt-3.5-turbo",
        messages=messages
    )

    return response.choices[0].message.content

# Gradio interface setup
iface = gr.Interface(
    fn=generate_health_advice,
    inputs=[
        gr.Textbox(label="Health Goals", placeholder="Enter your health goals...", lines=2),
        gr.Textbox(label="Dietary Preferences", placeholder="Enter your dietary preferences...", lines=2),
        gr.Textbox(label="Dietary Restrictions", placeholder="List any dietary restrictions...", lines=2),
        gr.Textbox(label="Nutritional Interests", placeholder="Enter your nutritional interests...", lines=2)
    ],
    outputs=gr.Text(label="Personalized Health Advice"),
    title="Personalized Health and Nutrition Advisor",
    description="Get personalized health advice based on your goals, preferences, and restrictions. Input your information below and receive guidance."
)

# Launch the interface with sharing enabled
iface.launch(share=True, debug=True)