File size: 3,359 Bytes
3d6a12e
 
 
 
 
2cbbc23
 
465300f
3d6a12e
465300f
 
 
 
3d6a12e
 
 
f397951
 
 
 
 
 
3d6a12e
 
31610d3
71965fb
31610d3
 
 
71965fb
31610d3
 
 
71965fb
 
 
31610d3
 
 
 
 
2e4eb94
 
 
 
31610d3
 
2e4eb94
3d6a12e
 
 
 
 
 
71965fb
3d6a12e
 
 
 
 
 
31610d3
 
 
 
 
 
 
 
 
2e4eb94
 
 
465300f
2e4eb94
 
 
3d6a12e
 
 
2e4eb94
 
3d6a12e
 
4e71b4d
 
 
 
3d6a12e
71965fb
 
 
 
5c028cc
71965fb
3d6a12e
2e4eb94
31610d3
 
2e4eb94
3d6a12e
 
 
 
 
2e4eb94
3d6a12e
 
 
 
 
 
5c028cc
3d6a12e
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import httpx
import gradio as gr


def submit(inputs):
    if not inputs:
        return

    payload = {"content": inputs, "author": "anna nymous"}
    try:
        httpx.post("http://localhost:8080/submit/", json=payload)
    except httpx.ConnectError:
        pass


def check_status():
    try:
        response = httpx.get("http://localhost:8080/check_job_status/")
        result = response.json()
    except httpx.ConnectError:
        result = "Server could not be reached"
    return result


def check_tags():
    url = "http://localhost:8080/tag_counts/"
    try:
        tag_counts = {
            key.strip("#"): val
            for key, val in httpx.get(url).json().items()
            if key.strip("#")
        }
        sorted_tags = sorted(list(tag_counts.items()), key=lambda tup: -tup[1])
        result = "Most common tags: " + ", ".join(
            f"{t} ({c})" for t, c in sorted_tags[:5]
        )
    except httpx.ConnectError:
        result = ""
    return result


def get_results(inputs: list[str]):
    if not inputs:
        response = httpx.get("http://localhost:8080/recent/")
    else:
        tags = inputs.replace(",", " ").split()
        tags = [tag.lstrip("#") for tag in tags]
        response = httpx.get("http://localhost:8080/recent/" + ",".join(tags))
    entries = response.json()
    texts: list[str] = []
    for i, entry in enumerate(entries, start=1):
        texts.append(
            f"## {i}. author: {entry['author']}\n\n"
            f"Date: _{entry['date']}_\n\n"
            f"Source: _{entry['source_snippet']}_\n\n"
            f"**Summary**: {entry['summary']}\n\n"
            f"tags: _{' '.join(entry['tags'])}_"
        )
    return "\n\n---\n\n".join(texts)


INPUT_DESCRIPTION = """# Gistillery

The no-fuss knowledge dump.

Paste information into the field below and submit, _Gistillery_ will create a
short summary and hashtags for it. Then show results for most recent entries,
optionally filtered by hashtag.

Input currently supports:
- plain text
- a URL to a webpage
- a URL to a youtube video (the video will be transcribed)
- a URL to an image (url ending in .png, .jpg, etc.; the image description will be used)
"""


def get_demo():
    with gr.Blocks() as demo:
        # submit new input
        gr.Markdown(INPUT_DESCRIPTION)
        inputs = gr.Textbox(lines=3, label="Input")
        btn_submit = gr.Button("Submit")

        # clear text box
        btn_submit.click(lambda x: gr.update(value=''), [], [inputs])
        inputs.submit(lambda x: gr.update(value=''), [], [inputs])

        # check job status
        gr.Markdown(
            "Processing can take a couple of minutes, the info box below gives an "
            "update on how many jobs are in the queue."
        )
        gr.HTML(value=check_status, label="Status", every=3)
        gr.HTML(value=check_tags, label="Status", every=9)

        # check box of tags to filter on
        msg = "Enter hashtags to filter on (comma separated if multiple)"
        tags = gr.Textbox(label=msg)

        # display output
        btn_output = gr.Button("Show results")
        output = gr.Markdown()

        btn_submit.click(submit, inputs=inputs)
        btn_output.click(get_results, inputs=[tags], outputs=[output])

    return demo


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