Encourage-AI / app.py
arpit13's picture
Update app.py
508fcad verified
raw
history blame
4.19 kB
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 = {
"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.",
"Stress Management": "Offer calming techniques and advice to handle stress effectively.",
# (Continue with all other categories...)
"Friendly Buddy": "Respond as a supportive and fun friend. Be informal, cheerful, and light-hearted."
}
system_message = system_messages.get(category, "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)
history.append((f"You: {user_input}", f"Bot: {bot_response}"))
return history, history
# Gradio Interface with grouped categories
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"
]
}
def get_subcategories(main_category):
return categories.get(main_category, [])
with gr.Blocks(css="""
/* Add custom CSS here */
""") 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=get_subcategories("Academic Support")
)
def update_subcategories(main_category):
return gr.Dropdown.update(choices=get_subcategories(main_category), value=None)
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")
# Add functionality to handle interactions
def handle_chat(user_input, category, history):
if not user_input.strip():
return history, history
updated_history, _ = chatbot(user_input, 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()