freddyaboulton HF staff commited on
Commit
de8d3f6
1 Parent(s): c4b03df

Upload folder using huggingface_hub

Browse files
Dockerfile CHANGED
@@ -7,12 +7,10 @@ COPY --link --chown=1000 . .
7
 
8
  RUN mkdir -p /tmp/cache/
9
  RUN chmod a+rwx -R /tmp/cache/
10
- RUN apt-get update && apt-get install -y poppler-utils tesseract-ocr chromium
11
- ENV TRANSFORMERS_CACHE=/tmp/cache/
12
 
13
  RUN pip install --no-cache-dir -r requirements.txt
14
 
15
  ENV PYTHONUNBUFFERED=1 GRADIO_ALLOW_FLAGGING=never GRADIO_NUM_PORTS=1 GRADIO_SERVER_NAME=0.0.0.0 GRADIO_SERVER_PORT=7860 SYSTEM=spaces
16
 
17
- CMD ["python", "app.py"]
18
-
 
7
 
8
  RUN mkdir -p /tmp/cache/
9
  RUN chmod a+rwx -R /tmp/cache/
10
+ ENV TRANSFORMERS_CACHE=/tmp/cache/
 
11
 
12
  RUN pip install --no-cache-dir -r requirements.txt
13
 
14
  ENV PYTHONUNBUFFERED=1 GRADIO_ALLOW_FLAGGING=never GRADIO_NUM_PORTS=1 GRADIO_SERVER_NAME=0.0.0.0 GRADIO_SERVER_PORT=7860 SYSTEM=spaces
15
 
16
+ CMD ["python", "space.py"]
 
README.md CHANGED
@@ -1,17 +1,10 @@
1
 
2
  ---
3
- tags: [gradio-custom-component,Documents,PDF,Document QA,gradio,gradio-template-Fallback]
4
- title: gradio_pdf V0.0.3
5
  colorFrom: green
6
- colorTo: pink
7
  sdk: docker
8
  pinned: false
9
  license: apache-2.0
10
  ---
11
-
12
-
13
- # Name: gradio_pdf
14
-
15
- Description: Easily display PDFs in Gradio
16
-
17
- Install with: pip install gradio_pdf
 
1
 
2
  ---
3
+ tags: [gradio-custom-component]
4
+ title: gradio_pdf V0.0.6
5
  colorFrom: green
6
+ colorTo: gray
7
  sdk: docker
8
  pinned: false
9
  license: apache-2.0
10
  ---
 
 
 
 
 
 
 
__pycache__/__init__.cpython-311.pyc ADDED
Binary file (173 Bytes). View file
 
__pycache__/_app.cpython-310.pyc CHANGED
Binary files a/__pycache__/_app.cpython-310.pyc and b/__pycache__/_app.cpython-310.pyc differ
 
__pycache__/_app.cpython-311.pyc ADDED
Binary file (1.79 kB). View file
 
__pycache__/app.cpython-311.pyc ADDED
Binary file (6.23 kB). View file
 
app.py CHANGED
@@ -1,44 +1,4 @@
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,84 +28,3 @@ demo = gr.Interface(
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()
 
1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  import gradio as gr
3
  from gradio_pdf import PDF
4
  from pdf2image import convert_from_path
 
28
 
29
  if __name__ == "__main__":
30
  demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
space.py ADDED
@@ -0,0 +1,152 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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': 'typing.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[Ellipsis, typing.Any] | None', 'default': 'None', 'description': None}, 'every': {'type': 'float | None', 'default': 'None', 'description': None}}, 'postprocess': {'value': {'type': 'str | None', 'description': "The output data received by the component from the user's function in the backend."}}, 'preprocess': {'return': {'type': 'str', 'description': "The preprocessed input data sent to the user's function in the backend."}, '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>
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
45
+ from transformers import pipeline
46
+ from pathlib import Path
47
+
48
+ dir_ = Path(__file__).parent
49
+
50
+ p = pipeline(
51
+ "document-question-answering",
52
+ model="impira/layoutlm-document-qa",
53
+ )
54
+
55
+ def qa(question: str, doc: str) -> str:
56
+ img = convert_from_path(doc)[0]
57
+ output = p(img, question)
58
+ return sorted(output, key=lambda x: x["score"], reverse=True)[0]['answer']
59
+
60
+
61
+ demo = gr.Interface(
62
+ qa,
63
+ [gr.Textbox(label="Question"), PDF(label="Document")],
64
+ gr.Textbox(),
65
+ examples=[["What is the total gross worth?", str(dir_ / "invoice_2.pdf")],
66
+ ["Whos is being invoiced?", str(dir_ / "sample_invoice.pdf")]]
67
+ )
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
+ - **As input:** Is passed, the preprocessed input data sent to the user's function in the backend.
103
+ - **As output:** Should return, the output data received by the component from the user's function in the backend.
104
+
105
+ ```python
106
+ def predict(
107
+ value: str
108
+ ) -> str | None:
109
+ return value
110
+ ```
111
+ """, elem_classes=["md-custom", "PDF-user-fn"], header_links=True)
112
+
113
+
114
+
115
+
116
+ demo.load(None, js=r"""function() {
117
+ const refs = {};
118
+ const user_fn_refs = {
119
+ PDF: [], };
120
+ requestAnimationFrame(() => {
121
+
122
+ Object.entries(user_fn_refs).forEach(([key, refs]) => {
123
+ if (refs.length > 0) {
124
+ const el = document.querySelector(`.${key}-user-fn`);
125
+ if (!el) return;
126
+ refs.forEach(ref => {
127
+ el.innerHTML = el.innerHTML.replace(
128
+ new RegExp("\\b"+ref+"\\b", "g"),
129
+ `<a href="#h-${ref.toLowerCase()}">${ref}</a>`
130
+ );
131
+ })
132
+ }
133
+ })
134
+
135
+ Object.entries(refs).forEach(([key, refs]) => {
136
+ if (refs.length > 0) {
137
+ const el = document.querySelector(`.${key}`);
138
+ if (!el) return;
139
+ refs.forEach(ref => {
140
+ el.innerHTML = el.innerHTML.replace(
141
+ new RegExp("\\b"+ref+"\\b", "g"),
142
+ `<a href="#h-${ref.toLowerCase()}">${ref}</a>`
143
+ );
144
+ })
145
+ }
146
+ })
147
+ })
148
+ }
149
+
150
+ """)
151
+
152
+ demo.launch()