Spaces:
Running
Running
Commit
•
dd5ca28
1
Parent(s):
3a57cef
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,44 @@
|
|
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import gradio as gr
|
3 |
from gradio_pdf import PDF
|
4 |
from pdf2image import convert_from_path
|
@@ -28,3 +68,84 @@ demo = gr.Interface(
|
|
28 |
|
29 |
if __name__ == "__main__":
|
30 |
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
|
2 |
+
import gradio as gr
|
3 |
+
from _app import demo as app
|
4 |
+
import os
|
5 |
+
|
6 |
+
_docs = {'PDF': {'description': 'A base class for defining methods that all input/output components should have.', 'members': {'__init__': {'value': {'type': 'Any', 'default': 'None', 'description': None}, 'height': {'type': 'int | None', 'default': 'None', 'description': None}, 'label': {'type': 'str | None', 'default': 'None', 'description': None}, 'info': {'type': 'str | None', 'default': 'None', 'description': None}, 'show_label': {'type': 'bool | None', 'default': 'None', 'description': None}, 'container': {'type': 'bool', 'default': 'True', 'description': None}, 'scale': {'type': 'int | None', 'default': 'None', 'description': None}, 'min_width': {'type': 'int | None', 'default': 'None', 'description': None}, 'interactive': {'type': 'bool | None', 'default': 'None', 'description': None}, 'visible': {'type': 'bool', 'default': 'True', 'description': None}, 'elem_id': {'type': 'str | None', 'default': 'None', 'description': None}, 'elem_classes': {'type': 'list[str] | str | None', 'default': 'None', 'description': None}, 'render': {'type': 'bool', 'default': 'True', 'description': None}, 'load_fn': {'type': 'Callable[..., Any] | None', 'default': 'None', 'description': None}, 'every': {'type': 'float | None', 'default': 'None', 'description': None}}, 'postprocess': {'value': {'type': 'str | None', 'description': None}}, 'preprocess': {'return': {'type': 'str', 'description': None}, 'value': None}}, 'events': {'change': {'type': None, 'default': None, 'description': ''}, 'upload': {'type': None, 'default': None, 'description': ''}}}, '__meta__': {'additional_interfaces': {}, 'user_fn_refs': {'PDF': []}}}
|
7 |
+
|
8 |
+
abs_path = os.path.join(os.path.dirname(__file__), "css.css")
|
9 |
+
|
10 |
+
with gr.Blocks(
|
11 |
+
css=abs_path,
|
12 |
+
theme=gr.themes.Default(
|
13 |
+
font_mono=[
|
14 |
+
gr.themes.GoogleFont("Inconsolata"),
|
15 |
+
"monospace",
|
16 |
+
],
|
17 |
+
),
|
18 |
+
) as demo:
|
19 |
+
gr.Markdown(
|
20 |
+
"""
|
21 |
+
# `gradio_pdf`
|
22 |
+
|
23 |
+
<div style="display: flex; gap: 7px;">
|
24 |
+
<a href="https://pypi.org/project/gradio_pdf/" target="_blank"><img alt="PyPI - Version" src="https://img.shields.io/pypi/v/gradio_pdf"></a> <a href="https://github.com/freddyaboulton/gradio-pdf/issues" target="_blank"><img alt="Static Badge" src="https://img.shields.io/badge/Issues-white?logo=github&logoColor=black"></a> <a href="https://huggingface.co/spaces/freddyaboulton/gradio_pdf/discussions" target="_blank"><img alt="Static Badge" src="https://img.shields.io/badge/%F0%9F%A4%97%20Discuss-%23097EFF?style=flat&logoColor=black"></a>
|
25 |
+
</div>
|
26 |
+
|
27 |
+
Easily display PDFs in Gradio
|
28 |
+
""", elem_classes=["md-custom"], header_links=True)
|
29 |
+
app.render()
|
30 |
+
gr.Markdown(
|
31 |
+
"""
|
32 |
+
## Installation
|
33 |
+
|
34 |
+
```bash
|
35 |
+
pip install gradio_pdf
|
36 |
+
```
|
37 |
+
|
38 |
+
## Usage
|
39 |
+
|
40 |
+
```python
|
41 |
+
|
42 |
import gradio as gr
|
43 |
from gradio_pdf import PDF
|
44 |
from pdf2image import convert_from_path
|
|
|
68 |
|
69 |
if __name__ == "__main__":
|
70 |
demo.launch()
|
71 |
+
|
72 |
+
```
|
73 |
+
""", elem_classes=["md-custom"], header_links=True)
|
74 |
+
|
75 |
+
|
76 |
+
gr.Markdown("""
|
77 |
+
## `PDF`
|
78 |
+
|
79 |
+
### Initialization
|
80 |
+
""", elem_classes=["md-custom"], header_links=True)
|
81 |
+
|
82 |
+
gr.ParamViewer(value=_docs["PDF"]["members"]["__init__"], linkify=[])
|
83 |
+
|
84 |
+
|
85 |
+
gr.Markdown("### Events")
|
86 |
+
gr.ParamViewer(value=_docs["PDF"]["events"], linkify=['Event'])
|
87 |
+
|
88 |
+
|
89 |
+
|
90 |
+
|
91 |
+
gr.Markdown("""
|
92 |
+
|
93 |
+
### User function
|
94 |
+
|
95 |
+
The impact on the users predict function varies depending on whether the component is used as an input or output for an event (or both).
|
96 |
+
|
97 |
+
- When used as an Input, the component only impacts the input signature of the user function.
|
98 |
+
- When used as an output, the component only impacts the return signature of the user function.
|
99 |
+
|
100 |
+
The code snippet below is accurate in cases where the component is used as both an input and an output.
|
101 |
+
|
102 |
+
|
103 |
+
|
104 |
+
```python
|
105 |
+
def predict(
|
106 |
+
value: str
|
107 |
+
) -> str | None:
|
108 |
+
return value
|
109 |
+
```
|
110 |
+
""", elem_classes=["md-custom", "PDF-user-fn"], header_links=True)
|
111 |
+
|
112 |
+
|
113 |
+
|
114 |
+
|
115 |
+
demo.load(None, js=r"""function() {
|
116 |
+
const refs = {};
|
117 |
+
const user_fn_refs = {
|
118 |
+
PDF: [], };
|
119 |
+
requestAnimationFrame(() => {
|
120 |
+
|
121 |
+
Object.entries(user_fn_refs).forEach(([key, refs]) => {
|
122 |
+
if (refs.length > 0) {
|
123 |
+
const el = document.querySelector(`.${key}-user-fn`);
|
124 |
+
if (!el) return;
|
125 |
+
refs.forEach(ref => {
|
126 |
+
el.innerHTML = el.innerHTML.replace(
|
127 |
+
new RegExp("\\b"+ref+"\\b", "g"),
|
128 |
+
`<a href="#h-${ref.toLowerCase()}">${ref}</a>`
|
129 |
+
);
|
130 |
+
})
|
131 |
+
}
|
132 |
+
})
|
133 |
+
|
134 |
+
Object.entries(refs).forEach(([key, refs]) => {
|
135 |
+
if (refs.length > 0) {
|
136 |
+
const el = document.querySelector(`.${key}`);
|
137 |
+
if (!el) return;
|
138 |
+
refs.forEach(ref => {
|
139 |
+
el.innerHTML = el.innerHTML.replace(
|
140 |
+
new RegExp("\\b"+ref+"\\b", "g"),
|
141 |
+
`<a href="#h-${ref.toLowerCase()}">${ref}</a>`
|
142 |
+
);
|
143 |
+
})
|
144 |
+
}
|
145 |
+
})
|
146 |
+
})
|
147 |
+
}
|
148 |
+
|
149 |
+
""")
|
150 |
+
|
151 |
+
demo.launch()
|