Spaces:
Sleeping
Sleeping
Update app.py
Browse files6 unmodified lines7891011121314151645 unmodified lines626364656667627 unmodified lines69569669769869970070 unmodified lines7717727737746 unmodified linesimport stringclass AIChatbot:def __init__(self, database_url: str = "https://sqldatabase-t7po.onrender.com"):self.database_url = database_urlself.conversation_history = []# Simple conversation patternsself.greeting_patterns = [r'\b(hi|hello|hey|good morning|good afternoon|good evening)\b',45 unmodified linesreturn Truereturn Falsedef get_greeting_response(self) -> str:"""Generate a greeting response"""responses = [627 unmodified linesif not message.strip():return "Please enter a message so I can help you!"# Store conversation historyself.conversation_history.append(("user", message))70 unmodified linesserver_port=7860,share=False,debug=True)6 unmodified lines789101112131415161718192021222324252645 unmodified lines72737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131627 unmodified lines75976076176276376476576676776876977077177270 unmodified lines8438448458466 unmodified linesimport stringclass AIChatbot:def __init__(self, database_url: str = "https://database-dhe2.onrender.com"):self.database_url = database_urlself.conversation_history = []# Profanity filter - list of bad words to filterself.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 patternsself.greeting_patterns = [r'\b(hi|hello|hey|good morning|good afternoon|good evening)\b',45 unmodified linesreturn Truereturn Falsedef contains_profanity(self, message: str) -> bool:"""Check if the message contains any profanity"""# Normalize message: convert to lowercasemessage_lower = message.lower()# First, normalize common obfuscation characters# Replace common character substitutions (numbers/symbols) with lettersobfuscation_map = {'0': 'o', '1': 'i', '3': 'e', '4': 'a', '5': 's','7': 't', '@': 'a', '!': 'i', '$': 's', '&': 'a'}# Create a normalized version for checkingnormalized = message_lowerfor char, replacement in obfuscation_map.items():normalized = normalized.replace(char, replacement)# Remove all non-word characters (except spaces) for word boundary checkingmessage_clean = re.sub(r'[^\w\s]', '', normalized)words = message_clean.split()# Check for exact word matches in cleaned messagefor 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 boundariesfor 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 wordpattern2 = r'\b' + re.escape(bad_word) + r'\b'if re.search(pattern2, message_clean):return Truereturn Falsedef 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 randomreturn random.choice(responses)def get_greeting_response(self) -> str:"""Generate a greeting response"""responses = [627 unmodified linesif not message.strip():return "Please enter a message so I can help you!"# Check for profanity firstif 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 historyself.conversation_history.append(("user", message))70 unmodified linesserver_port=7860,share=False,debug=True)
app.py+74-2