Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,57 +1,30 @@
|
|
| 1 |
# pdf_viewer_app.py
|
| 2 |
-
import base64
|
| 3 |
-
from pathlib import Path
|
| 4 |
-
from typing import Union, BinaryIO
|
| 5 |
import gradio as gr
|
| 6 |
|
| 7 |
|
| 8 |
-
def
|
| 9 |
-
|
| 10 |
-
scale: float = 1.0,
|
| 11 |
-
height: int = 800,
|
| 12 |
-
) -> str:
|
| 13 |
-
"""把 PDF 转成可嵌入浏览器的 HTML 片段(<iframe> + Blob URL)"""
|
| 14 |
-
# 读取二进制
|
| 15 |
-
if isinstance(pdf, (str, Path)):
|
| 16 |
-
raw = Path(pdf).read_bytes()
|
| 17 |
-
elif isinstance(pdf, bytes):
|
| 18 |
-
raw = pdf
|
| 19 |
-
else: # file-like
|
| 20 |
-
raw = pdf.read()
|
| 21 |
|
| 22 |
-
b64 = base64.b64encode(raw).decode()
|
| 23 |
-
zoom = int(scale * 100) # 转百分比
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
document.getElementById("pdf_view").src = url + "#zoom={zoom}";
|
| 34 |
-
</script>
|
| 35 |
-
"""
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
with gr.Blocks(title="自定义 PDF 预览") as demo:
|
| 45 |
-
gr.Markdown("## 📄 PDF 预览 Demo(Blob URL + iframe)")
|
| 46 |
-
|
| 47 |
-
with gr.Row():
|
| 48 |
-
uploader = gr.File(label="上传 PDF", file_types=[".pdf"])
|
| 49 |
-
zoom = gr.Slider(0.5, 3, value=1, step=0.1, label="缩放倍数")
|
| 50 |
-
|
| 51 |
-
html_out = gr.HTML() # 关闭净化,允许 <script>
|
| 52 |
-
|
| 53 |
-
# 事件绑定
|
| 54 |
-
uploader.change(show_pdf, [uploader, zoom], html_out)
|
| 55 |
-
zoom.change(show_pdf, [uploader, zoom], html_out)
|
| 56 |
-
|
| 57 |
-
demo.launch()
|
|
|
|
| 1 |
# pdf_viewer_app.py
|
|
|
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
|
| 4 |
|
| 5 |
+
def render_markdown(text: str):
|
| 6 |
+
return text or ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
if __name__ == "__main__":
|
| 10 |
+
with gr.Blocks() as demo:
|
| 11 |
+
input_md = gr.Textbox(
|
| 12 |
+
label="Input",
|
| 13 |
+
placeholder="Type Markdown/LaTeX here...",
|
| 14 |
+
lines=12,
|
| 15 |
+
)
|
| 16 |
+
render_btn = gr.Button("Render", variant="primary")
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
md = gr.Markdown(
|
| 19 |
+
label="Markdown rendering",
|
| 20 |
+
show_copy_button=True,
|
| 21 |
+
line_breaks=True,
|
| 22 |
+
)
|
| 23 |
|
| 24 |
+
render_btn.click(
|
| 25 |
+
fn=render_markdown,
|
| 26 |
+
inputs=input_md,
|
| 27 |
+
outputs=md,
|
| 28 |
+
)
|
| 29 |
|
| 30 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|