Spaces:
Runtime error
Runtime error
Update web_ui.py
Browse files
web_ui.py
CHANGED
|
@@ -2,6 +2,8 @@ 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')
|
|
@@ -74,7 +76,7 @@ def refresh_file_display(state_dict: Dict) -> List[str]:
|
|
| 74 |
|
| 75 |
|
| 76 |
def restart_ui(history: List) -> Tuple[List, Dict, Dict, Dict, Dict]:
|
| 77 |
-
history.
|
| 78 |
return (
|
| 79 |
history,
|
| 80 |
gr.Textbox.update(value="", interactive=False),
|
|
@@ -95,7 +97,7 @@ def bot(state_dict: Dict, history: List) -> List:
|
|
| 95 |
while bot_backend.finish_reason in ('new_input', 'function_call'):
|
| 96 |
if history[-1][0] is None:
|
| 97 |
history.append(
|
| 98 |
-
[
|
| 99 |
)
|
| 100 |
else:
|
| 101 |
history[-1][1] = ""
|
|
@@ -116,33 +118,150 @@ def bot(state_dict: Dict, history: List) -> List:
|
|
| 116 |
|
| 117 |
if __name__ == '__main__':
|
| 118 |
config = get_config()
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
"""
|
| 121 |
Reference: https://www.gradio.app/guides/creating-a-chatbot-fast
|
| 122 |
"""
|
| 123 |
# UI components
|
| 124 |
state = gr.State(value={"bot_backend": None})
|
| 125 |
-
with gr.
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
)
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
|
| 147 |
# Components function binding
|
| 148 |
txt_msg = text_box.submit(add_text, [state, chatbot, text_box], [chatbot, text_box], queue=False).then(
|
|
@@ -150,36 +269,17 @@ if __name__ == '__main__':
|
|
| 150 |
)
|
| 151 |
txt_msg.then(fn=refresh_file_display, inputs=[state], outputs=[file_output])
|
| 152 |
txt_msg.then(lambda: gr.update(interactive=True), None, [text_box], queue=False)
|
| 153 |
-
txt_msg.then(lambda: gr.Button.update(interactive=False), None,
|
| 154 |
|
| 155 |
file_msg = file_upload_button.upload(
|
| 156 |
add_file, [state, chatbot, file_upload_button], [chatbot], queue=False
|
| 157 |
).then(
|
| 158 |
bot, [state, chatbot], chatbot
|
| 159 |
)
|
| 160 |
-
file_msg.then(lambda: gr.Button.update(interactive=True), None,
|
| 161 |
file_msg.then(fn=refresh_file_display, inputs=[state], outputs=[file_output])
|
| 162 |
|
| 163 |
-
|
| 164 |
-
fn=undo_upload_file, inputs=[state, chatbot], outputs=[chatbot, undo_file_button]
|
| 165 |
-
).then(
|
| 166 |
-
fn=refresh_file_display, inputs=[state], outputs=[file_output]
|
| 167 |
-
)
|
| 168 |
-
|
| 169 |
-
restart_button.click(
|
| 170 |
-
fn=restart_ui, inputs=[chatbot],
|
| 171 |
-
outputs=[chatbot, text_box, restart_button, file_upload_button, undo_file_button]
|
| 172 |
-
).then(
|
| 173 |
-
fn=restart_bot_backend, inputs=[state], queue=False
|
| 174 |
-
).then(
|
| 175 |
-
fn=refresh_file_display, inputs=[state], outputs=[file_output]
|
| 176 |
-
).then(
|
| 177 |
-
fn=lambda: (gr.Textbox.update(interactive=True), gr.Button.update(interactive=True),
|
| 178 |
-
gr.Button.update(interactive=True)),
|
| 179 |
-
inputs=None, outputs=[text_box, restart_button, file_upload_button], queue=False
|
| 180 |
-
)
|
| 181 |
-
|
| 182 |
-
block.load(fn=initialization, inputs=[state])
|
| 183 |
|
| 184 |
block.queue()
|
| 185 |
block.launch(inbrowser=True)
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
|
| 4 |
|
| 5 |
+
|
| 6 |
+
|
| 7 |
def initialization(state_dict: Dict) -> None:
|
| 8 |
if not os.path.exists('cache'):
|
| 9 |
os.mkdir('cache')
|
|
|
|
| 76 |
|
| 77 |
|
| 78 |
def restart_ui(history: List) -> Tuple[List, Dict, Dict, Dict, Dict]:
|
| 79 |
+
history.append(["Welcome to the chatbot!", ""])
|
| 80 |
return (
|
| 81 |
history,
|
| 82 |
gr.Textbox.update(value="", interactive=False),
|
|
|
|
| 97 |
while bot_backend.finish_reason in ('new_input', 'function_call'):
|
| 98 |
if history[-1][0] is None:
|
| 99 |
history.append(
|
| 100 |
+
['welcome', ""]
|
| 101 |
)
|
| 102 |
else:
|
| 103 |
history[-1][1] = ""
|
|
|
|
| 118 |
|
| 119 |
if __name__ == '__main__':
|
| 120 |
config = get_config()
|
| 121 |
+
custom_css = """
|
| 122 |
+
.gradio-container {
|
| 123 |
+
background-color: white ;
|
| 124 |
+
margin: 0 !important ;
|
| 125 |
+
padding: 0 !important ;
|
| 126 |
+
space : 0
|
| 127 |
+
}
|
| 128 |
+
|
| 129 |
+
#mainDiv {
|
| 130 |
+
width :100%;
|
| 131 |
+
border : none;
|
| 132 |
+
height: 100vh ;
|
| 133 |
+
}
|
| 134 |
+
|
| 135 |
+
#chatbot_div{
|
| 136 |
+
padding : 10px
|
| 137 |
+
}
|
| 138 |
+
|
| 139 |
+
#chatbot {
|
| 140 |
+
border-color: #a6a6a6 ;
|
| 141 |
+
background-color: white ;
|
| 142 |
+
border-radius: 5px;
|
| 143 |
+
}
|
| 144 |
+
|
| 145 |
+
#sidebar {
|
| 146 |
+
background-color: #f2f2f2;
|
| 147 |
+
padding : 5px ;
|
| 148 |
+
}
|
| 149 |
+
|
| 150 |
+
#files {
|
| 151 |
+
|
| 152 |
+
height : 60% ;
|
| 153 |
+
color : #f2f2f2 ;
|
| 154 |
+
background-color : #f2f2f2 ;
|
| 155 |
+
border : none
|
| 156 |
+
}
|
| 157 |
+
|
| 158 |
+
#gpt4_button {
|
| 159 |
+
border-radius: 0px ;
|
| 160 |
+
background-color : #0CAFFF ;
|
| 161 |
+
padding : 10px ;
|
| 162 |
+
}
|
| 163 |
+
|
| 164 |
+
#gpt4_button:hover {
|
| 165 |
+
background-color : #0ca6ff ;
|
| 166 |
+
}
|
| 167 |
+
|
| 168 |
+
#textbox {
|
| 169 |
+
border-color : #a6a6a6 ;
|
| 170 |
+
background-color : white ;
|
| 171 |
+
}
|
| 172 |
+
|
| 173 |
+
#textbox:hover {
|
| 174 |
+
border-color : black ;
|
| 175 |
+
}
|
| 176 |
+
|
| 177 |
+
#upload_button {
|
| 178 |
+
background-color : #b3b3b3;
|
| 179 |
+
}
|
| 180 |
+
|
| 181 |
+
#upload_button:hover {
|
| 182 |
+
background-color : #8c8c8c ;
|
| 183 |
+
box-shadow: 0 12px 16px 0 rgba(0,0,0,0.24),0 17px 50px 0 rgba(0,0,0,0.19);
|
| 184 |
+
}
|
| 185 |
+
|
| 186 |
+
/* width */
|
| 187 |
+
::-webkit-scrollbar {
|
| 188 |
+
width: 4px;
|
| 189 |
+
}
|
| 190 |
+
|
| 191 |
+
/* Track */
|
| 192 |
+
::-webkit-scrollbar-track {
|
| 193 |
+
background: #f1f1f1;
|
| 194 |
+
}
|
| 195 |
+
|
| 196 |
+
/* Handle */
|
| 197 |
+
::-webkit-scrollbar-thumb {
|
| 198 |
+
background: #0CAFFF;
|
| 199 |
+
}
|
| 200 |
+
|
| 201 |
+
/* Handle on hover */
|
| 202 |
+
::-webkit-scrollbar-thumb:hover {
|
| 203 |
+
background: #555;
|
| 204 |
+
}
|
| 205 |
+
|
| 206 |
+
footer{display:none !important}
|
| 207 |
+
|
| 208 |
+
"""
|
| 209 |
+
|
| 210 |
+
javascript_code = """
|
| 211 |
+
function get_browser_height() {
|
| 212 |
+
return window.innerHeight;
|
| 213 |
+
}
|
| 214 |
+
"""
|
| 215 |
+
with gr.Blocks(theme=gr.themes.Base(),css= custom_css) as block:
|
| 216 |
"""
|
| 217 |
Reference: https://www.gradio.app/guides/creating-a-chatbot-fast
|
| 218 |
"""
|
| 219 |
# UI components
|
| 220 |
state = gr.State(value={"bot_backend": None})
|
| 221 |
+
with gr.Row( elem_id="mainDiv"):
|
| 222 |
+
with gr.Column( elem_id="sidebar" , scale=0.20):
|
| 223 |
+
sidebar_header = gr.HTML("<h1 style='color:black; font-weight:bold; text-align:center; margin-top : 10px ;'>DMO-GPT-Interpreter</h1>")
|
| 224 |
+
hr_linr = gr.HTML("<hr/>")
|
| 225 |
+
file_section = gr.HTML("<h2 style='color:gray; font-weight:bold; text-align:start; margin-top : 5px ; font-size: 18px ; margin-bottom: -3px'>Files</h2>")
|
| 226 |
+
file_output = gr.Files(elem_id="files", show_label=False )
|
| 227 |
+
setting_section = gr.HTML("<h2 style='color:gray; font-weight:bold; text-align:start; margin-top : 5px ; font-size: 18px ; margin-bottom: -3px'>Settings</h2>")
|
| 228 |
+
check_box = gr.Checkbox(label="Use with GPT-4 ✨", interactive=config['model']['GPT-4']['available'] , elem_id="gpt4_button" , scale=2)
|
| 229 |
+
check_box.change(fn=switch_to_gpt4, inputs=[state, check_box])
|
| 230 |
+
with gr.Column( elem_id="chatbot_div"):
|
| 231 |
+
chatbot = gr.Chatbot([], elem_id="chatbot", height=600 , label="Welcome to DMO-GPT-Interpreter!")
|
| 232 |
+
with gr.Row():
|
| 233 |
+
with gr.Column(scale=0.85 ):
|
| 234 |
+
text_box = gr.Textbox(
|
| 235 |
+
show_label=False,
|
| 236 |
+
placeholder="Enter text and press enter, or upload a file",
|
| 237 |
+
container=False,
|
| 238 |
+
elem_id='textbox'
|
| 239 |
)
|
| 240 |
+
with gr.Column(scale=0.15, min_width=0):
|
| 241 |
+
file_upload_button = gr.UploadButton("📁 Upload log files", file_types=['file'] , elem_id="upload_button")
|
| 242 |
+
|
| 243 |
+
# with gr.Tab("Chat"):
|
| 244 |
+
# chatbot = gr.Chatbot([], elem_id="chatbot", label="Local Code Interpreter", height=500)
|
| 245 |
+
# with gr.Row():
|
| 246 |
+
# with gr.Column(scale=0.85 ):
|
| 247 |
+
# text_box = gr.Textbox(
|
| 248 |
+
# show_label=False,
|
| 249 |
+
# placeholder="Enter text and press enter, or upload a file",
|
| 250 |
+
# container=False
|
| 251 |
+
#
|
| 252 |
+
# )
|
| 253 |
+
# with gr.Column(scale=0.15, min_width=0):
|
| 254 |
+
# file_upload_button = gr.UploadButton("📁", file_types=['file'])
|
| 255 |
+
# with gr.Row(equal_height=True):
|
| 256 |
+
# with gr.Column(scale=0.7):
|
| 257 |
+
# check_box = gr.Checkbox(label="Use GPT-4", interactive=config['model']['GPT-4']['available'])
|
| 258 |
+
# check_box.change(fn=switch_to_gpt4, inputs=[state, check_box])
|
| 259 |
+
# with gr.Column(scale=0.15, min_width=0):
|
| 260 |
+
# restart_button = gr.Button(value='🔄 Restart')
|
| 261 |
+
# with gr.Column(scale=0.15, min_width=0):
|
| 262 |
+
# undo_file_button = gr.Button(value="↩️Undo upload file", interactive=False)
|
| 263 |
+
# with gr.Tab("Files"):
|
| 264 |
+
# file_output = gr.Files()
|
| 265 |
|
| 266 |
# Components function binding
|
| 267 |
txt_msg = text_box.submit(add_text, [state, chatbot, text_box], [chatbot, text_box], queue=False).then(
|
|
|
|
| 269 |
)
|
| 270 |
txt_msg.then(fn=refresh_file_display, inputs=[state], outputs=[file_output])
|
| 271 |
txt_msg.then(lambda: gr.update(interactive=True), None, [text_box], queue=False)
|
| 272 |
+
txt_msg.then(lambda: gr.Button.update(interactive=False), None, queue=False)
|
| 273 |
|
| 274 |
file_msg = file_upload_button.upload(
|
| 275 |
add_file, [state, chatbot, file_upload_button], [chatbot], queue=False
|
| 276 |
).then(
|
| 277 |
bot, [state, chatbot], chatbot
|
| 278 |
)
|
| 279 |
+
file_msg.then(lambda: gr.Button.update(interactive=True), None, queue=False)
|
| 280 |
file_msg.then(fn=refresh_file_display, inputs=[state], outputs=[file_output])
|
| 281 |
|
| 282 |
+
block.load(fn=initialization, inputs=[state] , _js=javascript_code)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 283 |
|
| 284 |
block.queue()
|
| 285 |
block.launch(inbrowser=True)
|