Spaces:
Runtime error
Runtime error
| 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) | |