Spaces:
Sleeping
Sleeping
import gradio as gr | |
import openai | |
import os | |
# Set OpenAI API Key | |
openai.api_key = os.getenv("TRY_NEW_THINGS") | |
openai.api_base = "https://api.groq.com/openai/v1" | |
# Function to get response from GROQ API | |
def get_groq_response(message, category): | |
# System message to tailor responses based on the category | |
system_message = "" | |
if category == "Stress Management": | |
system_message = "Provide soothing advice and tips to help the user manage stress. Be calm and empathetic." | |
elif category == "Career Advice": | |
system_message = "Provide professional and constructive career advice. Be encouraging and helpful." | |
elif category == "General": | |
system_message = "Provide general conversation. Be friendly and easygoing." | |
elif category == "Friendly Buddy": | |
system_message = "Respond as a supportive and fun friend. Be informal and light-hearted." | |
try: | |
response = openai.ChatCompletion.create( | |
model="llama-3.1-70b-versatile", | |
messages=[ | |
{"role": "system", "content": system_message}, | |
{"role": "user", "content": message} | |
] | |
) | |
return response.choices[0].message["content"] | |
except Exception as e: | |
return f"Error: {str(e)}" | |
# Chatbot function | |
def chatbot(user_input, category, history=[]): | |
bot_response = get_groq_response(user_input, category) | |
history.append((user_input, bot_response)) | |
return history, history | |
# Gradio Interface setup | |
chat_interface = gr.Interface( | |
fn=chatbot, | |
inputs=[ | |
"text", # User input | |
gr.Dropdown(choices=["Stress Management", "Career Advice", "General", "Friendly Buddy"], label="Choose Chat Category"), # Category selection | |
"state" # History | |
], | |
outputs=["chatbot", "state"], | |
live=False, | |
title="Meet your Personal Assistant: Your Helpful and Caring Chatbot", | |
description="This chatbot is here to help you with anything you need, whether it’s answering questions, offering support, or guiding you through tasks. With a friendly and empathetic approach, it listens carefully to your concerns and provides thoughtful, understanding responses. Whether you’re seeking information or just someone to chat with, our chatbot is designed to make you feel heard and supported. It’s more than just a tool—it’s a companion dedicated to making your day easier and more enjoyable." | |
) | |
chat_interface.launch() | |