LLMLingua / app.py
iofu728's picture
Feature(LLMLingua): changing the style
4d19dd8
raw
history blame
No virus
5.32 kB
import gradio as gr
from llmlingua import PromptCompressor
llm_lingua = PromptCompressor("lgaalves/gpt2-dolly", device_map="cpu")
INTRO = """
# LLMLingua: Compressing Prompts for Accelerated Inference of Large Language Models
This is an early demo of the prompt compression method LLMLingua.
It should be noted that due to limited resources, we only provide the **GPT2-Small** size language model in this demo. Using the **LLaMA2-7B** as a small language model would result in a significant performance improvement, especially at high compression ratios.
To use it, upload your prompt and set the compression target.
1. βœ… Set the different components of the prompt separately, including instruction, context, and question. Leave the corresponding field empty if a particular component does not exist.
- Question: This refers to the directives given by the user to the LLMs, such as inquiries, questions, or requests. Positioned after the instruction and context modules, the question module has a high sensitivity to compression.
- Context: This module provides the supplementary context needed to address the question, such as documents, demonstrations, web search results, or API call results. Located between the instruction and question modules, its sensitivity to compression is relatively low.
- Instruction: This module consists of directives given by the user to the LLMs, such as task descriptions. Placed before the instruction and context modules, the instruction module exhibits a high sensitivity to compression.
2. βœ… Set the target_token or compression ratio.
3. πŸ€” Try experimenting with different target compression ratios or other hyperparameters to optimize the performance.
You can check our [repo](https://aka.ms/LLMLingua)!
"""
custom_css = """
#image-upload {
flex-grow: 1;
}
#params .tabs {
display: flex;
flex-direction: column;
flex-grow: 1;
}
#params .tabitem[style="display: block;"] {
flex-grow: 1;
display: flex !important;
}
#params .gap {
flex-grow: 1;
}
#params .form {
flex-grow: 1 !important;
}
#params .form > :last-child{
flex-grow: 1;
}
.md ol, .md ul {
margin-left: 1rem;
}
.md img {
margin-bottom: 1rem;
}
"""
def compress_prompt(context, instruction, question, ratio, target_token):
context, instruction, question = context.replace("\\n", "\n"), instruction.replace("\\n", "\n"), question.replace("\\n", "\n")
compressed_prompt = llm_lingua.compress_prompt(context.split("\n\n"), instruction, question, float(ratio), float(target_token))
return [compressed_prompt[key] for key in ["compressed_prompt", "origin_tokens", "compressed_tokens", "ratio", "saving"]]
with gr.Blocks(css=custom_css) as iface:
gr.Markdown(INTRO)
with gr.Row():
with gr.Column(elem_id="prompt", scale=2):
with gr.Tab('Prompts'):
instruction = gr.Textbox(
label="Instruction",
lines=1,
value="",
)
context = gr.Textbox(
label="Context",
lines=3,
value="",
)
question = gr.Textbox(
label="Question",
lines=1,
value="",
)
with gr.Column(elem_id="params", scale=1):
with gr.Tab('Compression Target'):
target_token = gr.Textbox(
label="Target Token (To use this, set Compression Ratio to 0)",
value=200,
)
ratio = gr.Textbox(
label="Compression Ratio (To use this, set Target Token to -1)",
value=0,
)
gen_button = gr.Button(value="Compress Prompt!", variant="primary")
with gr.Row():
with gr.Column(elem_id="Results", scale=2):
with gr.Tab('Compressed Prompts'):
compressed_prompt = gr.Textbox(
label="compressed_prompt",
lines=5,
)
with gr.Column(elem_id="Results_2", scale=1):
with gr.Tab('Saving'):
origin_tokens = gr.Textbox(
label="The tokens number of original prompt",
)
compressed_tokens = gr.Textbox(
label="The tokens number of compressed prompt",
)
saving_ratio = gr.Textbox(
label="Actual Compression Ratio",
)
saving = gr.Textbox(
label="Saving Cost",
)
# gr.Examples(
# examples=EXAMPLES,
# inputs=[image_upload, positive_prompt, negative_prompt],
# )
gen_button.click(
fn=compress_prompt,
inputs=[
context,
instruction,
question,
ratio,
target_token
],
outputs=[
compressed_prompt,
origin_tokens,
compressed_tokens,
saving_ratio,
saving
],
)
iface.queue(max_size=10, api_open=False).launch(show_api=False)