Spaces:
Sleeping
Sleeping
import os | |
import openai | |
import gradio as gr | |
# Retrieve credentials from environment variables | |
openai_api_key = os.getenv("OPENAI_API_KEY","xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx") | |
openai_model = "gpt-3.5-turbo" | |
# Initialize OpenAI client | |
client = openai.OpenAI(api_key=openai_api_key) | |
# Define the Fitness Assistant system message | |
system_message = """You are a Smart Fitness Assistant designed to help users with their fitness-related inquiries. | |
You can assist with workout plans, exercise routines, nutrition guidance, calorie tracking, personalized fitness recommendations, recovery tips, and health monitoring insights. | |
Please provide relevant details like fitness goals, experience level, and any health considerations for more precise responses. | |
Note: I do not provide medical diagnoses or professional healthcare advice—always consult a certified professional for medical concerns. | |
""" | |
# Store conversation history | |
messages_array = [{"role": "system", "content": system_message}] | |
def fitness_response(user_query, history): | |
global messages_array | |
# Append user input to messages array | |
messages_array.append({"role": "user", "content": user_query}) | |
# Call OpenAI API | |
response = client.chat.completions.create( | |
model=openai_model, | |
temperature=0.7, | |
max_tokens=1200, | |
messages=messages_array | |
) | |
# Extract assistant response | |
generated_text = response.choices[0].message.content | |
# Append assistant response to messages array | |
messages_array.append({"role": "assistant", "content": generated_text}) | |
# Return generated text | |
return generated_text | |
# Create Gradio interface | |
chatbot = gr.ChatInterface( | |
fn=fitness_response, | |
title="Fitness APP", | |
type="messages", # ✅ Use OpenAI-style message format | |
) | |
# Launch the chatbot | |
if __name__ == "__main__": | |
chatbot.launch() |