Spaces:
Running
Running
Delete ai_chatbot.py
Browse files- ai_chatbot.py +0 -100
ai_chatbot.py
DELETED
|
@@ -1,100 +0,0 @@
|
|
| 1 |
-
from sentence_transformers import SentenceTransformer
|
| 2 |
-
import numpy as np
|
| 3 |
-
from typing import List, Dict, Tuple
|
| 4 |
-
import re
|
| 5 |
-
|
| 6 |
-
class AIChatbot:
|
| 7 |
-
def __init__(self):
|
| 8 |
-
# Load the pre-trained model (can use a smaller model for more speed)
|
| 9 |
-
self.model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 10 |
-
# Warm up the model to avoid first-request slowness
|
| 11 |
-
_ = self.model.encode(["Hello, world!"])
|
| 12 |
-
self.faq_embeddings = None
|
| 13 |
-
self.faqs = None
|
| 14 |
-
self.load_faqs()
|
| 15 |
-
|
| 16 |
-
def load_faqs(self):
|
| 17 |
-
"""Disable FAQs entirely; operate as a general-conversation bot."""
|
| 18 |
-
self.faqs = []
|
| 19 |
-
self.faq_embeddings = None
|
| 20 |
-
|
| 21 |
-
def save_unanswered_question(self, question):
|
| 22 |
-
"""Log unanswered questions to console (can be extended to save to file)"""
|
| 23 |
-
print(f"Unanswered question logged: {question}")
|
| 24 |
-
# In a real implementation, you could save this to a file or send to an admin
|
| 25 |
-
|
| 26 |
-
def _tokenize(self, text: str):
|
| 27 |
-
if not text:
|
| 28 |
-
return []
|
| 29 |
-
return [t for t in re.findall(r"[a-z0-9]+", text.lower()) if len(t) > 2]
|
| 30 |
-
|
| 31 |
-
def _overlap_ratio(self, q_tokens, faq_tokens):
|
| 32 |
-
if not q_tokens or not faq_tokens:
|
| 33 |
-
return 0.0
|
| 34 |
-
q_set = set(q_tokens)
|
| 35 |
-
f_set = set(faq_tokens)
|
| 36 |
-
inter = len(q_set & f_set)
|
| 37 |
-
denom = max(len(q_set), 1)
|
| 38 |
-
return inter / denom
|
| 39 |
-
|
| 40 |
-
def _wh_class(self, text: str) -> str:
|
| 41 |
-
if not text:
|
| 42 |
-
return ''
|
| 43 |
-
s = text.strip().lower()
|
| 44 |
-
# simple heuristic classification by leading wh-word
|
| 45 |
-
for key in ['who', 'where', 'when', 'what', 'how', 'why', 'which']:
|
| 46 |
-
if s.startswith(key + ' ') or s.startswith(key + "?"):
|
| 47 |
-
return key
|
| 48 |
-
# also check presence if not leading
|
| 49 |
-
for key in ['who', 'where', 'when', 'what', 'how', 'why', 'which']:
|
| 50 |
-
if f' {key} ' in f' {s} ':
|
| 51 |
-
return key
|
| 52 |
-
return ''
|
| 53 |
-
|
| 54 |
-
def find_best_match(self, question: str, threshold: float = 0.7) -> Tuple[str, float]:
|
| 55 |
-
print(f"find_best_match called with: {question}") # Debug print
|
| 56 |
-
# Always act as a general-conversation bot
|
| 57 |
-
return self._generate_general_response(question)
|
| 58 |
-
|
| 59 |
-
def _generate_general_response(self, question: str) -> Tuple[str, float]:
|
| 60 |
-
"""Generate general conversation responses for non-FAQ questions"""
|
| 61 |
-
question_lower = question.lower().strip()
|
| 62 |
-
|
| 63 |
-
# Greeting responses
|
| 64 |
-
if any(greeting in question_lower for greeting in ['hello', 'hi', 'hey', 'good morning', 'good afternoon', 'good evening']):
|
| 65 |
-
return "Hello! I'm the PSAU AI assistant. I'm here to help you with questions about university admissions, courses, and general information about Pangasinan State University. How can I assist you today?", 0.8
|
| 66 |
-
|
| 67 |
-
# Thank you responses
|
| 68 |
-
if any(thanks in question_lower for thanks in ['thank you', 'thanks', 'thank', 'appreciate']):
|
| 69 |
-
return "You're very welcome! I'm happy to help. Is there anything else you'd like to know about PSAU or university admissions?", 0.9
|
| 70 |
-
|
| 71 |
-
# Goodbye responses
|
| 72 |
-
if any(goodbye in question_lower for goodbye in ['bye', 'goodbye', 'see you', 'farewell']):
|
| 73 |
-
return "Goodbye! It was nice chatting with you. Feel free to come back anytime if you have more questions about PSAU. Good luck with your academic journey!", 0.9
|
| 74 |
-
|
| 75 |
-
# How are you responses
|
| 76 |
-
if any(how in question_lower for how in ['how are you', 'how do you do', 'how is it going']):
|
| 77 |
-
return "I'm doing great, thank you for asking! I'm here and ready to help you with any questions about PSAU admissions, courses, or university life. What would you like to know?", 0.8
|
| 78 |
-
|
| 79 |
-
# What can you do responses
|
| 80 |
-
if any(what in question_lower for what in ['what can you do', 'what do you do', 'what are your capabilities']):
|
| 81 |
-
return "I can help you with:\n• University admission requirements and procedures\n• Course information and recommendations\n• General questions about PSAU\n• Academic guidance and support\n• Information about campus life\n\nWhat specific information are you looking for?", 0.9
|
| 82 |
-
|
| 83 |
-
# About PSAU responses
|
| 84 |
-
if any(about in question_lower for about in ['about psa', 'about psu', 'about pangasinan state', 'tell me about']):
|
| 85 |
-
return "Pangasinan State University (PSAU) is a premier state university in the Philippines offering quality education across various fields. We provide undergraduate and graduate programs in areas like Computer Science, Business, Education, Nursing, and more. We're committed to academic excellence and student success. What would you like to know more about?", 0.8
|
| 86 |
-
|
| 87 |
-
# Help responses
|
| 88 |
-
if any(help in question_lower for help in ['help', 'assist', 'support']):
|
| 89 |
-
return "I'm here to help! I can assist you with:\n• Admission requirements and deadlines\n• Course information and recommendations\n• Academic programs and majors\n• Campus facilities and services\n• General university information\n\nJust ask me any question and I'll do my best to help you!", 0.9
|
| 90 |
-
|
| 91 |
-
# Default general response
|
| 92 |
-
return "I understand you're asking about something, but I'm specifically designed to help with PSAU-related questions like admissions, courses, and university information. Could you rephrase your question to be more specific about what you'd like to know about Pangasinan State University? I'm here to help with academic guidance and university-related inquiries!", 0.6
|
| 93 |
-
|
| 94 |
-
def get_suggested_questions(self, question: str, num_suggestions: int = 3) -> List[str]:
|
| 95 |
-
"""No suggestions when FAQs are disabled."""
|
| 96 |
-
return []
|
| 97 |
-
|
| 98 |
-
def add_faq(self, question: str, answer: str) -> bool:
|
| 99 |
-
"""No-op when FAQs are disabled."""
|
| 100 |
-
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|