Spaces:
Sleeping
Sleeping
ADDED SANITIZE FUNCTION
Browse files- pipeline.py +15 -0
pipeline.py
CHANGED
|
@@ -228,9 +228,24 @@ def merge_responses(kb_answer: str, web_answer: str) -> str:
|
|
| 228 |
return f"Knowledge Base Answer: {kb_answer.strip()}\n\nWeb Search Result: {web_answer.strip()}"
|
| 229 |
except Exception as e:
|
| 230 |
return f"Error merging responses: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 231 |
|
|
|
|
| 232 |
def run_pipeline(query: str) -> str:
|
| 233 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 234 |
# Validate and moderate input
|
| 235 |
moderation_result = moderate_text(query)
|
| 236 |
if not moderation_result.is_safe:
|
|
|
|
| 228 |
return f"Knowledge Base Answer: {kb_answer.strip()}\n\nWeb Search Result: {web_answer.strip()}"
|
| 229 |
except Exception as e:
|
| 230 |
return f"Error merging responses: {str(e)}"
|
| 231 |
+
def sanitize_message(message: Any) -> str:
|
| 232 |
+
"""Sanitize message input to ensure it's a valid string."""
|
| 233 |
+
if hasattr(message, 'content'):
|
| 234 |
+
return str(message.content)
|
| 235 |
+
if isinstance(message, (list, dict)):
|
| 236 |
+
return str(message)
|
| 237 |
+
return str(message)
|
| 238 |
|
| 239 |
+
# Modify your run_pipeline function to include the sanitization
|
| 240 |
def run_pipeline(query: str) -> str:
|
| 241 |
try:
|
| 242 |
+
# Sanitize input
|
| 243 |
+
query = sanitize_message(query)
|
| 244 |
+
|
| 245 |
+
# Rest of your pipeline code...
|
| 246 |
+
moderation_result = moderate_text(query)
|
| 247 |
+
if not moderation_result.is_safe:
|
| 248 |
+
return "Sorry, this query contains harmful or inappropriate content."
|
| 249 |
# Validate and moderate input
|
| 250 |
moderation_result = moderate_text(query)
|
| 251 |
if not moderation_result.is_safe:
|