Spaces:
Sleeping
Sleeping
Upload 6 files
Browse filesFiles changed (5)
28 unmodified lines29303132333428 unmodified linesreturn str(obj.get("answer") or ""), rows, Nonedef csv_attachment(records: list[dict], sources: list[dict]) -> dict | None:if not records:return None28 unmodified lines2930313233343536373839404142434445464748495051525354555657585928 unmodified linesreturn str(obj.get("answer") or ""), rows, NoneNUMERIC_FIELDS = {"minimum", "maximum", "mean", "std", "value", "x", "y", "percent_value"}def normalize_records(records: list[dict]) -> tuple[list[dict], list[str]]:"""Normalize unambiguous numeric fields, retain originals when text formatting differs.Never infer a missing sign, range, classification, or null value."""result, warnings = [], []for index, original in enumerate(records, 1):item = dict(original)for key in NUMERIC_FIELDS & item.keys():value = item[key]if isinstance(value, str):cleaned = value.strip()normalized = cleaned.replace(",", "")if re.fullmatch(r"[+-]?(?:\d+(?:\.\d*)?|\.\d+)", normalized):item[key + "_printed"] = valueitem[key] = float(normalized) if "." in normalized else int(normalized)elif cleaned:warnings.append(f"Row {index}: {key} is not an unambiguous number: {value!r}")result.append(item)return result, warningsdef csv_attachment(records: list[dict], sources: list[dict]) -> dict | None:if not records:return None
118 unmodified lines1191201211221231241254 unmodified lines13013113213313413510 unmodified lines146147148149150151152153154155156118 unmodified linesdef analyze_media_question(question: str, selected_papers: list[dict], *, enabled: bool | None = None,figures_lookup=None, asset_resolver=None, vision_client=None, provider: str = "azure", model: str | None = None, max_output_tokens: int | None = None,evidence_mode: str = "vision", export_csv: bool = False) -> dict:"""Explicit opt-in entry point. Never silently substitutes a text summary answer."""if enabled is None:enabled = os.getenv("WATERSCOPE_ENABLE_MEDIA_ANALYSIS", "0").lower() in ("1", "true", "yes")4 unmodified linesif vision_client is None:from .vision_client import analyze_imagesvision_client = analyze_imagesif evidence_mode not in ("vision", "json", "combined"):raise ValueError("evidence_mode must be vision, json or combined")if evidence_mode != "vision":10 unmodified linesmax_output_tokens=max_output_tokens, evidence_mode=evidence_mode,structured_output=export_csv)if export_csv:from .data_export import decode_structured_answer, csv_attachmentanswer, records, warning = decode_structured_answer(result.get("answer", ""))result["answer"] = answerresult["structured_data"] = recordsresult["csv_attachment"] = csv_attachment(records, result.get("sources") or [])if warning:result["export_warning"] = warningreturn {"status": "ok", "vision_inspected": evidence_mode != "json", "evidence_mode": evidence_mode, **result}118 unmodified lines1191201211221231241254 unmodified lines13013113213313413513613713813914014110 unmodified lines152153154155156157158159160161162163164118 unmodified linesdef analyze_media_question(question: str, selected_papers: list[dict], *, enabled: bool | None = None,figures_lookup=None, asset_resolver=None, vision_client=None, provider: str = "azure", model: str | None = None, max_output_tokens: int | None = None,evidence_mode: str = "vision", export_csv: bool = False, analysis_mode: str = "auto") -> dict:"""Explicit opt-in entry point. Never silently substitutes a text summary answer."""if enabled is None:enabled = os.getenv("WATERSCOPE_ENABLE_MEDIA_ANALYSIS", "0").lower() in ("1", "true", "yes")4 unmodified linesif vision_client is None:from .vision_client import analyze_imagesvision_client = analyze_imagesif analysis_mode not in ("auto", "extract_all", "interpret"):raise ValueError("analysis_mode must be auto, extract_all, or interpret")if analysis_mode == "extract_all":question = question + "\n[Task: extract every available data row and classification subrow from the referenced media.]"elif analysis_mode == "interpret":question = question + "\n[Task: prioritize interpretation; extract only data relevant to the question.]"if evidence_mode not in ("vision", "json", "combined"):raise ValueError("evidence_mode must be vision, json or combined")if evidence_mode != "vision":10 unmodified linesmax_output_tokens=max_output_tokens, evidence_mode=evidence_mode,structured_output=export_csv)if export_csv:from .data_export import decode_structured_answer, csv_attachment, normalize_recordsanswer, records, warning = decode_structured_answer(result.get("answer", ""))result["answer"] = answerrecords, normalization_warnings = normalize_records(records)result["structured_data"] = recordsresult["normalization_warnings"] = normalization_warningsresult["csv_attachment"] = csv_attachment(records, result.get("sources") or [])if warning:result["export_warning"] = warningreturn {"status": "ok", "vision_inspected": evidence_mode != "json", "evidence_mode": evidence_mode, "analysis_mode": analysis_mode, **result}
33 unmodified lines343536373839404142434445464748495033 unmodified linescells = table.get("cells")if not isinstance(rows, list) or len(rows) > 500:raise ValueError("Invalid/oversized table text rows")# Keep word x/y coordinates so model can align columns. Truncate payload# deterministically rather than sending the enormous extraction JSON.compact_rows = []for row in rows:words = []for word in (row.get("words") or [])[:60]:bbox = word.get("bbox_points") or []words.append({"text": str(word.get("text", ""))[:100],"x": round(float(bbox[0]), 1) if len(bbox) == 4 else None,"y": round(float(bbox[1]), 1) if len(bbox) == 4 else None})compact_rows.append({"text": str(row.get("text", ""))[:1000], "words": words})return {"label": label, "data_file": filename,"structure_extracted": bool(table.get("structure_extracted")),33 unmodified lines3435363738394041424344454647484950515253545533 unmodified linescells = table.get("cells")if not isinstance(rows, list) or len(rows) > 500:raise ValueError("Invalid/oversized table text rows")# Preserve every source row, but replace redundant per-word bounding boxes# with a compact word@horizontal-position representation. A text row is NOT# a verified table cell row, especially for merged/multi-line groups.compact_rows = []for row in rows:words = []for word in (row.get("words") or [])[:100]:bbox = word.get("bbox_points") or []token = str(word.get("text", ""))[:120]if len(bbox) == 4:words.append([token, round(float(bbox[0]), 1)])else:words.append([token, None])compact_rows.append({"text": str(row.get("text", ""))[:1600], "positioned_words": words})# Include all nonempty rows, including continuation rows and classifications.# The original JSON remains in the dataset; this is only an inference view.return {"label": label, "data_file": filename,"structure_extracted": bool(table.get("structure_extracted")),
47 unmodified lines48495051525316 unmodified lines7071727374757647 unmodified linesevidence_mode = str(body.get("evidence_mode", "vision")).strip().lower()if evidence_mode not in ("vision", "json", "combined"):return jsonify({"error": "evidence_mode must be vision, json or combined"}), 400export_csv = body.get("export_csv", False)if not isinstance(export_csv, bool):return jsonify({"error": "export_csv must be boolean"}), 40016 unmodified linessanitized.append(clean)try:result = analyze_media_question(question, sanitized, enabled=True, provider=provider, model=model, max_output_tokens=max_output_tokens,evidence_mode=evidence_mode, export_csv=export_csv)return jsonify(result)except (MediaSelectionError, VisionConfigurationError, ValueError, FileNotFoundError) as exc:return jsonify({"error": str(exc), "vision_inspected": False}), 40047 unmodified lines48495051525354555616 unmodified lines7374757677787947 unmodified linesevidence_mode = str(body.get("evidence_mode", "vision")).strip().lower()if evidence_mode not in ("vision", "json", "combined"):return jsonify({"error": "evidence_mode must be vision, json or combined"}), 400analysis_mode = str(body.get("analysis_mode", "auto")).strip().lower()if analysis_mode not in ("auto", "extract_all", "interpret"):return jsonify({"error": "analysis_mode must be auto, extract_all, or interpret"}), 400export_csv = body.get("export_csv", False)if not isinstance(export_csv, bool):return jsonify({"error": "export_csv must be boolean"}), 40016 unmodified linessanitized.append(clean)try:result = analyze_media_question(question, sanitized, enabled=True, provider=provider, model=model, max_output_tokens=max_output_tokens,evidence_mode=evidence_mode, export_csv=export_csv, analysis_mode=analysis_mode)return jsonify(result)except (MediaSelectionError, VisionConfigurationError, ValueError, FileNotFoundError) as exc:return jsonify({"error": str(exc), "vision_inspected": False}), 400
115 unmodified lines116117118119120121122123124125126127115 unmodified lines)if structured_output:instructions += (" Return ONLY a valid JSON object with keys answer (scientific narrative string) ""and records (array of objects, one per requested data row). Include units, ""estimated (boolean), extraction_method (printed_table|positioned_text|visual_estimate), ""and source_quality when possible. For multiple media items each record MUST ""identify paper_id and media_label. Do not fabricate rows or values. ""For uncertain cells use null and explain uncertainty in answer.")content: list[dict] = [{"type": "input_text", "text": f"Question: {question.strip()}"}]used = []115 unmodified lines116117118119120121122123124125126127128129130131132133134135136137115 unmodified lines)if structured_output:instructions += (" Return ONLY valid JSON with keys answer (brief readable summary) and records ""(array containing exactly the requested extracted rows). For a request to extract ""all data, include every parameter and every classification subrow, standard range, ""percentage, reference, units and descriptive statistics, without silently omitting ""rows. In repeated subrows, do not invent or repeat descriptive statistics that ""occur only in the first source row: use null. ""Use stable field names: parameter, unit, minimum, maximum, mean, std, ""standard_range, class_label, classification, percent_samples, references, ""estimated (boolean), extraction_method (printed_table|positioned_text|visual_estimate), ""and source_quality. Preserve original printed parameter names, units, inequality ""signs, and numeric precision. Numeric values must be JSON numbers (no thousands ""separators); use *_printed fields if preservation of original formatting matters. ""For ambiguous OCR signs, compare against image; if still unclear use null and ""explain ambiguity. Source-quality is descriptive, never a proof of verification. ""When extracting multiple sources include paper_id and media_label per record. ""The answer must not claim a complete extraction if rows are omitted.")content: list[dict] = [{"type": "input_text", "text": f"Question: {question.strip()}"}]used = []
backend/media_analysis/data_export.py+25
backend/media_analysis/figure_analysis.py+11-3
backend/media_analysis/table_data.py+12-7
backend/media_analysis/test_routes.py+4-1
backend/media_analysis/vision_client.py+15-5