SkalskiP commited on
Commit
9fdba0a
1 Parent(s): 67bb418

Add text streaming effect

Browse files
Files changed (1) hide show
  1. app.py +32 -11
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  from typing import List, Tuple, Optional
2
 
3
  import google.generativeai as genai
@@ -30,9 +31,12 @@ def preprocess_stop_sequences(stop_sequences: str) -> Optional[List[str]]:
30
  return [sequence.strip() for sequence in stop_sequences.split(",")]
31
 
32
 
33
- def predict(
 
 
 
 
34
  google_key: str,
35
- text_prompt: str,
36
  image_prompt: Optional[Image.Image],
37
  temperature: float,
38
  max_output_tokens: int,
@@ -40,12 +44,13 @@ def predict(
40
  top_k: int,
41
  top_p: float,
42
  chatbot: List[Tuple[str, str]]
43
- ) -> Tuple[str, List[Tuple[str, str]]]:
44
  if not google_key:
45
  raise ValueError(
46
  "GOOGLE_API_KEY is not set. "
47
  "Please follow the instructions in the README to set it up.")
48
 
 
49
  genai.configure(api_key=google_key)
50
  generation_config = genai.types.GenerationConfig(
51
  temperature=temperature,
@@ -69,8 +74,14 @@ def predict(
69
  generation_config=generation_config)
70
  response.resolve()
71
 
72
- chatbot.append((text_prompt, response.text))
73
- return "", chatbot
 
 
 
 
 
 
74
 
75
 
76
  google_key_component = gr.Textbox(
@@ -151,9 +162,13 @@ top_p_component = gr.Slider(
151
  "the next token (using temperature). "
152
  ))
153
 
154
- inputs = [
155
- google_key_component,
156
  text_prompt_component,
 
 
 
 
 
157
  image_prompt_component,
158
  temperature_component,
159
  max_output_tokens_component,
@@ -183,15 +198,21 @@ with gr.Blocks() as demo:
183
  top_p_component.render()
184
 
185
  run_button_component.click(
186
- fn=predict,
187
- inputs=inputs,
188
  outputs=[text_prompt_component, chatbot_component],
 
 
 
189
  )
190
 
191
  text_prompt_component.submit(
192
- fn=predict,
193
- inputs=inputs,
194
  outputs=[text_prompt_component, chatbot_component],
 
 
 
195
  )
196
 
197
  demo.queue(max_size=99).launch(debug=True)
 
1
+ import time
2
  from typing import List, Tuple, Optional
3
 
4
  import google.generativeai as genai
 
31
  return [sequence.strip() for sequence in stop_sequences.split(",")]
32
 
33
 
34
+ def user(text_prompt: str, chatbot: List[Tuple[str, str]]):
35
+ return "", chatbot + [[text_prompt, None]]
36
+
37
+
38
+ def bot(
39
  google_key: str,
 
40
  image_prompt: Optional[Image.Image],
41
  temperature: float,
42
  max_output_tokens: int,
 
44
  top_k: int,
45
  top_p: float,
46
  chatbot: List[Tuple[str, str]]
47
+ ):
48
  if not google_key:
49
  raise ValueError(
50
  "GOOGLE_API_KEY is not set. "
51
  "Please follow the instructions in the README to set it up.")
52
 
53
+ text_prompt = chatbot[-1][0]
54
  genai.configure(api_key=google_key)
55
  generation_config = genai.types.GenerationConfig(
56
  temperature=temperature,
 
74
  generation_config=generation_config)
75
  response.resolve()
76
 
77
+ # streaming effect
78
+ chatbot[-1][1] = ""
79
+ for chunk in response:
80
+ for i in range(0, len(chunk.text), 10):
81
+ section = chunk.text[i:i + 10]
82
+ chatbot[-1][1] += section
83
+ time.sleep(0.01)
84
+ yield chatbot
85
 
86
 
87
  google_key_component = gr.Textbox(
 
162
  "the next token (using temperature). "
163
  ))
164
 
165
+ user_inputs = [
 
166
  text_prompt_component,
167
+ chatbot_component
168
+ ]
169
+
170
+ bot_inputs = [
171
+ google_key_component,
172
  image_prompt_component,
173
  temperature_component,
174
  max_output_tokens_component,
 
198
  top_p_component.render()
199
 
200
  run_button_component.click(
201
+ fn=user,
202
+ inputs=user_inputs,
203
  outputs=[text_prompt_component, chatbot_component],
204
+ queue=False
205
+ ).then(
206
+ fn=bot, inputs=bot_inputs, outputs=[chatbot_component],
207
  )
208
 
209
  text_prompt_component.submit(
210
+ fn=user,
211
+ inputs=user_inputs,
212
  outputs=[text_prompt_component, chatbot_component],
213
+ queue=False
214
+ ).then(
215
+ fn=bot, inputs=bot_inputs, outputs=[chatbot_component],
216
  )
217
 
218
  demo.queue(max_size=99).launch(debug=True)