File size: 4,254 Bytes
8df15f7
3ba6e71
4fbdca3
3ba6e71
72f9a72
b9a3331
3ba6e71
 
46bfe87
b9a3331
508fcad
 
 
 
 
 
 
 
 
8df15f7
 
 
 
b9a3331
 
8df15f7
 
 
 
 
72f9a72
46bfe87
b9a3331
 
6434192
 
0a7bc28
27488f0
508fcad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4bde0d5
 
27488f0
 
4bde0d5
 
508fcad
4bde0d5
 
508fcad
4bde0d5
508fcad
 
 
 
d32fa9d
508fcad
 
27488f0
 
508fcad
 
27488f0
 
 
 
508fcad
1643319
508fcad
 
d32fa9d
508fcad
d32fa9d
508fcad
d32fa9d
508fcad
6434192
27488f0
f063c08
 
27488f0
aaca04e
bf58fec
d32fa9d
f063c08
508fcad
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
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_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.",
        "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

# 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
with gr.Blocks() 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.Dropdown.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")

    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()