import argparse import json import os import requests import gradio as gr from utils import DocAction def parseArgs(): parser = argparse.ArgumentParser() parser.add_argument("--http_host", default="0.0.0.0") parser.add_argument("--http_port", type=int, default=13666) return parser.parse_args() def get_response(data, url): headers = {"Content-type": "application/json"} response = requests.post(url, json=data, headers=headers) response = json.loads(response.content) return response def add_text(history, text): history = history + [(text, None)] return history, gr.update(value=None, interactive=True) def add_file(history, files): files_string = "\n".join([os.path.basename(file.name) for file in files]) doc_files = [file.name for file in files] data = { "doc_files": doc_files, "action": DocAction.ADD } response = get_response(data, update_url)["response"] history = history + [(files_string, response)] return history def bot(history): data = { "user_input": history[-1][0].strip() } response = get_response(data, gen_url) if response["error"] != "": raise gr.Error(response["error"]) history[-1][1] = response["response"] yield history def restart(chatbot, txt): # Reset the conversation state and clear the chat history data = { "doc_files": "", "action": DocAction.CLEAR } response = get_response(data, update_url) return gr.update(value=None), gr.update(value=None, interactive=True) CSS = """ .contain { display: flex; flex-direction: column; height: 100vh } #component-0 { height: 100%; } #chatbot { flex-grow: 1; } """ header_html = """