Spaces:
Sleeping
Sleeping
File size: 9,103 Bytes
8df15f7 3ba6e71 4fbdca3 3ba6e71 72f9a72 b9a3331 3ba6e71 46bfe87 b9a3331 b014d75 060bcff 8df15f7 b9a3331 8df15f7 72f9a72 46bfe87 b9a3331 694df17 6434192 0a7bc28 b014d75 2a7d8c7 b014d75 2a7d8c7 b014d75 2a7d8c7 595d768 2a7d8c7 8c8cc19 2a7d8c7 b014d75 2a7d8c7 595d768 8c8cc19 4bde0d5 b014d75 2a7d8c7 b014d75 2a7d8c7 b52577b 2a7d8c7 8c8cc19 2a7d8c7 8c8cc19 2a7d8c7 b014d75 8c8cc19 b014d75 8c8cc19 2a7d8c7 4bde0d5 b014d75 8c8cc19 b014d75 4bde0d5 508fcad 4bde0d5 508fcad 4bde0d5 b014d75 508fcad b014d75 d32fa9d b014d75 d32fa9d b014d75 d32fa9d 694df17 6434192 b014d75 f063c08 b014d75 aaca04e bf58fec d32fa9d f063c08 8274289 d32fa9d 595d768 d32fa9d |
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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
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):
# Define system message based on category
system_messages = {
"Stress Management": "Provide soothing advice and tips to help the user manage stress. Be calm, empathetic, and reassuring.",
"Career Advice": "Offer professional and constructive career advice. Be encouraging, insightful, and action-oriented.",
"General": "Engage in general conversation. Be friendly, approachable, and easygoing.",
"Funny": "Respond with humorous remarks and witty commentary. Be entertaining but remain respectful.",
"Flirty": "Respond playfully and with charm. Keep it light-hearted, fun, and appropriate.",
"Scary": "Respond with spooky or thrilling remarks. Set a mysterious and eerie tone.",
"Business Mind": "Respond with a sharp, professional focus. Offer strategic advice and maintain a results-driven tone.",
"Extrovert": "Respond with high energy and enthusiasm. Be engaging, sociable, and vibrant.",
"Friendly Buddy": "Respond as a supportive and fun friend. Be informal, cheerful, and light-hearted.",
"Study Tips": "Provide effective study strategies, time management advice, and tips for staying organized and focused.",
"Exam Preparation": "Offer tips for preparing for exams, managing anxiety, and optimizing performance on test day.",
"Project Guidance": "Provide advice on how to tackle academic projects, group assignments, and presentations effectively.",
"Time Management": "Help the user manage their time effectively with practical scheduling and prioritization tips.",
"Motivation & Focus": "Provide encouraging advice to stay motivated and maintain focus on long-term goals.",
"Building Confidence": "Offer advice to help boost self-esteem, overcome self-doubt, and build confidence.",
"Friendship & Social Skills": "Provide tips for making friends, improving communication skills, and navigating social dynamics.",
"Networking & Career Building": "Offer advice on networking, building professional relationships, and finding internships or job opportunities.",
"Work-Life Balance": "Help the user balance academics, social life, and personal well-being.",
"Mental Health": "Provide empathetic advice to manage stress, anxiety, and other mental health challenges. Encourage seeking professional help when needed.",
"Physical Health": "Share tips for maintaining physical health, including fitness, sleep, and nutrition.",
"Budgeting Tips": "Provide practical advice for managing money, creating a budget, and saving effectively as a student.",
"Finding Part-Time Jobs": "Share tips for finding and balancing part-time work with academic responsibilities.",
"Scholarships & Financial Aid": "Provide advice on applying for scholarships, grants, and understanding financial aid options.",
"Resume Building": "Offer tips for creating an impressive resume tailored for internships or entry-level positions.",
"Interview Preparation": "Provide advice on how to prepare for interviews, including common questions and presentation tips.",
"Skill Development": "Suggest ways to build skills, gain certifications, and stand out in competitive fields.",
"Exploring New Hobbies": "Encourage trying new activities and provide ideas for hobbies that fit college life.",
"Clubs & Extracurriculars": "Offer advice on joining clubs, participating in activities, and enhancing the college experience.",
"Creative Projects": "Provide inspiration and resources for personal or group creative endeavors.",
}
system_message = system_messages.get(category, "Category not recognized. Respond appropriately to the user's input.")
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)
# Format messages to comply with 'messages' format
history.append({"role": "user", "content": user_input})
history.append({"role": "assistant", "content": bot_response})
return history, history
# Categories grouped into main and subcategories
categories = {
"Academic Support": [
"Study Tips", "Exam Preparation", "Project Guidance", "Time Management", "Building Confidence"
],
"Mental & Physical Wellness": [
"Stress Management", "Motivation & Focus", "Mental Health", "Physical Health", "Work-Life Balance"
],
"Career & Financial": [
"Networking & Career Building", "Finding Part-Time Jobs", "Scholarships & Financial Aid",
"Resume Building", "Interview Preparation", "Budgeting Tips", "Skill Development"
],
"Social & Personal Growth": [
"Friendship & Social Skills", "Exploring New Hobbies", "Clubs & Extracurriculars", "Creative Projects"
],
"Fun & Miscellaneous": [
"Funny", "Flirty", "Scary", "Business Mind", "Extrovert", "Friendly Buddy"
]
}
# Gradio Interface with grouped categories and custom CSS for colors and styles
with gr.Blocks(css="""
body {
font-family: 'Poppins', sans-serif;
background: linear-gradient(120deg, #ff9a9e, #fad0c4, #a1c4fd);
animation: gradientBG 10s ease infinite;
margin: 0;
padding: 0;
color: #333;
}
@keyframes gradientBG {
0% { background-position: 0% 50%; }
50% { background-position: 100% 50%; }
100% { background-position: 0% 50%; }
}
button {
background: linear-gradient(90deg, #ff7eb3, #ff758c); /* Bright gradient */
color: white;
padding: 0.6rem 1.2rem;
font-size: 0.9rem;
font-weight: bold;
border-radius: 20px;
border: none;
cursor: pointer;
transition: transform 0.2s ease, background 0.2s ease;
}
button:hover {
background: linear-gradient(90deg, #ff758c, #ff7eb3); /* Reverse gradient for hover effect */
transform: scale(1.05);
}
header {
text-align: center;
margin-bottom: 20px;
padding: 10px;
border-radius: 15px;
background: linear-gradient(90deg, #ff758c, #ff7eb3);
color: white;
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
}
.chat-container {
border: 2px solid #ff7eb3;
background: rgba(255, 255, 255, 0.9);
border-radius: 15px;
padding: 20px;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
max-height: 350px;
width: 80%; /* Wider chat container */
margin: 0 auto;
overflow-y: auto;
font-size: 1.1rem;
color: #333;
line-height: 1.6;
}
.chat-container .user-message {
font-weight: bold;
color: #6a11cb;
margin-bottom: 10px;
}
.chat-container .bot-message {
font-weight: normal;
color: #2575fc;
margin-bottom: 10px;
}
""") as chat_interface:
with gr.Row():
gr.Markdown("<h1 style='text-align:center;'>π Vibrant Personal Assistant Chatbot π</h1>")
with gr.Row():
gr.Markdown("<p style='text-align:center;'>Select a category and type your message to get tailored responses.</p>")
with gr.Row():
main_category = gr.Radio(
label="Main Category",
choices=list(categories.keys()),
value="Academic Support"
)
sub_category = gr.Dropdown(
label="Subcategory",
choices=categories["Academic Support"],
value="Study Tips"
)
def update_subcategories(selected_main_category):
"""Update the subcategory dropdown based on the main category."""
new_subcategories = categories.get(selected_main_category, [])
return gr.update(choices=new_subcategories, value=new_subcategories[0] if new_subcategories else None)
# Handle main category change to update subcategories
main_category.change(update_subcategories, inputs=main_category, outputs=sub_category)
with gr.Row():
user_input = gr.Textbox(label="Your Message", placeholder="Type something...", lines=2)
send_button = gr.Button("Send")
with gr.Row():
chatbot_output = gr.Chatbot(label="Chat History", type="messages", elem_id="chat-container")
def handle_chat(user_input, sub_category, history):
if not user_input.strip():
return history, history
updated_history, _ = chatbot(user_input, sub_category, history)
return updated_history, updated_history
send_button.click(
handle_chat,
inputs=[user_input, sub_category, chatbot_output],
outputs=[chatbot_output, chatbot_output]
)
chat_interface.launch()
|