Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -103,24 +103,48 @@ def _normalize_lines(text: str) -> List[str]:
|
|
| 103 |
return [raw.strip()] if raw.strip() else []
|
| 104 |
|
| 105 |
def _ensure_numbering(text: str) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
lines = _normalize_lines(text)
|
| 107 |
if not lines:
|
| 108 |
return text or ""
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
r"^\s*(?:\d+[.\)\]]\s+|(?:step\s*\d+:?)\s+|[\-\*\u2022]\s+)",
|
| 113 |
"",
|
| 114 |
ln,
|
| 115 |
flags=re.IGNORECASE,
|
| 116 |
).strip()
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
| 120 |
parts = [p.strip() for p in re.split(r"(?:;|\.\s+(?=[A-Z0-9]))", para) if p.strip()]
|
| 121 |
if len(parts) > 1:
|
| 122 |
-
|
| 123 |
-
|
|
|
|
|
|
|
| 124 |
|
| 125 |
def _friendly_permission_reply(raw: str) -> str:
|
| 126 |
line = (raw or "").strip()
|
|
@@ -235,7 +259,8 @@ def _normalize_for_match(text: str) -> str:
|
|
| 235 |
return t
|
| 236 |
|
| 237 |
def _split_sentences(ctx: str) -> List[str]:
|
| 238 |
-
|
|
|
|
| 239 |
return [s.strip() for s in raw_sents if s and len(s.strip()) > 2]
|
| 240 |
|
| 241 |
def _filter_context_for_query(context: str, query: str) -> Tuple[str, Dict[str, Any]]:
|
|
@@ -316,7 +341,7 @@ async def chat_with_ai(input_data: ChatInput):
|
|
| 316 |
try:
|
| 317 |
msg_norm = (input_data.user_message or "").lower().strip()
|
| 318 |
|
| 319 |
-
#
|
| 320 |
if msg_norm in ("yes", "y", "sure", "ok", "okay"):
|
| 321 |
return {
|
| 322 |
"bot_response": "Great! Tell me what you'd like to do next — check another ticket, create an incident, or describe your issue.",
|
|
|
|
| 103 |
return [raw.strip()] if raw.strip() else []
|
| 104 |
|
| 105 |
def _ensure_numbering(text: str) -> str:
|
| 106 |
+
"""
|
| 107 |
+
Cleanly number steps: 1) 2) 3) ...
|
| 108 |
+
- Merge dangling numeric lines like '2' that are followed by text.
|
| 109 |
+
- Strip existing numbers/bullets and re-number.
|
| 110 |
+
"""
|
| 111 |
lines = _normalize_lines(text)
|
| 112 |
if not lines:
|
| 113 |
return text or ""
|
| 114 |
+
|
| 115 |
+
# Merge 'dangling number' lines (e.g., "2" on its own line)
|
| 116 |
+
merged: List[str] = []
|
| 117 |
+
i = 0
|
| 118 |
+
while i < len(lines):
|
| 119 |
+
ln = lines[i].strip()
|
| 120 |
+
if re.fullmatch(r"\d+[.)]?", ln) and (i + 1) < len(lines):
|
| 121 |
+
# merge number with the next line's text
|
| 122 |
+
merged.append(lines[i + 1].strip())
|
| 123 |
+
i += 2
|
| 124 |
+
else:
|
| 125 |
+
merged.append(ln)
|
| 126 |
+
i += 1
|
| 127 |
+
|
| 128 |
+
# Remove any bullet/number prefix
|
| 129 |
+
cleaned = [
|
| 130 |
+
re.sub(
|
| 131 |
r"^\s*(?:\d+[.\)\]]\s+|(?:step\s*\d+:?)\s+|[\-\*\u2022]\s+)",
|
| 132 |
"",
|
| 133 |
ln,
|
| 134 |
flags=re.IGNORECASE,
|
| 135 |
).strip()
|
| 136 |
+
for ln in merged
|
| 137 |
+
]
|
| 138 |
+
|
| 139 |
+
# If single paragraph, split on separators
|
| 140 |
+
if len(cleaned) == 1:
|
| 141 |
+
para = cleaned[0]
|
| 142 |
parts = [p.strip() for p in re.split(r"(?:;|\.\s+(?=[A-Z0-9]))", para) if p.strip()]
|
| 143 |
if len(parts) > 1:
|
| 144 |
+
cleaned = parts
|
| 145 |
+
|
| 146 |
+
# Final numbering
|
| 147 |
+
return "\n".join([f"{idx+1}) {ln}" for idx, ln in enumerate(cleaned) if ln])
|
| 148 |
|
| 149 |
def _friendly_permission_reply(raw: str) -> str:
|
| 150 |
line = (raw or "").strip()
|
|
|
|
| 259 |
return t
|
| 260 |
|
| 261 |
def _split_sentences(ctx: str) -> List[str]:
|
| 262 |
+
# Non-capturing split to avoid stray tokens that break numbering
|
| 263 |
+
raw_sents = re.split(r"(?<=[.!?])\s+|\n+|\-\s*|\*\s*", ctx or "")
|
| 264 |
return [s.strip() for s in raw_sents if s and len(s.strip()) > 2]
|
| 265 |
|
| 266 |
def _filter_context_for_query(context: str, query: str) -> Tuple[str, Dict[str, Any]]:
|
|
|
|
| 341 |
try:
|
| 342 |
msg_norm = (input_data.user_message or "").lower().strip()
|
| 343 |
|
| 344 |
+
# Quick yes/no
|
| 345 |
if msg_norm in ("yes", "y", "sure", "ok", "okay"):
|
| 346 |
return {
|
| 347 |
"bot_response": "Great! Tell me what you'd like to do next — check another ticket, create an incident, or describe your issue.",
|