Spaces:
Runtime error
Runtime error
updating again
Browse files
app.py
CHANGED
@@ -1,59 +1,57 @@
|
|
1 |
-
import
|
2 |
-
from
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
top_p,
|
14 |
-
):
|
15 |
-
messages = [{"role": "system", "content": system_message}]
|
16 |
-
|
17 |
-
for val in history:
|
18 |
-
if val[0]:
|
19 |
-
messages.append({"role": "user", "content": val[0]})
|
20 |
-
if val[1]:
|
21 |
-
messages.append({"role": "assistant", "content": val[1]})
|
22 |
-
|
23 |
-
messages.append({"role": "user", "content": message})
|
24 |
-
|
25 |
-
response = ""
|
26 |
-
|
27 |
-
for message in client.chat_completion(
|
28 |
-
messages,
|
29 |
-
max_tokens=max_tokens,
|
30 |
-
stream=True,
|
31 |
-
temperature=temperature,
|
32 |
-
top_p=top_p,
|
33 |
-
):
|
34 |
-
token = message.choices[0].delta.content
|
35 |
-
|
36 |
-
response += token
|
37 |
-
yield response
|
38 |
-
|
39 |
-
"""
|
40 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
41 |
-
"""
|
42 |
-
demo = gr.ChatInterface(
|
43 |
-
respond,
|
44 |
-
additional_inputs=[
|
45 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
46 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
47 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
48 |
-
gr.Slider(
|
49 |
-
minimum=0.1,
|
50 |
-
maximum=1.0,
|
51 |
-
value=0.95,
|
52 |
-
step=0.05,
|
53 |
-
label="Top-p (nucleus sampling)",
|
54 |
-
),
|
55 |
-
],
|
56 |
)
|
57 |
|
58 |
-
|
59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextStreamer
|
2 |
+
from transformers import pipeline
|
3 |
+
import torch
|
4 |
+
|
5 |
+
model_id = "MaziyarPanahi/Llama-3-70B-Instruct-DPO-v0.2"
|
6 |
+
|
7 |
+
model = AutoModelForCausalLM.from_pretrained(
|
8 |
+
model_id,
|
9 |
+
torch_dtype=torch.bfloat16,
|
10 |
+
device_map="auto",
|
11 |
+
trust_remote_code=True,
|
12 |
+
# attn_implementation="flash_attention_2"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
)
|
14 |
|
15 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
16 |
+
model_id,
|
17 |
+
trust_remote_code=True
|
18 |
+
)
|
19 |
+
|
20 |
+
streamer = TextStreamer(tokenizer)
|
21 |
+
|
22 |
+
pipeline = pipeline(
|
23 |
+
"text-generation",
|
24 |
+
model=model,
|
25 |
+
tokenizer=tokenizer,
|
26 |
+
model_kwargs={"torch_dtype": torch.bfloat16},
|
27 |
+
streamer=streamer
|
28 |
+
)
|
29 |
+
|
30 |
+
# Then you can use the pipeline to generate text.
|
31 |
+
|
32 |
+
messages = [
|
33 |
+
{"role": "system", "content": "You are a pirate chatbot who always responds in pirate speak!"},
|
34 |
+
{"role": "user", "content": "Who are you?"},
|
35 |
+
]
|
36 |
+
|
37 |
+
prompt = tokenizer.apply_chat_template(
|
38 |
+
messages,
|
39 |
+
tokenize=False,
|
40 |
+
add_generation_prompt=True
|
41 |
+
)
|
42 |
+
|
43 |
+
terminators = [
|
44 |
+
tokenizer.eos_token_id,
|
45 |
+
tokenizer.convert_tokens_to_ids("<|im_end|>"),
|
46 |
+
tokenizer.convert_tokens_to_ids("<|eot_id|>") # safer to have this too
|
47 |
+
]
|
48 |
+
|
49 |
+
outputs = pipeline(
|
50 |
+
prompt,
|
51 |
+
max_new_tokens=2048,
|
52 |
+
eos_token_id=terminators,
|
53 |
+
do_sample=True,
|
54 |
+
temperature=0.6,
|
55 |
+
top_p=0.95,
|
56 |
+
)
|
57 |
+
print(outputs[0]["generated_text"][len(prompt):])
|