File size: 1,354 Bytes
1a5e5f8
883ac11
 
1a5e5f8
19d0356
 
6df5b31
 
8f55818
 
883ac11
8f55818
872ae6a
8f55818
883ac11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8f55818
6df5b31
 
883ac11
 
 
 
6df5b31
1a5e5f8
19d0356
883ac11
 
872ae6a
883ac11
 
19d0356
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
import json
import os
import requests

import gradio as gr

from autocards.autocards import Autocards

AUTO = Autocards()

def cardify(content, mode):
    global AUTO

    AUTO.clear_qa()
    if mode != "raw" and mode != "url":
        r = requests.get(content)
        if mode == "pdf":
            with open("temp.pdf", 'wb') as f:
                f.write(r.content) 
            AUTO.consume_pdf(content)
            os.remove("temp.pdf")
        elif mode == "epub":
            with open("temp.epub", 'wb') as f:
                f.write(r.content)
            AUTO.consume_epub(content)
            os.remove("temp.epub")
        elif mode == "textfile":
            with open("temp.txt", 'w') as f:
                f.write(r.text)
            AUTO.consume_textfile(content)
            os.remove("temp.txt")
    else:
        if mode == "raw":
            AUTO.consume_var(content)
        elif mode == "url":
            AUTO.consume_web(content)

    AUTO.to_json("output.json", prefix="")
    with open("output_basic.json") as f:
        res = json.load(f)
    
    # delete the output files
    os.remove("output_basic.json")
    os.remove("output_cloze.json")

    return json.dumps(res)

iface = gr.Interface(
    fn=cardify,
    inputs=["text", gr.inputs.Radio(["raw", "epub", "pdf", "textfile", "url"])],
    outputs="text"
)
iface.launch()