slush0 commited on
Commit
dac87a3
·
1 Parent(s): f2475e8

Adding stop generating button to Prompt mode.

Browse files
Files changed (2) hide show
  1. chat.py +2 -5
  2. prompt.py +32 -10
chat.py CHANGED
@@ -96,11 +96,9 @@ def _generate(state, prompt, model, endseq, max_length,
96
  ):
97
 
98
  if not state['generate']:
99
- print("Stopping generation.")
100
  client.close_session()
101
  yield state, [], None, ''
102
  return
103
- #return state, state['history'], None, prompt2 + output_raw
104
 
105
  output_raw += out
106
  output += out
@@ -168,8 +166,7 @@ with gr.Blocks() as iface_chat:
168
 
169
  with gr.Row():
170
  button_generate = gr.Button("Generate")
171
- button_reset = gr.Button("Reset/Clear session")
172
- # button_stop = gr.Button("Stop") # TODO, not supported by websocket API yet.
173
 
174
  with gr.Accordion("Raw prompt log", open=False):
175
  output = gr.Textbox(lines=3, show_label=False).style(container=False)
@@ -187,7 +184,7 @@ with gr.Blocks() as iface_chat:
187
 
188
  examples = gr.Examples(inputs=[context, prompt, model, do_sample, top_k, top_p, temperature],
189
  examples=[
190
- ["A Human talks to a powerful AI that follows the human's instructions. "
191
  "AI is talkative, friendly, positive and provides detailed answers to any question.</s>\n"
192
  "Human: Hi!</s>\n"
193
  "AI: Hi! How can I help you?",
 
96
  ):
97
 
98
  if not state['generate']:
 
99
  client.close_session()
100
  yield state, [], None, ''
101
  return
 
102
 
103
  output_raw += out
104
  output += out
 
166
 
167
  with gr.Row():
168
  button_generate = gr.Button("Generate")
169
+ button_reset = gr.Button("Reset session")
 
170
 
171
  with gr.Accordion("Raw prompt log", open=False):
172
  output = gr.Textbox(lines=3, show_label=False).style(container=False)
 
184
 
185
  examples = gr.Examples(inputs=[context, prompt, model, do_sample, top_k, top_p, temperature],
186
  examples=[
187
+ ["A Human talks to a powerful AI that follows the Human's instructions. "
188
  "AI is talkative, friendly, positive and provides detailed answers to any question.</s>\n"
189
  "Human: Hi!</s>\n"
190
  "AI: Hi! How can I help you?",
prompt.py CHANGED
@@ -8,7 +8,17 @@ import chat_client
8
  CHAT_URL='ws://chat.petals.ml/api/v2/generate'
9
  #CHAT_URL='ws://localhost:8000/api/v2/generate'
10
 
11
- def generate(prompt, model, endseq, max_length,
 
 
 
 
 
 
 
 
 
 
12
  do_sample, top_k, top_p, temperature,
13
  add_stoptoken, copy_output):
14
 
@@ -17,7 +27,7 @@ def generate(prompt, model, endseq, max_length,
17
  client.open_session(f"bigscience/{model}-petals", max_length)
18
  except Exception:
19
  print(traceback.format_exc())
20
- yield [prompt, "Error: " + traceback.format_exc()]
21
  return
22
 
23
  if add_stoptoken:
@@ -51,7 +61,7 @@ def generate(prompt, model, endseq, max_length,
51
 
52
  # This render prompt dialog immediately and
53
  # don't wait to generator to return first result
54
- yield [prompt2, output]
55
 
56
  try:
57
  for out in client.generate(prompt,
@@ -63,16 +73,25 @@ def generate(prompt, model, endseq, max_length,
63
  extra_stop_sequences=seq
64
  ):
65
 
 
 
 
 
66
  output += out
67
  if copy_output:
68
  prompt2 += out
69
 
70
- yield [prompt2, output]
71
  except Exception:
72
  print(traceback.format_exc())
73
- yield [prompt, output + "\nError: " + traceback.format_exc()]
74
  return
75
 
 
 
 
 
 
76
  with gr.Blocks() as iface_prompt:
77
  gr.Markdown("""**Useful for testing raw prompts with zero, one or few-shot prompting.**""")
78
 
@@ -84,7 +103,7 @@ with gr.Blocks() as iface_prompt:
84
  value=["\\n", "</s>"], label='Extra end sequences')
85
 
86
  # Maximum length of inference session
87
- max_length = gr.Radio([64, 128, 256, 512, 1024, 2048], value=128, interactive=True, label="Max length")
88
 
89
  with gr.Row():
90
  with gr.Column():
@@ -103,26 +122,29 @@ with gr.Blocks() as iface_prompt:
103
  temperature = gr.Number(value=0.75, precision=2, interactive=True, label="Temperature")
104
 
105
  prompt = gr.Textbox(lines=3, label='Prompt', placeholder="Prompt Here...")
 
106
 
107
  with gr.Row():
108
  button_generate = gr.Button("Generate")
109
- # button_stop = gr.Button("Stop") # TODO, not supported by websocket API yet.
110
 
111
  # Automatically copy the output at the end of prompt
112
  copy_output = gr.Checkbox(label="Output -> Prompt")
113
 
114
  output = gr.Textbox(lines=3, label='Output')
115
 
116
- inputs = [prompt, model, endseq, max_length, do_sample,
117
  top_k, top_p, temperature, add_stoptoken, copy_output]
118
- outputs = [prompt, output]
119
  button_generate.click(generate, inputs=inputs, outputs=outputs)
 
120
 
121
  examples = gr.Examples(inputs=[prompt, model, do_sample, top_k, top_p, temperature, add_stoptoken],
122
  examples=[
123
  ["The SQL command to extract all the users whose name starts with A is: ", "bloom-7b1", False, 0, 0, 1, False],
124
  ["The Spanish translation of thank you for your help is: ", "bloom-7b1", False, 0, 0, 1, False],
125
- ["A human talks to a powerful AI that follows the human's instructions.</s>\n"
 
126
  "Human: Hi!</s>\n"
127
  "AI: Hi! How can I help you?</s>\n"
128
  "Human: What's the capital of Portugal?</s>\n"
 
8
  CHAT_URL='ws://chat.petals.ml/api/v2/generate'
9
  #CHAT_URL='ws://localhost:8000/api/v2/generate'
10
 
11
+ def generate(state, *args):
12
+ # Save that we're in generating loop
13
+ state['generate'] = True
14
+
15
+ try:
16
+ for x in _generate(state, *args):
17
+ yield x
18
+ finally:
19
+ state['generate'] = False
20
+
21
+ def _generate(state, prompt, model, endseq, max_length,
22
  do_sample, top_k, top_p, temperature,
23
  add_stoptoken, copy_output):
24
 
 
27
  client.open_session(f"bigscience/{model}-petals", max_length)
28
  except Exception:
29
  print(traceback.format_exc())
30
+ yield state, prompt, "Error: " + traceback.format_exc()
31
  return
32
 
33
  if add_stoptoken:
 
61
 
62
  # This render prompt dialog immediately and
63
  # don't wait to generator to return first result
64
+ yield [state, prompt2, output]
65
 
66
  try:
67
  for out in client.generate(prompt,
 
73
  extra_stop_sequences=seq
74
  ):
75
 
76
+ if not state['generate']:
77
+ client.close_session()
78
+ return
79
+
80
  output += out
81
  if copy_output:
82
  prompt2 += out
83
 
84
+ yield state, prompt2, output
85
  except Exception:
86
  print(traceback.format_exc())
87
+ yield state, prompt, output + "\nError: " + traceback.format_exc()
88
  return
89
 
90
+ def stop(state):
91
+ """Stops generating."""
92
+ state.update({"generate": False})
93
+ return state
94
+
95
  with gr.Blocks() as iface_prompt:
96
  gr.Markdown("""**Useful for testing raw prompts with zero, one or few-shot prompting.**""")
97
 
 
103
  value=["\\n", "</s>"], label='Extra end sequences')
104
 
105
  # Maximum length of inference session
106
+ max_length = gr.Radio([64, 128, 256, 512, 1024, 2048], value=512, interactive=True, label="Max length")
107
 
108
  with gr.Row():
109
  with gr.Column():
 
122
  temperature = gr.Number(value=0.75, precision=2, interactive=True, label="Temperature")
123
 
124
  prompt = gr.Textbox(lines=3, label='Prompt', placeholder="Prompt Here...")
125
+ state = gr.State({'generate': False})
126
 
127
  with gr.Row():
128
  button_generate = gr.Button("Generate")
129
+ button_stop = gr.Button("Stop")
130
 
131
  # Automatically copy the output at the end of prompt
132
  copy_output = gr.Checkbox(label="Output -> Prompt")
133
 
134
  output = gr.Textbox(lines=3, label='Output')
135
 
136
+ inputs = [state, prompt, model, endseq, max_length, do_sample,
137
  top_k, top_p, temperature, add_stoptoken, copy_output]
138
+ outputs = [state, prompt, output]
139
  button_generate.click(generate, inputs=inputs, outputs=outputs)
140
+ button_stop.click(stop, inputs=[state], outputs=[state])
141
 
142
  examples = gr.Examples(inputs=[prompt, model, do_sample, top_k, top_p, temperature, add_stoptoken],
143
  examples=[
144
  ["The SQL command to extract all the users whose name starts with A is: ", "bloom-7b1", False, 0, 0, 1, False],
145
  ["The Spanish translation of thank you for your help is: ", "bloom-7b1", False, 0, 0, 1, False],
146
+ ["A human talks to a powerful AI that follows the Human's instructions.\n"
147
+ "AI is talkative, friendly, positive and provides detailed answers to any question.</s>\n"
148
  "Human: Hi!</s>\n"
149
  "AI: Hi! How can I help you?</s>\n"
150
  "Human: What's the capital of Portugal?</s>\n"