Commit
•
764ee8b
1
Parent(s):
8d96198
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,134 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import gc, copy, re
|
3 |
+
from huggingface_hub import hf_hub_download
|
4 |
+
from rwkv.model import RWKV
|
5 |
+
from rwkv.utils import PIPELINE, PIPELINE_ARGS
|
6 |
+
|
7 |
+
ctx_limit = 2048
|
8 |
+
title = "RWKV-5-World-0.4B-v2-20231113-ctx4096.pth"
|
9 |
+
|
10 |
+
model_path = hf_hub_download(repo_id="BlinkDL/rwkv-5-world", filename=f"{title}")
|
11 |
+
model = RWKV(model=model_path, strategy='cpu bf16')
|
12 |
+
pipeline = PIPELINE(model, "rwkv_vocab_v20230424")
|
13 |
+
|
14 |
+
def generate_prompt(instruction, input=None):
|
15 |
+
instruction = instruction.strip().replace('\r\n','\n').replace('\n\n','\n').replace('\n\n','\n')
|
16 |
+
input = input.strip().replace('\r\n','\n').replace('\n\n','\n').replace('\n\n','\n')
|
17 |
+
if input and len(input) > 0:
|
18 |
+
return f"""Instruction: {instruction}
|
19 |
+
|
20 |
+
Input: {input}
|
21 |
+
|
22 |
+
Response:"""
|
23 |
+
else:
|
24 |
+
return f"""User: hi
|
25 |
+
|
26 |
+
Assistant: Hi. I am your assistant and I will provide expert full response in full details. Please feel free to ask any question and I will always answer it.
|
27 |
+
|
28 |
+
User: {instruction}
|
29 |
+
|
30 |
+
Assistant:"""
|
31 |
+
|
32 |
+
examples = [
|
33 |
+
["東京で訪れるべき素晴らしい場所とその紹介をいくつか挙げてください。", "", 300, 1.2, 0.5, 0.4, 0.4],
|
34 |
+
["Écrivez un programme Python pour miner 1 Bitcoin, avec des commentaires.", "", 300, 1.2, 0.5, 0.4, 0.4],
|
35 |
+
["Write a song about ravens.", "", 300, 1.2, 0.5, 0.4, 0.4],
|
36 |
+
["Explain the following metaphor: Life is like cats.", "", 300, 1.2, 0.5, 0.4, 0.4],
|
37 |
+
["Write a story using the following information", "A man named Alex chops a tree down", 300, 1.2, 0.5, 0.4, 0.4],
|
38 |
+
["Generate a list of adjectives that describe a person as brave.", "", 300, 1.2, 0.5, 0.4, 0.4],
|
39 |
+
["You have $100, and your goal is to turn that into as much money as possible with AI and Machine Learning. Please respond with detailed plan.", "", 300, 1.2, 0.5, 0.4, 0.4],
|
40 |
+
]
|
41 |
+
|
42 |
+
def evaluate(
|
43 |
+
instruction,
|
44 |
+
input=None,
|
45 |
+
token_count=200,
|
46 |
+
temperature=1.0,
|
47 |
+
top_p=0.5,
|
48 |
+
presencePenalty = 0.4,
|
49 |
+
countPenalty = 0.4,
|
50 |
+
):
|
51 |
+
args = PIPELINE_ARGS(temperature = max(0.2, float(temperature)), top_p = float(top_p),
|
52 |
+
alpha_frequency = countPenalty,
|
53 |
+
alpha_presence = presencePenalty,
|
54 |
+
token_ban = [], # ban the generation of some tokens
|
55 |
+
token_stop = [0]) # stop generation whenever you see any token here
|
56 |
+
|
57 |
+
instruction = re.sub(r'\n{2,}', '\n', instruction).strip().replace('\r\n','\n')
|
58 |
+
input = re.sub(r'\n{2,}', '\n', input).strip().replace('\r\n','\n')
|
59 |
+
ctx = generate_prompt(instruction, input)
|
60 |
+
print(ctx + "\n")
|
61 |
+
|
62 |
+
all_tokens = []
|
63 |
+
out_last = 0
|
64 |
+
out_str = ''
|
65 |
+
occurrence = {}
|
66 |
+
state = None
|
67 |
+
for i in range(int(token_count)):
|
68 |
+
out, state = model.forward(pipeline.encode(ctx)[-ctx_limit:] if i == 0 else [token], state)
|
69 |
+
for n in occurrence:
|
70 |
+
out[n] -= (args.alpha_presence + occurrence[n] * args.alpha_frequency)
|
71 |
+
|
72 |
+
token = pipeline.sample_logits(out, temperature=args.temperature, top_p=args.top_p)
|
73 |
+
if token in args.token_stop:
|
74 |
+
break
|
75 |
+
all_tokens += [token]
|
76 |
+
for xxx in occurrence:
|
77 |
+
occurrence[xxx] *= 0.996
|
78 |
+
if token not in occurrence:
|
79 |
+
occurrence[token] = 1
|
80 |
+
else:
|
81 |
+
occurrence[token] += 1
|
82 |
+
|
83 |
+
tmp = pipeline.decode(all_tokens[out_last:])
|
84 |
+
if '\ufffd' not in tmp:
|
85 |
+
out_str += tmp
|
86 |
+
yield out_str.strip()
|
87 |
+
out_last = i + 1
|
88 |
+
if '\n\n' in out_str:
|
89 |
+
break
|
90 |
+
|
91 |
+
del out
|
92 |
+
del state
|
93 |
+
gc.collect()
|
94 |
+
yield out_str.strip()
|
95 |
+
|
96 |
+
def user(message, chatbot):
|
97 |
+
chatbot = chatbot or []
|
98 |
+
return "", chatbot + [[message, None]]
|
99 |
+
|
100 |
+
def alternative(chatbot, history):
|
101 |
+
if not chatbot or not history:
|
102 |
+
return chatbot, history
|
103 |
+
|
104 |
+
chatbot[-1][1] = None
|
105 |
+
history[0] = copy.deepcopy(history[1])
|
106 |
+
|
107 |
+
return chatbot, history
|
108 |
+
|
109 |
+
|
110 |
+
with gr.Blocks(title=title) as demo:
|
111 |
+
gr.HTML(f"<div style=\"text-align: center;\">\n<h1>🌍World - {title}</h1>\n</div>")
|
112 |
+
with gr.Tab("Instruct mode"):
|
113 |
+
gr.Markdown(f"100% RNN RWKV-LM **trained on 100+ world languages**. Demo limited to ctxlen {ctx_limit}. Finetuned on alpaca, gpt4all, codealpaca and more. For best results, ** keep you prompt short and clear **.</b>.") # <b>UPDATE: now with Chat (see above, as a tab) ==> turn off as of now due to VRAM leak caused by buggy code.
|
114 |
+
with gr.Row():
|
115 |
+
with gr.Column():
|
116 |
+
instruction = gr.Textbox(lines=2, label="Instruction", value='東京で訪れるべき素晴らしい場所とその紹介をいくつか挙げてください。')
|
117 |
+
input = gr.Textbox(lines=2, label="Input", placeholder="")
|
118 |
+
token_count = gr.Slider(10, 300, label="Max Tokens", step=10, value=300)
|
119 |
+
temperature = gr.Slider(0.2, 2.0, label="Temperature", step=0.1, value=1.2)
|
120 |
+
top_p = gr.Slider(0.0, 1.0, label="Top P", step=0.05, value=0.5)
|
121 |
+
presence_penalty = gr.Slider(0.0, 1.0, label="Presence Penalty", step=0.1, value=0.4)
|
122 |
+
count_penalty = gr.Slider(0.0, 1.0, label="Count Penalty", step=0.1, value=0.4)
|
123 |
+
with gr.Column():
|
124 |
+
with gr.Row():
|
125 |
+
submit = gr.Button("Submit", variant="primary")
|
126 |
+
clear = gr.Button("Clear", variant="secondary")
|
127 |
+
output = gr.Textbox(label="Output", lines=5)
|
128 |
+
data = gr.Dataset(components=[instruction, input, token_count, temperature, top_p, presence_penalty, count_penalty], samples=examples, label="Example Instructions", headers=["Instruction", "Input", "Max Tokens", "Temperature", "Top P", "Presence Penalty", "Count Penalty"])
|
129 |
+
submit.click(evaluate, [instruction, input, token_count, temperature, top_p, presence_penalty, count_penalty], [output])
|
130 |
+
clear.click(lambda: None, [], [output])
|
131 |
+
data.click(lambda x: x, [data], [instruction, input, token_count, temperature, top_p, presence_penalty, count_penalty])
|
132 |
+
|
133 |
+
demo.queue(max_size=10)
|
134 |
+
demo.launch(share=False)
|