Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -307,32 +307,75 @@ async def process_file(file_path):
|
|
| 307 |
import gradio as gr
|
| 308 |
import base64, pathlib
|
| 309 |
|
| 310 |
-
def embed_file(file, scale):
|
| 311 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 312 |
path = pathlib.Path(file.name)
|
|
|
|
| 313 |
data = base64.b64encode(path.read_bytes()).decode()
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
|
|
|
| 317 |
return f"""
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 330 |
else:
|
| 331 |
-
# 图片:CSS transform 实现 zoom;支持拖拽滚轮缩放
|
| 332 |
return (
|
| 333 |
f'<div style="overflow:auto;border:1px solid #ccc;'
|
| 334 |
f'width:100%;height:800px;text-align:center">'
|
| 335 |
-
f'<img src="data:image/{
|
| 336 |
f'style="transform:scale({scale});transform-origin:0 0;" />'
|
| 337 |
f'</div>'
|
| 338 |
)
|
|
@@ -360,7 +403,7 @@ if __name__ == '__main__':
|
|
| 360 |
pdf_show = PDF(label='Preview', interactive=False, visible=True, height=800)
|
| 361 |
|
| 362 |
slider = gr.Slider(0.5, 3.0, value=1.0, step=0.1, label="缩放倍数")
|
| 363 |
-
out = gr.HTML()
|
| 364 |
|
| 365 |
file.change(embed_file, [file, slider], out)
|
| 366 |
slider.change(embed_file, [file, slider], out)
|
|
|
|
| 307 |
import gradio as gr
|
| 308 |
import base64, pathlib
|
| 309 |
|
| 310 |
+
# def embed_file(file, scale):
|
| 311 |
+
# """根据后缀决定用 PDF 还是 <img>."""
|
| 312 |
+
# path = pathlib.Path(file.name)
|
| 313 |
+
# data = base64.b64encode(path.read_bytes()).decode()
|
| 314 |
+
# if path.suffix.lower() == ".pdf":
|
| 315 |
+
# b64 = base64.b64encode(pathlib.Path(file.name).read_bytes()).decode()
|
| 316 |
+
# # 注意:把 JS/HTML 写在一起返回给 gr.HTML
|
| 317 |
+
# return f"""
|
| 318 |
+
# <script>
|
| 319 |
+
# const pdf = atob("{b64}");
|
| 320 |
+
# const bytes = Uint8Array.from(pdf, c => c.charCodeAt(0));
|
| 321 |
+
# const blob = new Blob([bytes], {{type: "application/pdf"}});
|
| 322 |
+
# const url = URL.createObjectURL(blob);
|
| 323 |
+
# const iframe = document.getElementById("pdf_view") || document.createElement("iframe");
|
| 324 |
+
# iframe.id = "pdf_view";
|
| 325 |
+
# iframe.style = "width:100%;height:800px;border:none";
|
| 326 |
+
# iframe.src = url + "#zoom={int(scale*100)}";
|
| 327 |
+
# document.currentScript.parentElement.replaceChildren(iframe);
|
| 328 |
+
# </script>
|
| 329 |
+
# """
|
| 330 |
+
# else:
|
| 331 |
+
# # 图片:CSS transform 实现 zoom;支持拖拽滚轮缩放
|
| 332 |
+
# return (
|
| 333 |
+
# f'<div style="overflow:auto;border:1px solid #ccc;'
|
| 334 |
+
# f'width:100%;height:800px;text-align:center">'
|
| 335 |
+
# f'<img src="data:image/{path.suffix[1:]};base64,{data}" '
|
| 336 |
+
# f'style="transform:scale({scale});transform-origin:0 0;" />'
|
| 337 |
+
# f'</div>'
|
| 338 |
+
# )
|
| 339 |
+
|
| 340 |
+
|
| 341 |
+
def embed_file(file, scale: float):
|
| 342 |
+
"""
|
| 343 |
+
将上传的文件转换为可嵌入 HTML:
|
| 344 |
+
• PDF → 生成 blob:URL,用 <iframe> + #zoom=xx% 展示
|
| 345 |
+
• 图片 → <img style="transform:scale()"> 并允许拖拽缩放
|
| 346 |
+
"""
|
| 347 |
+
if file is None:
|
| 348 |
+
return ""
|
| 349 |
+
|
| 350 |
path = pathlib.Path(file.name)
|
| 351 |
+
ext = path.suffix.lower()
|
| 352 |
data = base64.b64encode(path.read_bytes()).decode()
|
| 353 |
+
|
| 354 |
+
# ---------- 1. PDF ----------
|
| 355 |
+
if ext == ".pdf":
|
| 356 |
+
# 输出 JS:把 Base64 转 Blob,然后注入/更新 iframe
|
| 357 |
return f"""
|
| 358 |
+
<script>
|
| 359 |
+
const bytes = Uint8Array.from(atob("{data}"), c => c.charCodeAt(0));
|
| 360 |
+
const blob = new Blob([bytes], {{ type: "application/pdf" }});
|
| 361 |
+
const url = URL.createObjectURL(blob);
|
| 362 |
+
|
| 363 |
+
let iframe = document.getElementById("pdf_view");
|
| 364 |
+
if (!iframe) {{
|
| 365 |
+
iframe = document.createElement("iframe");
|
| 366 |
+
iframe.id = "pdf_view";
|
| 367 |
+
iframe.style = "width:100%;height:800px;border:none;";
|
| 368 |
+
document.currentScript.parentElement.appendChild(iframe);
|
| 369 |
+
}}
|
| 370 |
+
iframe.src = url + "#zoom={int(scale*100)}";
|
| 371 |
+
</script>
|
| 372 |
+
"""
|
| 373 |
+
# ---------- 2. 图片 ----------
|
| 374 |
else:
|
|
|
|
| 375 |
return (
|
| 376 |
f'<div style="overflow:auto;border:1px solid #ccc;'
|
| 377 |
f'width:100%;height:800px;text-align:center">'
|
| 378 |
+
f'<img src="data:image/{ext[1:]};base64,{data}" '
|
| 379 |
f'style="transform:scale({scale});transform-origin:0 0;" />'
|
| 380 |
f'</div>'
|
| 381 |
)
|
|
|
|
| 403 |
pdf_show = PDF(label='Preview', interactive=False, visible=True, height=800)
|
| 404 |
|
| 405 |
slider = gr.Slider(0.5, 3.0, value=1.0, step=0.1, label="缩放倍数")
|
| 406 |
+
out = gr.HTML(sanitize=False)
|
| 407 |
|
| 408 |
file.change(embed_file, [file, slider], out)
|
| 409 |
slider.change(embed_file, [file, slider], out)
|