import httpx import gradio as gr client = httpx.Client() # TODO: update the tags somehow; re-fetching inside of check_status doesn't work try: tag_counts = { key.strip("#"): val for key, val in client.get("http://localhost:8080/tag_counts/").json().items() if key.strip("#") } except httpx.ConnectError: tag_counts = {} def submit(inputs): if not inputs: return payload = {"content": inputs, "author": "anna nymous"} httpx.post("http://localhost:8080/submit/", json=payload) 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 get_results(inputs: list[str]): if not inputs: response = httpx.get("http://localhost:8080/recent/") else: tags = [tag.split(" ", 1)[0] for tag in inputs] 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"**Summary**: {entry['summary']}\n\n" f"tags: _{' '.join(entry['tags'])}_" ) return "\n\n---\n\n".join(texts) INPUT_DESCRIPTION = """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 (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") # check job status gr.HTML(value=check_status, label="Status", every=3) # check box of tags to filter on tag_choices = sorted(f"{key} ({val})" for key, val in tag_counts.items()) tags = gr.CheckboxGroup( tag_choices, label="Filter on tags (no selection = all)" ) # 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()