Spaces:
Sleeping
Sleeping
Delete src/web_ui.py
Browse files- src/web_ui.py +0 -184
src/web_ui.py
DELETED
|
@@ -1,184 +0,0 @@
|
|
| 1 |
-
from response_parser import *
|
| 2 |
-
import gradio as gr
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
def initialization(state_dict: Dict) -> None:
|
| 6 |
-
if not os.path.exists('cache'):
|
| 7 |
-
os.mkdir('cache')
|
| 8 |
-
if state_dict["bot_backend"] is None:
|
| 9 |
-
state_dict["bot_backend"] = BotBackend()
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
def get_bot_backend(state_dict: Dict) -> BotBackend:
|
| 13 |
-
return state_dict["bot_backend"]
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
def switch_to_gpt4(state_dict: Dict, whether_switch: bool) -> None:
|
| 17 |
-
bot_backend = get_bot_backend(state_dict)
|
| 18 |
-
if whether_switch:
|
| 19 |
-
bot_backend.update_gpt_model_choice("GPT-4")
|
| 20 |
-
else:
|
| 21 |
-
bot_backend.update_gpt_model_choice("GPT-3.5")
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
def add_text(state_dict: Dict, history: List, text: str) -> Tuple[List, Dict]:
|
| 25 |
-
bot_backend = get_bot_backend(state_dict)
|
| 26 |
-
bot_backend.add_text_message(user_text=text)
|
| 27 |
-
|
| 28 |
-
history = history + [(text, None)]
|
| 29 |
-
|
| 30 |
-
return history, gr.update(value="", interactive=False)
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
def add_file(state_dict: Dict, history: List, file) -> List:
|
| 34 |
-
bot_backend = get_bot_backend(state_dict)
|
| 35 |
-
path = file.name
|
| 36 |
-
filename = os.path.basename(path)
|
| 37 |
-
|
| 38 |
-
bot_msg = [f'📁[{filename}]', None]
|
| 39 |
-
history.append(bot_msg)
|
| 40 |
-
|
| 41 |
-
bot_backend.add_file_message(path=path, bot_msg=bot_msg)
|
| 42 |
-
|
| 43 |
-
return history
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
def undo_upload_file(state_dict: Dict, history: List) -> Tuple[List, Dict]:
|
| 47 |
-
bot_backend = get_bot_backend(state_dict)
|
| 48 |
-
bot_msg = bot_backend.revoke_file()
|
| 49 |
-
|
| 50 |
-
if bot_msg is None:
|
| 51 |
-
return history, gr.Button.update(interactive=False)
|
| 52 |
-
|
| 53 |
-
else:
|
| 54 |
-
assert history[-1] == bot_msg
|
| 55 |
-
del history[-1]
|
| 56 |
-
if bot_backend.revocable_files:
|
| 57 |
-
return history, gr.Button.update(interactive=True)
|
| 58 |
-
else:
|
| 59 |
-
return history, gr.Button.update(interactive=False)
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
def refresh_file_display(state_dict: Dict) -> List[str]:
|
| 63 |
-
bot_backend = get_bot_backend(state_dict)
|
| 64 |
-
work_dir = bot_backend.jupyter_work_dir
|
| 65 |
-
filenames = os.listdir(work_dir)
|
| 66 |
-
paths = []
|
| 67 |
-
for filename in filenames:
|
| 68 |
-
paths.append(
|
| 69 |
-
os.path.join(work_dir, filename)
|
| 70 |
-
)
|
| 71 |
-
return paths
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
def restart_ui(history: List) -> Tuple[List, Dict, Dict, Dict, Dict]:
|
| 75 |
-
history.clear()
|
| 76 |
-
return (
|
| 77 |
-
history,
|
| 78 |
-
gr.Textbox.update(value="", interactive=False),
|
| 79 |
-
gr.Button.update(interactive=False),
|
| 80 |
-
gr.Button.update(interactive=False),
|
| 81 |
-
gr.Button.update(interactive=False)
|
| 82 |
-
)
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
def restart_bot_backend(state_dict: Dict) -> None:
|
| 86 |
-
bot_backend = get_bot_backend(state_dict)
|
| 87 |
-
bot_backend.restart()
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
def bot(state_dict: Dict, history: List) -> List:
|
| 91 |
-
bot_backend = get_bot_backend(state_dict)
|
| 92 |
-
|
| 93 |
-
while bot_backend.finish_reason in ('new_input', 'function_call'):
|
| 94 |
-
if history[-1][0] is None:
|
| 95 |
-
history.append(
|
| 96 |
-
[None, ""]
|
| 97 |
-
)
|
| 98 |
-
else:
|
| 99 |
-
history[-1][1] = ""
|
| 100 |
-
|
| 101 |
-
response = chat_completion(bot_backend=bot_backend)
|
| 102 |
-
for chunk in response:
|
| 103 |
-
history, weather_exit = parse_response(
|
| 104 |
-
chunk=chunk,
|
| 105 |
-
history=history,
|
| 106 |
-
bot_backend=bot_backend
|
| 107 |
-
)
|
| 108 |
-
yield history
|
| 109 |
-
if weather_exit:
|
| 110 |
-
exit(-1)
|
| 111 |
-
|
| 112 |
-
yield history
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
if __name__ == '__main__':
|
| 116 |
-
config = get_config()
|
| 117 |
-
with gr.Blocks(theme=gr.themes.Base()) as block:
|
| 118 |
-
"""
|
| 119 |
-
Reference: https://www.gradio.app/guides/creating-a-chatbot-fast
|
| 120 |
-
"""
|
| 121 |
-
# UI components
|
| 122 |
-
state = gr.State(value={"bot_backend": None})
|
| 123 |
-
with gr.Tab("Chat"):
|
| 124 |
-
chatbot = gr.Chatbot([], elem_id="chatbot", label="Local Code Interpreter", height=750)
|
| 125 |
-
with gr.Row():
|
| 126 |
-
with gr.Column(scale=0.85):
|
| 127 |
-
text_box = gr.Textbox(
|
| 128 |
-
show_label=False,
|
| 129 |
-
placeholder="Enter text and press enter, or upload a file",
|
| 130 |
-
container=False
|
| 131 |
-
)
|
| 132 |
-
with gr.Column(scale=0.15, min_width=0):
|
| 133 |
-
file_upload_button = gr.UploadButton("📁", file_types=['file'])
|
| 134 |
-
|
| 135 |
-
with gr.Row(equal_height=True):
|
| 136 |
-
with gr.Column(scale=0.7):
|
| 137 |
-
check_box = gr.Checkbox(label="Use GPT-4", interactive=config['model']['GPT-4']['available'])
|
| 138 |
-
check_box.change(fn=switch_to_gpt4, inputs=[state, check_box])
|
| 139 |
-
with gr.Column(scale=0.15, min_width=0):
|
| 140 |
-
restart_button = gr.Button(value='🔄 Restart')
|
| 141 |
-
with gr.Column(scale=0.15, min_width=0):
|
| 142 |
-
undo_file_button = gr.Button(value="↩️Undo upload file", interactive=False)
|
| 143 |
-
with gr.Tab("Files"):
|
| 144 |
-
file_output = gr.Files()
|
| 145 |
-
|
| 146 |
-
# Components function binding
|
| 147 |
-
txt_msg = text_box.submit(add_text, [state, chatbot, text_box], [chatbot, text_box], queue=False).then(
|
| 148 |
-
bot, [state, chatbot], chatbot
|
| 149 |
-
)
|
| 150 |
-
txt_msg.then(fn=refresh_file_display, inputs=[state], outputs=[file_output])
|
| 151 |
-
txt_msg.then(lambda: gr.update(interactive=True), None, [text_box], queue=False)
|
| 152 |
-
txt_msg.then(lambda: gr.Button.update(interactive=False), None, [undo_file_button], queue=False)
|
| 153 |
-
|
| 154 |
-
file_msg = file_upload_button.upload(
|
| 155 |
-
add_file, [state, chatbot, file_upload_button], [chatbot], queue=False
|
| 156 |
-
).then(
|
| 157 |
-
bot, [state, chatbot], chatbot
|
| 158 |
-
)
|
| 159 |
-
file_msg.then(lambda: gr.Button.update(interactive=True), None, [undo_file_button], queue=False)
|
| 160 |
-
file_msg.then(fn=refresh_file_display, inputs=[state], outputs=[file_output])
|
| 161 |
-
|
| 162 |
-
undo_file_button.click(
|
| 163 |
-
fn=undo_upload_file, inputs=[state, chatbot], outputs=[chatbot, undo_file_button]
|
| 164 |
-
).then(
|
| 165 |
-
fn=refresh_file_display, inputs=[state], outputs=[file_output]
|
| 166 |
-
)
|
| 167 |
-
|
| 168 |
-
restart_button.click(
|
| 169 |
-
fn=restart_ui, inputs=[chatbot],
|
| 170 |
-
outputs=[chatbot, text_box, restart_button, file_upload_button, undo_file_button]
|
| 171 |
-
).then(
|
| 172 |
-
fn=restart_bot_backend, inputs=[state], queue=False
|
| 173 |
-
).then(
|
| 174 |
-
fn=refresh_file_display, inputs=[state], outputs=[file_output]
|
| 175 |
-
).then(
|
| 176 |
-
fn=lambda: (gr.Textbox.update(interactive=True), gr.Button.update(interactive=True),
|
| 177 |
-
gr.Button.update(interactive=True)),
|
| 178 |
-
inputs=None, outputs=[text_box, restart_button, file_upload_button], queue=False
|
| 179 |
-
)
|
| 180 |
-
|
| 181 |
-
block.load(fn=initialization, inputs=[state])
|
| 182 |
-
|
| 183 |
-
block.queue()
|
| 184 |
-
block.launch(inbrowser=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|