Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,9 +3,13 @@ import gradio as gr
|
|
| 3 |
import json
|
| 4 |
import os
|
| 5 |
import time
|
|
|
|
| 6 |
from pathlib import Path
|
| 7 |
from huggingface_hub import InferenceClient
|
| 8 |
|
|
|
|
|
|
|
|
|
|
| 9 |
SAVE_DIR = Path("/tmp/saved_docs")
|
| 10 |
SAVE_DIR.mkdir(exist_ok=True)
|
| 11 |
MODEL = "meta-llama/Llama-3.3-70B-Instruct"
|
|
@@ -20,13 +24,12 @@ def extract_pdf_text(path):
|
|
| 20 |
if hasattr(path, "name"):
|
| 21 |
path = path.name
|
| 22 |
try:
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
pages.append(f"--- Page {i+1} ---\n{text.strip()}")
|
| 30 |
if not pages:
|
| 31 |
return "[No extractable text — may be a scanned PDF]"
|
| 32 |
full = "\n\n".join(pages)
|
|
|
|
| 3 |
import json
|
| 4 |
import os
|
| 5 |
import time
|
| 6 |
+
import subprocess
|
| 7 |
from pathlib import Path
|
| 8 |
from huggingface_hub import InferenceClient
|
| 9 |
|
| 10 |
+
subprocess.run(["pip", "install", "pypdf", "-q"], capture_output=True)
|
| 11 |
+
from pypdf import PdfReader
|
| 12 |
+
|
| 13 |
SAVE_DIR = Path("/tmp/saved_docs")
|
| 14 |
SAVE_DIR.mkdir(exist_ok=True)
|
| 15 |
MODEL = "meta-llama/Llama-3.3-70B-Instruct"
|
|
|
|
| 24 |
if hasattr(path, "name"):
|
| 25 |
path = path.name
|
| 26 |
try:
|
| 27 |
+
reader = PdfReader(path)
|
| 28 |
+
pages = []
|
| 29 |
+
for i, page in enumerate(reader.pages):
|
| 30 |
+
text = page.extract_text() or ""
|
| 31 |
+
if text.strip():
|
| 32 |
+
pages.append(f"--- Page {i+1} ---\n{text.strip()}")
|
|
|
|
| 33 |
if not pages:
|
| 34 |
return "[No extractable text — may be a scanned PDF]"
|
| 35 |
full = "\n\n".join(pages)
|