File size: 24,604 Bytes
8c3e275
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
from __future__ import annotations

import hashlib
import json
import shutil
import subprocess
import tempfile
import xml.etree.ElementTree as ET
import zipfile
from concurrent.futures import ThreadPoolExecutor, as_completed
from pathlib import Path

import cv2
import numpy as np

from pageparse.config import settings
from pageparse.ingest import load_image
from pageparse.ocr.printed import PrintedOCR
import pageparse.preprocess as pp_module
from pageparse.preprocess import preprocess, adaptive_preprocess


def _get_uploads_dir() -> Path:
    here = Path(__file__).resolve().parent.parent.parent
    uploads_dir = here / "web" / "static" / "uploads"
    if not (here / "web" / "static").exists():
        uploads_dir = Path.cwd() / "web" / "static" / "uploads"
    return uploads_dir


def _try_handwriting_ocr(image):
    try:
        from pageparse.ocr.handwriting import HandwritingOCR
        ocr = HandwritingOCR()
        return ocr.recognize(image)
    except Exception:
        return ""


def _ocr_image(img: np.ndarray, printed_ocr: PrintedOCR) -> str:
    try:
        return printed_ocr.recognize(img).strip()
    except Exception:
        return ""


def _ollama_vision_ocr(path: Path) -> str:
    import base64
    import urllib.error
    import urllib.request
    import os

    try:
        if os.environ.get("GEMINI_API_KEY"):
            b64 = base64.b64encode(path.read_bytes()).decode("utf-8")
            gemini_resp = _gemini_generate("This is a slice of handwritten text. Please transcribe the handwriting in this image word-for-word exactly without adding any comments.", b64)
            if gemini_resp:
                return gemini_resp
        img = cv2.imread(str(path))
        if img is None:
            return ""

        h, w, c = img.shape
        if h > w and h >= 300:
            slice_h = h // 3
            texts = []
            for i in range(3):
                start = i * slice_h
                end = (i + 1) * slice_h if i < 2 else h
                sliced = img[start:end, :]

                _, buffer = cv2.imencode(".png", sliced)
                b64 = base64.b64encode(buffer).decode("utf-8")

                payload = {
                    "model": settings.vision_model,
                    "prompt": "This is a slice of handwritten text. Please transcribe the handwriting in this image word-for-word exactly without adding any comments.",
                    "images": [b64],
                    "stream": False,
                    "options": {"temperature": 0.0},
                }
                url = f"{settings.ollama_url}/api/generate"
                data = json.dumps(payload).encode("utf-8")
                req = urllib.request.Request(url, data=data, headers={"Content-Type": "application/json"})
                with urllib.request.urlopen(req, timeout=120) as response:
                    res = json.loads(response.read().decode("utf-8"))
                    text = res.get("response", "").strip()

                is_desc = any(phrase in text.lower() for phrase in ["image shows", "shows a page", "written in", "notebook or journal"])
                if not text or is_desc:
                    payload["prompt"] = "Transcribe the text in this image word-for-word. Output only the text."
                    data = json.dumps(payload).encode("utf-8")
                    req_fb = urllib.request.Request(url, data=data, headers={"Content-Type": "application/json"})
                    with urllib.request.urlopen(req_fb, timeout=120) as response_fb:
                        res_fb = json.loads(response_fb.read().decode("utf-8"))
                        text = res_fb.get("response", "").strip()

                if text:
                    if text.startswith('"') and text.endswith('"'):
                        text = text[1:-1].strip()
                    texts.append(text)

            return "\n\n".join(texts)
        else:
            b64 = base64.b64encode(path.read_bytes()).decode("utf-8")
            payload = {
                "model": settings.vision_model,
                "prompt": "This is a slice of handwritten text. Please transcribe the handwriting in this image word-for-word exactly without adding any comments.",
                "images": [b64],
                "stream": False,
                "options": {"temperature": 0.0},
            }
            url = f"{settings.ollama_url}/api/generate"
            data = json.dumps(payload).encode("utf-8")
            req = urllib.request.Request(url, data=data, headers={"Content-Type": "application/json"})
            with urllib.request.urlopen(req, timeout=120) as response:
                res = json.loads(response.read().decode("utf-8"))
                text = res.get("response", "").strip()

            is_desc = any(phrase in text.lower() for phrase in ["image shows", "shows a page", "written in", "notebook or journal"])
            if not text or is_desc:
                payload["prompt"] = "Transcribe the text in this image word-for-word. Output only the text."
                data = json.dumps(payload).encode("utf-8")
                req_fb = urllib.request.Request(url, data=data, headers={"Content-Type": "application/json"})
                with urllib.request.urlopen(req_fb, timeout=120) as response_fb:
                    res_fb = json.loads(response_fb.read().decode("utf-8"))
                    text = res_fb.get("response", "").strip()

            if text.startswith('"') and text.endswith('"'):
                text = text[1:-1].strip()
            return text
    except urllib.error.HTTPError as e:
        body = e.read().decode("utf-8", errors="replace")
        if "does not support image input" not in body:
            print(f"Ollama vision HTTP error: {body}")
    except Exception as e:
        print(f"Ollama vision failed: {e}")
    return ""


def _gemini_generate(prompt: str, base64_image: str = None, image_mime: str = "image/png") -> str:
    import os
    import json
    import urllib.request
    import urllib.error

    api_key = os.environ.get("GEMINI_API_KEY")
    if not api_key:
        return ""

    url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key={api_key}"
    parts = [{"text": prompt}]
    if base64_image:
        parts.append({
            "inlineData": {
                "mimeType": image_mime,
                "data": base64_image
            }
        })

    payload = {
        "contents": [{"parts": parts}]
    }

    try:
        data = json.dumps(payload).encode("utf-8")
        req = urllib.request.Request(url, data=data, headers={"Content-Type": "application/json"})
        with urllib.request.urlopen(req, timeout=30) as response:
            res = json.loads(response.read().decode("utf-8"))
            return res["candidates"][0]["content"]["parts"][0]["text"].strip()
    except Exception as e:
        print(f"Gemini API call failed: {e}")
        return ""


def _llm_complete_local(prompt: str, system: str = "", temperature: float = 0.3, max_tokens: int = 512) -> str:
    import urllib.request
    import json

    gemini_resp = _gemini_generate(f"{system}\n\n{prompt}" if system else prompt)
    if gemini_resp:
        return gemini_resp

    payload = {
        "model": settings.slm_model,
        "prompt": f"{system}\n\n{prompt}" if system else prompt,
        "stream": False,
        "options": {"temperature": temperature, "num_predict": max_tokens},
    }
    url = f"{settings.ollama_url}/api/generate"
    try:
        data = json.dumps(payload).encode("utf-8")
        req = urllib.request.Request(url, data=data, headers={"Content-Type": "application/json"})
        with urllib.request.urlopen(req, timeout=90) as response:
            res = json.loads(response.read().decode("utf-8"))
            return res.get("response", "").strip()
    except Exception as e:
        print(f"Local LLM complete failed: {e}")
        return ""


def _ollama_vision_explain(path: Path) -> str:
    import base64
    import urllib.request
    import json

    try:
        b64 = base64.b64encode(path.read_bytes()).decode("utf-8")
        gemini_resp = _gemini_generate("Explain this image in very detail. Describe the layout, objects, text, colors, shapes, and overall context. Be as thorough as possible.", b64)
        if gemini_resp:
            return gemini_resp
    except Exception:
        pass

    model = settings.vision_model
    try:
        req = urllib.request.Request(f"{settings.ollama_url}/api/tags")
        with urllib.request.urlopen(req, timeout=5) as response:
            data = json.loads(response.read().decode("utf-8"))
            available_models = [m["name"] for m in data.get("models", [])]
            if model not in available_models:
                if f"{model}:latest" in available_models:
                    model = f"{model}:latest"
                elif "moondream:latest" in available_models:
                    model = "moondream:latest"
                elif "llava:7b" in available_models:
                    model = "llava:7b"
    except Exception:
        pass

    try:
        b64 = base64.b64encode(path.read_bytes()).decode("utf-8")
        payload = {
            "model": model,
            "prompt": "Explain this image in very detail. Describe the layout, objects, text, colors, shapes, and overall context. Be as thorough as possible.",
            "images": [b64],
            "stream": False,
            "options": {"temperature": 0.2},
        }
        url = f"{settings.ollama_url}/api/generate"
        data = json.dumps(payload).encode("utf-8")
        req = urllib.request.Request(url, data=data, headers={"Content-Type": "application/json"})
        with urllib.request.urlopen(req, timeout=120) as response:
            res = json.loads(response.read().decode("utf-8"))
            return res.get("response", "").strip()
    except Exception as e:
        print(f"Ollama vision explain failed: {e}")
        return ""


def _explain_audio_transcript(transcript: str, filename: str) -> str:
    if not transcript or transcript == "[inaudible]":
        return f"This is an audio file named '{filename}'. No clear speech could be transcribed, so a detailed audio explanation is not available."

    prompt = (
        "You are an expert audio analyst. Analyze the following transcript of an audio file and provide a very detailed explanation. "
        "Summarize the main topics discussed, analyze the speaker's likely intent or tone, point out key terms, and outline any action items.\n\n"
        f"Transcript:\n{transcript}\n\n"
        "Detailed Explanation:"
    )
    return _llm_complete_local(prompt, system="You are a detailed audio analysis assistant.", max_tokens=512)


def _explain_video_content(ocr_text: str, audio_transcript: str, filename: str) -> str:
    if (not ocr_text or ocr_text == "[UNCLEAR]") and (not audio_transcript or audio_transcript == "[inaudible]"):
        return f"This is a video file named '{filename}'. No speech or visual text could be extracted, so a detailed explanation is not available."

    prompt = (
        "You are an expert video analyst. Analyze the extracted visual text (OCR from video frames) "
        "and the speech transcript of a video file, and provide a very detailed explanation. "
        "Describe the main themes, key information shown on screen, topics discussed in the audio, "
        "and synthesize the visual and verbal content into a coherent description of what the video is about.\n\n"
        f"Extracted Visual Text:\n{ocr_text}\n\n"
        f"Speech Transcript:\n{audio_transcript}\n\n"
        "Detailed Explanation:"
    )
    return _llm_complete_local(prompt, system="You are a detailed video analysis assistant.", max_tokens=512)


def process_image(path: Path, language: str = "English") -> tuple[str, str, str, list[dict], list[list[str]]]:
    uploads_dir = _get_uploads_dir()
    uploads_dir.mkdir(exist_ok=True, parents=True)

    timestamp = int(Path(path).stat().st_mtime)
    orig_name = f"original_{timestamp}_{path.name}"
    clean_name = f"cleaned_{timestamp}_{path.name}"

    orig_dest = uploads_dir / orig_name
    clean_dest = uploads_dir / clean_name

    if path.resolve() != orig_dest.resolve():
        shutil.copy(str(path), str(orig_dest))

    img = load_image(path)
    printed_ocr = PrintedOCR()
    barcodes = []
    tables = []

    try:
        barcodes = printed_ocr.detect_barcodes(img)
    except Exception:
        pass

    try:
        tables = printed_ocr.detect_tables(img)
    except Exception:
        pass

    explanation = _ollama_vision_explain(path)

    raw_text = _ollama_vision_ocr(path)
    if raw_text:
        cleaned = preprocess(img)
        cv2.imwrite(str(clean_dest), cleaned)
    else:
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) if img.ndim == 3 else img

        strategies = [
            ("adaptive + denoise", pp_module.preprocess),
            ("adaptive (auto-tuned)", lambda x: adaptive_preprocess(x)[0]),
            ("otsu", pp_module.preprocess_otsu),
            ("clahe + otsu", pp_module.preprocess_light),
            ("grayscale", lambda x: gray.copy()),
            ("grayscale 2x", lambda x: pp_module.preprocess_with_scale(img, 2.0)),
            ("grayscale 3x", lambda x: pp_module.preprocess_with_scale(img, 3.0)),
        ]

        results = []
        for name, fn in strategies:
            try:
                cleaned = fn(img)
                text = _ocr_image(cleaned, printed_ocr)
                if text:
                    results.append((len(text), text, cleaned))
            except Exception:
                pass

        best_cleaned = None
        if results:
            results.sort(key=lambda x: x[0], reverse=True)
            _, raw_text, best_cleaned = results[0]
        else:
            raw_text = ""

        if best_cleaned is not None:
            cv2.imwrite(str(clean_dest), best_cleaned)
        else:
            cleaned_default = pp_module.preprocess(img)
            cv2.imwrite(str(clean_dest), cleaned_default)

        if not raw_text.strip():
            raw_text = _try_handwriting_ocr(best_cleaned or pp_module.preprocess(img))

        if not raw_text.strip():
            raw_text = "[UNCLEAR]"

    combined_parts = []
    if explanation:
        combined_parts.append("=========================\nIMAGE ANALYSIS & EXPLANATION\n=========================\n" + explanation)
    if raw_text and raw_text != "[UNCLEAR]":
        combined_parts.append("=========================\nEXTRACTED TEXT\n=========================\n" + raw_text)

    combined_text = "\n\n".join(combined_parts) if combined_parts else "[UNCLEAR]"
    return (combined_text, f"/static/uploads/{orig_name}", f"/static/uploads/{clean_name}", barcodes, tables)


def process_audio(path: Path, language: str = "English") -> tuple[str, str]:
    uploads_dir = _get_uploads_dir()
    uploads_dir.mkdir(exist_ok=True, parents=True)

    timestamp = int(Path(path).stat().st_mtime)
    dest_name = f"audio_{timestamp}_{path.name}"
    dest_path = uploads_dir / dest_name

    if path.resolve() != dest_path.resolve():
        shutil.copy(str(path), str(dest_path))

    transcript = ""
    try:
        transcript = _transcribe_whisper(path)
    except Exception as e:
        print(f"Whisper transcription failed, falling back to Sphinx: {e}")
        try:
            transcript = _transcribe_sphinx(path)
        except Exception as e2:
            print(f"Sphinx also failed: {e2}")

    if not transcript:
        transcript = "[inaudible]"

    explanation = _explain_audio_transcript(transcript, path.name)
    combined_parts = []
    if explanation:
        combined_parts.append("=========================\nAUDIO ANALYSIS & EXPLANATION\n=========================\n" + explanation)
    if transcript and transcript != "[inaudible]":
        combined_parts.append("=========================\nSPEECH TRANSCRIPT\n=========================\n" + transcript)

    combined_text = "\n\n".join(combined_parts) if combined_parts else "[inaudible]"
    return combined_text, f"/static/uploads/{dest_name}"


def process_video(path: Path, language: str = "English") -> tuple[str, str]:
    uploads_dir = _get_uploads_dir()
    uploads_dir.mkdir(exist_ok=True, parents=True)

    timestamp = int(Path(path).stat().st_mtime)
    dest_name = f"video_{timestamp}_{path.name}"
    dest_path = uploads_dir / dest_name

    if path.resolve() != dest_path.resolve():
        shutil.copy(str(path), str(dest_path))

    transcript = ""
    try:
        transcript = _transcribe_whisper(path)
    except Exception as e:
        print(f"Whisper transcription failed, falling back to Sphinx: {e}")
        try:
            transcript = _transcribe_sphinx(path)
        except Exception as e2:
            print(f"Sphinx also failed: {e2}")

    if not transcript:
        transcript = "[inaudible]"

    ocr_texts = []
    try:
        cap = cv2.VideoCapture(str(path))
        fps = cap.get(cv2.CAP_PROP_FPS) or 30.0
        frame_interval = int(fps * 5)
        frame_count = 0
        success = True
        printed_ocr = PrintedOCR()
        while success:
            success, frame = cap.read()
            if not success:
                break
            if frame_count % frame_interval == 0:
                try:
                    text = printed_ocr.recognize(frame)
                    if text.strip() and text.strip() not in ocr_texts:
                        ocr_texts.append(text.strip())
                except Exception:
                    pass
            frame_count += 1
        cap.release()
    except Exception:
        pass

    ocr_text = "\n".join(ocr_texts) if ocr_texts else "[UNCLEAR]"

    explanation = _explain_video_content(ocr_text, transcript, path.name)

    combined_parts = []
    if explanation:
        combined_parts.append("=========================\nVIDEO ANALYSIS & EXPLANATION\n=========================\n" + explanation)
    if ocr_texts:
        combined_parts.append("=========================\nEXTRACTED VISUAL TEXT\n=========================\n" + ocr_text)
    if transcript and transcript != "[inaudible]":
        combined_parts.append("=========================\nSPEECH TRANSCRIPT\n=========================\n" + transcript)

    combined_text = "\n\n".join(combined_parts) if combined_parts else "[UNCLEAR]"
    return combined_text, f"/static/uploads/{dest_name}"


def process_document(path: Path, language: str = "English") -> tuple[str, str]:
    uploads_dir = _get_uploads_dir()
    uploads_dir.mkdir(exist_ok=True, parents=True)

    timestamp = int(Path(path).stat().st_mtime)
    dest_name = f"doc_{timestamp}_{path.name}"
    dest_path = uploads_dir / dest_name

    if path.resolve() != dest_path.resolve():
        shutil.copy(str(path), str(dest_path))

    ext = path.suffix.lower()
    text = ""

    if ext == ".pdf":
        try:
            import pypdfium2 as pdfium
            pdf = pdfium.PdfDocument(str(path))
            text_list = []
            for page in pdf:
                textpage = page.get_textpage()
                t = textpage.get_text_range()
                if t:
                    text_list.append(t)
            text = "\n".join(text_list).strip()
        except Exception:
            pass

        if not text.strip():
            try:
                img = load_image(path)
                cleaned = preprocess(img)
                temp_dir = Path(tempfile.gettempdir())
                temp_img_path = temp_dir / f"{path.stem}_page1.jpg"
                cv2.imwrite(str(temp_img_path), cleaned)
                printed = PrintedOCR()
                text = printed.recognize(cleaned)
                if not text.strip():
                    text = _try_handwriting_ocr(cleaned)
                if temp_img_path.exists():
                    temp_img_path.unlink()
            except Exception:
                pass

        if not text.strip():
            text = "[UNCLEAR]"

    elif ext == ".docx":
        try:
            with zipfile.ZipFile(str(path)) as docx:
                xml_content = docx.read("word/document.xml")
                root = ET.fromstring(xml_content)
                namespaces = {"w": "http://schemas.openxmlformats.org/wordprocessingml/2006/main"}
                texts = [node.text for node in root.findall(".//w:t", namespaces) if node.text]
                text = " ".join(texts)
        except Exception:
            pass
    else:
        try:
            text = path.read_text(encoding="utf-8")
        except Exception:
            try:
                text = path.read_text(encoding="latin1")
            except Exception:
                pass

    if not text.strip():
        text = "[UNCLEAR]"

    return text, f"/static/uploads/{dest_name}"


def process_spreadsheet(path: Path, language: str = "English") -> tuple[str, str]:
    import csv

    uploads_dir = _get_uploads_dir()
    uploads_dir.mkdir(exist_ok=True, parents=True)

    timestamp = int(Path(path).stat().st_mtime)
    dest_name = f"sheet_{timestamp}_{path.name}"
    dest_path = uploads_dir / dest_name

    if path.resolve() != dest_path.resolve():
        shutil.copy(str(path), str(dest_path))

    text = ""
    ext = path.suffix.lower()

    if ext == ".csv":
        try:
            with open(path, encoding="utf-8") as f:
                reader = csv.reader(f)
                rows = list(reader)
                if rows:
                    text_lines = []
                    for r in rows:
                        text_lines.append(" | ".join(r))
                    text = "CSV Spreadsheet Data:\n" + "\n".join(text_lines)
        except Exception:
            pass
    elif ext in (".xlsx", ".xls"):
        try:
            import pandas as pd
            df = pd.read_excel(str(path), engine="openpyxl" if ext == ".xlsx" else "xlrd")
            text = "Spreadsheet Data:\n" + df.to_string(index=False)
        except Exception as e:
            print(f"Spreadsheet read failed: {e}")

    if not text.strip():
        text = "[UNCLEAR]"

    return text, f"/static/uploads/{dest_name}"


def process_batch(paths: list[Path], language: str = "English", schema_type: str = "todo") -> list[dict]:
    results = []
    with ThreadPoolExecutor(max_workers=settings.max_workers) as executor:
        future_map = {}
        for path in paths:
            ext = path.suffix.lower()
            image_exts = {".jpg", ".jpeg", ".png", ".bmp", ".tiff", ".tif", ".webp"}
            if ext in image_exts:
                future = executor.submit(process_image, path, language)
                future_map[future] = ("image", path)
            elif ext in {".mp3", ".wav", ".m4a", ".ogg", ".flac"}:
                future = executor.submit(process_audio, path, language)
                future_map[future] = ("audio", path)
            elif ext in {".mp4", ".mkv", ".mov", ".avi"}:
                future = executor.submit(process_video, path, language)
                future_map[future] = ("video", path)
            elif ext in {".pdf", ".docx", ".txt"}:
                future = executor.submit(process_document, path, language)
                future_map[future] = ("document", path)
            elif ext in {".csv", ".xlsx", ".xls"}:
                future = executor.submit(process_spreadsheet, path, language)
                future_map[future] = ("spreadsheet", path)

        for future in as_completed(future_map):
            source_type, path = future_map[future]
            try:
                if source_type == "image":
                    raw_text, image_url, cleaned_url, barcodes, tables = future.result()
                    results.append({
                        "path": str(path),
                        "source_type": source_type,
                        "raw_text": raw_text,
                        "image_url": image_url,
                        "cleaned_url": cleaned_url,
                        "barcodes": barcodes,
                        "tables": tables,
                    })
                else:
                    raw_text, file_url = future.result()
                    results.append({
                        "path": str(path),
                        "source_type": source_type,
                        "raw_text": raw_text,
                        "file_url": file_url,
                    })
            except Exception as e:
                results.append({
                    "path": str(path),
                    "source_type": source_type,
                    "error": str(e),
                })

    return results