ffreemt commited on
Commit
23fc95a
1 Parent(s): e178694

Update streaming...

Browse files
Files changed (2) hide show
  1. .gitignore +1 -0
  2. app.py +111 -40
.gitignore CHANGED
@@ -8,3 +8,4 @@ pyproject.toml
8
  models
9
  .ruff_cache
10
  run-nodemon.sh
 
 
8
  models
9
  .ruff_cache
10
  run-nodemon.sh
11
+ app-.py
app.py CHANGED
@@ -1,10 +1,6 @@
1
  """Run codes"""
2
  # pylint: disable=line-too-long, broad-exception-caught, invalid-name, missing-function-docstring, too-many-instance-attributes, missing-class-docstring
3
- # r uff: noqa: E501
4
- # import gradio
5
-
6
- # gradio.load("models/WizardLM/WizardCoder-15B-V1.0").launch()
7
-
8
  import os
9
  import time
10
  from dataclasses import asdict, dataclass
@@ -37,11 +33,60 @@ user_prefix = "[user]: "
37
  assistant_prefix = "[assistant]: "
38
 
39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  def predict(prompt, bot):
41
  # logger.debug(f"{prompt=}, {bot=}, {timeout=}")
42
  logger.debug(f"{prompt=}, {bot=}")
43
 
44
  ns.response = ""
 
45
  with about_time() as atime: # type: ignore
46
  try:
47
  # user_prompt = prompt
@@ -59,7 +104,12 @@ def predict(prompt, bot):
59
  response = ""
60
  buff.update(value="diggin...")
61
 
 
62
  for word in generator:
 
 
 
 
63
  # print(word, end="", flush=True)
64
  print(word, flush=True) # vertical stream
65
  response += word
@@ -268,6 +318,36 @@ css = """
268
  .disclaimer {font-variant-caps: all-small-caps; font-size: xx-small;}
269
  .xsmall {font-size: x-small;}
270
  """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
271
 
272
  with gr.Blocks(
273
  # title="mpt-30b-chat-ggml",
@@ -281,9 +361,11 @@ with gr.Blocks(
281
  # )
282
  gr.Markdown(
283
  f"""<h4><center>{MODEL_FILENAME}</center></h4>
 
 
284
 
285
- Most examples are meant for another model. You probably should try
286
- some coder-related prompts.
287
 
288
  Try to refresh the browser and try again when occasionally errors occur.
289
 
@@ -296,13 +378,13 @@ with gr.Blocks(
296
  chatbot = gr.Chatbot(height=700) # 500
297
  buff = gr.Textbox(show_label=False, visible=False)
298
  with gr.Row():
299
- with gr.Column(scale=4):
300
  msg = gr.Textbox(
301
  label="Chat Message Box",
302
  placeholder="Ask me anything (press Enter or click Submit to send)",
303
  show_label=False,
304
  ).style(container=False)
305
- with gr.Column(scale=1, min_width=100):
306
  with gr.Row():
307
  submit = gr.Button("Submit", elem_classes="xsmall")
308
  stop = gr.Button("Stop", visible=False)
@@ -322,37 +404,8 @@ with gr.Blocks(
322
  reset = gr.Button("Reset System Prompt")
323
 
324
  with gr.Accordion("Example Inputs", open=True):
325
- etext = """In America, where cars are an important part of the national psyche, a decade ago people had suddenly started to drive less, which had not happened since the oil shocks of the 1970s. """
326
  examples = gr.Examples(
327
- examples=[
328
- ["判断一个数是不是质数的 javascript 码"],
329
- ["实现python 里 range(10)的 javascript 码"],
330
- ["实现python 里 [*(range(10)]的 javascript 码"],
331
- ["Explain the plot of Cinderella in a sentence."],
332
- [
333
- "How long does it take to become proficient in French, and what are the best methods for retaining information?"
334
- ],
335
- ["What are some common mistakes to avoid when writing code?"],
336
- ["Build a prompt to generate a beautiful portrait of a horse"],
337
- ["Suggest four metaphors to describe the benefits of AI"],
338
- ["Write a pop song about leaving home for the sandy beaches."],
339
- ["Write a summary demonstrating my ability to tame lions"],
340
- ["鲁迅和周树人什么关系 说中文"],
341
- ["鲁迅和周树人什么关系"],
342
- ["鲁迅和周树人什么关系 用英文回答"],
343
- ["从前有一头牛,这头牛后面有什么?"],
344
- ["正无穷大加一大于正无穷大吗?"],
345
- ["正无穷大加正无穷大大于正无穷大吗?"],
346
- ["-2的平方根等于什么"],
347
- ["树上有5只鸟,猎人开枪打死了一只。树上还有几只鸟?"],
348
- ["树上有11只鸟,猎人开枪打死了一只。树上还有几只鸟?提示:需考虑鸟可能受惊吓飞走。"],
349
- ["以红楼梦的行文风格写一张委婉的请假条。不少于320字。"],
350
- [f"{etext} 翻成中文,列出3个版本"],
351
- [f"{etext} \n 翻成中文,保留原意,但使用文学性的语言。不要写解释。列出3个版本"],
352
- ["假定 1 + 2 = 4, 试求 7 + 8"],
353
- ["Erkläre die Handlung von Cinderella in einem Satz."],
354
- ["Erkläre die Handlung von Cinderella in einem Satz. Auf Deutsch"],
355
- ],
356
  inputs=[msg],
357
  examples_per_page=40,
358
  )
@@ -367,7 +420,7 @@ with gr.Blocks(
367
  "biased, or otherwise offensive outputs.",
368
  elem_classes=["disclaimer"],
369
  )
370
-
371
  msg.submit(
372
  # fn=conversation.user_turn,
373
  fn=predict,
@@ -384,6 +437,24 @@ with gr.Blocks(
384
  queue=True,
385
  show_progress="full",
386
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
387
  clear.click(lambda: None, None, chatbot, queue=False)
388
 
389
  # update buff Textbox, every: units in seconds)
 
1
  """Run codes"""
2
  # pylint: disable=line-too-long, broad-exception-caught, invalid-name, missing-function-docstring, too-many-instance-attributes, missing-class-docstring
3
+ # ruff: noqa: E501
 
 
 
 
4
  import os
5
  import time
6
  from dataclasses import asdict, dataclass
 
33
  assistant_prefix = "[assistant]: "
34
 
35
 
36
+ def predict_str(prompt, bot): # bot is in fact bot_history
37
+ # logger.debug(f"{prompt=}, {bot=}, {timeout=}")
38
+ logger.debug(f"{prompt=}, {bot=}")
39
+
40
+ try:
41
+ # user_prompt = prompt
42
+ generator = generate(
43
+ LLM,
44
+ GENERATION_CONFIG,
45
+ system_prompt=default_system_prompt,
46
+ user_prompt=prompt.strip(),
47
+ )
48
+
49
+ ns.generator = generator # for .then
50
+
51
+ except Exception as exc:
52
+ logger.error(exc)
53
+
54
+ # bot.append([prompt, f"{response} {_}"])
55
+ # return prompt, bot
56
+
57
+ return prompt, bot + [[prompt, None]]
58
+
59
+
60
+ def bot_str(bot):
61
+ if bot:
62
+ bot[-1][1] = ""
63
+ else:
64
+ bot = [["Something is wrong", ""]]
65
+
66
+ print(assistant_prefix, end=" ", flush=True)
67
+
68
+ response = ""
69
+
70
+ flag = 1
71
+ then = time.time()
72
+ for word in ns.generator:
73
+ # record first response time
74
+ if flag:
75
+ logger.debug(f"\t {time.time() - then:.1f}s")
76
+ flag = 0
77
+ print(word, end="", flush=True)
78
+ # print(word, flush=True) # vertical stream
79
+ response += word
80
+ bot[-1][1] = response
81
+ yield bot
82
+
83
+
84
  def predict(prompt, bot):
85
  # logger.debug(f"{prompt=}, {bot=}, {timeout=}")
86
  logger.debug(f"{prompt=}, {bot=}")
87
 
88
  ns.response = ""
89
+ then = time.time()
90
  with about_time() as atime: # type: ignore
91
  try:
92
  # user_prompt = prompt
 
104
  response = ""
105
  buff.update(value="diggin...")
106
 
107
+ flag = 1
108
  for word in generator:
109
+ # record first response time
110
+ if flag:
111
+ logger.debug(f"\t {time.time() - then:.1f}s")
112
+ flag = 0
113
  # print(word, end="", flush=True)
114
  print(word, flush=True) # vertical stream
115
  response += word
 
318
  .disclaimer {font-variant-caps: all-small-caps; font-size: xx-small;}
319
  .xsmall {font-size: x-small;}
320
  """
321
+ etext = """In America, where cars are an important part of the national psyche, a decade ago people had suddenly started to drive less, which had not happened since the oil shocks of the 1970s. """
322
+ examples = [
323
+ ["Explain the plot of Cinderella in a sentence."],
324
+ [
325
+ "How long does it take to become proficient in French, and what are the best methods for retaining information?"
326
+ ],
327
+ ["What are some common mistakes to avoid when writing code?"],
328
+ ["Build a prompt to generate a beautiful portrait of a horse"],
329
+ ["Suggest four metaphors to describe the benefits of AI"],
330
+ ["Write a pop song about leaving home for the sandy beaches."],
331
+ ["Write a summary demonstrating my ability to tame lions"],
332
+ ["鲁迅和周树人什么关系 说中文"],
333
+ ["鲁迅和周树人什么关系"],
334
+ ["鲁迅和周树人什么关系 用英文回答"],
335
+ ["从前有一头牛,这头牛后面有什么?"],
336
+ ["正无穷大加一大于正无穷大吗?"],
337
+ ["正无穷大加正无穷大大于正无穷大吗?"],
338
+ ["-2的平方根等于什么"],
339
+ ["树上有5只鸟,猎人开枪打死了一只。树上还有几只鸟?"],
340
+ ["树上有11只鸟,猎人开枪打死了一只。树上还有几只鸟?提示:需考虑鸟可能受惊吓飞走。"],
341
+ ["以红楼梦的行文风格写一张委婉的请假条。不少于320字。"],
342
+ [f"{etext} 翻成中文,列出3个版本"],
343
+ [f"{etext} \n 翻成中文,保留原意,但使用文学性的语言。不要写解释。列出3个版本"],
344
+ ["假定 1 + 2 = 4, 试求 7 + 8"],
345
+ ["判断一个数是不是质数的 javascript 码"],
346
+ ["实现python 里 range(10)的 javascript 码"],
347
+ ["实现python 里 [*(range(10)]的 javascript 码"],
348
+ ["Erkläre die Handlung von Cinderella in einem Satz."],
349
+ ["Erkläre die Handlung von Cinderella in einem Satz. Auf Deutsch"],
350
+ ]
351
 
352
  with gr.Blocks(
353
  # title="mpt-30b-chat-ggml",
 
361
  # )
362
  gr.Markdown(
363
  f"""<h4><center>{MODEL_FILENAME}</center></h4>
364
+ It takes about 100 seconds for the initial reply
365
+ message to appear. Average streaming rate ~1 sec/chat. The bot only speaks English.
366
 
367
+ Most examples are meant for another model. You probably should try to test
368
+ some related prompts.
369
 
370
  Try to refresh the browser and try again when occasionally errors occur.
371
 
 
378
  chatbot = gr.Chatbot(height=700) # 500
379
  buff = gr.Textbox(show_label=False, visible=False)
380
  with gr.Row():
381
+ with gr.Column(scale=5):
382
  msg = gr.Textbox(
383
  label="Chat Message Box",
384
  placeholder="Ask me anything (press Enter or click Submit to send)",
385
  show_label=False,
386
  ).style(container=False)
387
+ with gr.Column(scale=1, min_width=80):
388
  with gr.Row():
389
  submit = gr.Button("Submit", elem_classes="xsmall")
390
  stop = gr.Button("Stop", visible=False)
 
404
  reset = gr.Button("Reset System Prompt")
405
 
406
  with gr.Accordion("Example Inputs", open=True):
 
407
  examples = gr.Examples(
408
+ examples=examples,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
409
  inputs=[msg],
410
  examples_per_page=40,
411
  )
 
420
  "biased, or otherwise offensive outputs.",
421
  elem_classes=["disclaimer"],
422
  )
423
+ _ = """
424
  msg.submit(
425
  # fn=conversation.user_turn,
426
  fn=predict,
 
437
  queue=True,
438
  show_progress="full",
439
  )
440
+ # """
441
+ msg.submit(
442
+ # fn=conversation.user_turn,
443
+ fn=predict_str,
444
+ inputs=[msg, chatbot],
445
+ outputs=[msg, chatbot],
446
+ # queue=True,
447
+ show_progress="full",
448
+ api_name="predict",
449
+ ).then(bot_str, chatbot, chatbot)
450
+ submit.click(
451
+ fn=lambda x, y: ("",) + predict_str(x, y)[1:], # clear msg
452
+ inputs=[msg, chatbot],
453
+ outputs=[msg, chatbot],
454
+ queue=True,
455
+ show_progress="full",
456
+ ).then(bot_str, chatbot, chatbot)
457
+
458
  clear.click(lambda: None, None, chatbot, queue=False)
459
 
460
  # update buff Textbox, every: units in seconds)