Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -211,6 +211,20 @@ def detect_subject(message, history=None):
|
|
| 211 |
else:
|
| 212 |
return general_template, "General Mode"
|
| 213 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
|
| 215 |
def respond_with_enhanced_streaming(message, history):
|
| 216 |
"""Streams the bot's response, detecting the subject and handling errors."""
|
|
|
|
| 211 |
else:
|
| 212 |
return general_template, "General Mode"
|
| 213 |
|
| 214 |
+
def smart_truncate(text, max_length=3000):
|
| 215 |
+
"""Truncates text intelligently to the last full sentence or word."""
|
| 216 |
+
if len(text) <= max_length:
|
| 217 |
+
return text
|
| 218 |
+
|
| 219 |
+
# Try to split by sentence
|
| 220 |
+
sentences = re.split(r'(?<=[.!?])\s+', text[:max_length])
|
| 221 |
+
if len(sentences) > 1:
|
| 222 |
+
return ' '.join(sentences[:-1]) + "... [Response truncated - ask for continuation]"
|
| 223 |
+
# Otherwise, split by word
|
| 224 |
+
else:
|
| 225 |
+
words = text[:max_length].split()
|
| 226 |
+
return ' '.join(words[:-1]) + "... [Response truncated - ask for continuation]"
|
| 227 |
+
|
| 228 |
|
| 229 |
def respond_with_enhanced_streaming(message, history):
|
| 230 |
"""Streams the bot's response, detecting the subject and handling errors."""
|