File size: 4,563 Bytes
d09751f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5b025ea
d09751f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77f19ef
 
d09751f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f4f5633
d09751f
 
5b025ea
d09751f
 
 
 
 
 
 
5b025ea
d09751f
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
from os import environ
import gradio as gr
from huggingface_hub import HfApi
import subprocess
from pdf2image import convert_from_path
import tempfile

API = HfApi()
TOKEN = environ.get("TOKEN")

def _sha256sum_creation(file_path: str) -> str:
    sha_hash = subprocess.run(["sha256sum", file_path], stdout=subprocess.PIPE, text=True).stdout.split()[0]
    return sha_hash

def _rename_file(file_path: str, sha_hash: str) -> str:
    new_file_path = file_path.replace(file_path.split("/")[-1], sha_hash + "_" + file_path.split("/")[-1])
    subprocess.run(["mv", file_path, new_file_path])
    return new_file_path

def _upload_hub(file_path: str) -> None:
    sha256_hash = _sha256sum_creation(file_path)
    new_file_path = _rename_file(file_path, sha256_hash)
    API.upload_file(
        path_or_fileobj=new_file_path,
        path_in_repo=new_file_path.split("/")[-1],
        token=TOKEN,
        repo_type="dataset",
        repo_id="Felix92/docTR-multilingual-data-collection",
    )

def upload_to_hub(file_upload, camera_upload, agree):
    try:
        if not agree:
            return gr.Markdown("You must agree to the terms and conditions before proceeding."), None

        if file_upload:
            if file_upload.endswith(".pdf"):
                with tempfile.TemporaryDirectory() as path:
                    for file_path in convert_from_path(file_upload, output_folder=path, paths_only=True, fmt='png')[:10]:
                        _upload_hub(file_path)
        if camera_upload:
            _upload_hub(camera_upload)

        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)
    except Exception as e:
        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)

with gr.Blocks(fill_height=True) as demo:
    agreement_markdown = gr.Markdown(
        """
        <div style="text-align: center;">
        <h1>Document Upload Agreement</h1>

        <h3>This is a Hugging Face space for the docTR/OnnxTR community to collect multilingual data for the following project/s:</h3>

        <h3><a href="https://github.com/mindee/doctr">docTR</a></h3>

        <h3><a href="https://github.com/felixdittrich92/OnnxTR">OnnxTR</a></h3>
        </div>

        <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>

        <h3>All uploaded files can be found here: <a href="https://huggingface.co/datasets/Felix92/docTR-multilingual-data-collection">Hugging Face dataset</a></h3>

        <br>
        <br>

        <h3>By uploading a document, you explicitly agree to the following terms:</h3>

        <h3>1. You affirm that you are the owner or have the necessary rights to upload and share the document.</h3>

        <h3>2. You agree that the uploaded document will be made publicly available to everyone.</h3>

        <h3>3. You agree that the uploaded document can be used for any purpose, including commercial use, by any third party.</h3>
        """
    )

    agree_button = gr.Button("I Agree to the Terms and Conditions")
    agree_state = gr.State(value=False)  # State to store agreement status

    with gr.Column(visible=False) as upload_section:
        success_message = gr.Markdown(visible=True)
        gr.Markdown("Upload a document via camera capture.")
        camera_upload = gr.Image(label="Upload Image [JPG | PNG] via camera", type="filepath", sources=["webcam"], mirror_webcam=False)
        gr.Markdown("Upload a document via file upload.")
        file_upload = gr.File(label="Upload File [JPG | PNG | PDF]", file_types=["pdf", "jpg", "png"], type="filepath")
        submit_button = gr.Button("Submit")

    def toggle_agreement_visibility():
        return gr.update(visible=False), gr.update(visible=False), True, gr.update(visible=True)

    # Clicking the "I Agree" button hides the agreement and shows the upload section
    agree_button.click(fn=toggle_agreement_visibility, inputs=None, outputs=[agreement_markdown, agree_button, agree_state, upload_section])

    submit_button.click(fn=upload_to_hub, inputs=[file_upload, camera_upload, agree_state], outputs=[agree_button, success_message, file_upload, camera_upload])

if __name__ == "__main__":
    demo.launch()