Spaces:
Running
Running
File size: 2,394 Bytes
8a8fb5a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | from __future__ import annotations
import ast
import json
import re
from typing import Any
from models import ChatRequest
def clamp01(x: Any, default: float = 0.5) -> float:
try:
v = float(x)
return max(0.0, min(1.0, v))
except Exception:
return default
def normalize_spaces(text: str) -> str:
return re.sub(r"\s+", " ", text).strip()
def clean_math_text(text: str) -> str:
t = text
t = t.replace("×", "*").replace("÷", "/").replace("–", "-").replace("—", "-")
t = t.replace("−", "-").replace("^", "**")
return t
def extract_text_from_any_payload(payload: Any) -> str:
if payload is None:
return ""
if isinstance(payload, str):
s = payload.strip()
if (s.startswith("{") and s.endswith("}")) or (s.startswith("[") and s.endswith("]")):
try:
decoded = json.loads(s)
return extract_text_from_any_payload(decoded)
except Exception:
pass
try:
decoded = ast.literal_eval(s)
if isinstance(decoded, (dict, list)):
return extract_text_from_any_payload(decoded)
except Exception:
pass
return s
if isinstance(payload, dict):
for key in [
"message", "prompt", "query", "text", "user_message",
"input", "data", "payload", "body", "content"
]:
if key in payload:
extracted = extract_text_from_any_payload(payload[key])
if extracted:
return extracted
strings = []
for v in payload.values():
if isinstance(v, (str, dict, list)):
maybe = extract_text_from_any_payload(v)
if maybe:
strings.append(maybe)
return "\n".join(strings).strip()
if isinstance(payload, list):
parts = [extract_text_from_any_payload(x) for x in payload]
return "\n".join([p for p in parts if p]).strip()
return str(payload).strip()
def get_user_text(req: ChatRequest, raw_body: Any = None) -> str:
for field in ["message", "prompt", "query", "text", "user_message"]:
value = getattr(req, field, None)
if isinstance(value, str) and value.strip():
return value.strip()
return extract_text_from_any_payload(raw_body).strip() |