Update app.py
Browse files
app.py
CHANGED
@@ -1,366 +1,427 @@
|
|
1 |
-
import base64
|
2 |
-
import io
|
3 |
-
import json
|
4 |
-
import os
|
5 |
-
import re
|
6 |
-
import tempfile
|
7 |
-
import zipfile
|
8 |
-
from datetime import datetime
|
9 |
-
from pathlib import Path
|
10 |
-
|
11 |
-
import gradio as gr
|
12 |
-
import requests
|
13 |
-
from PIL import Image
|
14 |
-
import pdf2image
|
15 |
-
|
16 |
-
API_URL = "https://t707h6d9q6oftbx3.aistudio-app.com/layout-parsing"
|
17 |
-
TOKEN = os.getenv("API_TOKEN", "c9e4aaf9634724e215690ba66a66dbdbdf3222a2")
|
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 |
-
button {
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import base64
|
2 |
+
import io
|
3 |
+
import json
|
4 |
+
import os
|
5 |
+
import re
|
6 |
+
import tempfile
|
7 |
+
import zipfile
|
8 |
+
from datetime import datetime
|
9 |
+
from pathlib import Path
|
10 |
+
|
11 |
+
import gradio as gr
|
12 |
+
import requests
|
13 |
+
from PIL import Image
|
14 |
+
import pdf2image
|
15 |
+
|
16 |
+
API_URL = "https://t707h6d9q6oftbx3.aistudio-app.com/layout-parsing"
|
17 |
+
TOKEN = os.getenv("API_TOKEN", "c9e4aaf9634724e215690ba66a66dbdbdf3222a2")
|
18 |
+
|
19 |
+
|
20 |
+
CSS = """
|
21 |
+
:root {
|
22 |
+
--sand-color: #D7B4F8;
|
23 |
+
--white: #ffffff;
|
24 |
+
--shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
25 |
+
--text-color: #F5ECFD;
|
26 |
+
--black:#000000;
|
27 |
+
--link-hover: #F5ECFD;
|
28 |
+
--content-width: 1200px;
|
29 |
+
--button-color: #cbbdff; /* 新增按钮颜色变量 */
|
30 |
+
}
|
31 |
+
|
32 |
+
body {
|
33 |
+
display: flex;
|
34 |
+
justify-content: center;
|
35 |
+
background-color: var(--sand-color);
|
36 |
+
color: var(--black);
|
37 |
+
font-family: Arial, sans-serif;
|
38 |
+
}
|
39 |
+
|
40 |
+
.gradio-container {
|
41 |
+
max-width: var(--content-width) !important;
|
42 |
+
width: 100% !important;
|
43 |
+
margin: 20px auto;
|
44 |
+
padding: 20px;
|
45 |
+
background-color: var(--white) !important;
|
46 |
+
}
|
47 |
+
|
48 |
+
/* 修改1: 优化header容器样式 */
|
49 |
+
.header-container {
|
50 |
+
width: 100%;
|
51 |
+
background-color: var(--text-color) !important;
|
52 |
+
padding: 20px 0 10px 0; /* 减少底部padding */
|
53 |
+
margin-bottom: 20px;
|
54 |
+
border-radius: 8px;
|
55 |
+
}
|
56 |
+
|
57 |
+
.logo-container {
|
58 |
+
width: 100%;
|
59 |
+
margin-bottom: 15px; /* 减少logo与按钮间距 */
|
60 |
+
text-align: center;
|
61 |
+
}
|
62 |
+
|
63 |
+
.logo-img {
|
64 |
+
width: 100%;
|
65 |
+
max-width: var(--content-width);
|
66 |
+
margin: 0 auto;
|
67 |
+
display: block;
|
68 |
+
}
|
69 |
+
|
70 |
+
/* 修改2: 优化导航按钮布局 */
|
71 |
+
.nav-buttons {
|
72 |
+
display: flex;
|
73 |
+
justify-content: center;
|
74 |
+
gap: 20px; /* 减少按钮间距 */
|
75 |
+
margin-top: 10px; /* 减少顶部间距 */
|
76 |
+
width: 80%; /* 控制按钮区域宽度 */
|
77 |
+
margin-left: auto;
|
78 |
+
margin-right: auto;
|
79 |
+
}
|
80 |
+
|
81 |
+
.nav-button {
|
82 |
+
background-color: var(--button-color) !important; /* 修改为FAF9F6 */
|
83 |
+
color: var(--black) !important;
|
84 |
+
text-decoration: none;
|
85 |
+
font-weight: bold;
|
86 |
+
font-size: 20px;
|
87 |
+
padding: 6px 20px !important;
|
88 |
+
border-radius: 24px !important;
|
89 |
+
border: none !important;
|
90 |
+
transition: opacity 0.2s;
|
91 |
+
flex: 1;
|
92 |
+
max-width: 200px;
|
93 |
+
text-align: center;
|
94 |
+
}
|
95 |
+
|
96 |
+
.nav-button:hover {
|
97 |
+
opacity: 0.8 !important;
|
98 |
+
}
|
99 |
+
|
100 |
+
.upload-section {
|
101 |
+
width: 100%;
|
102 |
+
margin: 0 auto 30px;
|
103 |
+
padding: 20px;
|
104 |
+
background-color: var(--text-color) !important;
|
105 |
+
border-radius: 8px;
|
106 |
+
box-shadow: var(--shadow);
|
107 |
+
}
|
108 |
+
/* 强制底部链接为白色背景 */
|
109 |
+
#component-16, /* Use via API 部分 */
|
110 |
+
#component-17, /* Settings 部分 */
|
111 |
+
#component-18 { /* Built with Gradio 部分 */
|
112 |
+
background-color: var(--white) !important;
|
113 |
+
}
|
114 |
+
|
115 |
+
footer .gr-panel,
|
116 |
+
.gr-footer,
|
117 |
+
.gr-panel:has(a[href*="api"]), /* 针对Use via API */
|
118 |
+
.gr-panel:has(a[href*="settings"]) /* 针对Settings */ {
|
119 |
+
background-color: var(--white) !important;
|
120 |
+
color: var(--black) !important;
|
121 |
+
}
|
122 |
+
|
123 |
+
|
124 |
+
/* 其他样式保持不变 */
|
125 |
+
.result-container {
|
126 |
+
display: flex;
|
127 |
+
gap: 20px;
|
128 |
+
margin-bottom: 30px;
|
129 |
+
width: 100%;
|
130 |
+
}
|
131 |
+
|
132 |
+
.pdf-preview {
|
133 |
+
flex: 1;
|
134 |
+
min-width: 0;
|
135 |
+
}
|
136 |
+
|
137 |
+
.markdown-result {
|
138 |
+
flex: 1;
|
139 |
+
min-width: 0;
|
140 |
+
}
|
141 |
+
|
142 |
+
.gallery-container {
|
143 |
+
width: 100% !important;
|
144 |
+
}
|
145 |
+
|
146 |
+
.gallery-item {
|
147 |
+
width: 100% !important;
|
148 |
+
height: auto !important;
|
149 |
+
aspect-ratio: auto !important;
|
150 |
+
}
|
151 |
+
|
152 |
+
button {
|
153 |
+
background-color: var(--text-color) !important;
|
154 |
+
color: var(--black) !important;
|
155 |
+
border: none !important;
|
156 |
+
border-radius: 4px;
|
157 |
+
padding: 8px 16px;
|
158 |
+
}
|
159 |
+
button:hover {
|
160 |
+
opacity: 0.8 !important;
|
161 |
+
}
|
162 |
+
|
163 |
+
.radio-group {
|
164 |
+
margin-bottom: 15px !important;
|
165 |
+
}
|
166 |
+
|
167 |
+
.file-download {
|
168 |
+
margin-top: 15px !important;
|
169 |
+
}
|
170 |
+
.loader {
|
171 |
+
border: 5px solid #f3f3f3;
|
172 |
+
border-top: 5px solid #3498db;
|
173 |
+
border-radius: 50%;
|
174 |
+
width: 50px;
|
175 |
+
height: 50px;
|
176 |
+
animation: spin 1s linear infinite;
|
177 |
+
margin: 20px auto;
|
178 |
+
}
|
179 |
+
|
180 |
+
@keyframes spin {
|
181 |
+
0% { transform: rotate(0deg); }
|
182 |
+
100% { transform: rotate(360deg); }
|
183 |
+
}
|
184 |
+
|
185 |
+
.loader-container {
|
186 |
+
text-align: center;
|
187 |
+
margin: 20px 0;
|
188 |
+
}
|
189 |
+
"""
|
190 |
+
|
191 |
+
def clean_markdown_text(text):
|
192 |
+
if not text:
|
193 |
+
return ""
|
194 |
+
text = re.sub(r'<[^>]+>', '', text)
|
195 |
+
text = re.sub(r'\n{3,}', '\n\n', text)
|
196 |
+
return text.strip()
|
197 |
+
|
198 |
+
|
199 |
+
def pdf_to_images(pdf_path):
|
200 |
+
try:
|
201 |
+
images = pdf2image.convert_from_path(pdf_path)
|
202 |
+
return [image for image in images]
|
203 |
+
except:
|
204 |
+
return None
|
205 |
+
|
206 |
+
|
207 |
+
def process_file(file_path, file_type):
|
208 |
+
try:
|
209 |
+
with open(file_path, "rb") as f:
|
210 |
+
file_bytes = f.read()
|
211 |
+
|
212 |
+
file_data = base64.b64encode(file_bytes).decode("ascii")
|
213 |
+
headers = {
|
214 |
+
"Authorization": f"token {TOKEN}",
|
215 |
+
"Content-Type": "application/json"
|
216 |
+
}
|
217 |
+
|
218 |
+
response = requests.post(
|
219 |
+
API_URL,
|
220 |
+
json={"file": file_data, "fileType": 0 if file_type == "pdf" else 1},
|
221 |
+
headers=headers,
|
222 |
+
timeout=60
|
223 |
+
)
|
224 |
+
response.raise_for_status()
|
225 |
+
|
226 |
+
result = response.json()
|
227 |
+
layout_results = result.get("result", {}).get("layoutParsingResults", [])
|
228 |
+
|
229 |
+
markdown_contents = []
|
230 |
+
clean_markdown_contents = []
|
231 |
+
for res in layout_results:
|
232 |
+
markdown = res.get("markdown", {})
|
233 |
+
if isinstance(markdown, str):
|
234 |
+
original = markdown
|
235 |
+
elif isinstance(markdown, dict):
|
236 |
+
original = markdown.get("text", "")
|
237 |
+
|
238 |
+
markdown_contents.append(original)
|
239 |
+
clean_markdown_contents.append(clean_markdown_text(original))
|
240 |
+
|
241 |
+
if file_type == "pdf":
|
242 |
+
images = pdf_to_images(file_path)
|
243 |
+
else:
|
244 |
+
images = [Image.open(file_path)]
|
245 |
+
|
246 |
+
return {
|
247 |
+
"original_file": file_path,
|
248 |
+
"markdown_contents": markdown_contents,
|
249 |
+
"clean_markdown_contents": clean_markdown_contents,
|
250 |
+
"pdf_images": images,
|
251 |
+
"api_response": result
|
252 |
+
}
|
253 |
+
|
254 |
+
except Exception as e:
|
255 |
+
raise gr.Error(f"Error processing file: {str(e)}")
|
256 |
+
|
257 |
+
|
258 |
+
def create_zip_file(results):
|
259 |
+
try:
|
260 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
261 |
+
zip_filename = f"analysis_results_{timestamp}.zip"
|
262 |
+
|
263 |
+
temp_dir = tempfile.mkdtemp()
|
264 |
+
zip_path = os.path.join(temp_dir, zip_filename)
|
265 |
+
|
266 |
+
with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
|
267 |
+
original_path = results.get("original_file", "")
|
268 |
+
if original_path and Path(original_path).exists():
|
269 |
+
zipf.write(original_path, f"original/{Path(original_path).name}")
|
270 |
+
|
271 |
+
markdowns = results.get("markdown_contents", [])
|
272 |
+
for i, md_content in enumerate(markdowns):
|
273 |
+
if md_content:
|
274 |
+
zipf.writestr(f"markdown/original/markdown_{i + 1}.md", md_content)
|
275 |
+
|
276 |
+
api_response = results.get("api_response", {})
|
277 |
+
zipf.writestr("api_response.json", json.dumps(api_response, indent=2, ensure_ascii=False))
|
278 |
+
|
279 |
+
return zip_path
|
280 |
+
|
281 |
+
except Exception as e:
|
282 |
+
raise gr.Error(f"Error creating ZIP file: {str(e)}")
|
283 |
+
|
284 |
+
|
285 |
+
def export_markdown(results):
|
286 |
+
try:
|
287 |
+
markdowns = results.get("markdown_contents", [])
|
288 |
+
if not markdowns:
|
289 |
+
raise gr.Error("No markdown content to export")
|
290 |
+
|
291 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
292 |
+
filename = f"original_markdown_{timestamp}.md"
|
293 |
+
content = "\n\n".join(markdowns)
|
294 |
+
|
295 |
+
temp_dir = tempfile.mkdtemp()
|
296 |
+
file_path = os.path.join(temp_dir, filename)
|
297 |
+
|
298 |
+
with open(file_path, 'w', encoding='utf-8') as f:
|
299 |
+
f.write(content)
|
300 |
+
|
301 |
+
return file_path
|
302 |
+
|
303 |
+
except Exception as e:
|
304 |
+
raise gr.Error(f"Error exporting markdown: {str(e)}")
|
305 |
+
|
306 |
+
|
307 |
+
with gr.Blocks(css=CSS, title="Document Analysis System") as demo:
|
308 |
+
results_state = gr.State()
|
309 |
+
|
310 |
+
# 修改1: 将logo和导航按钮整合到一个容器中
|
311 |
+
with gr.Column(elem_classes=["header-container"]):
|
312 |
+
with gr.Column(elem_classes=["logo-container"]):
|
313 |
+
gr.Image("pp-structurev3_altered.png", elem_classes=["logo-img"], show_label=False)
|
314 |
+
|
315 |
+
# 修改2: 使用按钮代替链接
|
316 |
+
with gr.Row(elem_classes=["nav-buttons"]):
|
317 |
+
gr.Button("GitHub", link="https://github.com/PaddlePaddle/PaddleOCR", elem_classes=["nav-button"])
|
318 |
+
gr.Button("Homepage", link="https://paddleocr.ai", elem_classes=["nav-button"])
|
319 |
+
|
320 |
+
with gr.Column(elem_classes=["upload-section"]):
|
321 |
+
file_type = gr.Radio(
|
322 |
+
["pdf", "image"],
|
323 |
+
label="File type",
|
324 |
+
value="pdf",
|
325 |
+
interactive=True
|
326 |
+
)
|
327 |
+
file_input = gr.File(
|
328 |
+
label="Upload document",
|
329 |
+
file_types=[".pdf", ".jpg", ".jpeg", ".png"],
|
330 |
+
type="filepath"
|
331 |
+
)
|
332 |
+
process_btn = gr.Button("Analyze document", variant="primary")
|
333 |
+
|
334 |
+
loading_spinner = gr.Column(
|
335 |
+
visible=False,
|
336 |
+
elem_classes=["loader-container"]
|
337 |
+
)
|
338 |
+
with loading_spinner:
|
339 |
+
gr.HTML("""
|
340 |
+
<div class="loader"></div>
|
341 |
+
<p>Wait...</p>
|
342 |
+
""")
|
343 |
+
|
344 |
+
with gr.Row(elem_classes=["result-container"]):
|
345 |
+
with gr.Column(elem_classes=["pdf-preview"]):
|
346 |
+
gr.Markdown("### Original document preview")
|
347 |
+
pdf_display = gr.Gallery(
|
348 |
+
label="PDF page",
|
349 |
+
show_label=False,
|
350 |
+
elem_classes=["gallery-container"]
|
351 |
+
)
|
352 |
+
|
353 |
+
with gr.Column(elem_classes=["markdown-result"]):
|
354 |
+
with gr.Row(elem_classes=["radio-group"]):
|
355 |
+
display_mode = gr.Radio(
|
356 |
+
["Original Markdown", "Cleaned Text"],
|
357 |
+
label="Display Mode",
|
358 |
+
value="Original Markdown",
|
359 |
+
interactive=True
|
360 |
+
)
|
361 |
+
markdown_display = gr.HTML(label="Analysis Results")
|
362 |
+
with gr.Column(elem_classes=["download-section"]):
|
363 |
+
gr.Markdown("### Result Export")
|
364 |
+
with gr.Row():
|
365 |
+
download_md_btn = gr.Button("Download Original Markdown", variant="secondary")
|
366 |
+
download_all_btn = gr.Button("Download Complete Analysis Results (ZIP)", variant="primary")
|
367 |
+
download_file = gr.File(visible=False, label="Download file", elem_classes=["file-download"])
|
368 |
+
|
369 |
+
def toggle_spinner():
|
370 |
+
return gr.update(visible=True)
|
371 |
+
|
372 |
+
|
373 |
+
def hide_spinner():
|
374 |
+
return gr.update(visible=False)
|
375 |
+
|
376 |
+
|
377 |
+
process_btn.click(
|
378 |
+
toggle_spinner,
|
379 |
+
outputs=[loading_spinner]
|
380 |
+
).then(
|
381 |
+
process_file,
|
382 |
+
inputs=[file_input, file_type],
|
383 |
+
outputs=[results_state]
|
384 |
+
).then(
|
385 |
+
hide_spinner,
|
386 |
+
outputs=[loading_spinner]
|
387 |
+
).success(
|
388 |
+
lambda res: res["pdf_images"] if res and res.get("pdf_images") else [],
|
389 |
+
inputs=[results_state],
|
390 |
+
outputs=[pdf_display]
|
391 |
+
).success(
|
392 |
+
lambda res: res["markdown_contents"][0] if res and res.get("markdown_contents") else "",
|
393 |
+
inputs=[results_state],
|
394 |
+
outputs=[markdown_display]
|
395 |
+
)
|
396 |
+
|
397 |
+
display_mode.change(
|
398 |
+
lambda mode, res: (
|
399 |
+
res["markdown_contents"][0] if mode == "原始Markdown"
|
400 |
+
else res["clean_markdown_contents"][0]
|
401 |
+
) if res else "",
|
402 |
+
inputs=[display_mode, results_state],
|
403 |
+
outputs=[markdown_display]
|
404 |
+
)
|
405 |
+
|
406 |
+
download_md_btn.click(
|
407 |
+
export_markdown,
|
408 |
+
inputs=[results_state],
|
409 |
+
outputs=[download_file]
|
410 |
+
).then(
|
411 |
+
lambda x: gr.update(visible=True),
|
412 |
+
inputs=[download_file],
|
413 |
+
outputs=[download_file]
|
414 |
+
)
|
415 |
+
|
416 |
+
download_all_btn.click(
|
417 |
+
create_zip_file,
|
418 |
+
inputs=[results_state],
|
419 |
+
outputs=[download_file]
|
420 |
+
).then(
|
421 |
+
lambda x: gr.update(visible=True),
|
422 |
+
inputs=[download_file],
|
423 |
+
outputs=[download_file]
|
424 |
+
)
|
425 |
+
|
426 |
+
if __name__ == "__main__":
|
427 |
+
demo.launch(share=True)
|