Felix92 commited on
Commit
d09751f
·
1 Parent(s): 1eb23f0
Files changed (3) hide show
  1. README.md +1 -1
  2. app.py +98 -0
  3. requirements.txt +3 -0
README.md CHANGED
@@ -4,7 +4,7 @@ emoji: 🌖
4
  colorFrom: pink
5
  colorTo: red
6
  sdk: gradio
7
- sdk_version: 5.0.1
8
  app_file: app.py
9
  pinned: false
10
  license: apache-2.0
 
4
  colorFrom: pink
5
  colorTo: red
6
  sdk: gradio
7
+ sdk_version: 4.44.1
8
  app_file: app.py
9
  pinned: false
10
  license: apache-2.0
app.py ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from os import environ
2
+ import gradio as gr
3
+ from huggingface_hub import HfApi
4
+ import subprocess
5
+ from pdf2image import convert_from_path
6
+ import tempfile
7
+
8
+ API = HfApi()
9
+ TOKEN = environ.get("TOKEN")
10
+
11
+ def _sha256sum_creation(file_path: str) -> str:
12
+ sha_hash = subprocess.run(["sha256sum", file_path], stdout=subprocess.PIPE, text=True).stdout.split()[0]
13
+ return sha_hash
14
+
15
+ def _rename_file(file_path: str, sha_hash: str) -> str:
16
+ new_file_path = file_path.replace(file_path.split("/")[-1], sha_hash + "_" + file_path.split("/")[-1])
17
+ subprocess.run(["mv", file_path, new_file_path])
18
+ return new_file_path
19
+
20
+ def _upload_hub(file_path: str) -> None:
21
+ sha256_hash = _sha256sum_creation(file_path)
22
+ new_file_path = _rename_file(file_path, sha256_hash)
23
+ API.upload_file(
24
+ path_or_fileobj=new_file_path,
25
+ path_in_repo=new_file_path.split("/")[-1],
26
+ token=TOKEN,
27
+ repo_type="dataset",
28
+ repo_id="Felix92/docTR-multilingual-data-collection",
29
+ )
30
+
31
+ def upload_to_hub(file_upload, camera_upload, agree):
32
+ try:
33
+ if not agree:
34
+ return gr.Markdown("You must agree to the terms and conditions before proceeding."), None
35
+
36
+ if file_upload:
37
+ if file_upload.endswith(".pdf"):
38
+ with tempfile.TemporaryDirectory() as path:
39
+ for file_path in convert_from_path(file_upload, output_folder=path, paths_only=True, fmt='png')[:10]:
40
+ _upload_hub(file_path)
41
+ elif camera_upload:
42
+ _upload_hub(camera_upload)
43
+
44
+ return gr.update(visible=False), gr.Markdown("""<div style="text-align: center;"><h3>Upload was successful! You can upload another document.</h3></div>"""), gr.update(value=None), gr.update(value=None)
45
+ except Exception as e:
46
+ return gr.update(visible=False), gr.Markdown(f"""<div style="text-align: center;"><h3>An error occured: {e}</h3></div>"""), gr.update(value=None), gr.update(value=None)
47
+
48
+ with gr.Blocks(fill_height=True) as demo:
49
+ agreement_markdown = gr.Markdown(
50
+ """
51
+ <div style="text-align: center;">
52
+ <h1>Document Upload Agreement</h1>
53
+
54
+ <h3>This is a Hugging Face space for the docTR/OnnxTR community to collect multilingual data for the following project/s:</h3>
55
+
56
+ <h3><a href="https://github.com/mindee/doctr">docTR</a></h3>
57
+
58
+ <h3><a href="https://github.com/felixdittrich92/OnnxTR">OnnxTR</a></h3>
59
+ </div>
60
+
61
+ <h3>You can upload PDF (up to 10 sites), JPG, or PNG files to this space via file upload or webcam / from mobile phone. The uploaded documents will be used to train and evaluate models for the docTR/OnnxTR projects.</h3>
62
+
63
+ <br>
64
+ <br>
65
+
66
+ <h3>By uploading a document, you explicitly agree to the following terms:</h3>
67
+
68
+ <h3>1. You affirm that you are the owner or have the necessary rights to upload and share the document.</h3>
69
+
70
+ <h3>2. You agree that the uploaded document will be made publicly available to everyone.</h3>
71
+
72
+ <h3>3. You agree that the uploaded document can be used for any purpose, including commercial use, by any third party.</h3>
73
+ """
74
+ )
75
+
76
+ agree_button = gr.Button("I Agree to the Terms and Conditions")
77
+ agree_state = gr.State(value=False) # State to store agreement status
78
+
79
+ with gr.Column(visible=False) as upload_section:
80
+ success_message = gr.Markdown(visible=True)
81
+ gr.Markdown("Upload a document via camera capture.")
82
+ camera_upload = gr.Image(label="Upload Image [JPG | PNG] via camera", type="filepath", sources=["webcam"])
83
+ gr.Markdown("Upload a document via file upload.")
84
+ file_upload = gr.File(label="Upload File [JPG | PNG | PDF]", file_types=["pdf", "jpg", "png"], type="filepath")
85
+
86
+ def toggle_agreement_visibility():
87
+ return gr.update(visible=False), gr.update(visible=False), True, gr.update(visible=True)
88
+
89
+ # Clicking the "I Agree" button hides the agreement and shows the upload section
90
+ agree_button.click(fn=toggle_agreement_visibility, inputs=None, outputs=[agreement_markdown, agree_button, agree_state, upload_section])
91
+
92
+ # Automatically trigger upload when file is uploaded or camera capture is taken
93
+ file_upload.change(fn=upload_to_hub, inputs=[file_upload, camera_upload, agree_state], outputs=[agree_button, success_message, file_upload, camera_upload])
94
+ camera_upload.change(fn=upload_to_hub, inputs=[file_upload, camera_upload, agree_state], outputs=[agree_button, success_message, file_upload, camera_upload])
95
+
96
+
97
+ if __name__ == "__main__":
98
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ gradio>=4.37.1,<5.0.0
2
+ pdf2image>=1.17.0, <2.0.0
3
+ huggingface-hub>=0.25.2, <1.0.0