WinterGYC commited on
Commit
3caea9e
1 Parent(s): 227a9e8

Fix import

Browse files
Files changed (1) hide show
  1. app.py +23 -8
app.py CHANGED
@@ -70,16 +70,31 @@ def parse_text(text):
70
  text = "".join(lines)
71
  return text
72
 
 
73
 
74
  def predict(input, chatbot, max_length, top_p, temperature, history, past_key_values):
 
 
 
75
  chatbot.append((parse_text(input), ""))
76
- for response, history, past_key_values in model.stream_chat(tokenizer, input, history, past_key_values=past_key_values,
77
- return_past_key_values=True,
78
- max_length=max_length, top_p=top_p,
79
- temperature=temperature):
 
 
 
 
 
 
 
 
 
 
80
  chatbot[-1] = (parse_text(input), parse_text(response))
81
 
82
- yield chatbot, history, past_key_values
 
83
 
84
 
85
  def reset_user_input():
@@ -103,9 +118,9 @@ with gr.Blocks() as demo:
103
  submitBtn = gr.Button("Submit", variant="primary")
104
  with gr.Column(scale=1):
105
  emptyBtn = gr.Button("Clear History")
106
- max_length = gr.Slider(0, 32768, value=8192, step=1.0, label="Maximum length", interactive=True)
107
- top_p = gr.Slider(0, 1, value=0.8, step=0.01, label="Top P", interactive=True)
108
- temperature = gr.Slider(0, 1, value=0.95, step=0.01, label="Temperature", interactive=True)
109
 
110
  history = gr.State([])
111
  past_key_values = gr.State(None)
 
70
  text = "".join(lines)
71
  return text
72
 
73
+ stream = True
74
 
75
  def predict(input, chatbot, max_length, top_p, temperature, history, past_key_values):
76
+ model.generation_config.temperature = temperature
77
+ model.generation_config.top_p = top_p
78
+ model.generation_config.max_new_tokens = max_length
79
  chatbot.append((parse_text(input), ""))
80
+
81
+ history.append({"role": "user", "content": parse_text(input)})
82
+ if stream:
83
+ position = 0
84
+ for response in model.chat(tokenizer, history, stream=True):
85
+ chatbot[-1] = (parse_text(input), parse_text(response))
86
+ if torch.backends.mps.is_available():
87
+ torch.mps.empty_cache()
88
+ yield chatbot, history, past_key_values
89
+ print(response)
90
+ history.append({"role": "assistant", "content": response})
91
+ else:
92
+ response = model.chat(tokenizer, history)
93
+ print(response)
94
  chatbot[-1] = (parse_text(input), parse_text(response))
95
 
96
+ yield chatbot, history, past_key_values
97
+
98
 
99
 
100
  def reset_user_input():
 
118
  submitBtn = gr.Button("Submit", variant="primary")
119
  with gr.Column(scale=1):
120
  emptyBtn = gr.Button("Clear History")
121
+ max_length = gr.Slider(0, 2048, value=1024, step=1.0, label="Maximum length", interactive=True)
122
+ top_p = gr.Slider(0, 1, value=0.85, step=0.01, label="Top P", interactive=True)
123
+ temperature = gr.Slider(0, 1, value=0.9, step=0.01, label="Temperature", interactive=True)
124
 
125
  history = gr.State([])
126
  past_key_values = gr.State(None)