markobinario commited on
Commit
cefbb61
·
verified ·
1 Parent(s): 401fb99

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -2
app.py CHANGED
@@ -7,10 +7,20 @@ from difflib import SequenceMatcher
7
  import string
8
 
9
  class AIChatbot:
10
- def __init__(self, database_url: str = "https://sqldatabase-t7po.onrender.com"):
11
  self.database_url = database_url
12
  self.conversation_history = []
13
 
 
 
 
 
 
 
 
 
 
 
14
  # Simple conversation patterns
15
  self.greeting_patterns = [
16
  r'\b(hi|hello|hey|good morning|good afternoon|good evening)\b',
@@ -62,6 +72,60 @@ class AIChatbot:
62
  return True
63
  return False
64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  def get_greeting_response(self) -> str:
66
  """Generate a greeting response"""
67
  responses = [
@@ -695,6 +759,14 @@ Just type your question or start a conversation, and I'll do my best to help you
695
  if not message.strip():
696
  return "Please enter a message so I can help you!"
697
 
 
 
 
 
 
 
 
 
698
  # Store conversation history
699
  self.conversation_history.append(("user", message))
700
 
@@ -771,4 +843,4 @@ if __name__ == "__main__":
771
  server_port=7860,
772
  share=False,
773
  debug=True
774
- )
 
7
  import string
8
 
9
  class AIChatbot:
10
+ def __init__(self, database_url: str = "https://database-dhe2.onrender.com"):
11
  self.database_url = database_url
12
  self.conversation_history = []
13
 
14
+ # Profanity filter - list of bad words to filter
15
+ self.bad_words = {
16
+ 'damn', 'hell', 'crap', 'suck', 'idiot', 'fool', 'jerk', 'loser', 'dumb', 'trash',
17
+ 'butt', 'freak', 'nut', 'moron', 'dummy', 'bozo', 'twit', 'dope', 'dumbass',
18
+ 'poophead', 'jerkoff', 'bugger', 'wanker', 'tosser', 'bastard', 'scum', 'slime',
19
+ 'creep', 'brat', 'dweeb', 'goon', 'booby', 'puke', 'vomit', 'dung', 'sap',
20
+ 'clutz', 'knob', 'prick', 'ass', 'shit', 'fuck', 'cock', 'tits', 'pussy',
21
+ 'cunt', 'slut', 'bitch', 'whore', 'skank'
22
+ }
23
+
24
  # Simple conversation patterns
25
  self.greeting_patterns = [
26
  r'\b(hi|hello|hey|good morning|good afternoon|good evening)\b',
 
72
  return True
73
  return False
74
 
75
+ def contains_profanity(self, message: str) -> bool:
76
+ """Check if the message contains any profanity"""
77
+ # Normalize message: convert to lowercase
78
+ message_lower = message.lower()
79
+
80
+ # First, normalize common obfuscation characters
81
+ # Replace common character substitutions (numbers/symbols) with letters
82
+ obfuscation_map = {
83
+ '0': 'o', '1': 'i', '3': 'e', '4': 'a', '5': 's',
84
+ '7': 't', '@': 'a', '!': 'i', '$': 's', '&': 'a'
85
+ }
86
+
87
+ # Create a normalized version for checking
88
+ normalized = message_lower
89
+ for char, replacement in obfuscation_map.items():
90
+ normalized = normalized.replace(char, replacement)
91
+
92
+ # Remove all non-word characters (except spaces) for word boundary checking
93
+ message_clean = re.sub(r'[^\w\s]', '', normalized)
94
+ words = message_clean.split()
95
+
96
+ # Check for exact word matches in cleaned message
97
+ for word in words:
98
+ if word in self.bad_words:
99
+ return True
100
+
101
+ # Check for words that start with bad words (handles variations like "fucking" from "fuck")
102
+ # Also check the original message for word boundaries
103
+ for bad_word in self.bad_words:
104
+ # Pattern 1: Word boundary followed by bad word (handles "fuck", "fucking", etc.)
105
+ pattern1 = r'\b' + re.escape(bad_word) + r'\w*'
106
+ if re.search(pattern1, normalized):
107
+ return True
108
+
109
+ # Pattern 2: Check in cleaned message (handles words with punctuation removed)
110
+ if bad_word in message_clean:
111
+ # Make sure it's a whole word, not part of another word
112
+ pattern2 = r'\b' + re.escape(bad_word) + r'\b'
113
+ if re.search(pattern2, message_clean):
114
+ return True
115
+
116
+ return False
117
+
118
+ def get_profanity_warning(self) -> str:
119
+ """Get a polite response when profanity is detected"""
120
+ responses = [
121
+ "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.",
122
+ "I appreciate your message, but let's keep our conversation friendly and professional. How can I assist you today?",
123
+ "I'm here to help, but I'd prefer we keep our conversation appropriate. Is there something specific you'd like to ask me?",
124
+ "Let's maintain a respectful conversation. I'm happy to help you with any questions or information you need."
125
+ ]
126
+ import random
127
+ return random.choice(responses)
128
+
129
  def get_greeting_response(self) -> str:
130
  """Generate a greeting response"""
131
  responses = [
 
759
  if not message.strip():
760
  return "Please enter a message so I can help you!"
761
 
762
+ # Check for profanity first
763
+ if self.contains_profanity(message):
764
+ response = self.get_profanity_warning()
765
+ # Store conversation history (but don't process the message)
766
+ self.conversation_history.append(("user", "[Filtered]"))
767
+ self.conversation_history.append(("bot", response))
768
+ return response
769
+
770
  # Store conversation history
771
  self.conversation_history.append(("user", message))
772
 
 
843
  server_port=7860,
844
  share=False,
845
  debug=True
846
+ )