Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -27,8 +27,8 @@ from google import genai
|
|
| 27 |
OPENAI_MODEL = os.getenv("OPENAI_MODEL", "gpt-4o-mini") # change if you want
|
| 28 |
GEMINI_MODEL = os.getenv("GEMINI_MODEL", "gemini-1.5-pro")
|
| 29 |
|
| 30 |
-
MAX_SOURCES =
|
| 31 |
-
MAX_EXCERPT_CHARS =
|
| 32 |
FETCH_TIMEOUT = 25
|
| 33 |
|
| 34 |
# Simple in-memory rate limit (per session/ip best-effort)
|
|
@@ -58,18 +58,29 @@ def clip(s: str, n: int) -> str:
|
|
| 58 |
return s[:n]
|
| 59 |
|
| 60 |
def safe_json_extract(text: str) -> Any:
|
| 61 |
-
"""
|
| 62 |
-
Extract JSON even if model wraps it in fences or extra text.
|
| 63 |
-
"""
|
| 64 |
text = (text or "").strip()
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
text = re.sub(r"
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
|
| 75 |
# -----------------------------
|
|
|
|
| 27 |
OPENAI_MODEL = os.getenv("OPENAI_MODEL", "gpt-4o-mini") # change if you want
|
| 28 |
GEMINI_MODEL = os.getenv("GEMINI_MODEL", "gemini-1.5-pro")
|
| 29 |
|
| 30 |
+
MAX_SOURCES = 4
|
| 31 |
+
MAX_EXCERPT_CHARS = 2500
|
| 32 |
FETCH_TIMEOUT = 25
|
| 33 |
|
| 34 |
# Simple in-memory rate limit (per session/ip best-effort)
|
|
|
|
| 58 |
return s[:n]
|
| 59 |
|
| 60 |
def safe_json_extract(text: str) -> Any:
|
|
|
|
|
|
|
|
|
|
| 61 |
text = (text or "").strip()
|
| 62 |
+
|
| 63 |
+
# Strip fenced blocks
|
| 64 |
+
text = re.sub(r"^```(?:json)?\s*", "", text, flags=re.I).strip()
|
| 65 |
+
text = re.sub(r"\s*```$", "", text).strip()
|
| 66 |
+
|
| 67 |
+
# Try direct load first
|
| 68 |
+
try:
|
| 69 |
+
return json.loads(text)
|
| 70 |
+
except Exception:
|
| 71 |
+
pass
|
| 72 |
+
|
| 73 |
+
# Try to find a JSON object
|
| 74 |
+
m = re.search(r"(\{.*\})", text, flags=re.S)
|
| 75 |
+
if m:
|
| 76 |
+
return json.loads(m.group(1))
|
| 77 |
+
|
| 78 |
+
# Try to find a JSON array
|
| 79 |
+
m = re.search(r"(\[.*\])", text, flags=re.S)
|
| 80 |
+
if m:
|
| 81 |
+
return json.loads(m.group(1))
|
| 82 |
+
|
| 83 |
+
raise ValueError("Model output bevat geen JSON object/array.")
|
| 84 |
|
| 85 |
|
| 86 |
# -----------------------------
|