Spaces:
Sleeping
Sleeping
Update backend/main.py
Browse files- backend/main.py +19 -5
backend/main.py
CHANGED
|
@@ -6,6 +6,8 @@ from pathlib import Path
|
|
| 6 |
from typing import List
|
| 7 |
|
| 8 |
import fitz
|
|
|
|
|
|
|
| 9 |
from groq import Groq
|
| 10 |
from fastapi import FastAPI, File, UploadFile, HTTPException
|
| 11 |
from fastapi.middleware.cors import CORSMiddleware
|
|
@@ -87,10 +89,22 @@ Return ONLY valid JSON (no markdown fences, no extra text) using this schema:
|
|
| 87 |
def extract_text_from_pdf(pdf_path: str) -> str:
|
| 88 |
text = ""
|
| 89 |
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
-
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
if not text.strip():
|
| 96 |
raise ValueError("PDF text extraction failed.")
|
|
@@ -98,7 +112,7 @@ def extract_text_from_pdf(pdf_path: str) -> str:
|
|
| 98 |
if len(text.strip()) < 500:
|
| 99 |
text = text[:2000]
|
| 100 |
|
| 101 |
-
# Try to isolate Methods section
|
| 102 |
methods_match = re.search(
|
| 103 |
r'(?:^\s*(?:\d+\.?\d*\.?\s+)?'
|
| 104 |
r'(?:materials?\s+and\s+methods?|experimental\s+procedures?'
|
|
@@ -113,7 +127,7 @@ def extract_text_from_pdf(pdf_path: str) -> str:
|
|
| 113 |
text = methods_match.group(2)
|
| 114 |
else:
|
| 115 |
chars = len(text)
|
| 116 |
-
text
|
| 117 |
|
| 118 |
words = text.split()
|
| 119 |
if len(words) > 1500:
|
|
|
|
| 6 |
from typing import List
|
| 7 |
|
| 8 |
import fitz
|
| 9 |
+
import pytesseract
|
| 10 |
+
from pdf2image import convert_from_path
|
| 11 |
from groq import Groq
|
| 12 |
from fastapi import FastAPI, File, UploadFile, HTTPException
|
| 13 |
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
| 89 |
def extract_text_from_pdf(pdf_path: str) -> str:
|
| 90 |
text = ""
|
| 91 |
|
| 92 |
+
# ---------- Try PyMuPDF extraction ----------
|
| 93 |
+
try:
|
| 94 |
+
doc = fitz.open(pdf_path)
|
| 95 |
+
for page in doc:
|
| 96 |
+
text += page.get_text()
|
| 97 |
+
except Exception:
|
| 98 |
+
pass
|
| 99 |
|
| 100 |
+
# ---------- OCR fallback if no text ----------
|
| 101 |
+
if not text.strip():
|
| 102 |
+
try:
|
| 103 |
+
images = convert_from_path(pdf_path)
|
| 104 |
+
for img in images:
|
| 105 |
+
text += pytesseract.image_to_string(img)
|
| 106 |
+
except Exception:
|
| 107 |
+
raise ValueError("PDF text extraction failed.")
|
| 108 |
|
| 109 |
if not text.strip():
|
| 110 |
raise ValueError("PDF text extraction failed.")
|
|
|
|
| 112 |
if len(text.strip()) < 500:
|
| 113 |
text = text[:2000]
|
| 114 |
|
| 115 |
+
# ---------- Try to isolate Methods section ----------
|
| 116 |
methods_match = re.search(
|
| 117 |
r'(?:^\s*(?:\d+\.?\d*\.?\s+)?'
|
| 118 |
r'(?:materials?\s+and\s+methods?|experimental\s+procedures?'
|
|
|
|
| 127 |
text = methods_match.group(2)
|
| 128 |
else:
|
| 129 |
chars = len(text)
|
| 130 |
+
text = text[chars // 10: chars // 10 + 15000]
|
| 131 |
|
| 132 |
words = text.split()
|
| 133 |
if len(words) > 1500:
|