Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,74 +1,43 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
)
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
text = tokenizer.apply_chat_template(
|
37 |
-
messages,
|
38 |
-
tokenize=False,
|
39 |
-
add_generation_prompt=True
|
40 |
-
)
|
41 |
-
model_inputs = tokenizer([text], return_tensors="pt").to(device)
|
42 |
-
|
43 |
-
streamer = gr.utils.StreamingTextStreamer(tokenizer, skip_special_tokens=True, skip_prompt=True)
|
44 |
-
|
45 |
-
_ = model.generate(
|
46 |
-
model_inputs.input_ids,
|
47 |
-
max_new_tokens = max_tokens,
|
48 |
-
temperature = temperature,
|
49 |
-
top_p = top_p,
|
50 |
repetition_penalty=repetition_penalty,
|
51 |
-
streamer=streamer
|
52 |
)
|
|
|
|
|
53 |
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
additional_inputs=[
|
59 |
-
gr.Textbox(value="You are Magpie, a friendly Chatbot.", label="System message"),
|
60 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
61 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.6, step=0.1, label="Temperature"),
|
62 |
-
gr.Slider(
|
63 |
-
minimum=0.1,
|
64 |
-
maximum=1.0,
|
65 |
-
value=0.9,
|
66 |
-
step=0.05,
|
67 |
-
label="Top-p (nucleus sampling)",
|
68 |
-
),
|
69 |
-
gr.Slider(minimum=0.5, maximum=1.5, value=1.0, step=0.1, label="Repetation Penalty"),
|
70 |
-
],
|
71 |
-
)
|
72 |
-
|
73 |
-
if __name__ == "__main__":
|
74 |
-
demo.launch(share=True)
|
|
|
1 |
+
@spaces.GPU
|
2 |
+
def generate(
|
3 |
+
message: str,
|
4 |
+
chat_history: list[tuple[str, str]],
|
5 |
+
system_prompt: str,
|
6 |
+
max_new_tokens: int = 1024,
|
7 |
+
temperature: float = 0.6,
|
8 |
+
top_p: float = 0.9,
|
9 |
+
top_k: int = 50,
|
10 |
+
repetition_penalty: float = 1.2,
|
11 |
+
) -> Iterator[str]:
|
12 |
+
conversation = []
|
13 |
+
if system_prompt:
|
14 |
+
conversation.append({"role": "system", "content": system_prompt})
|
15 |
+
for user, assistant in chat_history:
|
16 |
+
conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}])
|
17 |
+
conversation.append({"role": "user", "content": message})
|
18 |
+
|
19 |
+
input_ids = tokenizer.apply_chat_template(conversation, return_tensors="pt")
|
20 |
+
if input_ids.shape[1] > MAX_INPUT_TOKEN_LENGTH:
|
21 |
+
input_ids = input_ids[:, -MAX_INPUT_TOKEN_LENGTH:]
|
22 |
+
gr.Warning(f"Trimmed input from conversation as it was longer than {MAX_INPUT_TOKEN_LENGTH} tokens.")
|
23 |
+
input_ids = input_ids.to(model.device)
|
24 |
+
|
25 |
+
streamer = TextIteratorStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True)
|
26 |
+
generate_kwargs = dict(
|
27 |
+
{"input_ids": input_ids},
|
28 |
+
streamer=streamer,
|
29 |
+
max_new_tokens=max_new_tokens,
|
30 |
+
do_sample=True,
|
31 |
+
top_p=top_p,
|
32 |
+
top_k=top_k,
|
33 |
+
temperature=temperature,
|
34 |
+
num_beams=1,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
repetition_penalty=repetition_penalty,
|
|
|
36 |
)
|
37 |
+
t = Thread(target=model.generate, kwargs=generate_kwargs)
|
38 |
+
t.start()
|
39 |
|
40 |
+
outputs = []
|
41 |
+
for text in streamer:
|
42 |
+
outputs.append(text)
|
43 |
+
yield "".join(outputs)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|