Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -51,66 +51,104 @@ if device.type == 'cuda':
|
|
51 |
import traceback
|
52 |
from concurrent.futures import ThreadPoolExecutor
|
53 |
|
54 |
-
def handle_file(file, prompt, extra_prompt, max_new_tokens, progress=gr.Progress()):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
try:
|
56 |
file_path = file.name if hasattr(file, "name") else file
|
57 |
filename = os.path.basename(file_path)
|
58 |
ext = filename.lower().split('.')[-1]
|
59 |
-
full_prompt = (prompt + "\n" + extra_prompt).strip() or ""
|
60 |
|
61 |
-
|
62 |
|
63 |
if ext == "pdf":
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
pages.append(img)
|
79 |
-
print(f"[INFO] Converted PDF → {len(pages)} pages")
|
80 |
-
|
81 |
-
except Exception as e:
|
82 |
-
traceback.print_exc()
|
83 |
-
return filename, f"[ERROR] PDF conversion failed: {e}"
|
84 |
-
|
85 |
-
outputs = []
|
86 |
-
with ThreadPoolExecutor(max_workers=4) as executor:
|
87 |
-
futures = [executor.submit(run_inference, img, full_prompt, max_new_tokens) for img in pages]
|
88 |
-
for idx, future in enumerate(futures):
|
89 |
-
try:
|
90 |
-
out = future.result()
|
91 |
-
except Exception as e:
|
92 |
-
traceback.print_exc()
|
93 |
-
out = f"[ERROR] Inference page {idx+1} failed: {e}"
|
94 |
-
outputs.append(out)
|
95 |
-
progress((idx) / len(pages), desc=f"Page {idx+1}/{len(pages)}")
|
96 |
-
|
97 |
-
result = "\n\n--- Page Break ---\n\n".join(outputs)
|
98 |
-
print("[INFO] handle_file done")
|
99 |
-
return filename, result
|
100 |
|
101 |
else:
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
except Exception as e:
|
106 |
-
traceback.print_exc()
|
107 |
-
return filename, f"[ERROR] Image open failed: {e}"
|
108 |
|
109 |
-
|
|
|
|
|
110 |
|
111 |
except Exception as e:
|
112 |
-
|
113 |
-
return "error", f"[ERROR] handle_file unexpected: {e}"
|
114 |
|
115 |
# def run_inference(img: Image.Image, prompt: str = "", max_new_tokens: int = 512) -> str:
|
116 |
# if img.mode != "RGB":
|
|
|
51 |
import traceback
|
52 |
from concurrent.futures import ThreadPoolExecutor
|
53 |
|
54 |
+
# def handle_file(file, prompt, extra_prompt, max_new_tokens, progress=gr.Progress()):
|
55 |
+
# try:
|
56 |
+
# file_path = file.name if hasattr(file, "name") else file
|
57 |
+
# filename = os.path.basename(file_path)
|
58 |
+
# ext = filename.lower().split('.')[-1]
|
59 |
+
# full_prompt = (prompt + "\n" + extra_prompt).strip() or ""
|
60 |
+
|
61 |
+
# print(f"[INFO] handle_file → {filename} (.{ext})")
|
62 |
+
|
63 |
+
# if ext == "pdf":
|
64 |
+
# try:
|
65 |
+
# with open(file_path, "rb") as f:
|
66 |
+
# pdf_bytes = f.read()
|
67 |
+
# print(f"[INFO] Read PDF bytes: {len(pdf_bytes)} bytes")
|
68 |
+
|
69 |
+
# doc = fitz.open(stream=pdf_bytes, filetype="pdf")
|
70 |
+
# pages = []
|
71 |
+
# zoom = DPI
|
72 |
+
# mat = fitz.Matrix(zoom, zoom)
|
73 |
+
# for i, page in enumerate(doc):
|
74 |
+
# pix = page.get_pixmap(matrix=mat, colorspace=fitz.csRGB)
|
75 |
+
# img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
|
76 |
+
# if max(img.size) > 3072:
|
77 |
+
# img.thumbnail((3072, 3072), Image.Resampling.LANCZOS)
|
78 |
+
# pages.append(img)
|
79 |
+
# print(f"[INFO] Converted PDF → {len(pages)} pages")
|
80 |
+
|
81 |
+
# except Exception as e:
|
82 |
+
# traceback.print_exc()
|
83 |
+
# return filename, f"[ERROR] PDF conversion failed: {e}"
|
84 |
+
|
85 |
+
# outputs = []
|
86 |
+
# with ThreadPoolExecutor(max_workers=4) as executor:
|
87 |
+
# futures = [executor.submit(run_inference, img, full_prompt, max_new_tokens) for img in pages]
|
88 |
+
# for idx, future in enumerate(futures):
|
89 |
+
# try:
|
90 |
+
# out = future.result()
|
91 |
+
# except Exception as e:
|
92 |
+
# traceback.print_exc()
|
93 |
+
# out = f"[ERROR] Inference page {idx+1} failed: {e}"
|
94 |
+
# outputs.append(out)
|
95 |
+
# progress((idx) / len(pages), desc=f"Page {idx+1}/{len(pages)}")
|
96 |
+
|
97 |
+
# result = "\n\n--- Page Break ---\n\n".join(outputs)
|
98 |
+
# print("[INFO] handle_file done")
|
99 |
+
# return filename, result
|
100 |
+
|
101 |
+
# else:
|
102 |
+
# try:
|
103 |
+
# img = Image.open(file_path)
|
104 |
+
# print(f"[INFO] Opened image: {img.mode}, {img.size}")
|
105 |
+
# except Exception as e:
|
106 |
+
# traceback.print_exc()
|
107 |
+
# return filename, f"[ERROR] Image open failed: {e}"
|
108 |
+
|
109 |
+
# return filename, run_inference(img, full_prompt, max_new_tokens)
|
110 |
+
|
111 |
+
# except Exception as e:
|
112 |
+
# traceback.print_exc()
|
113 |
+
# return "error", f"[ERROR] handle_file unexpected: {e}"
|
114 |
+
|
115 |
+
import time
|
116 |
+
|
117 |
+
def handle_file(file, prompt, extra_prompt, max_new_tokens, progress=None):
|
118 |
try:
|
119 |
file_path = file.name if hasattr(file, "name") else file
|
120 |
filename = os.path.basename(file_path)
|
121 |
ext = filename.lower().split('.')[-1]
|
|
|
122 |
|
123 |
+
start = time.perf_counter()
|
124 |
|
125 |
if ext == "pdf":
|
126 |
+
with open(file_path, "rb") as f:
|
127 |
+
pdf_bytes = f.read()
|
128 |
+
|
129 |
+
doc = fitz.open(stream=pdf_bytes, filetype="pdf")
|
130 |
+
scale = DPI / 72
|
131 |
+
mat = fitz.Matrix(scale, scale)
|
132 |
+
pages = []
|
133 |
+
|
134 |
+
for page in doc:
|
135 |
+
pix = page.get_pixmap(matrix=mat, colorspace=fitz.csRGB)
|
136 |
+
img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
|
137 |
+
if max(img.size) > 3072:
|
138 |
+
img.thumbnail((3072, 3072), Image.Resampling.LANCZOS)
|
139 |
+
pages.append(img)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
140 |
|
141 |
else:
|
142 |
+
img = Image.open(file_path)
|
143 |
+
if img.mode != "RGB":
|
144 |
+
img = img.convert("RGB")
|
|
|
|
|
|
|
145 |
|
146 |
+
end = time.perf_counter()
|
147 |
+
elapsed = end - start
|
148 |
+
return filename, f"OKE (time: {elapsed:.3f}s)"
|
149 |
|
150 |
except Exception as e:
|
151 |
+
return "error", f"[ERROR] handle_file test failed: {e}"
|
|
|
152 |
|
153 |
# def run_inference(img: Image.Image, prompt: str = "", max_new_tokens: int = 512) -> str:
|
154 |
# if img.mode != "RGB":
|