artificialguybr commited on
Commit
3997444
1 Parent(s): 547ab30

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +127 -0
app.py ADDED
@@ -0,0 +1,127 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ import mdtex2html
4
+ import torch
5
+ from transformers import AutoModelForCausalLM, AutoTokenizer
6
+ from transformers.generation import GenerationConfig
7
+
8
+ # Initialize model and tokenizer
9
+ tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen-7B-Chat", trust_remote_code=True)
10
+ model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen-7B-Chat", device_map="auto", trust_remote_code=True).eval()
11
+ model.generation_config = GenerationConfig.from_pretrained("Qwen/Qwen-7B-Chat", trust_remote_code=True)
12
+
13
+ # Postprocess function
14
+ def postprocess(self, y):
15
+ if y is None:
16
+ return []
17
+ for i, (message, response) in enumerate(y):
18
+ y[i] = (
19
+ None if message is None else mdtex2html.convert(message),
20
+ None if response is None else mdtex2html.convert(response),
21
+ )
22
+ return y
23
+
24
+ gr.Chatbot.postprocess = postprocess
25
+
26
+ # Text parsing function
27
+ def _parse_text(text):
28
+ lines = text.split("\n")
29
+ lines = [line for line in lines if line != ""]
30
+ count = 0
31
+ for i, line in enumerate(lines):
32
+ if "```" in line:
33
+ count += 1
34
+ items = line.split("`")
35
+ if count % 2 == 1:
36
+ lines[i] = f'<pre><code class="language-{items[-1]}">'
37
+ else:
38
+ lines[i] = f"<br></code></pre>"
39
+ else:
40
+ if i > 0:
41
+ if count % 2 == 1:
42
+ line = line.replace("`", r"\`")
43
+ line = line.replace("<", "&lt;")
44
+ line = line.replace(">", "&gt;")
45
+ line = line.replace(" ", "&nbsp;")
46
+ line = line.replace("*", "&ast;")
47
+ line = line.replace("_", "&lowbar;")
48
+ line = line.replace("-", "&#45;")
49
+ line = line.replace(".", "&#46;")
50
+ line = line.replace("!", "&#33;")
51
+ line = line.replace("(", "&#40;")
52
+ line = line.replace(")", "&#41;")
53
+ line = line.replace("$", "&#36;")
54
+ lines[i] = "<br>" + line
55
+ text = "".join(lines)
56
+ return text
57
+
58
+ # Demo launching function
59
+ def _launch_demo(args, model, tokenizer, config):
60
+ def predict(_query, _chatbot, _task_history):
61
+ print(f"User: {_parse_text(_query)}")
62
+ _chatbot.append((_parse_text(_query), ""))
63
+ full_response = ""
64
+
65
+ for response in model.chat_stream(tokenizer, _query, history=_task_history, generation_config=config):
66
+ _chatbot[-1] = (_parse_text(_query), _parse_text(response))
67
+
68
+ yield _chatbot
69
+ full_response = _parse_text(response)
70
+
71
+ print(f"History: {_task_history}")
72
+ _task_history.append((_query, full_response))
73
+ print(f"Qwen-Chat: {_parse_text(full_response)}")
74
+
75
+ def regenerate(_chatbot, _task_history):
76
+ if not _task_history:
77
+ yield _chatbot
78
+ return
79
+ item = _task_history.pop(-1)
80
+ _chatbot.pop(-1)
81
+ yield from predict(item[0], _chatbot, _task_history)
82
+
83
+ def reset_user_input():
84
+ return gr.update(value="")
85
+
86
+ def reset_state(_chatbot, _task_history):
87
+ _task_history.clear()
88
+ _chatbot.clear()
89
+ import gc
90
+ gc.collect()
91
+ torch.cuda.empty_cache()
92
+ return _chatbot
93
+
94
+ with gr.Blocks() as demo:
95
+ gr.Markdown("""
96
+ ## Qwen-14B-Chat: A Large Language Model by Alibaba Cloud
97
+ **Space created by [@artificialguybr](https://twitter.com/artificialguybr) based on QWEN Code. Thanks HF for GPU!**
98
+
99
+ ### Performance Metrics:
100
+ - **MMLU Accuracy**:
101
+ - 0-shot: 64.6
102
+ - 5-shot: 66.5
103
+ - **HumanEval Pass@1**: 43.9
104
+ - **GSM8K Accuracy**:
105
+ - 0-shot: 60.1
106
+ - 8-shot: 59.3
107
+ """)
108
+ chatbot = gr.Chatbot(label='Qwen-Chat', elem_classes="control-height", queue=True)
109
+ query = gr.Textbox(lines=2, label='Input')
110
+ task_history = gr.State([])
111
+
112
+ with gr.Row():
113
+ empty_btn = gr.Button("🧹 Clear History")
114
+ submit_btn = gr.Button("🚀 Submit")
115
+ regen_btn = gr.Button("🤔️ Regenerate")
116
+
117
+ submit_btn.click(predict, [query, chatbot, task_history], [chatbot], show_progress=True, queue=True) # Enable queue
118
+ submit_btn.click(reset_user_input, [], [query])
119
+ empty_btn.click(reset_state, [chatbot, task_history], outputs=[chatbot], show_progress=True)
120
+ regen_btn.click(regenerate, [chatbot, task_history], [chatbot], show_progress=True, queue=True) # Enable queue
121
+ demo.queue(max_size=20)
122
+ demo.launch(share=True)
123
+
124
+
125
+ # Main execution
126
+ if __name__ == "__main__":
127
+ _launch_demo(None, model, tokenizer, model.generation_config)