FredZhang7 commited on
Commit
5a6c57a
1 Parent(s): 7cc4c5c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -20
app.py CHANGED
@@ -1,11 +1,12 @@
 
1
  import gradio as gr
2
  import gc, copy, re
3
  import urllib.request
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
  url = f"https://huggingface.co/BlinkDL/rwkv-5-world/resolve/main/{title}"
11
  urllib.request.urlretrieve(url, title)
@@ -13,21 +14,22 @@ urllib.request.urlretrieve(url, title)
13
  model = RWKV(model=title, strategy='cpu bf16')
14
  pipeline = PIPELINE(model, "rwkv_vocab_v20230424")
15
 
16
- def generate_prompt(instruction, input=None):
 
 
 
 
 
17
  instruction = instruction.strip().replace('\r\n','\n').replace('\n\n','\n').replace('\n\n','\n')
18
  input = input.strip().replace('\r\n','\n').replace('\n\n','\n').replace('\n\n','\n')
19
  if input and len(input) > 0:
20
- return f"""Instruction: {instruction}
21
 
22
  Input: {input}
23
 
24
  Response:"""
25
  else:
26
- return f"""User: hi
27
-
28
- 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.
29
-
30
- User: {instruction}
31
 
32
  Assistant:"""
33
 
@@ -49,6 +51,7 @@ def evaluate(
49
  top_p=0.5,
50
  presencePenalty = 0.5,
51
  countPenalty = 0.5,
 
52
  ):
53
  args = PIPELINE_ARGS(temperature = max(0.2, float(temperature)), top_p = float(top_p),
54
  alpha_frequency = countPenalty,
@@ -58,7 +61,7 @@ def evaluate(
58
 
59
  instruction = re.sub(r'\n{2,}', '\n', instruction).strip().replace('\r\n','\n')
60
  input = re.sub(r'\n{2,}', '\n', input).strip().replace('\r\n','\n')
61
- ctx = generate_prompt(instruction, input)
62
  print(ctx + "\n")
63
 
64
  all_tokens = []
@@ -111,26 +114,28 @@ def alternative(chatbot, history):
111
 
112
  with gr.Blocks(title=title) as demo:
113
  gr.HTML(f"<div style=\"text-align: center;\">\n<h1>🌍World - {title}</h1>\n</div>")
 
114
  with gr.Tab("Instruct mode"):
115
- 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, <b>keep your prompt short and clear</b>.")
116
  with gr.Row():
117
  with gr.Column():
118
- instruction = gr.Textbox(lines=2, label="Instruction", value='東京で訪れるべき素晴らしい場所とその紹介をいくつか挙げてください。')
119
  input = gr.Textbox(lines=2, label="Input", placeholder="")
120
  token_count = gr.Slider(10, 300, label="Max Tokens", step=10, value=300)
121
  temperature = gr.Slider(0.2, 2.0, label="Temperature", step=0.1, value=1.2)
122
  top_p = gr.Slider(0.0, 1.0, label="Top P", step=0.05, value=0.3)
123
  presence_penalty = gr.Slider(0.0, 1.0, label="Presence Penalty", step=0.1, value=0)
124
  count_penalty = gr.Slider(0.0, 1.0, label="Count Penalty", step=0.1, value=0.7)
125
- with gr.Column():
126
- with gr.Row():
127
- submit = gr.Button("Submit", variant="primary")
128
- clear = gr.Button("Clear", variant="secondary")
129
- output = gr.Textbox(label="Output", lines=5)
130
  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"])
131
- submit.click(evaluate, [instruction, input, token_count, temperature, top_p, presence_penalty, count_penalty], [output])
132
- clear.click(lambda: None, [], [output])
133
  data.click(lambda x: x, [data], [instruction, input, token_count, temperature, top_p, presence_penalty, count_penalty])
134
 
 
 
 
 
 
 
 
 
135
  demo.queue(max_size=10)
136
- demo.launch(share=False)
 
1
+
2
  import gradio as gr
3
  import gc, copy, re
4
  import urllib.request
5
  from rwkv.model import RWKV
6
  from rwkv.utils import PIPELINE, PIPELINE_ARGS
7
 
8
+ ctx_limit = 4096
9
+ title = "RWKV-5-World-0.1B-v1-20230803-ctx4096.pth"
10
 
11
  url = f"https://huggingface.co/BlinkDL/rwkv-5-world/resolve/main/{title}"
12
  urllib.request.urlretrieve(url, title)
 
14
  model = RWKV(model=title, strategy='cpu bf16')
15
  pipeline = PIPELINE(model, "rwkv_vocab_v20230424")
16
 
17
+ def generate_prompt(instruction, input=None, history=None):
18
+ # Parse the chat history into a string of user and assistant messages
19
+ history_str = ""
20
+ for pair in history:
21
+ history_str += f"Instruction: {pair[0]}\n\nAssistant: {pair[1]}\n\n"
22
+
23
  instruction = instruction.strip().replace('\r\n','\n').replace('\n\n','\n').replace('\n\n','\n')
24
  input = input.strip().replace('\r\n','\n').replace('\n\n','\n').replace('\n\n','\n')
25
  if input and len(input) > 0:
26
+ return f"""{history_str}Instruction: {instruction}
27
 
28
  Input: {input}
29
 
30
  Response:"""
31
  else:
32
+ return f"""{history_str}User: {instruction}
 
 
 
 
33
 
34
  Assistant:"""
35
 
 
51
  top_p=0.5,
52
  presencePenalty = 0.5,
53
  countPenalty = 0.5,
54
+ history=None # Add the history parameter to the evaluate function
55
  ):
56
  args = PIPELINE_ARGS(temperature = max(0.2, float(temperature)), top_p = float(top_p),
57
  alpha_frequency = countPenalty,
 
61
 
62
  instruction = re.sub(r'\n{2,}', '\n', instruction).strip().replace('\r\n','\n')
63
  input = re.sub(r'\n{2,}', '\n', input).strip().replace('\r\n','\n')
64
+ ctx = generate_prompt(instruction, input, history) # Pass the history to the generate_prompt function
65
  print(ctx + "\n")
66
 
67
  all_tokens = []
 
114
 
115
  with gr.Blocks(title=title) as demo:
116
  gr.HTML(f"<div style=\"text-align: center;\">\n<h1>🌍World - {title}</h1>\n</div>")
117
+
118
  with gr.Tab("Instruct mode"):
119
+ gr.Markdown(f"100% RNN RWKV-LM **trained on 100+ natural languages**. Demo limited to ctxlen {ctx_limit}. For best results, <b>keep your prompt short and clear</b>.")
120
  with gr.Row():
121
  with gr.Column():
122
+ instruction = gr.Textbox(lines=2, label="Instruction", value="Please show me a table with a cheat sheet of Python's syntax.")
123
  input = gr.Textbox(lines=2, label="Input", placeholder="")
124
  token_count = gr.Slider(10, 300, label="Max Tokens", step=10, value=300)
125
  temperature = gr.Slider(0.2, 2.0, label="Temperature", step=0.1, value=1.2)
126
  top_p = gr.Slider(0.0, 1.0, label="Top P", step=0.05, value=0.3)
127
  presence_penalty = gr.Slider(0.0, 1.0, label="Presence Penalty", step=0.1, value=0)
128
  count_penalty = gr.Slider(0.0, 1.0, label="Count Penalty", step=0.1, value=0.7)
 
 
 
 
 
129
  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"])
 
 
130
  data.click(lambda x: x, [data], [instruction, input, token_count, temperature, top_p, presence_penalty, count_penalty])
131
 
132
+ with gr.Tab("Chat mode"):
133
+ chatbot = gr.ChatInterface(fn=evaluate,
134
+ additional_inputs=[instruction, input, token_count, temperature, top_p, presence_penalty, count_penalty],
135
+ additional_inputs_accordion="Parameters",
136
+ examples=["Hello", "Write a poem about love", "Generate a list of prime numbers"],
137
+ title="RWKV Chatbot",
138
+ description="A chatbot that can generate creative and informative content based on instructions and inputs")
139
+
140
  demo.queue(max_size=10)
141
+ demo.launch(share=False)