Spaces:
Sleeping
Sleeping
Update app.py
Browse files6 unmodified lines78910101112131415161718192021222324252645 unmodified lines72737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131627 unmodified lines75976076176276376476576676776876977077177270 unmodified lines8438448457748466 unmodified linesimport string
class AIChatbot: def __init__(self, database_url: str = "https://sqldatabase-t7po.onrender.com"): def __init__(self, database_url: str = "https://database-dhe2.onrender.com"): self.database_url = database_url self.conversation_history = [] # Profanity filter - list of bad words to filter self.bad_words = { 'damn', 'hell', 'crap', 'suck', 'idiot', 'fool', 'jerk', 'loser', 'dumb', 'trash', 'butt', 'freak', 'nut', 'moron', 'dummy', 'bozo', 'twit', 'dope', 'dumbass', 'poophead', 'jerkoff', 'bugger', 'wanker', 'tosser', 'bastard', 'scum', 'slime', 'creep', 'brat', 'dweeb', 'goon', 'booby', 'puke', 'vomit', 'dung', 'sap', 'clutz', 'knob', 'prick', 'ass', 'shit', 'fuck', 'cock', 'tits', 'pussy', 'cunt', 'slut', 'bitch', 'whore', 'skank' } # Simple conversation patterns self.greeting_patterns = [ r'\b(hi|hello|hey|good morning|good afternoon|good evening)\b',45 unmodified lines return True return False
def contains_profanity(self, message: str) -> bool: """Check if the message contains any profanity""" # Normalize message: convert to lowercase message_lower = message.lower() # First, normalize common obfuscation characters # Replace common character substitutions (numbers/symbols) with letters obfuscation_map = { '0': 'o', '1': 'i', '3': 'e', '4': 'a', '5': 's', '7': 't', '@': 'a', '!': 'i', '$': 's', '&': 'a' } # Create a normalized version for checking normalized = message_lower for char, replacement in obfuscation_map.items(): normalized = normalized.replace(char, replacement) # Remove all non-word characters (except spaces) for word boundary checking message_clean = re.sub(r'[^\w\s]', '', normalized) words = message_clean.split() # Check for exact word matches in cleaned message for word in words: if word in self.bad_words: return True # Check for words that start with bad words (handles variations like "fucking" from "fuck") # Also check the original message for word boundaries for bad_word in self.bad_words: # Pattern 1: Word boundary followed by bad word (handles "fuck", "fucking", etc.) pattern1 = r'\b' + re.escape(bad_word) + r'\w*' if re.search(pattern1, normalized): return True # Pattern 2: Check in cleaned message (handles words with punctuation removed) if bad_word in message_clean: # Make sure it's a whole word, not part of another word pattern2 = r'\b' + re.escape(bad_word) + r'\b' if re.search(pattern2, message_clean): return True return False
def get_profanity_warning(self) -> str: """Get a polite response when profanity is detected""" responses = [ "I understand you might be frustrated, but please keep our conversation respectful. I'm here to help you with any questions or concerns you might have.", "I appreciate your message, but let's keep our conversation friendly and professional. How can I assist you today?", "I'm here to help, but I'd prefer we keep our conversation appropriate. Is there something specific you'd like to ask me?", "Let's maintain a respectful conversation. I'm happy to help you with any questions or information you need." ] import random return random.choice(responses)
def get_greeting_response(self) -> str: """Generate a greeting response""" responses = [627 unmodified lines if not message.strip(): return "Please enter a message so I can help you!" # Check for profanity first if self.contains_profanity(message): response = self.get_profanity_warning() # Store conversation history (but don't process the message) self.conversation_history.append(("user", "[Filtered]")) self.conversation_history.append(("bot", response)) return response # Store conversation history self.conversation_history.append(("user", message)) 70 unmodified lines server_port=7860, share=False, debug=True ) )app.py+74-2