Spaces:
Running
Running
File size: 26,733 Bytes
f8ea354 8b04568 f8ea354 8b04568 b476fef 8b04568 b476fef 8b04568 b476fef 8b04568 b476fef 8b04568 b476fef 8b04568 b476fef 8b04568 b476fef 8b04568 b476fef 8b04568 b476fef 8b04568 b476fef 8b04568 b476fef 8b04568 b476fef 8b04568 f8ea354 8b04568 f8ea354 8b04568 b476fef 8b04568 b476fef 8b04568 b476fef 8b04568 b476fef 8b04568 b476fef 8b04568 b476fef 8b04568 f8ea354 8b04568 f8ea354 8b04568 f8ea354 8b04568 b476fef |
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 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 |
import gradio as gr
import requests
import json
import re
from typing import List, Tuple, Optional
from difflib import SequenceMatcher
import string
class AIChatbot:
def __init__(self, database_url: str = "https://database-dhe2.onrender.com"):
self.database_url = database_url
self.conversation_history = []
# Simple conversation patterns
self.greeting_patterns = [
r'\b(hi|hello|hey|good morning|good afternoon|good evening)\b',
r'\b(how are you|how\'s it going|what\'s up)\b'
]
self.help_patterns = [
r'\b(help|assist|support|guide)\b',
r'\b(what can you do|what do you do|your capabilities)\b'
]
self.thanks_patterns = [
r'\b(thank you|thanks|appreciate|grateful)\b'
]
self.goodbye_patterns = [
r'\b(bye|goodbye|see you|farewell|exit|quit)\b'
]
def is_greeting(self, message: str) -> bool:
"""Check if the message is a greeting"""
message_lower = message.lower()
for pattern in self.greeting_patterns:
if re.search(pattern, message_lower):
return True
return False
def is_help_request(self, message: str) -> bool:
"""Check if the message is asking for help"""
message_lower = message.lower()
for pattern in self.help_patterns:
if re.search(pattern, message_lower):
return True
return False
def is_thanks(self, message: str) -> bool:
"""Check if the message is expressing thanks"""
message_lower = message.lower()
for pattern in self.thanks_patterns:
if re.search(pattern, message_lower):
return True
return False
def is_goodbye(self, message: str) -> bool:
"""Check if the message is a goodbye"""
message_lower = message.lower()
for pattern in self.goodbye_patterns:
if re.search(pattern, message_lower):
return True
return False
def get_greeting_response(self) -> str:
"""Generate a greeting response"""
responses = [
"Hello! I'm your AI assistant. How can I help you today?",
"Hi there! I'm here to assist you with any questions you might have.",
"Hello! Welcome! I can help you with general conversation or answer specific questions from our database.",
"Hey! Nice to meet you! What can I do for you today?"
]
import random
return random.choice(responses)
def get_help_response(self) -> str:
"""Generate a help response"""
return """I'm an AI chatbot that can help you in two ways:
1. **General Conversation**: I can chat with you about various topics, answer greetings, and have friendly conversations.
2. **Specific Questions**: I can search our database for specific information and provide detailed answers to your questions.
**Smart Learning**: If I can't find an answer to your question, I'll automatically save it for review so we can improve our knowledge base and provide better answers in the future.
Just type your question or start a conversation, and I'll do my best to help you!"""
def get_thanks_response(self) -> str:
"""Generate a thanks response"""
responses = [
"You're welcome! I'm happy to help.",
"My pleasure! Feel free to ask if you need anything else.",
"Glad I could assist you! Is there anything else you'd like to know?",
"You're very welcome! I'm here whenever you need help."
]
import random
return random.choice(responses)
def get_goodbye_response(self) -> str:
"""Generate a goodbye response"""
responses = [
"Goodbye! Have a great day!",
"See you later! Take care!",
"Farewell! Feel free to come back anytime.",
"Bye! I enjoyed chatting with you!"
]
import random
return random.choice(responses)
def save_unanswered_question(self, question: str) -> bool:
"""Save unanswered question to the database"""
try:
# Try different possible endpoints for saving unanswered questions
endpoints = [
f"{self.database_url}/unanswered_questions",
f"{self.database_url}/api/unanswered_questions",
f"{self.database_url}/save_question",
f"{self.database_url}/api/save_question"
]
for endpoint in endpoints:
try:
# Try POST request with JSON body - matching your table structure
response = requests.post(
endpoint,
json={
"question": question,
"created_at": self._get_timestamp()
},
headers={"Content-Type": "application/json"},
timeout=10
)
if response.status_code in [200, 201]:
return True
except:
try:
# Try GET request with query parameters
response = requests.get(
endpoint,
params={
"question": question,
"created_at": self._get_timestamp()
},
timeout=10
)
if response.status_code in [200, 201]:
return True
except:
continue
return False
except Exception as e:
print(f"Error saving unanswered question: {e}")
return False
def _get_timestamp(self) -> str:
"""Get current timestamp in ISO format"""
from datetime import datetime
return datetime.now().isoformat()
def _normalize_text(self, text: str) -> str:
"""Normalize text for better matching"""
# Convert to lowercase
text = text.lower()
# Remove punctuation
text = text.translate(str.maketrans('', '', string.punctuation))
# Remove extra whitespace
text = ' '.join(text.split())
return text
def _extract_keywords(self, text: str) -> List[str]:
"""Extract important keywords from text with enhanced processing"""
# Extended stop words to ignore
stop_words = {
'the', 'a', 'an', 'and', 'or', 'but', 'in', 'on', 'at', 'to', 'for', 'of', 'with', 'by',
'is', 'are', 'was', 'were', 'be', 'been', 'being', 'have', 'has', 'had', 'do', 'does', 'did',
'will', 'would', 'could', 'should', 'may', 'might', 'can', 'what', 'how', 'when', 'where', 'why',
'who', 'which', 'this', 'that', 'these', 'those', 'i', 'you', 'he', 'she', 'it', 'we', 'they',
'me', 'him', 'her', 'us', 'them', 'my', 'your', 'his', 'her', 'its', 'our', 'their',
'there', 'here', 'some', 'any', 'all', 'each', 'every', 'much', 'many', 'more', 'most',
'very', 'just', 'only', 'also', 'even', 'still', 'yet', 'so', 'too', 'well', 'now', 'then'
}
# Normalize and split into words
words = self._normalize_text(text).split()
# Enhanced keyword extraction
keywords = []
for word in words:
# Filter out stop words and very short words
if word not in stop_words and len(word) > 2:
# Add the word
keywords.append(word)
# Add common variations and stems
if word.endswith('s') and len(word) > 3:
keywords.append(word[:-1]) # Remove 's' for plurals
if word.endswith('ing') and len(word) > 4:
keywords.append(word[:-3]) # Remove 'ing'
if word.endswith('ed') and len(word) > 3:
keywords.append(word[:-2]) # Remove 'ed'
return list(set(keywords)) # Remove duplicates
def _calculate_similarity(self, text1: str, text2: str) -> float:
"""Calculate similarity between two texts using advanced methods"""
norm1 = self._normalize_text(text1)
norm2 = self._normalize_text(text2)
# Method 1: Sequence matcher on normalized text
sequence_similarity = SequenceMatcher(None, norm1, norm2).ratio()
# Method 2: Enhanced keyword overlap with stemming
keywords1 = set(self._extract_keywords(text1))
keywords2 = set(self._extract_keywords(text2))
keyword_similarity = 0.0
if keywords1 and keywords2:
intersection = keywords1.intersection(keywords2)
union = keywords1.union(keywords2)
keyword_similarity = len(intersection) / len(union) if union else 0.0
# Method 3: Substring containment (both directions)
contains_similarity = 0.0
if norm1 in norm2:
contains_similarity = max(contains_similarity, 0.9 * (len(norm1) / len(norm2)))
if norm2 in norm1:
contains_similarity = max(contains_similarity, 0.9 * (len(norm2) / len(norm1)))
# Method 4: Word order similarity
words1 = norm1.split()
words2 = norm2.split()
word_order_similarity = 0.0
if words1 and words2:
# Check for common word sequences
common_sequences = 0
max_len = min(len(words1), len(words2))
for i in range(max_len):
if words1[i] == words2[i]:
common_sequences += 1
word_order_similarity = common_sequences / max_len if max_len > 0 else 0.0
# Method 5: Semantic similarity using word relationships
semantic_similarity = self._calculate_semantic_similarity(keywords1, keywords2)
# Method 6: Length similarity (shorter queries should match longer answers)
length_similarity = 0.0
if len(norm1) > 0 and len(norm2) > 0:
length_ratio = min(len(norm1), len(norm2)) / max(len(norm1), len(norm2))
length_similarity = length_ratio * 0.3 # Lower weight for length
# Method 7: Phrase matching (for common phrases)
phrase_similarity = self._calculate_phrase_similarity(norm1, norm2)
# Combine all methods with optimized weights
final_similarity = (
sequence_similarity * 0.25 +
keyword_similarity * 0.30 +
contains_similarity * 0.20 +
word_order_similarity * 0.10 +
semantic_similarity * 0.10 +
length_similarity * 0.03 +
phrase_similarity * 0.02
)
return min(final_similarity, 1.0) # Cap at 1.0
def _calculate_semantic_similarity(self, keywords1: set, keywords2: set) -> float:
"""Calculate semantic similarity using word relationships"""
if not keywords1 or not keywords2:
return 0.0
# Common semantic relationships
semantic_groups = {
'money': {'cost', 'price', 'tuition', 'fee', 'payment', 'money', 'financial', 'aid', 'scholarship'},
'time': {'deadline', 'when', 'time', 'date', 'schedule', 'duration', 'period'},
'contact': {'contact', 'phone', 'email', 'address', 'office', 'reach', 'call'},
'requirements': {'requirement', 'need', 'required', 'must', 'prerequisite', 'condition'},
'application': {'apply', 'application', 'submit', 'process', 'procedure'},
'programs': {'program', 'course', 'major', 'degree', 'study', 'academic'},
'admission': {'admission', 'admit', 'accept', 'enroll', 'entry', 'enter'}
}
# Check if keywords belong to the same semantic group
semantic_score = 0.0
for group, words in semantic_groups.items():
group1_match = any(keyword in words for keyword in keywords1)
group2_match = any(keyword in words for keyword in keywords2)
if group1_match and group2_match:
semantic_score += 0.3
return min(semantic_score, 1.0)
def _calculate_phrase_similarity(self, text1: str, text2: str) -> float:
"""Calculate similarity based on common phrases"""
# Common phrases that should match
common_phrases = [
('admission requirements', 'requirements admission'),
('financial aid', 'aid financial'),
('tuition cost', 'cost tuition'),
('application deadline', 'deadline application'),
('contact admissions', 'admissions contact'),
('gpa requirement', 'requirement gpa'),
('academic requirements', 'requirements academic')
]
phrase_score = 0.0
for phrase1, phrase2 in common_phrases:
if (phrase1 in text1 and phrase1 in text2) or (phrase2 in text1 and phrase2 in text2):
phrase_score += 0.5
elif (phrase1 in text1 and phrase2 in text2) or (phrase2 in text1 and phrase1 in text2):
phrase_score += 0.4
return min(phrase_score, 1.0)
def _find_best_match(self, user_question: str, database_questions: List[str], threshold: float = 0.25) -> Optional[str]:
"""Find the best matching question from database with improved logic"""
if not database_questions:
return None
best_match = None
best_score = 0.0
all_scores = []
# Calculate similarity for all questions
for db_question in database_questions:
similarity = self._calculate_similarity(user_question, db_question)
all_scores.append((db_question, similarity))
if similarity > best_score:
best_score = similarity
best_match = db_question
# Sort by similarity score
all_scores.sort(key=lambda x: x[1], reverse=True)
# If the best score is above threshold, return it
if best_score >= threshold:
return best_match
# If no single match is above threshold, try adaptive threshold
if all_scores:
# Use the top score if it's reasonably close to threshold
top_score = all_scores[0][1]
if top_score >= threshold * 0.8: # 80% of threshold
return all_scores[0][0]
# Last resort: if user question is very short, be more lenient
if len(user_question.split()) <= 3 and all_scores:
# For short queries, use a lower threshold
if all_scores[0][1] >= 0.15:
return all_scores[0][0]
return None
def fetch_from_database(self, question: str) -> str:
"""Fetch answer from the database with smart matching"""
try:
# First, try to get all available questions for smart matching
all_questions = self._get_all_questions()
# If we have all questions, try smart matching first
if all_questions:
best_match = self._find_best_match(question, all_questions)
if best_match:
# Try to get answer for the best matching question
answer = self._get_answer_for_question(best_match)
if answer and not self._is_no_answer_response(answer):
return answer
# Fallback to original method if smart matching doesn't work
endpoints = [
f"{self.database_url}/faqs",
f"{self.database_url}/search",
f"{self.database_url}/query",
f"{self.database_url}/api/faq"
]
for endpoint in endpoints:
try:
# Try GET request with query parameter
response = requests.get(endpoint, params={"question": question}, timeout=10)
if response.status_code == 200:
data = response.json()
if isinstance(data, dict):
answer = data.get('answer', data.get('response', str(data)))
# Check if we got a meaningful answer
if answer and answer.strip() and not self._is_no_answer_response(answer):
return answer
elif isinstance(data, list) and len(data) > 0:
answer = str(data[0])
if answer and answer.strip() and not self._is_no_answer_response(answer):
return answer
else:
answer = str(data)
if answer and answer.strip() and not self._is_no_answer_response(answer):
return answer
except:
try:
# Try POST request with JSON body
response = requests.post(
endpoint,
json={"question": question},
headers={"Content-Type": "application/json"},
timeout=10
)
if response.status_code == 200:
data = response.json()
if isinstance(data, dict):
answer = data.get('answer', data.get('response', str(data)))
# Check if we got a meaningful answer
if answer and answer.strip() and not self._is_no_answer_response(answer):
return answer
elif isinstance(data, list) and len(data) > 0:
answer = str(data[0])
if answer and answer.strip() and not self._is_no_answer_response(answer):
return answer
else:
answer = str(data)
if answer and answer.strip() and not self._is_no_answer_response(answer):
return answer
except:
continue
# If no answer found, save the question as unanswered
self.save_unanswered_question(question)
return "I'm sorry, I couldn't find a specific answer to your question in our database. I've saved your question for review, and we'll work on providing a better answer in the future. Could you try rephrasing your question or ask me something else?"
except requests.exceptions.Timeout:
# Save the question even if there's a timeout
self.save_unanswered_question(question)
return "I'm sorry, the database is taking too long to respond. I've saved your question for review. Please try again in a moment."
except requests.exceptions.ConnectionError:
# Save the question even if there's a connection error
self.save_unanswered_question(question)
return "I'm sorry, I'm having trouble connecting to our database right now. I've saved your question for review. Please try again later."
except Exception as e:
# Save the question even if there's an unexpected error
self.save_unanswered_question(question)
return f"I encountered an error while searching our database: {str(e)}. I've saved your question for review. Please try again."
def _get_all_questions(self) -> List[str]:
"""Get all available questions from the database for smart matching"""
try:
# Try different endpoints to get all questions
endpoints = [
f"{self.database_url}/questions",
f"{self.database_url}/faq/all",
f"{self.database_url}/api/questions",
f"{self.database_url}/all_questions"
]
for endpoint in endpoints:
try:
response = requests.get(endpoint, timeout=10)
if response.status_code == 200:
data = response.json()
if isinstance(data, list):
return [str(item) for item in data]
elif isinstance(data, dict) and 'questions' in data:
return [str(q) for q in data['questions']]
except:
continue
return []
except:
return []
def _get_answer_for_question(self, question: str) -> Optional[str]:
"""Get answer for a specific question"""
try:
endpoints = [
f"{self.database_url}/faqs",
f"{self.database_url}/search",
f"{self.database_url}/query",
f"{self.database_url}/api/faq"
]
for endpoint in endpoints:
try:
response = requests.get(endpoint, params={"question": question}, timeout=10)
if response.status_code == 200:
data = response.json()
if isinstance(data, dict):
return data.get('answer', data.get('response', str(data)))
elif isinstance(data, list) and len(data) > 0:
return str(data[0])
else:
return str(data)
except:
try:
response = requests.post(
endpoint,
json={"question": question},
headers={"Content-Type": "application/json"},
timeout=10
)
if response.status_code == 200:
data = response.json()
if isinstance(data, dict):
return data.get('answer', data.get('response', str(data)))
elif isinstance(data, list) and len(data) > 0:
return str(data[0])
else:
return str(data)
except:
continue
return None
except:
return None
def _is_no_answer_response(self, answer: str) -> bool:
"""Check if the response indicates no answer was found"""
no_answer_indicators = [
"no answer",
"not found",
"no results",
"no data",
"empty",
"null",
"none",
"i don't know",
"i don't have",
"cannot find",
"unable to find"
]
answer_lower = answer.lower().strip()
return any(indicator in answer_lower for indicator in no_answer_indicators)
def chat(self, message: str, history: List[List[str]]) -> str:
"""Main chat function"""
if not message.strip():
return "Please enter a message so I can help you!"
# Store conversation history
self.conversation_history.append(("user", message))
# Check for conversation patterns
if self.is_greeting(message):
response = self.get_greeting_response()
elif self.is_help_request(message):
response = self.get_help_response()
elif self.is_thanks(message):
response = self.get_thanks_response()
elif self.is_goodbye(message):
response = self.get_goodbye_response()
else:
# Try to fetch from database
response = self.fetch_from_database(message)
# Store bot response
self.conversation_history.append(("bot", response))
return response
# Initialize the chatbot
chatbot = AIChatbot()
# Create Gradio interface
def create_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 interface:
gr.Markdown(
"""
# 🤖 AI Chatbot Assistant
Welcome! I'm your AI assistant that can help you with:
- **General conversation** and friendly chat
- **Specific questions** answered from our knowledge database
Just type your message below and I'll do my best to help you!
"""
)
# Chat interface
chatbot_interface = gr.ChatInterface(
fn=chatbot.chat,
title="Chat with AI Assistant",
description="Ask me anything or just have a conversation!",
examples=[
"Hello!",
"What can you help me with?",
"How do I contact support?",
"What are your services?",
"Thank you for your help!"
],
cache_examples=False,
retry_btn="🔄 Retry",
undo_btn="↩️ Undo",
clear_btn="🗑️ Clear",
submit_btn="Send",
stop_btn="⏹️ Stop",
additional_inputs=None
)
# Footer
gr.Markdown(
"""
---
**Note**: This chatbot can handle general conversation and search our database for specific information.
If you don't get the answer you're looking for, try rephrasing your question!
"""
)
return interface
# Launch the application
if __name__ == "__main__":
interface = create_interface()
interface.launch(
server_name="0.0.0.0",
server_port=7860,
share=False,
debug=True
)
|