gururise commited on
Commit
2d7b91c
·
1 Parent(s): 48c9412

add username to chatbot

Browse files
Files changed (1) hide show
  1. app.py +35 -23
app.py CHANGED
@@ -62,7 +62,7 @@ def infer(
62
  assert 0.0 <= top_p <= 1.0
63
 
64
  if temperature == 0.0:
65
- temperature = 0.01
66
  if prompt == "":
67
  prompt = " "
68
 
@@ -108,6 +108,7 @@ def infer(
108
  def chat(
109
  prompt,
110
  history,
 
111
  max_new_tokens=10,
112
  temperature=0.1,
113
  top_p=1.0,
@@ -123,34 +124,41 @@ def chat(
123
  if (DEVICE == "cuda"):
124
  torch.cuda.empty_cache()
125
  model = get_model()
126
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
127
  if len(history) == 0:
128
  # no history, so lets reset chat state
129
  model.resetState()
130
  history = [[],model.emptyState]
131
  print("reset chat state")
132
- intro = '''The following is a verbose and detailed conversation between an AI assistant called FRITZ, and a human user called USER. FRITZ is intelligent, knowledgeable, wise and polite.
133
-
134
- USER: What year was the french revolution?
135
- FRITZ: The French Revolution started in 1789, and lasted 10 years until 1799.
136
- USER: 3+5=?
137
- FRITZ: The answer is 8.
138
- USER: What year did the Berlin Wall fall?
139
- FRITZ: The Berlin wall fell in 1989 and was the start of the collapse of the iron curtain.
140
- USER: solve for a: 9-a=2
141
- FRITZ: The answer is a=7, because 9-7 = 2.
142
- USER: wat is lhc
143
- FRITZ: The Large Hadron Collider (LHC) is a high-energy particle collider, built by CERN, and completed in 2008. It was used to confirm the existence of the Higgs boson in 2012.
144
- USER: Tell me about yourself.
145
- FRITZ: My name is Fritz. I am an RNN based Large Language Model (LLM) that use no attention layers unlike most other LLM's which are transformer based.
146
- '''
147
  else:
148
- model.setState(history[1])
 
 
 
 
 
 
 
149
 
150
  max_new_tokens = int(max_new_tokens)
151
  temperature = float(temperature)
152
  top_p = float(top_p)
153
- #stop = [x.strip(' ') for x in stop.split(',')]
154
  seed = seed
155
 
156
  assert 1 <= max_new_tokens <= 384
@@ -158,16 +166,16 @@ FRITZ: My name is Fritz. I am an RNN based Large Language Model (LLM) that use n
158
  assert 0.0 <= top_p <= 1.0
159
 
160
  if temperature == 0.0:
161
- temperature = 0.01
162
 
163
- prompt = "USER: " + prompt + "\n"
164
- print(f"CHAT ({datetime.now()}):\n-------\n{intro+prompt}")
165
  print(f"OUTPUT ({datetime.now()}):\n-------\n")
166
  # Load prompt
167
 
168
  model.loadContext(newctx=intro+prompt)
169
 
170
- out = model.forward(number=max_new_tokens, stopStrings=["<|endoftext|>","USER:"],temp=temperature,top_p_usual=top_p)
171
 
172
  generated_text = out["output"].lstrip("\n ")
173
  generated_text = generated_text.rstrip("USER:")
@@ -185,6 +193,9 @@ examples = [
185
  [
186
  # Question Answering
187
  '''Are humans good or bad?''',"Q/A", 150, 0.8, 0.8, "<|endoftext|>"],
 
 
 
188
  [
189
  # Chatbot
190
  '''This is a conversation between two AI large language models named Alex and Fritz. They are exploring each other's capabilities, and trying to ask interesting questions of one another to explore the limits of each others AI.
@@ -242,6 +253,7 @@ chatiface = gr.Interface(
242
  inputs=[
243
  gr.Textbox(lines=5, label="Message"), # prompt
244
  "state",
 
245
  gr.Slider(1, 256, value=60), # max_tokens
246
  gr.Slider(0.0, 1.0, value=0.8), # temperature
247
  gr.Slider(0.0, 1.0, value=0.85) # top_p
 
62
  assert 0.0 <= top_p <= 1.0
63
 
64
  if temperature == 0.0:
65
+ temperature = 0.05
66
  if prompt == "":
67
  prompt = " "
68
 
 
108
  def chat(
109
  prompt,
110
  history,
111
+ username,
112
  max_new_tokens=10,
113
  temperature=0.1,
114
  top_p=1.0,
 
124
  if (DEVICE == "cuda"):
125
  torch.cuda.empty_cache()
126
  model = get_model()
127
+
128
+ intro = f'''The following is a verbose and detailed conversation between an AI assistant called FRITZ, and a human user called USER. FRITZ is intelligent, knowledgeable, wise and polite.
129
+
130
+ {username}: What year was the french revolution?
131
+ FRITZ: The French Revolution started in 1789, and lasted 10 years until 1799.
132
+ {username}: 3+5=?
133
+ FRITZ: The answer is 8.
134
+ {username}: What year did the Berlin Wall fall?
135
+ FRITZ: The Berlin wall stood for 28 years and fell in 1989.
136
+ {username}: solve for a: 9-a=2
137
+ FRITZ: The answer is a=7, because 9-7 = 2.
138
+ {username}: wat is lhc
139
+ FRITZ: The Large Hadron Collider (LHC) is a high-energy particle collider, built by CERN, and completed in 2008. It was used to confirm the existence of the Higgs boson in 2012.
140
+ {username}: Tell me about yourself.
141
+ FRITZ: My name is Fritz. I am an RNN based Large Language Model (LLM).
142
+ '''
143
+
144
  if len(history) == 0:
145
  # no history, so lets reset chat state
146
  model.resetState()
147
  history = [[],model.emptyState]
148
  print("reset chat state")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
149
  else:
150
+ if (history[0][0][0].split(':')[0] != username):
151
+ model.resetState()
152
+ history = [[],model.emptyState]
153
+ print("username changed, reset state")
154
+ else:
155
+ model.setState(history[1])
156
+ intro = ""
157
+
158
 
159
  max_new_tokens = int(max_new_tokens)
160
  temperature = float(temperature)
161
  top_p = float(top_p)
 
162
  seed = seed
163
 
164
  assert 1 <= max_new_tokens <= 384
 
166
  assert 0.0 <= top_p <= 1.0
167
 
168
  if temperature == 0.0:
169
+ temperature = 0.05
170
 
171
+ prompt = f"{username}: " + prompt + "\n"
172
+ print(f"CHAT ({datetime.now()}):\n-------\n{prompt}")
173
  print(f"OUTPUT ({datetime.now()}):\n-------\n")
174
  # Load prompt
175
 
176
  model.loadContext(newctx=intro+prompt)
177
 
178
+ out = model.forward(number=max_new_tokens, stopStrings=["<|endoftext|>",username+":"],temp=temperature,top_p_usual=top_p)
179
 
180
  generated_text = out["output"].lstrip("\n ")
181
  generated_text = generated_text.rstrip("USER:")
 
193
  [
194
  # Question Answering
195
  '''Are humans good or bad?''',"Q/A", 150, 0.8, 0.8, "<|endoftext|>"],
196
+ [
197
+ # Question Answering
198
+ '''What is the purpose of Vitamin A?''',"Q/A", 50, 0.2, 0.8, "<|endoftext|>"],
199
  [
200
  # Chatbot
201
  '''This is a conversation between two AI large language models named Alex and Fritz. They are exploring each other's capabilities, and trying to ask interesting questions of one another to explore the limits of each others AI.
 
253
  inputs=[
254
  gr.Textbox(lines=5, label="Message"), # prompt
255
  "state",
256
+ gr.Text(lines=1, value="USER", label="Your Name", placeholder="Enter your Name"),
257
  gr.Slider(1, 256, value=60), # max_tokens
258
  gr.Slider(0.0, 1.0, value=0.8), # temperature
259
  gr.Slider(0.0, 1.0, value=0.85) # top_p