rwitz commited on
Commit
e9bb75c
1 Parent(s): ca86104

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -13
app.py CHANGED
@@ -11,11 +11,14 @@ enable_btn = gr.Button.update(interactive=True)
11
  with open('chatbot_urls.json', 'r') as file:
12
  chatbots = json.load(file)
13
  def clear_chat(state):
14
- if state is not None:
15
- state = {}
16
- return state, None,None,gr.Button.update(interactive=False),gr.Button.update(interactive=False),gr.Textbox.update(interactive=False),gr.Button.update(interactive=False)
17
-
18
-
 
 
 
19
 
20
 
21
 
@@ -79,15 +82,16 @@ def get_bot_response(url, prompt,state,bot_index):
79
  return response.json()['output'].split('### Instruction')[0]
80
 
81
  def chat_with_bots(user_input, state):
82
- bot_names = list(chatbots.keys())
83
- random.shuffle(bot_names)
84
- bot1_url, bot2_url = chatbots[bot_names[0]], chatbots[bot_names[1]]
 
 
85
 
86
- # Update the state with the names of the last bots
87
- state.update({'last_bots': [bot_names[0], bot_names[1]]})
88
 
89
- bot1_response = get_bot_response(bot1_url, user_input,state,0)
90
- bot2_response = get_bot_response(bot2_url, user_input,state,1)
91
 
92
  return bot1_response, bot2_response
93
 
@@ -202,4 +206,4 @@ with gr.Blocks() as demo:
202
 
203
  # Launch the Gradio interface
204
  if __name__ == "__main__":
205
- demo.launch(share=True)
 
11
  with open('chatbot_urls.json', 'r') as file:
12
  chatbots = json.load(file)
13
  def clear_chat(state):
14
+ # Reset state including the chatbot order
15
+ state = {} if state is not None else state
16
+ # Shuffle and reinitialize chatbots in the state
17
+ bot_names = list(chatbots.keys())
18
+ random.shuffle(bot_names)
19
+ state['last_bots'] = [bot_names[0], bot_names[1]]
20
+ # Reset other components
21
+ return state, None, None, gr.Button.update(interactive=False), gr.Button.update(interactive=False), gr.Textbox.update(interactive=False), gr.Button.update(interactive=False)
22
 
23
 
24
 
 
82
  return response.json()['output'].split('### Instruction')[0]
83
 
84
  def chat_with_bots(user_input, state):
85
+ # Use existing bot order from state if available, otherwise shuffle and initialize
86
+ if 'last_bots' not in state or not state['last_bots']:
87
+ bot_names = list(chatbots.keys())
88
+ random.shuffle(bot_names)
89
+ state['last_bots'] = [bot_names[0], bot_names[1]]
90
 
91
+ bot1_url, bot2_url = chatbots[state['last_bots'][0]], chatbots[state['last_bots'][1]]
 
92
 
93
+ bot1_response = get_bot_response(bot1_url, user_input, state, 0)
94
+ bot2_response = get_bot_response(bot2_url, user_input, state, 1)
95
 
96
  return bot1_response, bot2_response
97
 
 
206
 
207
  # Launch the Gradio interface
208
  if __name__ == "__main__":
209
+ demo.launch()