Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,234 +1,240 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
if not message.strip():
|
| 11 |
-
return "Please enter a message."
|
| 12 |
|
| 13 |
-
# Get
|
| 14 |
-
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
return
|
| 23 |
-
|
| 24 |
-
def get_system_info():
|
| 25 |
-
"""Get system information"""
|
| 26 |
-
faq_count = chatbot.get_qa_count()
|
| 27 |
-
return f"System Status: ✅ Active\nFAQ Pairs Loaded: {faq_count}\nCourse Recommender: ✅ Ready"
|
| 28 |
|
| 29 |
def get_course_recommendations(stanine, gwa, strand, hobbies):
|
| 30 |
"""Get course recommendations"""
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
# Create Gradio interface
|
| 34 |
-
with gr.Blocks(
|
| 35 |
-
title="AI Chatbot",
|
| 36 |
-
theme=gr.themes.Soft(),
|
| 37 |
-
css="""
|
| 38 |
-
.gradio-container {
|
| 39 |
-
max-width: 800px !important;
|
| 40 |
-
margin: auto !important;
|
| 41 |
-
}
|
| 42 |
-
.chat-message {
|
| 43 |
-
padding: 10px;
|
| 44 |
-
margin: 5px 0;
|
| 45 |
-
border-radius: 10px;
|
| 46 |
-
}
|
| 47 |
-
"""
|
| 48 |
-
) as demo:
|
| 49 |
-
|
| 50 |
gr.Markdown(
|
| 51 |
"""
|
| 52 |
-
# 🤖 AI
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
**Features:**
|
| 57 |
-
- FAQ Chat: Get instant answers from our knowledge base
|
| 58 |
-
- Course Recommendations: Get personalized course suggestions based on your profile
|
| 59 |
"""
|
| 60 |
)
|
| 61 |
|
| 62 |
with gr.Tabs():
|
| 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 |
-
gr.Markdown("### FAQ Instructions")
|
| 95 |
-
gr.Markdown(
|
| 96 |
-
"""
|
| 97 |
-
**How to use:**
|
| 98 |
-
1. Type your question in the text box
|
| 99 |
-
2. Click Send or press Enter
|
| 100 |
-
3. Get AI-powered answers from FAQ database
|
| 101 |
-
|
| 102 |
-
**Tips:**
|
| 103 |
-
- Ask specific questions for better results
|
| 104 |
-
- Try rephrasing if you don't get a good match
|
| 105 |
-
"""
|
| 106 |
-
)
|
| 107 |
|
| 108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
with gr.Row():
|
| 110 |
-
with gr.Column(
|
| 111 |
-
gr.
|
| 112 |
-
|
| 113 |
-
stanine_input = gr.Slider(
|
| 114 |
-
minimum=1,
|
| 115 |
-
maximum=9,
|
| 116 |
-
step=1,
|
| 117 |
-
value=5,
|
| 118 |
label="Stanine Score (1-9)",
|
| 119 |
-
|
|
|
|
|
|
|
| 120 |
)
|
| 121 |
-
|
| 122 |
-
gwa_input = gr.Slider(
|
| 123 |
-
minimum=75,
|
| 124 |
-
maximum=100,
|
| 125 |
-
step=0.1,
|
| 126 |
-
value=85,
|
| 127 |
label="GWA (75-100)",
|
| 128 |
-
|
|
|
|
|
|
|
| 129 |
)
|
| 130 |
-
|
| 131 |
strand_input = gr.Dropdown(
|
| 132 |
choices=["STEM", "ABM", "HUMSS"],
|
| 133 |
-
|
| 134 |
-
|
|
|
|
| 135 |
)
|
| 136 |
-
|
| 137 |
hobbies_input = gr.Textbox(
|
| 138 |
label="Hobbies & Interests",
|
| 139 |
-
placeholder="e.g., programming, gaming, business, teaching...",
|
| 140 |
-
lines=3,
|
| 141 |
info="Describe your interests and hobbies"
|
| 142 |
)
|
| 143 |
|
| 144 |
-
recommend_btn = gr.Button("Get Recommendations", variant="primary"
|
| 145 |
|
| 146 |
-
with gr.Column(
|
| 147 |
-
gr.Markdown(
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
- BSPsych: Bachelor of Science in Psychology
|
| 170 |
-
|
| 171 |
-
**Other Courses:**
|
| 172 |
-
- BSHM: Bachelor of Science in Hospitality Management
|
| 173 |
-
- BSAgri: Bachelor of Science in Agriculture
|
| 174 |
-
"""
|
| 175 |
-
)
|
| 176 |
-
|
| 177 |
-
# Event handlers
|
| 178 |
-
def user(user_message, history):
|
| 179 |
-
return "", history + [[user_message, None]]
|
| 180 |
-
|
| 181 |
-
def bot(history):
|
| 182 |
-
user_message = history[-1][0]
|
| 183 |
-
bot_message = chat_interface(user_message, history)
|
| 184 |
-
history[-1][1] = bot_message
|
| 185 |
-
return history
|
| 186 |
-
|
| 187 |
-
def refresh_system_info():
|
| 188 |
-
return get_system_info()
|
| 189 |
-
|
| 190 |
-
# Connect FAQ Chat events
|
| 191 |
-
submit_btn.click(
|
| 192 |
-
fn=user,
|
| 193 |
-
inputs=[msg, chatbot_interface],
|
| 194 |
-
outputs=[msg, chatbot_interface],
|
| 195 |
-
queue=False
|
| 196 |
-
).then(
|
| 197 |
-
fn=bot,
|
| 198 |
-
inputs=chatbot_interface,
|
| 199 |
-
outputs=chatbot_interface,
|
| 200 |
-
queue=True
|
| 201 |
-
)
|
| 202 |
-
|
| 203 |
-
msg.submit(
|
| 204 |
-
fn=user,
|
| 205 |
-
inputs=[msg, chatbot_interface],
|
| 206 |
-
outputs=[msg, chatbot_interface],
|
| 207 |
-
queue=False
|
| 208 |
-
).then(
|
| 209 |
-
fn=bot,
|
| 210 |
-
inputs=chatbot_interface,
|
| 211 |
-
outputs=chatbot_interface,
|
| 212 |
-
queue=True
|
| 213 |
-
)
|
| 214 |
-
|
| 215 |
-
# Connect Course Recommendation events
|
| 216 |
-
recommend_btn.click(
|
| 217 |
-
fn=get_course_recommendations,
|
| 218 |
-
inputs=[stanine_input, gwa_input, strand_input, hobbies_input],
|
| 219 |
-
outputs=recommendations_output
|
| 220 |
-
)
|
| 221 |
-
|
| 222 |
-
refresh_btn.click(
|
| 223 |
-
fn=refresh_system_info,
|
| 224 |
-
outputs=system_info
|
| 225 |
-
)
|
| 226 |
|
| 227 |
if __name__ == "__main__":
|
| 228 |
demo.launch(
|
| 229 |
server_name="0.0.0.0",
|
| 230 |
server_port=7860,
|
| 231 |
share=False,
|
| 232 |
-
show_error=True
|
| 233 |
-
|
| 234 |
-
)
|
|
|
|
| 1 |
+
import os
|
| 2 |
import gradio as gr
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import numpy as np
|
| 5 |
+
from ai_chatbot import AIChatbot
|
| 6 |
+
from database_recommender import CourseRecommender
|
| 7 |
+
import warnings
|
| 8 |
+
import logging
|
| 9 |
|
| 10 |
+
# Suppress warnings
|
| 11 |
+
warnings.filterwarnings('ignore')
|
| 12 |
+
logging.getLogger('tensorflow').setLevel(logging.ERROR)
|
| 13 |
|
| 14 |
+
# Initialize components
|
| 15 |
+
try:
|
| 16 |
+
chatbot = AIChatbot()
|
| 17 |
+
print("✅ Chatbot initialized successfully")
|
| 18 |
+
except Exception as e:
|
| 19 |
+
print(f"⚠️ Warning: Could not initialize chatbot: {e}")
|
| 20 |
+
chatbot = None
|
| 21 |
+
|
| 22 |
+
try:
|
| 23 |
+
recommender = CourseRecommender()
|
| 24 |
+
print("✅ Recommender initialized successfully")
|
| 25 |
+
except Exception as e:
|
| 26 |
+
print(f"⚠️ Warning: Could not initialize recommender: {e}")
|
| 27 |
+
recommender = None
|
| 28 |
+
|
| 29 |
+
def chat_with_bot(message, history):
|
| 30 |
+
"""Handle chatbot interactions"""
|
| 31 |
+
if chatbot is None:
|
| 32 |
+
return "Sorry, the chatbot is not available at the moment. Please try again later."
|
| 33 |
+
|
| 34 |
if not message.strip():
|
| 35 |
+
return "Please enter a message to start the conversation."
|
| 36 |
|
| 37 |
+
# Get answer from chatbot
|
| 38 |
+
answer, confidence = chatbot.find_best_match(message)
|
| 39 |
|
| 40 |
+
# For general conversation, just return the answer
|
| 41 |
+
# For FAQ questions, include suggested questions
|
| 42 |
+
if confidence > 0.7: # High confidence FAQ match
|
| 43 |
+
suggested_questions = chatbot.get_suggested_questions(message)
|
| 44 |
+
if suggested_questions:
|
| 45 |
+
response = f"{answer}\n\n**Related Questions:**\n"
|
| 46 |
+
for i, q in enumerate(suggested_questions, 1):
|
| 47 |
+
response += f"{i}. {q}\n"
|
| 48 |
+
return response
|
| 49 |
|
| 50 |
+
# For general conversation or low confidence, just return the answer
|
| 51 |
+
return answer
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
def get_course_recommendations(stanine, gwa, strand, hobbies):
|
| 54 |
"""Get course recommendations"""
|
| 55 |
+
if recommender is None:
|
| 56 |
+
return "Sorry, the recommendation system is not available at the moment. Please try again later."
|
| 57 |
+
|
| 58 |
+
try:
|
| 59 |
+
# Validate and convert inputs
|
| 60 |
+
try:
|
| 61 |
+
stanine = int(stanine.strip()) if stanine else 0
|
| 62 |
+
except (ValueError, AttributeError):
|
| 63 |
+
return "❌ Stanine score must be a valid number between 1 and 9"
|
| 64 |
+
|
| 65 |
+
try:
|
| 66 |
+
gwa = float(gwa.strip()) if gwa else 0
|
| 67 |
+
except (ValueError, AttributeError):
|
| 68 |
+
return "❌ GWA must be a valid number between 75 and 100"
|
| 69 |
+
|
| 70 |
+
# Validate ranges
|
| 71 |
+
if not (1 <= stanine <= 9):
|
| 72 |
+
return "❌ Stanine score must be between 1 and 9"
|
| 73 |
+
|
| 74 |
+
if not (75 <= gwa <= 100):
|
| 75 |
+
return "❌ GWA must be between 75 and 100"
|
| 76 |
+
|
| 77 |
+
if not strand:
|
| 78 |
+
return "❌ Please select a strand"
|
| 79 |
+
|
| 80 |
+
if not hobbies or not hobbies.strip():
|
| 81 |
+
return "❌ Please enter your hobbies/interests"
|
| 82 |
+
|
| 83 |
+
# Get recommendations
|
| 84 |
+
recommendations = recommender.recommend_courses(
|
| 85 |
+
stanine=stanine,
|
| 86 |
+
gwa=gwa,
|
| 87 |
+
strand=strand,
|
| 88 |
+
hobbies=hobbies
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
if not recommendations:
|
| 92 |
+
return "No recommendations available at the moment."
|
| 93 |
+
|
| 94 |
+
# Format recommendations
|
| 95 |
+
response = f"## 🎯 Course Recommendations for You\n\n"
|
| 96 |
+
response += f"**Profile:** Stanine {stanine}, GWA {gwa}, {strand} Strand\n"
|
| 97 |
+
response += f"**Interests:** {hobbies}\n\n"
|
| 98 |
+
|
| 99 |
+
for i, rec in enumerate(recommendations, 1):
|
| 100 |
+
response += f"### {i}. {rec['code']} - {rec['name']}\n"
|
| 101 |
+
response += f"**Match Score:** {rec.get('rating', rec.get('probability', 0)):.1f}%\n\n"
|
| 102 |
+
|
| 103 |
+
return response
|
| 104 |
+
|
| 105 |
+
except Exception as e:
|
| 106 |
+
return f"❌ Error getting recommendations: {str(e)}"
|
| 107 |
+
|
| 108 |
+
def get_faqs():
|
| 109 |
+
"""Get available FAQs"""
|
| 110 |
+
if chatbot and chatbot.faqs:
|
| 111 |
+
faq_text = "## 📚 Frequently Asked Questions\n\n"
|
| 112 |
+
for i, faq in enumerate(chatbot.faqs, 1):
|
| 113 |
+
faq_text += f"**{i}. {faq['question']}**\n"
|
| 114 |
+
faq_text += f"{faq['answer']}\n\n"
|
| 115 |
+
return faq_text
|
| 116 |
+
return "No FAQs available at the moment."
|
| 117 |
+
|
| 118 |
+
def get_available_courses():
|
| 119 |
+
"""Get available courses"""
|
| 120 |
+
if recommender and recommender.courses:
|
| 121 |
+
course_text = "## 🎓 Available Courses\n\n"
|
| 122 |
+
for code, name in recommender.courses.items():
|
| 123 |
+
course_text += f"**{code}** - {name}\n"
|
| 124 |
+
return course_text
|
| 125 |
+
return "No courses available at the moment."
|
| 126 |
|
| 127 |
# Create Gradio interface
|
| 128 |
+
with gr.Blocks(title="PSAU AI Chatbot & Course Recommender", theme=gr.themes.Soft()) as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
gr.Markdown(
|
| 130 |
"""
|
| 131 |
+
# 🤖 PSAU AI Chatbot & Course Recommender
|
| 132 |
|
| 133 |
+
Welcome to the Pangasinan State University AI-powered admission assistant!
|
| 134 |
+
Get instant answers to your questions and receive personalized course recommendations.
|
|
|
|
|
|
|
|
|
|
| 135 |
"""
|
| 136 |
)
|
| 137 |
|
| 138 |
with gr.Tabs():
|
| 139 |
+
# Chatbot Tab
|
| 140 |
+
with gr.Tab("🤖 AI Chatbot"):
|
| 141 |
+
gr.Markdown("""
|
| 142 |
+
**Chat with the PSAU AI Assistant!**
|
| 143 |
+
|
| 144 |
+
I can help you with:
|
| 145 |
+
• University admission questions
|
| 146 |
+
• Course information and guidance
|
| 147 |
+
• General conversation
|
| 148 |
+
• Academic support
|
| 149 |
+
|
| 150 |
+
Just type your message below and I'll respond naturally!
|
| 151 |
+
""")
|
| 152 |
+
|
| 153 |
+
chatbot_interface = gr.ChatInterface(
|
| 154 |
+
fn=chat_with_bot,
|
| 155 |
+
title="PSAU AI Assistant",
|
| 156 |
+
description="Chat with me about university admissions, courses, or just say hello!",
|
| 157 |
+
examples=[
|
| 158 |
+
"Hello!",
|
| 159 |
+
"What are the admission requirements?",
|
| 160 |
+
"How are you?",
|
| 161 |
+
"What courses are available?",
|
| 162 |
+
"Tell me about PSAU",
|
| 163 |
+
"What can you help me with?",
|
| 164 |
+
"Thank you",
|
| 165 |
+
"Goodbye"
|
| 166 |
+
],
|
| 167 |
+
cache_examples=True
|
| 168 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
|
| 170 |
+
# Course Recommender Tab
|
| 171 |
+
with gr.Tab("🎯 Course Recommender"):
|
| 172 |
+
gr.Markdown("""
|
| 173 |
+
Get personalized course recommendations based on your academic profile and interests!
|
| 174 |
+
|
| 175 |
+
**Input Guidelines:**
|
| 176 |
+
- **Stanine Score**: Enter a number between 1-9 (from your entrance exam)
|
| 177 |
+
- **GWA**: Enter your General Weighted Average (75-100)
|
| 178 |
+
- **Strand**: Select your senior high school strand
|
| 179 |
+
- **Hobbies**: Describe your interests and hobbies in detail
|
| 180 |
+
""")
|
| 181 |
+
|
| 182 |
with gr.Row():
|
| 183 |
+
with gr.Column():
|
| 184 |
+
stanine_input = gr.Textbox(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
label="Stanine Score (1-9)",
|
| 186 |
+
placeholder="Enter your stanine score (1-9)",
|
| 187 |
+
info="Your stanine score from entrance examination",
|
| 188 |
+
value="7"
|
| 189 |
)
|
| 190 |
+
gwa_input = gr.Textbox(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
label="GWA (75-100)",
|
| 192 |
+
placeholder="Enter your GWA (75-100)",
|
| 193 |
+
info="Your General Weighted Average",
|
| 194 |
+
value="85.0"
|
| 195 |
)
|
|
|
|
| 196 |
strand_input = gr.Dropdown(
|
| 197 |
choices=["STEM", "ABM", "HUMSS"],
|
| 198 |
+
value="STEM",
|
| 199 |
+
label="High School Strand",
|
| 200 |
+
info="Your senior high school strand"
|
| 201 |
)
|
|
|
|
| 202 |
hobbies_input = gr.Textbox(
|
| 203 |
label="Hobbies & Interests",
|
| 204 |
+
placeholder="e.g., programming, gaming, business, teaching, healthcare...",
|
|
|
|
| 205 |
info="Describe your interests and hobbies"
|
| 206 |
)
|
| 207 |
|
| 208 |
+
recommend_btn = gr.Button("Get Recommendations", variant="primary")
|
| 209 |
|
| 210 |
+
with gr.Column():
|
| 211 |
+
recommendations_output = gr.Markdown()
|
| 212 |
+
|
| 213 |
+
recommend_btn.click(
|
| 214 |
+
fn=get_course_recommendations,
|
| 215 |
+
inputs=[stanine_input, gwa_input, strand_input, hobbies_input],
|
| 216 |
+
outputs=recommendations_output
|
| 217 |
+
)
|
| 218 |
+
|
| 219 |
+
# Information Tab
|
| 220 |
+
with gr.Tab("📚 Information"):
|
| 221 |
+
with gr.Row():
|
| 222 |
+
with gr.Column():
|
| 223 |
+
gr.Markdown("### FAQ Section")
|
| 224 |
+
faq_btn = gr.Button("Show FAQs")
|
| 225 |
+
faq_output = gr.Markdown()
|
| 226 |
+
faq_btn.click(fn=get_faqs, outputs=faq_output)
|
| 227 |
+
|
| 228 |
+
with gr.Column():
|
| 229 |
+
gr.Markdown("### Available Courses")
|
| 230 |
+
courses_btn = gr.Button("Show Courses")
|
| 231 |
+
courses_output = gr.Markdown()
|
| 232 |
+
courses_btn.click(fn=get_available_courses, outputs=courses_output)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 233 |
|
| 234 |
if __name__ == "__main__":
|
| 235 |
demo.launch(
|
| 236 |
server_name="0.0.0.0",
|
| 237 |
server_port=7860,
|
| 238 |
share=False,
|
| 239 |
+
show_error=True
|
| 240 |
+
)
|
|
|