iofu728 commited on
Commit
0db3dd6
β€’
1 Parent(s): 533e3ac

Feature(LLMLingua): build the LLMLingua space demo

Browse files
Files changed (2) hide show
  1. app.py +144 -0
  2. requirements.txt +1 -0
app.py ADDED
@@ -0,0 +1,144 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from llmlingua import PromptCompressor
3
+
4
+ llm_lingua = PromptCompressor("gpt2", "cpu")
5
+
6
+ INTRO = """
7
+ # LLMLingua: Compressing Prompts for Accelerated Inference of Large Language Models
8
+ This is an early demo of the prompt compression method LLMLingua.
9
+ To use it, upload your prompt and set the compression target.
10
+ 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.
11
+ - 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.
12
+ - 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.
13
+ - 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.
14
+ 2. βœ… Set the target_token or compression ratio.
15
+ 3. πŸ€” Try experimenting with different target compression ratios or other hyperparameters to optimize the performance.
16
+
17
+ You can check our [repo](https://aka.ms/LLMLingua)!
18
+ """
19
+
20
+ custom_css = """
21
+ #image-upload {
22
+ flex-grow: 1;
23
+ }
24
+ #params .tabs {
25
+ display: flex;
26
+ flex-direction: column;
27
+ flex-grow: 1;
28
+ }
29
+ #params .tabitem[style="display: block;"] {
30
+ flex-grow: 1;
31
+ display: flex !important;
32
+ }
33
+ #params .gap {
34
+ flex-grow: 1;
35
+ }
36
+ #params .form {
37
+ flex-grow: 1 !important;
38
+ }
39
+ #params .form > :last-child{
40
+ flex-grow: 1;
41
+ }
42
+ .md ol, .md ul {
43
+ margin-left: 1rem;
44
+ }
45
+ .md img {
46
+ margin-bottom: 1rem;
47
+ }
48
+ """
49
+
50
+ def compress_prompt(context, instruct, question, ratio, target_token):
51
+ compressed_prompt = llm_lingua.compress_prompt(context.split("\n\n"), instruction, question, ratio, target_token)
52
+
53
+ return [compressed_prompt[key] for key in ["compressed_prompt", "origin_tokens", "compressed_tokens", "ratio", "saving"]]
54
+
55
+ iface = gr.Interface(fn=compress_prompt, inputs="text", outputs="text")
56
+ iface.launch()
57
+
58
+
59
+ with gr.Blocks(css=custom_css) as iface:
60
+ gr.Markdown(INTRO)
61
+
62
+ with gr.Row():
63
+ with gr.Column(elem_id="params"):
64
+ with gr.Tab('Prompts'):
65
+ instruction = gr.Textbox(
66
+ label="Instruction",
67
+ lines=3,
68
+ value="",
69
+ )
70
+ context = gr.Textbox(
71
+ label="Context",
72
+ lines=3,
73
+ value="",
74
+ )
75
+ question = gr.Textbox(
76
+ label="Question",
77
+ lines=3,
78
+ value="",
79
+ )
80
+ with gr.Tab('Compression Target'):
81
+ ratio = gr.Textbox(
82
+ label="Compression Ratio",
83
+ value=0.5,
84
+ )
85
+ target_token = gr.Textbox(
86
+ label="Target Token",
87
+ value=-1,
88
+ )
89
+
90
+ gen_button = gr.Button(value="Compress Prompt!", variant="primary")
91
+
92
+ # with gr.Tab('Results'):
93
+ # results = gr.Gallery(
94
+ # show_label=False,
95
+ # object_fit="contain",
96
+ # columns=4
97
+ # )
98
+
99
+ with gr.Row():
100
+ with gr.Column(elem_id="Results"):
101
+ with gr.Tab('Compressed Prompts'):
102
+ compressed_prompt = gr.Textbox(
103
+ label="compressed_prompt",
104
+ lines=5,
105
+ )
106
+ with gr.Tab('Saving'):
107
+ origin_tokens = gr.Textbox(
108
+ label="The tokens number of original prompt",
109
+ )
110
+ compressed_tokens = gr.Textbox(
111
+ label="The tokens number of compressed prompt",
112
+ )
113
+ saving_ratio = gr.Textbox(
114
+ label="Actual Compression Ratio",
115
+ )
116
+ saving = gr.Textbox(
117
+ label="Saving Cost",
118
+ )
119
+
120
+
121
+ # gr.Examples(
122
+ # examples=EXAMPLES,
123
+ # inputs=[image_upload, positive_prompt, negative_prompt],
124
+ # )
125
+
126
+ gen_button.click(
127
+ fn=compress_prompt,
128
+ inputs=[
129
+ context,
130
+ instruction,
131
+ question,
132
+ ratio,
133
+ target_token
134
+ ],
135
+ outputs=[
136
+ compressed_prompt,
137
+ origin_tokens,
138
+ compressed_tokens,
139
+ saving_ratio,
140
+ saving
141
+ ],
142
+ )
143
+
144
+ iface.queue(max_size=10, api_open=False).launch(show_api=False)
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ llmlingua