artificialguybr commited on
Commit
880e945
1 Parent(s): 946cd44

Create app.py

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