Hashintha commited on
Commit
585f6e4
·
1 Parent(s): e517895

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +3 -103
app.py CHANGED
@@ -2,9 +2,6 @@ from response_parser import *
2
  import gradio as gr
3
  import os
4
 
5
- USERNAME = os.getenv('USERNAME')
6
- PASSWORD = os.getenv('PASSWORD')
7
-
8
 
9
  def initialization(state_dict: Dict) -> None:
10
  if not os.path.exists('cache'):
@@ -19,12 +16,6 @@ def get_bot_backend(state_dict: Dict) -> BotBackend:
19
  return state_dict["bot_backend"]
20
 
21
 
22
- def switch_to_gpt4(state_dict: Dict, whether_switch: bool) -> None:
23
- bot_backend = get_bot_backend(state_dict)
24
- if whether_switch:
25
- bot_backend.update_gpt_model_choice("GPT-4")
26
- else:
27
- bot_backend.update_gpt_model_choice("GPT-3.5")
28
 
29
 
30
  def add_text(state_dict: Dict, history: List, text: str) -> Tuple[List, Dict]:
@@ -36,62 +27,6 @@ def add_text(state_dict: Dict, history: List, text: str) -> Tuple[List, Dict]:
36
  return history, gr.update(value="", interactive=False)
37
 
38
 
39
- def add_file(state_dict: Dict, history: List, file) -> List:
40
- bot_backend = get_bot_backend(state_dict)
41
- path = file.name
42
- filename = os.path.basename(path)
43
-
44
- bot_msg = [f'📁[{filename}]', None]
45
- history.append(bot_msg)
46
-
47
- bot_backend.add_file_message(path=path, bot_msg=bot_msg)
48
-
49
- return history
50
-
51
-
52
- def undo_upload_file(state_dict: Dict, history: List) -> Tuple[List, Dict]:
53
- bot_backend = get_bot_backend(state_dict)
54
- bot_msg = bot_backend.revoke_file()
55
-
56
- if bot_msg is None:
57
- return history, gr.Button.update(interactive=False)
58
-
59
- else:
60
- assert history[-1] == bot_msg
61
- del history[-1]
62
- if bot_backend.revocable_files:
63
- return history, gr.Button.update(interactive=True)
64
- else:
65
- return history, gr.Button.update(interactive=False)
66
-
67
-
68
- def refresh_file_display(state_dict: Dict) -> List[str]:
69
- bot_backend = get_bot_backend(state_dict)
70
- work_dir = bot_backend.jupyter_work_dir
71
- filenames = os.listdir(work_dir)
72
- paths = []
73
- for filename in filenames:
74
- paths.append(
75
- os.path.join(work_dir, filename)
76
- )
77
- return paths
78
-
79
-
80
- def restart_ui(history: List) -> Tuple[List, Dict, Dict, Dict, Dict]:
81
- history.clear()
82
- return (
83
- history,
84
- gr.Textbox.update(value="", interactive=False),
85
- gr.Button.update(interactive=False),
86
- gr.Button.update(interactive=False),
87
- gr.Button.update(interactive=False)
88
- )
89
-
90
-
91
- def restart_bot_backend(state_dict: Dict) -> None:
92
- bot_backend = get_bot_backend(state_dict)
93
- bot_backend.restart()
94
-
95
 
96
  def bot(state_dict: Dict, history: List) -> List:
97
  bot_backend = get_bot_backend(state_dict)
@@ -135,14 +70,8 @@ if __name__ == '__main__':
135
  placeholder="Enter text and press enter, or upload a file",
136
  container=False
137
  )
138
- with gr.Column(scale=0.15, min_width=0):
139
- file_upload_button = gr.UploadButton("📁", file_types=['file'])
140
  with gr.Row(equal_height=True):
141
- with gr.Column(scale=0.7):
142
- check_box = gr.Checkbox(label="Use GPT-4", interactive=config['model']['GPT-4']['available'])
143
- check_box.change(fn=switch_to_gpt4, inputs=[state, check_box])
144
- with gr.Column(scale=0.15, min_width=0):
145
- restart_button = gr.Button(value='🔄 Restart')
146
  with gr.Column(scale=0.15, min_width=0):
147
  undo_file_button = gr.Button(value="↩️Undo upload file", interactive=False)
148
  with gr.Tab("Files"):
@@ -152,38 +81,9 @@ if __name__ == '__main__':
152
  txt_msg = text_box.submit(add_text, [state, chatbot, text_box], [chatbot, text_box], queue=False).then(
153
  bot, [state, chatbot], chatbot
154
  )
155
- txt_msg.then(fn=refresh_file_display, inputs=[state], outputs=[file_output])
156
- txt_msg.then(lambda: gr.update(interactive=True), None, [text_box], queue=False)
157
- txt_msg.then(lambda: gr.Button.update(interactive=False), None, [undo_file_button], queue=False)
158
-
159
- file_msg = file_upload_button.upload(
160
- add_file, [state, chatbot, file_upload_button], [chatbot], queue=False
161
- ).then(
162
- bot, [state, chatbot], chatbot
163
- )
164
- file_msg.then(lambda: gr.Button.update(interactive=True), None, [undo_file_button], queue=False)
165
- file_msg.then(fn=refresh_file_display, inputs=[state], outputs=[file_output])
166
-
167
- undo_file_button.click(
168
- fn=undo_upload_file, inputs=[state, chatbot], outputs=[chatbot, undo_file_button]
169
- ).then(
170
- fn=refresh_file_display, inputs=[state], outputs=[file_output]
171
- )
172
-
173
- restart_button.click(
174
- fn=restart_ui, inputs=[chatbot],
175
- outputs=[chatbot, text_box, restart_button, file_upload_button, undo_file_button]
176
- ).then(
177
- fn=restart_bot_backend, inputs=[state], queue=False
178
- ).then(
179
- fn=refresh_file_display, inputs=[state], outputs=[file_output]
180
- ).then(
181
- fn=lambda: (gr.Textbox.update(interactive=True), gr.Button.update(interactive=True),
182
- gr.Button.update(interactive=True)),
183
- inputs=None, outputs=[text_box, restart_button, file_upload_button], queue=False
184
- )
185
 
186
  block.load(fn=initialization, inputs=[state])
187
 
188
  block.queue()
189
- block.launch(inbrowser=True , auth=(USERNAME, PASSWORD))
 
2
  import gradio as gr
3
  import os
4
 
 
 
 
5
 
6
  def initialization(state_dict: Dict) -> None:
7
  if not os.path.exists('cache'):
 
16
  return state_dict["bot_backend"]
17
 
18
 
 
 
 
 
 
 
19
 
20
 
21
  def add_text(state_dict: Dict, history: List, text: str) -> Tuple[List, Dict]:
 
27
  return history, gr.update(value="", interactive=False)
28
 
29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
  def bot(state_dict: Dict, history: List) -> List:
32
  bot_backend = get_bot_backend(state_dict)
 
70
  placeholder="Enter text and press enter, or upload a file",
71
  container=False
72
  )
 
 
73
  with gr.Row(equal_height=True):
74
+
 
 
 
 
75
  with gr.Column(scale=0.15, min_width=0):
76
  undo_file_button = gr.Button(value="↩️Undo upload file", interactive=False)
77
  with gr.Tab("Files"):
 
81
  txt_msg = text_box.submit(add_text, [state, chatbot, text_box], [chatbot, text_box], queue=False).then(
82
  bot, [state, chatbot], chatbot
83
  )
84
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
 
86
  block.load(fn=initialization, inputs=[state])
87
 
88
  block.queue()
89
+ block.launch(inbrowser=True)