File size: 13,446 Bytes
92038dd
a10eb48
 
 
 
 
51096c9
 
d28ed33
51096c9
 
 
d28ed33
51096c9
 
 
f9508a4
 
 
 
 
 
 
 
 
 
51096c9
178c618
f9508a4
178c618
f9508a4
51096c9
a10eb48
 
 
 
51096c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a10eb48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d28ed33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51096c9
f9508a4
 
51096c9
a10eb48
 
51096c9
 
6a408fb
a10eb48
 
 
 
 
 
4ce40cf
51096c9
a10eb48
 
51096c9
a10eb48
f9508a4
51096c9
f9508a4
51096c9
f9508a4
 
a10eb48
 
 
51096c9
a10eb48
f9508a4
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# -*- coding: utf-8 -*-
"""Headless file parsing helpers used by both Streamlit and R3-Free executor.

P0-R7-B keeps parsing fail-soft for historical generation calls, while exposing
an explicit validator that the UI can run before task creation/quota reservation.
"""
from __future__ import annotations

import hashlib
import io
import logging
import os
import re
from pathlib import Path
from typing import BinaryIO, Iterable

from docx import Document
from config import APP_CONFIG

try:
    import pandas as pd
    PANDAS_AVAILABLE = True
except ImportError:
    PANDAS_AVAILABLE = False

try:
    import pymupdf
    PYMUPDF_AVAILABLE = True
except ImportError:
    PYMUPDF_AVAILABLE = False


class FileParseError(RuntimeError):
    """A user file cannot be parsed reliably by the current runtime."""


def _name(file) -> str:
    if isinstance(file, (str, os.PathLike, Path)):
        return os.fspath(file)
    return str(getattr(file, "name", "uploaded_file"))


def _read_bytes(file) -> bytes:
    if isinstance(file, (str, os.PathLike, Path)):
        return Path(file).read_bytes()
    try:
        file.seek(0)
    except Exception:
        pass
    if hasattr(file, "getvalue"):
        data = file.getvalue()
    else:
        data = file.read()
    try:
        file.seek(0)
    except Exception:
        pass
    return bytes(data or b"")


def _binary_stream(file) -> io.BytesIO | BinaryIO:
    if isinstance(file, (str, os.PathLike, Path)):
        return io.BytesIO(Path(file).read_bytes())
    try:
        file.seek(0)
    except Exception:
        pass
    return file


def _decode_text_bytes(data: bytes) -> str:
    if not data:
        return ""
    for encoding in ("utf-8-sig", "utf-8", "utf-16", "gb18030"):
        try:
            return data.decode(encoding)
        except UnicodeDecodeError:
            continue
    raise FileParseError("text_encoding_unsupported")


def _read_table(file, lower_name: str):
    if not PANDAS_AVAILABLE:
        raise FileParseError("pandas_unavailable")
    source = os.fspath(file) if isinstance(file, (str, os.PathLike, Path)) else file
    if not isinstance(file, (str, os.PathLike, Path)):
        try:
            file.seek(0)
        except Exception:
            pass
    if lower_name.endswith(".csv"):
        last_exc = None
        for encoding in ("utf-8-sig", "utf-8", "gb18030"):
            try:
                if not isinstance(file, (str, os.PathLike, Path)):
                    file.seek(0)
                return pd.read_csv(source, encoding=encoding)
            except UnicodeDecodeError as exc:
                last_exc = exc
                continue
        if last_exc:
            raise last_exc
        return pd.read_csv(source)
    return pd.read_excel(source)


def extract_file_content(file, max_chars=None) -> str:
    """Parse one supported user file and raise FileParseError on failure.

    The returned text is suitable for either literature context or a bounded
    research-material bundle. Structured files are converted to Markdown.
    """
    if max_chars is None:
        max_chars = APP_CONFIG.get("max_extract_chars", 150000)
    max_chars = max(1, int(max_chars))
    filename = _name(file)
    lower_name = filename.lower()
    try:
        if lower_name.endswith(".pdf"):
            if not PYMUPDF_AVAILABLE:
                raise FileParseError("pdf_parser_unavailable")
            pdf_bytes = _read_bytes(file)
            parts = []
            with pymupdf.open(stream=pdf_bytes, filetype="pdf") as pdf_doc:
                for page in pdf_doc:
                    text = page.get_text()
                    if text:
                        parts.append(text)
                    if sum(len(part) for part in parts) >= max_chars:
                        break
            result = "\n".join(parts)
        elif lower_name.endswith((".docx", ".doc")):
            stream = _binary_stream(file)
            doc = Document(stream)
            parts = []
            total = 0
            for para in doc.paragraphs:
                value = str(para.text or "")
                if value:
                    parts.append(value)
                    total += len(value)
                if total >= max_chars:
                    break
            result = "\n".join(parts)
        elif lower_name.endswith((".csv", ".xls", ".xlsx")):
            df = _read_table(file, lower_name)
            if len(df) > 100:
                logging.warning("Data file %s truncated safely to first 100 rows.", filename)
                df = df.head(100)
            result = df.to_markdown(index=False)
        elif lower_name.endswith((".txt", ".md")):
            result = _decode_text_bytes(_read_bytes(file))
        else:
            raise FileParseError("unsupported_file_extension")
    except FileParseError:
        raise
    except Exception as exc:
        raise FileParseError(f"parse_failed:{exc.__class__.__name__}") from exc
    finally:
        if not isinstance(file, (str, os.PathLike, Path)):
            try:
                file.seek(0)
            except Exception:
                pass

    normalized = str(result or "").strip()
    if not normalized:
        raise FileParseError("parsed_content_empty")
    return normalized[:max_chars]


def validate_file_parseable(file, max_chars=4000) -> dict:
    """Return an explicit, UI-safe parseability result for one file."""
    filename = os.path.basename(_name(file))
    try:
        content = extract_file_content(file, max_chars=max_chars)
        return {
            "ok": True,
            "filename": filename,
            "text_chars": len(content),
            "error": "",
        }
    except Exception as exc:
        return {
            "ok": False,
            "filename": filename,
            "text_chars": 0,
            "error": str(exc)[:160],
        }




_STRICT_RESEARCH_TEMPLATE_EXTENSIONS = {
    "qualitative_materials": ".txt",
    "survey_quantitative_data": ".csv",
}


def _validate_qualitative_template_text(text: str) -> None:
    value = str(text or "").strip()
    if not value:
        raise FileParseError("template_content_empty")
    metadata_lines = re.findall(r"(?m)^\s*\[[^\]\n]{2,100}\]\s*[::]\s*\S+", value)
    dialogue_lines = [
        line.strip()
        for line in value.splitlines()
        if line.strip() and not line.lstrip().startswith("[") and re.search(r"[::]", line)
    ]
    if not re.search(r"\bINT-\d+\b", value, flags=re.I):
        raise FileParseError("template_missing_interview_id")
    if len(metadata_lines) < 3 or len(dialogue_lines) < 2:
        raise FileParseError("template_structure_mismatch")
    # The downloaded template contains explicit placeholders.  If any remain,
    # the file is not yet research material and must not enter generation.
    if re.search(r"\[(?:在这里输入|Enter\b|Введите\b|[^\]\n]*осында енгізіңіз)", value, flags=re.I):
        raise FileParseError("template_placeholders_not_replaced")


def _validate_survey_template_text(text: str) -> None:
    value = str(text or "").strip()
    if not value:
        raise FileParseError("template_content_empty")
    lines = [line.strip() for line in value.splitlines() if line.strip()]
    section_markers = [line for line in lines if line.startswith("---") and line.endswith("---")]
    has_descriptive = any(",SD," in line and line.count(",") >= 4 for line in lines)
    has_reliability = any("Cronbach's Alpha" in line and ",CR," in line and ",AVE" in line for line in lines)
    has_hypothesis = any("Beta" in line and ("P-value" in line or "P-значение" in line or "P-мәні" in line) for line in lines)
    if len(section_markers) < 3 or not (has_descriptive and has_reliability and has_hypothesis):
        raise FileParseError("template_structure_mismatch")


def _research_material_size_hint(file) -> int | None:
    if isinstance(file, (str, os.PathLike, Path)):
        try:
            return int(Path(file).stat().st_size)
        except OSError:
            return None
    value = getattr(file, "size", None)
    try:
        if value is not None:
            return int(value)
    except (TypeError, ValueError):
        pass
    try:
        return int(file.getbuffer().nbytes)
    except Exception:
        return None


def research_material_text_fingerprint(text: str) -> str:
    """Stable fingerprint for detecting an unchanged downloadable template."""
    normalized = str(text or "").replace("\r\n", "\n").replace("\r", "\n")
    normalized = "\n".join(line.rstrip() for line in normalized.splitlines()).strip()
    return hashlib.sha256(normalized.encode("utf-8")).hexdigest()


def _strict_research_material_text(file, template_profile: str, *, max_bytes: int | None = None) -> tuple[str, int]:
    profile = str(template_profile or "").strip().casefold()
    expected_ext = _STRICT_RESEARCH_TEMPLATE_EXTENSIONS.get(profile)
    if not expected_ext:
        raise FileParseError("template_profile_unsupported")
    filename = _name(file)
    suffix = Path(filename).suffix.casefold()
    if suffix != expected_ext:
        raise FileParseError(f"template_extension_mismatch:expected={expected_ext}")
    byte_limit = int(max_bytes or 0)
    size_hint = _research_material_size_hint(file)
    if byte_limit > 0 and size_hint is not None and size_hint > byte_limit:
        raise FileParseError(f"research_material_file_too_large:{size_hint}:{byte_limit}")
    try:
        raw = _read_bytes(file)
        if byte_limit > 0 and len(raw) > byte_limit:
            raise FileParseError(f"research_material_file_too_large:{len(raw)}:{byte_limit}")
        value = _decode_text_bytes(raw).strip()
    finally:
        if not isinstance(file, (str, os.PathLike, Path)):
            try:
                file.seek(0)
            except Exception:
                pass
    if profile == "qualitative_materials":
        _validate_qualitative_template_text(value)
    elif profile == "survey_quantitative_data":
        _validate_survey_template_text(value)
    return value, len(raw)


def extract_research_material_content(file, template_profile: str, max_chars=None, *, max_bytes: int | None = None) -> str:
    """Read one strict research-material file under the selected template contract."""
    value, _byte_size = _strict_research_material_text(file, template_profile, max_bytes=max_bytes)
    if max_chars is None:
        max_chars = APP_CONFIG.get("max_extract_chars", 150000)
    max_chars = max(1, int(max_chars))
    return value[:max_chars]


def validate_research_material_template(
    file,
    template_profile: str,
    max_chars=None,
    *,
    max_bytes: int | None = None,
    disallowed_content_fingerprints: set[str] | None = None,
) -> dict:
    """Return an exact, UI-safe strict-template validation result.

    Validation counts the full decoded text before any bundle truncation, so the
    caller can reject oversized bundles before task creation/quota reservation.
    """
    filename = os.path.basename(_name(file))
    try:
        content, byte_size = _strict_research_material_text(file, template_profile, max_bytes=max_bytes)
        fingerprint = research_material_text_fingerprint(content)
        if disallowed_content_fingerprints and fingerprint in disallowed_content_fingerprints:
            raise FileParseError("template_example_not_replaced")
        shown = content if max_chars is None else content[: max(1, int(max_chars))]
        return {
            "ok": True,
            "filename": filename,
            "text_chars": len(content),
            "preview_chars": len(shown),
            "byte_size": byte_size,
            "fingerprint": fingerprint,
            "error": "",
        }
    except Exception as exc:
        return {
            "ok": False,
            "filename": filename,
            "text_chars": 0,
            "preview_chars": 0,
            "byte_size": int(_research_material_size_hint(file) or 0),
            "fingerprint": "",
            "error": str(exc)[:200],
        }

def extract_text_from_files(uploaded_files: Iterable, max_chars=None):
    if max_chars is None:
        max_chars = APP_CONFIG.get("max_extract_chars", 150000)

    extracted = []
    total = 0
    for file in uploaded_files or []:
        filename = _name(file)
        try:
            remaining = max(1, int(max_chars) - total)
            text = extract_file_content(file, max_chars=remaining)
            extracted.append(text)
            total += len(text)
            if total >= int(max_chars):
                logging.warning("File %s caused aggregate text to reach %s characters.", filename, max_chars)
                break
        except Exception as exc:
            # Historical generation calls remain fail-soft. P0-R7-B's UI gate
            # uses validate_file_parseable() before task/quota creation.
            logging.warning("File parse failed for %s: %s", filename, exc)
    return "\n".join(extracted)[: int(max_chars)]


def extract_data_from_files(uploaded_file, max_chars=20000):
    if not uploaded_file:
        return ""
    try:
        # P0-R7-B research bundles are UTF-8 text. Existing CSV/XLS/XLSX and
        # document inputs remain supported through the same parser.
        return extract_file_content(uploaded_file, max_chars=max_chars)
    except Exception as exc:
        logging.warning("Data parse failed for %s: %s", _name(uploaded_file), exc)
        return ""