import json import os import gradio as gr import requests RAG_STATE = {"conversation_ready": False, # Conversation is not ready until files are uploaded and RAG chain is initialized "embed_model_name": os.environ.get("EMB_MODEL", "m3e"), "llm_name": os.environ.get("CHAT_LLM", "chatgpt")} URL = "http://localhost:13666" def get_response(client_data, URL): headers = {"Content-type": "application/json"} print(f"Sending request to server url: {URL}") response = requests.post(URL, data=json.dumps(client_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): global RAG_STATE RAG_STATE["conversation_ready"] = False # after adding new files, reset the ChatBot RAG_STATE["upload_files"]=[file.name for file in files] files_string = "\n".join([os.path.basename(path) for path in RAG_STATE["upload_files"]]) print(files_string) history = history + [(files_string, None)] return history def bot(history): print(history) global RAG_STATE if not RAG_STATE["conversation_ready"]: # Upload files and initialize models client_data = { "docs": RAG_STATE["upload_files"], "embed_model_name": RAG_STATE["embed_model_name"], # Select embedding model name here "llm_name": RAG_STATE["llm_name"], # Select LLM model name here. ["pangu", "chatglm2"] "conversation_ready": RAG_STATE["conversation_ready"] } else: client_data = {} client_data["conversation_ready"] = RAG_STATE["conversation_ready"] client_data["user_input"] = history[-1][0].strip() response = get_response(client_data, URL) # TODO: async request, to avoid users waiting the model initialization too long print(response) if response["error"] != "": raise gr.Error(response["error"]) RAG_STATE["conversation_ready"] = response["conversation_ready"] history[-1][1] = response["response"] yield history CSS = """ .contain { display: flex; flex-direction: column; height: 100vh } #component-0 { height: 100%; } #chatbot { flex-grow: 1; } """ header_html = """