Spaces:
Running
on
Zero
Running
on
Zero
Add Qwen2.5-7B-Instruct-1M
Browse files
app.py
CHANGED
|
@@ -24,13 +24,17 @@ press_dict = {
|
|
| 24 |
"TOVAPress": TOVAPress,
|
| 25 |
}
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
@spaces.GPU
|
| 29 |
-
def process_request(url, question, press_name, compression_ratio):
|
| 30 |
""" """
|
| 31 |
|
| 32 |
if press_name not in press_dict:
|
| 33 |
-
return f"Invalid press
|
| 34 |
|
| 35 |
# Fetch the Wikipedia article
|
| 36 |
try:
|
|
@@ -45,8 +49,8 @@ def process_request(url, question, press_name, compression_ratio):
|
|
| 45 |
|
| 46 |
# Initialize the press
|
| 47 |
press = press_dict[press_name](compression_ratio)
|
| 48 |
-
num_tokens =
|
| 49 |
-
pred_answer =
|
| 50 |
|
| 51 |
return pred_answer, num_tokens, int(num_tokens * (1 - compression_ratio))
|
| 52 |
except Exception as e:
|
|
@@ -61,13 +65,12 @@ def gradio_interface():
|
|
| 61 |
gr.Markdown(
|
| 62 |
"""
|
| 63 |
# Wikipedia Article Question Answering with kvpress
|
| 64 |
-
This demo uses the llama 3.1 8B Instruct model to answer questions about any given Wikipedia article.
|
| 65 |
Under the hood, [kvpress](https://github.com/NVIDIA/kvpress) *compresses the key-value (KV) cache* associated with the article, helping reduce memory usage and accelerate decoding.
|
| 66 |
-
|
| 67 |
**How to use:**
|
| 68 |
-
1. Enter a Wikipedia article URL
|
| 69 |
2. Type your question
|
| 70 |
-
3. Select a press
|
| 71 |
4. Press "Submit" to see the answer, along with token statistics before and after compression
|
| 72 |
"""
|
| 73 |
)
|
|
@@ -77,10 +80,17 @@ def gradio_interface():
|
|
| 77 |
question_input = gr.Textbox(label="Question", placeholder="Type your question here")
|
| 78 |
|
| 79 |
with gr.Row():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
press_selector = gr.Dropdown(
|
| 81 |
choices=list(press_dict.keys()),
|
| 82 |
value="ExpectedAttentionPress",
|
| 83 |
-
label="Select Press
|
| 84 |
)
|
| 85 |
compression_slider = gr.Slider(minimum=0.0, maximum=0.9, step=0.1, value=0.5, label="Compression Ratio")
|
| 86 |
|
|
@@ -104,7 +114,7 @@ def gradio_interface():
|
|
| 104 |
"ExpectedAttentionPress",
|
| 105 |
0.5,
|
| 106 |
],
|
| 107 |
-
|
| 108 |
"https://en.wikipedia.org/wiki/World_Chess_Championship_2024",
|
| 109 |
"On which move did the world chess championship end?",
|
| 110 |
"ExpectedAttentionPress",
|
|
@@ -116,7 +126,7 @@ def gradio_interface():
|
|
| 116 |
|
| 117 |
submit_button.click(
|
| 118 |
process_request,
|
| 119 |
-
inputs=[url_input, question_input, press_selector, compression_slider],
|
| 120 |
outputs=[output, output_num_tokens, output_compressed_num_tokens],
|
| 121 |
)
|
| 122 |
|
|
@@ -125,11 +135,6 @@ def gradio_interface():
|
|
| 125 |
|
| 126 |
if __name__ == "__main__":
|
| 127 |
|
| 128 |
-
# Load pipeline
|
| 129 |
-
device = "cuda:0"
|
| 130 |
-
ckpt = "meta-llama/Meta-Llama-3.1-8B-Instruct"
|
| 131 |
-
pipe = pipeline("kv-press-text-generation", model=ckpt, device=device, torch_dtype="auto")
|
| 132 |
-
|
| 133 |
# Launch demo
|
| 134 |
demo = gradio_interface()
|
| 135 |
demo.launch()
|
|
|
|
| 24 |
"TOVAPress": TOVAPress,
|
| 25 |
}
|
| 26 |
|
| 27 |
+
pipe_dict = dict(
|
| 28 |
+
(ckpt, pipeline("kv-press-text-generation", model=ckpt, device="cuda:0", torch_dtype="auto"))
|
| 29 |
+
for ckpt in ["meta-llama/Meta-Llama-3.1-8B-Instruct", "Qwen/Qwen2.5-7B-Instruct-1M"]
|
| 30 |
+
)
|
| 31 |
|
| 32 |
@spaces.GPU
|
| 33 |
+
def process_request(url, question, press_name, pipe_name, compression_ratio):
|
| 34 |
""" """
|
| 35 |
|
| 36 |
if press_name not in press_dict:
|
| 37 |
+
return f"Invalid press selected: {press_name}", -1, -1
|
| 38 |
|
| 39 |
# Fetch the Wikipedia article
|
| 40 |
try:
|
|
|
|
| 49 |
|
| 50 |
# Initialize the press
|
| 51 |
press = press_dict[press_name](compression_ratio)
|
| 52 |
+
num_tokens = pipe_dict[pipe_name].tokenizer(context, return_tensors="pt")["input_ids"].shape[1]
|
| 53 |
+
pred_answer = pipe_dict[pipe_name](context, question=question, press=press)["answer"]
|
| 54 |
|
| 55 |
return pred_answer, num_tokens, int(num_tokens * (1 - compression_ratio))
|
| 56 |
except Exception as e:
|
|
|
|
| 65 |
gr.Markdown(
|
| 66 |
"""
|
| 67 |
# Wikipedia Article Question Answering with kvpress
|
| 68 |
+
This demo uses the llama 3.1 8B Instruct model to answer questions about any given Wikipedia article.
|
| 69 |
Under the hood, [kvpress](https://github.com/NVIDIA/kvpress) *compresses the key-value (KV) cache* associated with the article, helping reduce memory usage and accelerate decoding.
|
|
|
|
| 70 |
**How to use:**
|
| 71 |
+
1. Enter a Wikipedia article URL
|
| 72 |
2. Type your question
|
| 73 |
+
3. Select a model, a press and the desired compression ratio
|
| 74 |
4. Press "Submit" to see the answer, along with token statistics before and after compression
|
| 75 |
"""
|
| 76 |
)
|
|
|
|
| 80 |
question_input = gr.Textbox(label="Question", placeholder="Type your question here")
|
| 81 |
|
| 82 |
with gr.Row():
|
| 83 |
+
|
| 84 |
+
pipe_selector = gr.Dropdown(
|
| 85 |
+
choices=list(pipe_dict.keys()),
|
| 86 |
+
value="meta-llama/Meta-Llama-3.1-8B-Instruct",
|
| 87 |
+
label="Select Model",
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
press_selector = gr.Dropdown(
|
| 91 |
choices=list(press_dict.keys()),
|
| 92 |
value="ExpectedAttentionPress",
|
| 93 |
+
label="Select Press",
|
| 94 |
)
|
| 95 |
compression_slider = gr.Slider(minimum=0.0, maximum=0.9, step=0.1, value=0.5, label="Compression Ratio")
|
| 96 |
|
|
|
|
| 114 |
"ExpectedAttentionPress",
|
| 115 |
0.5,
|
| 116 |
],
|
| 117 |
+
[
|
| 118 |
"https://en.wikipedia.org/wiki/World_Chess_Championship_2024",
|
| 119 |
"On which move did the world chess championship end?",
|
| 120 |
"ExpectedAttentionPress",
|
|
|
|
| 126 |
|
| 127 |
submit_button.click(
|
| 128 |
process_request,
|
| 129 |
+
inputs=[url_input, question_input, press_selector, pipe_selector, compression_slider],
|
| 130 |
outputs=[output, output_num_tokens, output_compressed_num_tokens],
|
| 131 |
)
|
| 132 |
|
|
|
|
| 135 |
|
| 136 |
if __name__ == "__main__":
|
| 137 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
# Launch demo
|
| 139 |
demo = gradio_interface()
|
| 140 |
demo.launch()
|