Spaces:
Sleeping
Sleeping
File size: 4,719 Bytes
8df15f7 3ba6e71 bbd40cc 3ba6e71 72f9a72 8df15f7 3ba6e71 db6c1b8 8df15f7 72f9a72 8df15f7 72f9a72 db6c1b8 72f9a72 db6c1b8 8df15f7 db6c1b8 8df15f7 72f9a72 db6c1b8 72f9a72 8df15f7 db6c1b8 8df15f7 db6c1b8 8df15f7 db6c1b8 8df15f7 1f183a9 db6c1b8 8df15f7 |
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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
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 based on category keywords
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)}"
# Function to recognize the category based on keywords
def recognize_category(user_input):
stress_keywords = ["stress", "anxiety", "overwhelmed", "burnout", "calm"]
career_keywords = ["career", "job", "work", "interview", "skills"]
general_keywords = ["hello", "hi", "how", "what", "why", "chat", "talk"]
friendly_keywords = ["friend", "buddy", "hangout", "fun", "relax"]
# Check for category based on keywords
if any(keyword in user_input.lower() for keyword in stress_keywords):
return "Stress Management"
elif any(keyword in user_input.lower() for keyword in career_keywords):
return "Career Advice"
elif any(keyword in user_input.lower() for keyword in general_keywords):
return "General"
elif any(keyword in user_input.lower() for keyword in friendly_keywords):
return "Friendly Buddy"
else:
return "General" # Default category if no keywords match
# Chatbot function
def chatbot(user_input, history=[]):
category = recognize_category(user_input) # Determine the category based on user input
bot_response = get_groq_response(user_input, category)
history.append((category, user_input, bot_response)) # Save category, user input, and bot response
return history, history
# Gradio Interface setup with a vertical layout for category responses
chat_interface = gr.Interface(
fn=chatbot,
inputs=[
gr.Textbox(label="Your Message"), # User input
"state" # History
],
outputs=["chatbot", "state"],
live=True,
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.",
layout="vertical"
)
# Custom layout to show the outputs under different vertical columns based on categories
def custom_output(history):
stress_responses = []
career_responses = []
general_responses = []
friendly_responses = []
for category, user_input, bot_response in history:
if category == "Stress Management":
stress_responses.append(f"**User**: {user_input}\n**Bot**: {bot_response}")
elif category == "Career Advice":
career_responses.append(f"**User**: {user_input}\n**Bot**: {bot_response}")
elif category == "General":
general_responses.append(f"**User**: {user_input}\n**Bot**: {bot_response}")
elif category == "Friendly Buddy":
friendly_responses.append(f"**User**: {user_input}\n**Bot**: {bot_response}")
return [
gr.Column([gr.Markdown(f"### Stress Management\n{response}") for response in stress_responses]),
gr.Column([gr.Markdown(f"### Career Advice\n{response}") for response in career_responses]),
gr.Column([gr.Markdown(f"### General\n{response}") for response in general_responses]),
gr.Column([gr.Markdown(f"### Friendly Buddy\n{response}") for response in friendly_responses])
]
chat_interface.launch()
|