Katieearl1513 commited on
Commit
42f79de
·
verified ·
1 Parent(s): 0ef5320

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -5
app.py CHANGED
@@ -29,20 +29,19 @@ model = genai.GenerativeModel(
29
  generation_config=generation_config,
30
  system_instruction=system_instruction
31
  )
32
- '''
33
  # Function to detect inappropriate language
34
  def contains_inappropriate_language(text):
35
  inappropriate_patterns = [
36
- r"\b(insult|offensive_word1|offensive_word2)\b", # Replace with actual patterns
37
- r"\b(maleducado|grosero)\b", # Spanish examples
38
- r"\b(ofensiu|insultant)\b" # Catalan examples
39
  ]
40
  for pattern in inappropriate_patterns:
41
  if re.search(pattern, text, re.IGNORECASE):
42
  return True
43
  return False
44
- '''
45
 
 
46
  def chatbot(user_input, history, chat_session):
47
  if contains_inappropriate_language(user_input):
48
  warning = "El lenguaje inapropiado no está permitido. Por favor, formula tu pregunta de manera respetuosa."
@@ -67,6 +66,16 @@ def chatbot(user_input, history, chat_session):
67
  return gradio_history, history, chat_session
68
 
69
  # Handling errors
 
 
 
 
 
 
 
 
 
 
70
  with gr.Blocks() as demo:
71
  gr.Markdown("# Chatbot de Estadística 📊\nInteractúa con un tutor experto en estadística básica. Tus preguntas deben ser en castellano o catalán.")
72
 
 
29
  generation_config=generation_config,
30
  system_instruction=system_instruction
31
  )
32
+
33
  # Function to detect inappropriate language
34
  def contains_inappropriate_language(text):
35
  inappropriate_patterns = [
36
+ r"\b(joder|maleducado)\b", # Replace with actual patterns
37
+ # Spanish examples: r"\b(maleducado|grosero)\b",
 
38
  ]
39
  for pattern in inappropriate_patterns:
40
  if re.search(pattern, text, re.IGNORECASE):
41
  return True
42
  return False
 
43
 
44
+ # main code
45
  def chatbot(user_input, history, chat_session):
46
  if contains_inappropriate_language(user_input):
47
  warning = "El lenguaje inapropiado no está permitido. Por favor, formula tu pregunta de manera respetuosa."
 
66
  return gradio_history, history, chat_session
67
 
68
  # Handling errors
69
+ def chatbot_with_error_handling(user_input, history, chat_session):
70
+ try:
71
+ return chatbot(user_input, history, chat_session)
72
+ except Exception as e:
73
+ error_message = f"Ha ocurrido un error: {str(e)}"
74
+ history.append({"role": "assistant", "content": error_message})
75
+ gradio_history = [[msg["role"], msg["content"]] for msg in history]
76
+ return gradio_history, history, chat_session
77
+
78
+ #interface
79
  with gr.Blocks() as demo:
80
  gr.Markdown("# Chatbot de Estadística 📊\nInteractúa con un tutor experto en estadística básica. Tus preguntas deben ser en castellano o catalán.")
81