qminh369 commited on
Commit
27dfbbe
1 Parent(s): 0209b48

Upload 3 files

Browse files
app.py ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import json
3
+ from llmlingua import PromptCompressor
4
+ import tiktoken
5
+
6
+ compressors = {
7
+ "xlm-roberta": PromptCompressor(
8
+ #model_name="microsoft/llmlingua-2-xlm-roberta-large-meetingbank",
9
+ model_name='qminh369/token-classification-llmlingua2-xlm-roberta-42k_merge_1_epoch',
10
+ use_llmlingua2=True,
11
+ device_map="cpu"
12
+ )
13
+ }
14
+
15
+ tokenizer = tiktoken.encoding_for_model("gpt-4")
16
+
17
+ with open('data/benchmark_33_bctn_so_lieu_5context.json', 'r') as f:
18
+ examples = json.load(f)
19
+
20
+ def compress(original_prompt, compression_rate, base_model="xlm-roberta-large", force_tokens=['\n'], chunk_end_tokens=['.', '\n']):
21
+ if '\\n' in force_tokens:
22
+ idx = force_tokens.index('\\n')
23
+ force_tokens[idx] = '\n'
24
+
25
+ compressor = compressors.get(base_model, compressors["mbert-base"])
26
+ results = compressor.compress_prompt_llmlingua2(
27
+ original_prompt,
28
+ rate=compression_rate,
29
+ force_tokens=force_tokens,
30
+ chunk_end_tokens=chunk_end_tokens,
31
+ return_word_label=True,
32
+ drop_consecutive=True
33
+ )
34
+
35
+ compressed_prompt = results["compressed_prompt"]
36
+ n_word_compressed = len(tokenizer.encode(compressed_prompt))
37
+
38
+ word_sep = "\t\t|\t\t"
39
+ label_sep = " "
40
+ lines = results["fn_labeled_original_prompt"].split(word_sep)
41
+ preserved_tokens = []
42
+ for line in lines:
43
+ word, label = line.split(label_sep)
44
+ preserved_tokens.append((word, '+') if label == '1' else (word, None))
45
+
46
+ return compressed_prompt, preserved_tokens, n_word_compressed
47
+
48
+ title = "LLMLingua-2"
49
+
50
+ header = """# LLMLingua-2
51
+ """
52
+
53
+ theme = "soft"
54
+ css = """#anno-img .mask {opacity: 0.5; transition: all 0.2s ease-in-out;}
55
+ #anno-img .mask.active {opacity: 0.7}"""
56
+
57
+ original_prompt_text = """
58
+ """
59
+
60
+ with gr.Blocks(title=title, css=css) as app:
61
+ gr.Markdown(header)
62
+ with gr.Row():
63
+ with gr.Column(scale=3):
64
+ original_prompt = gr.Textbox(value=original_prompt_text, label="Original Prompt", lines=10, max_lines=10, interactive=True)
65
+ compressed_prompt = gr.Textbox(value='', label="Compressed Prompt", lines=10, max_lines=10, interactive=False)
66
+
67
+ with gr.Column(scale=1):
68
+ base_model = gr.Radio(["xlm-roberta"], label="Base Model", value="xlm-roberta", interactive=True)
69
+ force_tokens = gr.Dropdown(['\\n', '.', '!', '?', ','],
70
+ label="Tokens to Preserve",
71
+ value=['\\n', '.', '!', '?', ','],
72
+ multiselect=True,
73
+ interactive=True)
74
+ compression_rate = gr.Slider(minimum=0.1, maximum=1.0, step=0.1, value=0.7, label="Compression rate", info="after compr. / befor compr.", interactive=True)
75
+ n_word_original = gr.Textbox(lines=1, label="Original (GPT-4 Tokens)", interactive=False, value=len(tokenizer.encode(original_prompt_text)))
76
+ n_word_compressed = gr.Textbox(lines=1, label="Compressed (GPT-4 Tokens)", interactive=False)
77
+ button = gr.Button("⚡Click to Compress")
78
+ with gr.Accordion(label="Compression Details", open=False):
79
+ diff_text = gr.HighlightedText(label="Diff", combine_adjacent=False, show_legend=True, color_map={"+": "green"})
80
+
81
+ original_prompt.change(lambda x: len(tokenizer.encode(x)), inputs=[original_prompt], outputs=[n_word_original])
82
+ original_prompt.change(lambda x: ("", "", []), inputs=[original_prompt], outputs=[compressed_prompt, n_word_compressed, diff_text])
83
+
84
+ button.click(fn=compress,
85
+ inputs=[original_prompt, compression_rate, base_model, force_tokens],
86
+ outputs=[compressed_prompt, diff_text, n_word_compressed])
87
+
88
+ app.queue(max_size=10, api_open=False).launch(show_api=False)
data/benchmark_33_bctn_so_lieu_5context.json ADDED
The diff for this file is too large to render. See raw diff
 
requirements.txt ADDED
File without changes