Spaces:
Sleeping
Sleeping
Upload 2 files
Browse filesFiles changed (2)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687"""Opt-in public media analysis (no research token in client code).
Enable only after setting budget-aware operational controls. In-memory throttlingis per Python worker, not a distributed quota; add a gateway quota for production."""from __future__ import annotationsimport osimport timefrom collections import defaultdict, dequefrom threading import Lock, BoundedSemaphorefrom flask import Blueprint, jsonify, requestfrom .figure_analysis import analyze_media_question, MediaSelectionErrorfrom .vision_client import VisionConfigurationError
media_public_bp = Blueprint("media_analysis_public", __name__)_hits = defaultdict(deque)_guard = Lock()_busy = BoundedSemaphore(value=1)
def _enabled(): return os.getenv("WATERSCOPE_ENABLE_PUBLIC_MEDIA", "0").lower() in ("1", "true", "yes")
@media_public_bp.post("/api/media/analyze")def media_analyze_public(): if not _enabled(): return jsonify({"error": "Public media analysis is not enabled"}), 404 body = request.get_json(silent=True) if not isinstance(body, dict): return jsonify({"error": "JSON object required"}), 400 question = body.get("question") idx = body.get("metadata_row_index") if not isinstance(question, str) or not (1 <= len(question.strip()) <= 1200): return jsonify({"error": "question must be 1-1200 characters"}), 400 if isinstance(idx, bool) or not isinstance(idx, int) or idx < 0: return jsonify({"error": "Select one indexed publication with available media"}), 400 # The caller may request only a public indexed paper; source paths are resolved on the server. from metadata_search.search_service import get_rows rows = get_rows() if idx >= len(rows): return jsonify({"error": "Paper index out of range"}), 400 row = rows[idx] paper = {"paper_id": str(idx), "metadata_row_index": idx, "paper_title": str(row.get("Title") or row.get("title") or "")[:400], "doi": str(row.get("DOI") or row.get("doi") or "")[:200]} # Match only explicit Figure N or Table N; media is never chosen arbitrarily. import re if not re.search(r"\b(?:figure|fig\.?|table)\s*\d+\b", question, re.I): return jsonify({"error": "Specify Figure N or Table N"}), 400 # Fixed provider and model on server, not client. Hard bound to avoid runaway bills. provider = os.getenv("WATERSCOPE_PUBLIC_MEDIA_PROVIDER", "deepseek").strip().lower() if provider not in ("azure", "deepseek"): return jsonify({"error": "Media provider is not configured"}), 503 model = os.getenv("WATERSCOPE_PUBLIC_MEDIA_MODEL", "deepseek-flash").strip() or None limit = max(1, min(30, int(os.getenv("WATERSCOPE_PUBLIC_MEDIA_HOURLY_PER_IP", "4")))) ip = request.remote_addr or "unknown" # Use proxy-address validation at trusted gateway. now = time.monotonic() with _guard: q = _hits[ip] while q and now - q[0] > 3600: q.popleft() if len(q) >= limit: return jsonify({"error": "Media analysis hourly request limit reached"}), 429 q.append(now) if len(_hits) > 5000: for key in list(_hits): if not _hits[key] or now - _hits[key][-1] > 3600: del _hits[key] if not _busy.acquire(blocking=False): with _guard: q = _hits.get(ip) if q: try: q.remove(now) except ValueError: pass return jsonify({"error": "Media analysis is busy; please retry shortly"}), 429 try: result = analyze_media_question(question.strip(), [paper], enabled=True, provider=provider, model=model, max_output_tokens=4096, evidence_mode="combined", export_csv=True, analysis_mode="auto") # Do not send large duplicate tabular records: CSV already carries the data. result.pop("structured_data", None) return jsonify(result) except (MediaSelectionError, VisionConfigurationError, ValueError, FileNotFoundError) as exc: return jsonify({"error": str(exc)}), 400 except Exception as exc: return jsonify({"error": "Media analysis temporarily unavailable", "error_type": type(exc).__name__}), 502 finally: _busy.release()140 unmodified lines141142143144145146140 unmodified linesfrom backend.media_analysis.test_routes import media_test_bpapp.register_blueprint(media_test_bp)UI_DIR = PROJECT_ROOT / "github_pages"140 unmodified lines141142143144145146147148149150140 unmodified linesfrom backend.media_analysis.test_routes import media_test_bpapp.register_blueprint(media_test_bp)# Public media route is gated by WATERSCOPE_ENABLE_PUBLIC_MEDIA.from backend.media_analysis.public_routes import media_public_bpapp.register_blueprint(media_public_bp)UI_DIR = PROJECT_ROOT / "github_pages"
backend/media_analysis/public_routes.pyAdded+87
metadata_search/app.py+4