File size: 2,332 Bytes
839d8d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from huggingface_hub import get_repo_discussions, get_discussion_details
from Crypto.PublicKey import RSA
from Crypto.Cipher import AES, PKCS1_OAEP
import base64


def decrypt_claim(encrypted_claim_encoded, private_key_file):
    base64_encoded = encrypted_claim_encoded.split("\n")[1]
    encrypted_claim_encoded = encrypted_claim_encoded.split('\n')[1]
    b64_fixed = bytes(base64_encoded[2:-1], 'utf-8')
    encrypted_claim = base64.b64decode(b64_fixed)

    private_key = RSA.import_key(open(private_key_file.name).read())

    enc_session_key, nonce, tag, ciphertext = \
        [encrypted_claim for x in (private_key.size_in_bytes(), 16, 16, -1)]

    # Decrypt the session key with the private RSA key
    cipher_rsa = PKCS1_OAEP.new(private_key)
    session_key = cipher_rsa.decrypt(enc_session_key)

    # Decrypt the data with the AES session key
    cipher_aes = AES.new(session_key, AES.MODE_EAX, nonce)
    data = cipher_aes.decrypt_and_verify(ciphertext, tag)
    decrypted_claim = data.decode("utf-8")

    return decrypted_claim


def get_claim(claim_id, private_key):
    claims = []

    # TODO: Currently fetching ALL discussions, in case we want to build a view to see list of discussions in the claim-review space
    # for discussion in get_repo_discussions(repo_id="Team8/dataset", repo_type="dataset"):
    #     claims = claims + [discussion]
    #     # print(f"{discussion.num} - {discussion.title}, pr: {discussion.is_pull_request}")
    #
    # claim = [c for c in claims if c.num == claim_id][0]

    claim = get_discussion_details(
        repo_id="Team8/dataset",
        repo_type="dataset",
        discussion_num=int(claim_id)
    )

    return gr.Textbox.update(value=claim.title), gr.Textbox.update(value=decrypt_claim(claim.events[0].content, private_key))


demo = gr.Blocks()

with demo:
    gr.Markdown(
        """
    # Flip Text!
    Start typing below to see the output.
    """
    )
    button = gr.Button(value="Fetch Claim")
    private_key = gr.File(label="Private Key")

    claim_id = gr.Number()
    claim_title = gr.Textbox(label="Claim Title", value="")
    claim_text = gr.Textbox(label="Claim Text", value="")

    button.click(
        fn=get_claim,
        inputs=[claim_id, private_key],
        outputs=[claim_title, claim_text]
    )

demo.launch()