Spaces:
Sleeping
Sleeping
Update ai_chatbot.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160from sentence_transformers import SentenceTransformerimport numpy as npfrom typing import List, Dict, Tupleimport reclass AIChatbot:def __init__(self):# Load the pre-trained model (can use a smaller model for more speed)self.model = SentenceTransformer('all-MiniLM-L6-v2')# Warm up the model to avoid first-request slowness_ = self.model.encode(["Hello, world!"])self.faq_embeddings = Noneself.faqs = Noneself.load_faqs()def load_faqs(self):"""Load static FAQs and compute their normalized embeddings"""# Static FAQ dataself.faqs = [{"id": 1, "question": "What are the admission requirements?", "answer": "To apply for admission, you need to submit your high school diploma, transcript of records, 2x2 ID photo, and completed application form. You also need to take the entrance examination."},{"id": 2, "question": "When is the application deadline?", "answer": "The application deadline is usually in March for the first semester and October for the second semester. Please check our website for the exact dates."},{"id": 3, "question": "What courses are available?", "answer": "We offer various courses including BS Computer Science, BS Information Technology, BS Business Administration, BS Education, BS Nursing, BS Architecture, and more. Check our course catalog for the complete list."},{"id": 4, "question": "How much is the tuition fee?", "answer": "Tuition fees vary by program. For undergraduate programs, it ranges from ₱15,000 to ₱25,000 per semester. Please contact the registrar's office for specific program fees."},{"id": 5, "question": "Do you offer scholarships?", "answer": "Yes, we offer various scholarships including academic scholarships, athletic scholarships, and need-based financial aid. Applications are available at the student affairs office."},{"id": 6, "question": "What is the minimum GWA requirement?", "answer": "The minimum GWA requirement is 80% for most programs. Some programs may have higher requirements. Please check the specific requirements for your chosen program."},{"id": 7, "question": "How can I contact the admissions office?", "answer": "You can contact the admissions office at (02) 123-4567 or email admissions@psau.edu.ph. Office hours are Monday to Friday, 8:00 AM to 5:00 PM."},{"id": 8, "question": "Is there a dormitory available?", "answer": "Yes, we have dormitory facilities for both male and female students. Dormitory fees are separate from tuition. Please contact the housing office for availability and rates."},{"id": 9, "question": "What documents do I need for enrollment?", "answer": "For enrollment, you need your admission letter, original and photocopy of birth certificate, original and photocopy of high school diploma, 2x2 ID photos, and medical certificate."},{"id": 10, "question": "Can I transfer from another school?", "answer": "Yes, we accept transferees. You need to submit your transcript of records, honorable dismissal, and other required documents. Some credits may be credited depending on the program."}]if self.faqs:# Compute and normalize embeddings for all questionsquestions = [faq['question'] for faq in self.faqs]embeddings = self.model.encode(questions, normalize_embeddings=True)self.faq_embeddings = np.array(embeddings)def save_unanswered_question(self, question):"""Log unanswered questions to console (can be extended to save to file)"""print(f"Unanswered question logged: {question}")# In a real implementation, you could save this to a file or send to an admindef _tokenize(self, text: str):if not text:return []return [t for t in re.findall(r"[a-z0-9]+", text.lower()) if len(t) > 2]def _overlap_ratio(self, q_tokens, faq_tokens):if not q_tokens or not faq_tokens:return 0.0q_set = set(q_tokens)f_set = set(faq_tokens)inter = len(q_set & f_set)denom = max(len(q_set), 1)return inter / denomdef _wh_class(self, text: str) -> str:if not text:return ''s = text.strip().lower()# simple heuristic classification by leading wh-wordfor key in ['who', 'where', 'when', 'what', 'how', 'why', 'which']:if s.startswith(key + ' ') or s.startswith(key + "?"):return key# also check presence if not leadingfor key in ['who', 'where', 'when', 'what', 'how', 'why', 'which']:if f' {key} ' in f' {s} ':return keyreturn ''def find_best_match(self, question: str, threshold: float = 0.7) -> Tuple[str, float]:print(f"find_best_match called with: {question}") # Debug printif not self.faqs or self.faq_embeddings is None:return "I'm sorry, I couldn't find any FAQs in the database.", 0.0# Compute and normalize embedding for the input questionquestion_embedding = self.model.encode([question], normalize_embeddings=True)[0]similarities = np.dot(self.faq_embeddings, question_embedding)# Compute keyword overlap with each FAQ questionq_tokens = self._tokenize(question)overlap_scores = []for faq in self.faqs:overlap_scores.append(self._overlap_ratio(q_tokens, self._tokenize(faq['question'])))similarities = np.array(similarities)overlap_scores = np.array(overlap_scores)# Combined score to reduce false positivescombined = 0.7 * similarities + 0.3 * overlap_scores# Apply WH-word intent consistency penaltyq_wh = self._wh_class(question)if q_wh:for i, faq in enumerate(self.faqs):f_wh = self._wh_class(faq['question'])if f_wh and f_wh != q_wh:combined[i] *= 0.6 # penalize mismatched intent significantlybest_idx = int(np.argmax(combined))best_semantic = float(similarities[best_idx])best_overlap = float(overlap_scores[best_idx])best_combined = float(combined[best_idx])best_wh = self._wh_class(self.faqs[best_idx]['question'])# Acceptance criteria: require good semantic OR strong combined with overlapaccept = (best_semantic >= max(0.7, threshold)or (best_combined >= threshold and best_overlap >= 0.3))# Enforce WH intent match when presentif accept and q_wh and best_wh and q_wh != best_wh:accept = Falseif accept:return self.faqs[best_idx]['answer'], best_combinedelse:# Log as unanswered so admins can curate (ignore errors)try:self.save_unanswered_question(question)except Exception:passfallback = ("Sorry, I don’t have the knowledge to answer that yet.\n""I’ll notify an admin about your question and we’ll add the answer soon.\n""Please come back in a while.")return (fallback, best_combined)def get_suggested_questions(self, question: str, num_suggestions: int = 3) -> List[str]:"""Get suggested questions based on the input question"""if not self.faqs or self.faq_embeddings is None:return []# Compute and normalize embedding for the input questionquestion_embedding = self.model.encode([question], normalize_embeddings=True)[0]# Calculate cosine similaritysimilarities = np.dot(self.faq_embeddings, question_embedding)# Get top N similar questionstop_indices = np.argsort(similarities)[-num_suggestions:][::-1]return [self.faqs[idx]['question'] for idx in top_indices if similarities[idx] > 0.3]def add_faq(self, question: str, answer: str) -> bool:"""Add a new FAQ to the static list (for demonstration purposes)"""try:new_id = max([faq['id'] for faq in self.faqs]) + 1 if self.faqs else 1new_faq = {"id": new_id, "question": question, "answer": answer}self.faqs.append(new_faq)# Recompute embeddingsquestions = [faq['question'] for faq in self.faqs]embeddings = self.model.encode(questions, normalize_embeddings=True)self.faq_embeddings = np.array(embeddings)print(f"FAQ added: {question}")return Trueexcept Exception as e:print(f"Error adding FAQ: {e}")return False123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114import randomimport refrom typing import List, Dictclass AIChatbot:def __init__(self):"""Initialize the AI chatbot with basic interaction patterns"""self.conversation_history = []self.greeting_patterns = ["hello", "hi", "hey", "good morning", "good afternoon", "good evening"]self.farewell_patterns = ["bye", "goodbye", "see you", "farewell", "take care"]self.thanks_patterns = ["thank you", "thanks", "appreciate", "grateful"]# Basic response templatesself.responses = {"greeting": ["Hello! How can I help you today?","Hi there! What can I do for you?","Good day! How may I assist you?","Hello! I'm here to help. What would you like to know?"],"farewell": ["Goodbye! Have a great day!","See you later! Take care!","Bye! Feel free to come back anytime!","Farewell! I hope I was helpful!"],"thanks": ["You're welcome! Is there anything else I can help with?","My pleasure! Happy to help!","No problem! Feel free to ask if you need anything else!","You're very welcome! Anything else I can assist you with?"],"default": ["That's an interesting question! I'm still learning, but I'd be happy to help with basic interactions.","I understand you're asking about that. While I'm designed for basic conversations, I'm here to chat!","Thanks for sharing that with me! I'm always happy to engage in conversation.","I appreciate your message! I'm here to have friendly conversations with you.","That's a thoughtful question! I'm designed to be helpful and engaging in our chat."]}def _clean_text(self, text: str) -> str:"""Clean and normalize input text"""if not text:return ""return re.sub(r'[^\w\s]', '', text.lower().strip())def _detect_intent(self, text: str) -> str:"""Detect the intent of the user's message"""cleaned_text = self._clean_text(text)# Check for greetingsfor pattern in self.greeting_patterns:if pattern in cleaned_text:return "greeting"# Check for farewellsfor pattern in self.farewell_patterns:if pattern in cleaned_text:return "farewell"# Check for thanksfor pattern in self.thanks_patterns:if pattern in cleaned_text:return "thanks"return "default"def generate_response(self, user_message: str) -> str:"""Generate an AI response based on user input"""if not user_message or not user_message.strip():return "I didn't quite catch that. Could you please say something?"# Add to conversation historyself.conversation_history.append({"user": user_message, "bot": ""})# Detect intentintent = self._detect_intent(user_message)# Generate appropriate responseif intent in self.responses:response = random.choice(self.responses[intent])else:response = random.choice(self.responses["default"])# Update conversation historyself.conversation_history[-1]["bot"] = responsereturn responsedef get_conversation_history(self) -> List[Dict]:"""Get the conversation history"""return self.conversation_historydef clear_history(self):"""Clear the conversation history"""self.conversation_history = []return "Conversation history cleared!"def get_suggested_responses(self) -> List[str]:"""Get some suggested conversation starters"""return ["Hello! How are you today?","Tell me about yourself","What's on your mind?","How can I help you?","What would you like to talk about?"]
ai_chatbot.py+100-146