GiantPandas commited on
Commit
98742b0
·
verified ·
1 Parent(s): f1561f2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -1
app.py CHANGED
@@ -303,6 +303,31 @@ async def process_file(file_path):
303
  return str(tmp_file_path)
304
 
305
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
306
  if __name__ == '__main__':
307
  with gr.Blocks() as demo:
308
  with gr.Row():
@@ -322,7 +347,14 @@ if __name__ == '__main__':
322
  with gr.Row():
323
  change_bu = gr.Button('Parse')
324
  clear_bu = gr.ClearButton(value='Clear')
325
- pdf_show = PDF(label='Preview', interactive=False, visible=True, height=800)
 
 
 
 
 
 
 
326
 
327
 
328
 
 
303
  return str(tmp_file_path)
304
 
305
 
306
+
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
+ # PDF:借浏览器内建 viewer,使用 #zoom=xxx 控制
316
+ return (
317
+ f'<iframe style="width:100%;height:800px;border:none" '
318
+ f'src="data:application/pdf;base64,{data}#zoom={int(scale*100)}"></iframe>'
319
+ )
320
+ else:
321
+ # 图片:CSS transform 实现 zoom;支持拖拽滚轮缩放
322
+ return (
323
+ f'<div style="overflow:auto;border:1px solid #ccc;'
324
+ f'width:100%;height:800px;text-align:center">'
325
+ f'<img src="data:image/{path.suffix[1:]};base64,{data}" '
326
+ f'style="transform:scale({scale});transform-origin:0 0;" />'
327
+ f'</div>'
328
+ )
329
+
330
+
331
  if __name__ == '__main__':
332
  with gr.Blocks() as demo:
333
  with gr.Row():
 
347
  with gr.Row():
348
  change_bu = gr.Button('Parse')
349
  clear_bu = gr.ClearButton(value='Clear')
350
+ # pdf_show = PDF(label='Preview', interactive=False, visible=True, height=800)
351
+
352
+ slider = gr.Slider(0.5, 3.0, value=1.0, step=0.1, label="缩放倍数")
353
+ out = gr.HTML()
354
+
355
+ file.change(embed_file, [file, slider], out)
356
+ slider.change(embed_file, [file, slider], out)
357
+
358
 
359
 
360