Spaces:
Running
Running
File size: 8,155 Bytes
f8ea354 |
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 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
import gradio as gr
from chatbot import Chatbot
import json
# Initialize chatbot
chatbot = Chatbot()
def chat_interface(message, history):
"""Handle chat interface with Gradio"""
if not message.strip():
return "Please enter a message."
# Get response from chatbot
response = chatbot.get_response(message)
# Format the response for display (removed confidence)
if response['status'] == 'success':
formatted_response = response['answer']
else:
formatted_response = response['answer']
return formatted_response
def get_system_info():
"""Get system information"""
faq_count = chatbot.get_qa_count()
return f"System Status: β
Active\nFAQ Pairs Loaded: {faq_count}\nCourse Recommender: β
Ready"
def get_course_recommendations(stanine, gwa, strand, hobbies):
"""Get course recommendations"""
return chatbot.get_course_recommendations(stanine, gwa, strand, hobbies)
# Create Gradio interface
with gr.Blocks(
title="AI Chatbot",
theme=gr.themes.Soft(),
css="""
.gradio-container {
max-width: 800px !important;
margin: auto !important;
}
.chat-message {
padding: 10px;
margin: 5px 0;
border-radius: 10px;
}
"""
) as demo:
gr.Markdown(
"""
# π€ AI Student Assistant
Get answers to your questions and personalized course recommendations!
**Features:**
- FAQ Chat: Get instant answers from our knowledge base
- Course Recommendations: Get personalized course suggestions based on your profile
"""
)
with gr.Tabs():
with gr.TabItem("π¬ FAQ Chat"):
with gr.Row():
with gr.Column(scale=3):
chatbot_interface = gr.Chatbot(
label="FAQ Chat",
height=400,
show_label=True,
container=True,
bubble_full_width=False
)
with gr.Row():
msg = gr.Textbox(
placeholder="Type your question here...",
show_label=False,
scale=4,
container=False
)
submit_btn = gr.Button("Send", variant="primary", scale=1)
with gr.Column(scale=1):
gr.Markdown("### System Info")
system_info = gr.Textbox(
value=get_system_info(),
label="Status",
interactive=False,
lines=4
)
refresh_btn = gr.Button("Refresh Status", variant="secondary")
gr.Markdown("### FAQ Instructions")
gr.Markdown(
"""
**How to use:**
1. Type your question in the text box
2. Click Send or press Enter
3. Get AI-powered answers from FAQ database
**Tips:**
- Ask specific questions for better results
- Try rephrasing if you don't get a good match
"""
)
with gr.TabItem("π― Course Recommendations"):
with gr.Row():
with gr.Column(scale=2):
gr.Markdown("### π Student Profile")
stanine_input = gr.Slider(
minimum=1,
maximum=9,
step=1,
value=5,
label="Stanine Score (1-9)",
info="Your stanine score from standardized tests"
)
gwa_input = gr.Slider(
minimum=75,
maximum=100,
step=0.1,
value=85,
label="GWA (75-100)",
info="Your General Weighted Average"
)
strand_input = gr.Dropdown(
choices=["STEM", "ABM", "HUMSS"],
label="Senior High School Strand",
info="Select your SHS strand"
)
hobbies_input = gr.Textbox(
label="Hobbies & Interests",
placeholder="e.g., programming, gaming, business, teaching...",
lines=3,
info="Describe your interests and hobbies"
)
recommend_btn = gr.Button("Get Recommendations", variant="primary", size="lg")
with gr.Column(scale=3):
gr.Markdown("### π― Your Course Recommendations")
recommendations_output = gr.Markdown(
value="Enter your profile details and click 'Get Recommendations' to see personalized course suggestions.",
label="Recommendations"
)
gr.Markdown("### π Available Courses")
gr.Markdown(
"""
**STEM Courses:**
- BSCS: Bachelor of Science in Computer Science
- BSIT: Bachelor of Science in Information Technology
- BSArch: Bachelor of Science in Architecture
- BSIE: Bachelor of Science in Industrial Engineering
- BSN: Bachelor of Science in Nursing
**ABM Courses:**
- BSBA: Bachelor of Science in Business Administration
- BSA: Bachelor of Science in Accountancy
**HUMSS Courses:**
- BSED: Bachelor of Science in Education
- BSPsych: Bachelor of Science in Psychology
**Other Courses:**
- BSHM: Bachelor of Science in Hospitality Management
- BSAgri: Bachelor of Science in Agriculture
"""
)
# Event handlers
def user(user_message, history):
return "", history + [[user_message, None]]
def bot(history):
user_message = history[-1][0]
bot_message = chat_interface(user_message, history)
history[-1][1] = bot_message
return history
def refresh_system_info():
return get_system_info()
# Connect FAQ Chat events
submit_btn.click(
fn=user,
inputs=[msg, chatbot_interface],
outputs=[msg, chatbot_interface],
queue=False
).then(
fn=bot,
inputs=chatbot_interface,
outputs=chatbot_interface,
queue=True
)
msg.submit(
fn=user,
inputs=[msg, chatbot_interface],
outputs=[msg, chatbot_interface],
queue=False
).then(
fn=bot,
inputs=chatbot_interface,
outputs=chatbot_interface,
queue=True
)
# Connect Course Recommendation events
recommend_btn.click(
fn=get_course_recommendations,
inputs=[stanine_input, gwa_input, strand_input, hobbies_input],
outputs=recommendations_output
)
refresh_btn.click(
fn=refresh_system_info,
outputs=system_info
)
if __name__ == "__main__":
demo.launch(
server_name="0.0.0.0",
server_port=7860,
share=False,
show_error=True,
quiet=False
) |