Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -325,8 +325,8 @@ def sample_questions(mode: str, num_rounds: int) -> list[dict]:
|
|
| 325 |
|
| 326 |
|
| 327 |
def get_image_path(rel_path: str) -> Path:
|
| 328 |
-
"""Get image absolute path
|
| 329 |
-
return
|
| 330 |
|
| 331 |
|
| 332 |
def _create_placeholder_image() -> Path:
|
|
@@ -437,9 +437,22 @@ def build_round_table_and_stats(round_results: list[dict]) -> tuple[list[list],
|
|
| 437 |
|
| 438 |
|
| 439 |
def _extract_markdown_json_block(text: str) -> str:
|
| 440 |
-
"""Extract JSON text from ```json ... ```."""
|
| 441 |
-
|
| 442 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 443 |
|
| 444 |
|
| 445 |
def _extract_balanced_json_object(text: str) -> str | None:
|
|
@@ -492,6 +505,7 @@ def parse_ai_analysis_payload(raw: str | dict | None) -> tuple[dict, str]:
|
|
| 492 |
data = None
|
| 493 |
method = "fallback-regex"
|
| 494 |
if text:
|
|
|
|
| 495 |
candidate = _extract_markdown_json_block(text)
|
| 496 |
try:
|
| 497 |
data = json.loads(candidate)
|
|
@@ -501,13 +515,16 @@ def parse_ai_analysis_payload(raw: str | dict | None) -> tuple[dict, str]:
|
|
| 501 |
data = json.loads(text)
|
| 502 |
method = "json-direct"
|
| 503 |
except Exception:
|
| 504 |
-
|
|
|
|
| 505 |
if obj:
|
| 506 |
try:
|
| 507 |
data = json.loads(obj)
|
| 508 |
method = "balanced-object-json"
|
| 509 |
except Exception:
|
| 510 |
data = None
|
|
|
|
|
|
|
| 511 |
if data is None:
|
| 512 |
# Fallback: regex extract partial info
|
| 513 |
def _rx(pattern: str) -> str:
|
|
@@ -737,8 +754,8 @@ def build_result_map(
|
|
| 737 |
user_label = "Your Answer"
|
| 738 |
true_label = "True Location"
|
| 739 |
markers = [
|
| 740 |
-
{"lat": disp_true_lat, "lng": disp_true_lng, "color": "
|
| 741 |
-
{"lat": disp_user_lat, "lng": disp_user_lng, "color": "
|
| 742 |
]
|
| 743 |
lines = [
|
| 744 |
{
|
|
@@ -766,8 +783,8 @@ def build_ai_map(
|
|
| 766 |
ai_label = "AI Answer"
|
| 767 |
true_label = "True Location"
|
| 768 |
markers = [
|
| 769 |
-
{"lat": disp_true_lat, "lng": disp_true_lng, "color": "
|
| 770 |
-
{"lat": disp_ai_lat, "lng": disp_ai_lng, "color": "
|
| 771 |
]
|
| 772 |
lines = [
|
| 773 |
{
|
|
@@ -796,7 +813,7 @@ def on_lat_lng_change(lat, lng, state: dict) -> tuple:
|
|
| 796 |
except (TypeError, ValueError):
|
| 797 |
return gr.update(), gr.update(), state
|
| 798 |
mode = state.get("mode", "world")
|
| 799 |
-
markers = [{"lat": lat_f, "lng": lng_f, "color": "
|
| 800 |
m = _create_folium_map(mode=mode, center=(lat_f, lng_f), zoom=8, markers=markers)
|
| 801 |
return gr.update(value=m), gr.update(value=coord), {**state, "pending_lat": lat_f, "pending_lng": lng_f}
|
| 802 |
|
|
|
|
| 325 |
|
| 326 |
|
| 327 |
def get_image_path(rel_path: str) -> Path:
|
| 328 |
+
"""Get image absolute path under data/images/ (filename or subpath)."""
|
| 329 |
+
return IMAGES_DIR / rel_path
|
| 330 |
|
| 331 |
|
| 332 |
def _create_placeholder_image() -> Path:
|
|
|
|
| 437 |
|
| 438 |
|
| 439 |
def _extract_markdown_json_block(text: str) -> str:
|
| 440 |
+
"""Extract JSON text from ```json ... ``` or ``` ... ``` (robust for stored strings)."""
|
| 441 |
+
t = text.strip()
|
| 442 |
+
# 1) Regex: 匹配 ```json 或 ``` 与结尾 ```
|
| 443 |
+
m = re.search(r"```(?:json)?\s*([\s\S]*?)\s*```", t, flags=re.IGNORECASE)
|
| 444 |
+
if m:
|
| 445 |
+
return m.group(1).strip()
|
| 446 |
+
# 2) 手动剥离:以 ``` 开头时去掉首行并截到最后一个 ```
|
| 447 |
+
if t.startswith("```"):
|
| 448 |
+
first_line_end = t.find("\n")
|
| 449 |
+
if first_line_end >= 0:
|
| 450 |
+
t = t[first_line_end + 1 :]
|
| 451 |
+
end = t.rfind("```")
|
| 452 |
+
if end >= 0:
|
| 453 |
+
t = t[:end]
|
| 454 |
+
return t.strip()
|
| 455 |
+
return t
|
| 456 |
|
| 457 |
|
| 458 |
def _extract_balanced_json_object(text: str) -> str | None:
|
|
|
|
| 505 |
data = None
|
| 506 |
method = "fallback-regex"
|
| 507 |
if text:
|
| 508 |
+
# 优先剥离 ```json ... ``` 再解析,兼容 selected_results 等存储格式
|
| 509 |
candidate = _extract_markdown_json_block(text)
|
| 510 |
try:
|
| 511 |
data = json.loads(candidate)
|
|
|
|
| 515 |
data = json.loads(text)
|
| 516 |
method = "json-direct"
|
| 517 |
except Exception:
|
| 518 |
+
# 对已剥离的 candidate 再试一次平衡括号提取(防止尾部杂项)
|
| 519 |
+
obj = _extract_balanced_json_object(candidate) or _extract_balanced_json_object(text)
|
| 520 |
if obj:
|
| 521 |
try:
|
| 522 |
data = json.loads(obj)
|
| 523 |
method = "balanced-object-json"
|
| 524 |
except Exception:
|
| 525 |
data = None
|
| 526 |
+
else:
|
| 527 |
+
data = None
|
| 528 |
if data is None:
|
| 529 |
# Fallback: regex extract partial info
|
| 530 |
def _rx(pattern: str) -> str:
|
|
|
|
| 754 |
user_label = "Your Answer"
|
| 755 |
true_label = "True Location"
|
| 756 |
markers = [
|
| 757 |
+
{"lat": disp_true_lat, "lng": disp_true_lng, "color": "green", "label": true_label},
|
| 758 |
+
{"lat": disp_user_lat, "lng": disp_user_lng, "color": "red", "label": user_label},
|
| 759 |
]
|
| 760 |
lines = [
|
| 761 |
{
|
|
|
|
| 783 |
ai_label = "AI Answer"
|
| 784 |
true_label = "True Location"
|
| 785 |
markers = [
|
| 786 |
+
{"lat": disp_true_lat, "lng": disp_true_lng, "color": "green", "label": true_label},
|
| 787 |
+
{"lat": disp_ai_lat, "lng": disp_ai_lng, "color": "red", "label": ai_label},
|
| 788 |
]
|
| 789 |
lines = [
|
| 790 |
{
|
|
|
|
| 813 |
except (TypeError, ValueError):
|
| 814 |
return gr.update(), gr.update(), state
|
| 815 |
mode = state.get("mode", "world")
|
| 816 |
+
markers = [{"lat": lat_f, "lng": lng_f, "color": "red", "label": "To Be Confirmed"}]
|
| 817 |
m = _create_folium_map(mode=mode, center=(lat_f, lng_f), zoom=8, markers=markers)
|
| 818 |
return gr.update(value=m), gr.update(value=coord), {**state, "pending_lat": lat_f, "pending_lng": lng_f}
|
| 819 |
|