Commit
•
5188e86
0
Parent(s):
Super-squash branch 'main' using huggingface_hub
Browse files- .gitattributes +35 -0
- README.md +12 -0
- app.py +90 -0
- model_configs.json +55 -0
- requirements.txt +3 -0
.gitattributes
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
28 |
+
*.tar filter=lfs diff=lfs merge=lfs -text
|
29 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
30 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
31 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
32 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
33 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: Magpie
|
3 |
+
emoji: 🔥
|
4 |
+
colorFrom: red
|
5 |
+
colorTo: indigo
|
6 |
+
sdk: gradio
|
7 |
+
sdk_version: 4.36.1
|
8 |
+
app_file: app.py
|
9 |
+
pinned: false
|
10 |
+
---
|
11 |
+
|
12 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import transformers
|
3 |
+
import torch
|
4 |
+
import json
|
5 |
+
from transformers import AutoTokenizer
|
6 |
+
import os
|
7 |
+
from huggingface_hub import login
|
8 |
+
import spaces
|
9 |
+
|
10 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
11 |
+
login(HF_TOKEN)
|
12 |
+
# Load the model
|
13 |
+
model_id = "meta-llama/Meta-Llama-3-8B-Instruct"
|
14 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id, add_special_tokens=True)
|
15 |
+
|
16 |
+
pipeline = transformers.pipeline(
|
17 |
+
"text-generation",
|
18 |
+
model=model_id,
|
19 |
+
model_kwargs={"torch_dtype": torch.bfloat16},
|
20 |
+
device="cuda",
|
21 |
+
)
|
22 |
+
|
23 |
+
# Load the model configuration
|
24 |
+
with open("model_configs.json", "r") as f:
|
25 |
+
model_configs = json.load(f)
|
26 |
+
model_config = model_configs[model_id]
|
27 |
+
|
28 |
+
# Extract instruction
|
29 |
+
extract_input = model_config["extract_input"]
|
30 |
+
|
31 |
+
|
32 |
+
@spaces.GPU
|
33 |
+
def generate_instruction_response():
|
34 |
+
terminators = [
|
35 |
+
tokenizer.eos_token_id,
|
36 |
+
tokenizer.convert_tokens_to_ids("<|eot_id|>"),
|
37 |
+
]
|
38 |
+
|
39 |
+
instruction = pipeline(
|
40 |
+
extract_input,
|
41 |
+
max_new_tokens=2048,
|
42 |
+
eos_token_id=terminators,
|
43 |
+
do_sample=True,
|
44 |
+
temperature=1,
|
45 |
+
top_p=1,
|
46 |
+
)
|
47 |
+
|
48 |
+
sanitized_instruction = instruction[0]["generated_text"][
|
49 |
+
len(extract_input) :
|
50 |
+
].split("\n")[0]
|
51 |
+
|
52 |
+
response_template = f"""<|begin_of_text|><|start_header_id|>user<|end_header_id|>\n\n{sanitized_instruction}<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n"""
|
53 |
+
|
54 |
+
response = pipeline(
|
55 |
+
response_template,
|
56 |
+
max_new_tokens=2048,
|
57 |
+
eos_token_id=terminators,
|
58 |
+
do_sample=True,
|
59 |
+
temperature=1,
|
60 |
+
top_p=1,
|
61 |
+
)
|
62 |
+
|
63 |
+
user_message = sanitized_instruction
|
64 |
+
assistant_response = response[0]["generated_text"][len(response_template) :]
|
65 |
+
|
66 |
+
return user_message, assistant_response
|
67 |
+
|
68 |
+
|
69 |
+
title = "Magpie demo"
|
70 |
+
description = """
|
71 |
+
This Gradio demo allows you to explore the approach outlined in the Magpie paper. "Magpie is a data synthesis pipeline that generates high-quality alignment data. Magpie does not rely on prompt engineering or seed questions. Instead, it directly constructs instruction data by prompting aligned LLMs with a pre-query template for sampling instructions." Essentially, instead of prompting the model with a question or a starting query, this approach relies on the pre-query template of the model to generate instructions. Essentially, you are giving the model only the template up to the point where a user instruction would start, and then the model generates the instruction and the response.
|
72 |
+
|
73 |
+
In this demo, you can see how the model generates a user instruction and a model response.
|
74 |
+
|
75 |
+
You can learn more about the approach [in the paper](https://huggingface.co/papers/2406.08464).
|
76 |
+
"""
|
77 |
+
# Create the Gradio interface
|
78 |
+
iface = gr.Interface(
|
79 |
+
fn=generate_instruction_response,
|
80 |
+
inputs=[],
|
81 |
+
outputs=[
|
82 |
+
gr.Text(label="Generated User Instruction"),
|
83 |
+
gr.Text(label="Generated Model Response"),
|
84 |
+
],
|
85 |
+
title=title,
|
86 |
+
description=description,
|
87 |
+
)
|
88 |
+
|
89 |
+
# Launch the app
|
90 |
+
iface.launch(debug=True)
|
model_configs.json
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"meta-llama/Meta-Llama-3-8B-Instruct": {
|
3 |
+
"model_name": "meta-llama/Meta-Llama-3-8B-Instruct",
|
4 |
+
"stop_tokens": [
|
5 |
+
"<|eot_id|>",
|
6 |
+
"<|end_of_text|>",
|
7 |
+
"<|starter_header_id|>",
|
8 |
+
"<|end_header_id|>",
|
9 |
+
"assistant"
|
10 |
+
],
|
11 |
+
"stop_token_ids": [
|
12 |
+
128009,
|
13 |
+
128001,
|
14 |
+
128006,
|
15 |
+
128007,
|
16 |
+
78191
|
17 |
+
],
|
18 |
+
"extract_input": "<|begin_of_text|><|start_header_id|>user<|end_header_id|>\n\n",
|
19 |
+
"extract_input_with_system_prompt": "<|begin_of_text|><|start_header_id|>system<|end_header_id|>\n\nA chat between a curious user and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the user's questions.<|eot_id|><|start_header_id|>user<|end_header_id|>\n\n"
|
20 |
+
},
|
21 |
+
"meta-llama/Meta-Llama-3-70B-Instruct": {
|
22 |
+
"model_name": "meta-llama/Meta-Llama-3-70B-Instruct",
|
23 |
+
"stop_tokens": [
|
24 |
+
"<|eot_id|>",
|
25 |
+
"<|end_of_text|>",
|
26 |
+
"<|starter_header_id|>",
|
27 |
+
"<|end_header_id|>",
|
28 |
+
"assistant"
|
29 |
+
],
|
30 |
+
"stop_token_ids": [
|
31 |
+
128009,
|
32 |
+
128001,
|
33 |
+
128006,
|
34 |
+
128007,
|
35 |
+
78191
|
36 |
+
],
|
37 |
+
"extract_input": "<|begin_of_text|><|start_header_id|>user<|end_header_id|>\n\n"
|
38 |
+
},
|
39 |
+
"meta-llama/Llama-2-7b-chat-hf": {
|
40 |
+
"model_name": "meta-llama/Llama-2-7b-chat-hf",
|
41 |
+
"stop_tokens": [
|
42 |
+
"</s>",
|
43 |
+
"<s>",
|
44 |
+
"<unk>",
|
45 |
+
"assistant"
|
46 |
+
],
|
47 |
+
"stop_token_ids": [
|
48 |
+
2,
|
49 |
+
1,
|
50 |
+
0,
|
51 |
+
20255
|
52 |
+
],
|
53 |
+
"extract_input": "[INST] "
|
54 |
+
}
|
55 |
+
}
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
transformers[torch]
|
2 |
+
accelerate
|
3 |
+
gradio
|