Spaces:
Sleeping
Sleeping
cornzz
commited on
Commit
·
020cd59
1
Parent(s):
5ede078
Create gradio demo
Browse files- .gitignore +3 -1
- demo.py +52 -0
- prompt.txt +3 -0
- requirements.txt +129 -0
- test.ipynb +84 -0
- test.py +0 -10
.gitignore
CHANGED
@@ -1 +1,3 @@
|
|
1 |
-
venv/
|
|
|
|
|
|
1 |
+
venv/
|
2 |
+
flagged/
|
3 |
+
__pycache__/
|
demo.py
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from llmlingua import PromptCompressor
|
2 |
+
import gradio as gr
|
3 |
+
import torch
|
4 |
+
import pandas as pd
|
5 |
+
|
6 |
+
llm_lingua = PromptCompressor(
|
7 |
+
model_name="microsoft/llmlingua-2-xlm-roberta-large-meetingbank",
|
8 |
+
use_llmlingua2=True,
|
9 |
+
device_map="mps" if torch.backends.mps.is_available() else "cuda" if torch.cuda.is_available() else "cpu",
|
10 |
+
)
|
11 |
+
|
12 |
+
|
13 |
+
def compress_prompt(prompt, rate):
|
14 |
+
result = llm_lingua.compress_prompt(prompt, rate=rate)
|
15 |
+
|
16 |
+
return (
|
17 |
+
result["compressed_prompt"],
|
18 |
+
pd.DataFrame(
|
19 |
+
{
|
20 |
+
"Original / Compressed Tokens": [f'{result["origin_tokens"]} / {result["compressed_tokens"]}'],
|
21 |
+
"Ratio": [result["ratio"]],
|
22 |
+
"Rate": [result["rate"]],
|
23 |
+
"Saving": [result["saving"]],
|
24 |
+
}
|
25 |
+
),
|
26 |
+
)
|
27 |
+
|
28 |
+
|
29 |
+
demo = gr.Interface(
|
30 |
+
fn=compress_prompt,
|
31 |
+
inputs=[gr.Textbox(lines=10, label="Prompt"), gr.Slider(0.1, 1, 0.5, label="Rate")],
|
32 |
+
outputs=[
|
33 |
+
gr.Textbox(lines=10, label="Compressed Prompt"),
|
34 |
+
gr.Dataframe(label="Metrics", headers=["Original / compressed tokens", "Ratio", "Rate", "Saving"], row_count=1),
|
35 |
+
],
|
36 |
+
title="Prompt Compressor",
|
37 |
+
description="Compress a prompt using LLM-Lingua.",
|
38 |
+
allow_flagging="never",
|
39 |
+
examples=[
|
40 |
+
[
|
41 |
+
"John: So, um, I've been thinking about the project, you know, and I believe we need to, uh, make some changes. I mean, we want the project to succeed, right? So, like, I think we should consider maybe revising the timeline. Sarah: I totally agree, John. I mean, we have to be realistic, you know. The timeline is, like, too tight. You know what I mean? We should definitely extend it.",
|
42 |
+
0.3,
|
43 |
+
],
|
44 |
+
[
|
45 |
+
"Item 15, report from City Manager Recommendation to adopt three resolutions. First, to join the Victory Pace program. Second, to join the California first program. And number three, consenting to to inclusion of certain properties within the jurisdiction in the California Hero program. It was emotion, motion, a second and public comment. CNN. Please cast your vote. Oh. Was your public comment? Yeah. Please come forward. I thank you, Mr. Mayor. Thank you. Members of the council. My name is Alex Mitchell. I represent the hero program. Just wanted to let you know that the hero program. Has been in California for the last three and a half years. We’re in. Over 20. We’re in 28 counties, and we’ve completed over 29,000 energy efficient projects to make homes. Greener and more energy efficient. And this includes anything. From solar to water. Efficiency. We’ve done. Almost.$550 million in home improvements.",
|
46 |
+
0.5,
|
47 |
+
],
|
48 |
+
],
|
49 |
+
)
|
50 |
+
|
51 |
+
|
52 |
+
demo.launch()
|
prompt.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
The fn argument is very flexible -- you can pass any Python function that you want to wrap with a UI. In the example above, we saw a relatively simple function, but the function could be anything from a music generator to a tax calculator to the prediction function of a pretrained machine learning model.
|
2 |
+
|
3 |
+
The inputs and outputs arguments take one or more Gradio components. As we'll see, Gradio includes more than 30 built-in components (such as the gr.Textbox(), gr.Image(), and gr.HTML() components) that are designed for machine learning applications.
|
requirements.txt
CHANGED
@@ -1,30 +1,159 @@
|
|
1 |
accelerate==0.32.1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
certifi==2024.7.4
|
|
|
3 |
charset-normalizer==3.3.2
|
4 |
click==8.1.7
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
filelock==3.15.4
|
|
|
|
|
6 |
fsspec==2024.6.1
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
huggingface-hub==0.23.5
|
8 |
idna==3.7
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
Jinja2==3.1.4
|
10 |
joblib==1.4.2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
llmlingua==0.2.2
|
|
|
12 |
MarkupSafe==2.1.5
|
|
|
|
|
|
|
|
|
13 |
mpmath==1.3.0
|
|
|
|
|
|
|
|
|
14 |
networkx==3.3
|
15 |
nltk==3.8.1
|
|
|
|
|
16 |
numpy==1.26.4
|
|
|
|
|
17 |
packaging==24.1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
psutil==6.0.0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
PyYAML==6.0.1
|
|
|
|
|
|
|
|
|
20 |
regex==2024.5.15
|
21 |
requests==2.32.3
|
|
|
|
|
|
|
|
|
|
|
22 |
safetensors==0.4.3
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
sympy==1.13.0
|
|
|
24 |
tiktoken==0.7.0
|
|
|
25 |
tokenizers==0.19.1
|
|
|
|
|
26 |
torch==2.3.1
|
|
|
27 |
tqdm==4.66.4
|
|
|
28 |
transformers==4.42.4
|
|
|
|
|
29 |
typing_extensions==4.12.2
|
|
|
|
|
30 |
urllib3==2.2.2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
accelerate==0.32.1
|
2 |
+
aiofiles==23.2.1
|
3 |
+
altair==5.3.0
|
4 |
+
annotated-types==0.7.0
|
5 |
+
anyio==4.4.0
|
6 |
+
appnope==0.1.4
|
7 |
+
argon2-cffi==23.1.0
|
8 |
+
argon2-cffi-bindings==21.2.0
|
9 |
+
arrow==1.3.0
|
10 |
+
asttokens==2.4.1
|
11 |
+
async-lru==2.0.4
|
12 |
+
attrs==23.2.0
|
13 |
+
Babel==2.15.0
|
14 |
+
beautifulsoup4==4.12.3
|
15 |
+
bleach==6.1.0
|
16 |
certifi==2024.7.4
|
17 |
+
cffi==1.16.0
|
18 |
charset-normalizer==3.3.2
|
19 |
click==8.1.7
|
20 |
+
comm==0.2.2
|
21 |
+
contourpy==1.2.1
|
22 |
+
cycler==0.12.1
|
23 |
+
debugpy==1.8.2
|
24 |
+
decorator==5.1.1
|
25 |
+
defusedxml==0.7.1
|
26 |
+
dnspython==2.6.1
|
27 |
+
email_validator==2.2.0
|
28 |
+
executing==2.0.1
|
29 |
+
fastapi==0.111.1
|
30 |
+
fastapi-cli==0.0.4
|
31 |
+
fastjsonschema==2.20.0
|
32 |
+
ffmpy==0.3.2
|
33 |
filelock==3.15.4
|
34 |
+
fonttools==4.53.1
|
35 |
+
fqdn==1.5.1
|
36 |
fsspec==2024.6.1
|
37 |
+
gradio==4.38.1
|
38 |
+
gradio_client==1.1.0
|
39 |
+
h11==0.14.0
|
40 |
+
httpcore==1.0.5
|
41 |
+
httptools==0.6.1
|
42 |
+
httpx==0.27.0
|
43 |
huggingface-hub==0.23.5
|
44 |
idna==3.7
|
45 |
+
importlib_resources==6.4.0
|
46 |
+
ipykernel==6.29.5
|
47 |
+
ipython==8.26.0
|
48 |
+
ipywidgets==8.1.3
|
49 |
+
isoduration==20.11.0
|
50 |
+
jedi==0.19.1
|
51 |
Jinja2==3.1.4
|
52 |
joblib==1.4.2
|
53 |
+
json5==0.9.25
|
54 |
+
jsonpointer==3.0.0
|
55 |
+
jsonschema==4.23.0
|
56 |
+
jsonschema-specifications==2023.12.1
|
57 |
+
jupyter==1.0.0
|
58 |
+
jupyter-console==6.6.3
|
59 |
+
jupyter-events==0.10.0
|
60 |
+
jupyter-lsp==2.2.5
|
61 |
+
jupyter_client==8.6.2
|
62 |
+
jupyter_core==5.7.2
|
63 |
+
jupyter_server==2.14.2
|
64 |
+
jupyter_server_terminals==0.5.3
|
65 |
+
jupyterlab==4.2.3
|
66 |
+
jupyterlab_pygments==0.3.0
|
67 |
+
jupyterlab_server==2.27.3
|
68 |
+
jupyterlab_widgets==3.0.11
|
69 |
+
kiwisolver==1.4.5
|
70 |
llmlingua==0.2.2
|
71 |
+
markdown-it-py==3.0.0
|
72 |
MarkupSafe==2.1.5
|
73 |
+
matplotlib==3.9.1
|
74 |
+
matplotlib-inline==0.1.7
|
75 |
+
mdurl==0.1.2
|
76 |
+
mistune==3.0.2
|
77 |
mpmath==1.3.0
|
78 |
+
nbclient==0.10.0
|
79 |
+
nbconvert==7.16.4
|
80 |
+
nbformat==5.10.4
|
81 |
+
nest-asyncio==1.6.0
|
82 |
networkx==3.3
|
83 |
nltk==3.8.1
|
84 |
+
notebook==7.2.1
|
85 |
+
notebook_shim==0.2.4
|
86 |
numpy==1.26.4
|
87 |
+
orjson==3.10.6
|
88 |
+
overrides==7.7.0
|
89 |
packaging==24.1
|
90 |
+
pandas==2.2.2
|
91 |
+
pandocfilters==1.5.1
|
92 |
+
parso==0.8.4
|
93 |
+
pexpect==4.9.0
|
94 |
+
pillow==10.4.0
|
95 |
+
platformdirs==4.2.2
|
96 |
+
prometheus_client==0.20.0
|
97 |
+
prompt_toolkit==3.0.47
|
98 |
psutil==6.0.0
|
99 |
+
ptyprocess==0.7.0
|
100 |
+
pure-eval==0.2.2
|
101 |
+
pycparser==2.22
|
102 |
+
pydantic==2.8.2
|
103 |
+
pydantic_core==2.20.1
|
104 |
+
pydub==0.25.1
|
105 |
+
Pygments==2.18.0
|
106 |
+
pyparsing==3.1.2
|
107 |
+
python-dateutil==2.9.0.post0
|
108 |
+
python-dotenv==1.0.1
|
109 |
+
python-json-logger==2.0.7
|
110 |
+
python-multipart==0.0.9
|
111 |
+
pytz==2024.1
|
112 |
PyYAML==6.0.1
|
113 |
+
pyzmq==26.0.3
|
114 |
+
qtconsole==5.5.2
|
115 |
+
QtPy==2.4.1
|
116 |
+
referencing==0.35.1
|
117 |
regex==2024.5.15
|
118 |
requests==2.32.3
|
119 |
+
rfc3339-validator==0.1.4
|
120 |
+
rfc3986-validator==0.1.1
|
121 |
+
rich==13.7.1
|
122 |
+
rpds-py==0.19.0
|
123 |
+
ruff==0.5.2
|
124 |
safetensors==0.4.3
|
125 |
+
semantic-version==2.10.0
|
126 |
+
Send2Trash==1.8.3
|
127 |
+
shellingham==1.5.4
|
128 |
+
six==1.16.0
|
129 |
+
sniffio==1.3.1
|
130 |
+
soupsieve==2.5
|
131 |
+
stack-data==0.6.3
|
132 |
+
starlette==0.37.2
|
133 |
sympy==1.13.0
|
134 |
+
terminado==0.18.1
|
135 |
tiktoken==0.7.0
|
136 |
+
tinycss2==1.3.0
|
137 |
tokenizers==0.19.1
|
138 |
+
tomlkit==0.12.0
|
139 |
+
toolz==0.12.1
|
140 |
torch==2.3.1
|
141 |
+
tornado==6.4.1
|
142 |
tqdm==4.66.4
|
143 |
+
traitlets==5.14.3
|
144 |
transformers==4.42.4
|
145 |
+
typer==0.12.3
|
146 |
+
types-python-dateutil==2.9.0.20240316
|
147 |
typing_extensions==4.12.2
|
148 |
+
tzdata==2024.1
|
149 |
+
uri-template==1.3.0
|
150 |
urllib3==2.2.2
|
151 |
+
uvicorn==0.30.1
|
152 |
+
uvloop==0.19.0
|
153 |
+
watchfiles==0.22.0
|
154 |
+
wcwidth==0.2.13
|
155 |
+
webcolors==24.6.0
|
156 |
+
webencodings==0.5.1
|
157 |
+
websocket-client==1.8.0
|
158 |
+
websockets==11.0.3
|
159 |
+
widgetsnbextension==4.0.11
|
test.ipynb
ADDED
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": 1,
|
6 |
+
"metadata": {},
|
7 |
+
"outputs": [],
|
8 |
+
"source": [
|
9 |
+
"from llmlingua import PromptCompressor"
|
10 |
+
]
|
11 |
+
},
|
12 |
+
{
|
13 |
+
"cell_type": "code",
|
14 |
+
"execution_count": 2,
|
15 |
+
"metadata": {},
|
16 |
+
"outputs": [],
|
17 |
+
"source": [
|
18 |
+
"llm_lingua = PromptCompressor(\n",
|
19 |
+
" model_name=\"microsoft/llmlingua-2-xlm-roberta-large-meetingbank\",\n",
|
20 |
+
" use_llmlingua2=True,\n",
|
21 |
+
" device_map=\"mps\"\n",
|
22 |
+
")"
|
23 |
+
]
|
24 |
+
},
|
25 |
+
{
|
26 |
+
"cell_type": "code",
|
27 |
+
"execution_count": 4,
|
28 |
+
"metadata": {},
|
29 |
+
"outputs": [
|
30 |
+
{
|
31 |
+
"name": "stdout",
|
32 |
+
"output_type": "stream",
|
33 |
+
"text": [
|
34 |
+
"compressed_prompt The fn argument is very flexible -- you can pass any Python function that you want to wrap with a UI. In the example above, we saw a relatively simple function, but the function could be anything from a music generator to a tax calculator to the prediction function of a pretrained machine learning model. The inputs and outputs arguments take one or more Gradio components. As we'll see, Gradio includes more than 30 built-in components (such as the gr.Textbox(), gr.Image(), and gr.HTML() components) that are designed for machine learning applications.\n",
|
35 |
+
"compressed_prompt_list [\"The fn argument is very flexible -- you can pass any Python function that you want to wrap with a UI. In the example above, we saw a relatively simple function, but the function could be anything from a music generator to a tax calculator to the prediction function of a pretrained machine learning model. The inputs and outputs arguments take one or more Gradio components. As we'll see, Gradio includes more than 30 built-in components (such as the gr.Textbox(), gr.Image(), and gr.HTML() components) that are designed for machine learning applications.\"]\n",
|
36 |
+
"origin_tokens 113\n",
|
37 |
+
"compressed_tokens 112\n",
|
38 |
+
"ratio 1.0x\n",
|
39 |
+
"rate 99.1%\n",
|
40 |
+
"saving , Saving $0.0 in GPT-4.\n"
|
41 |
+
]
|
42 |
+
},
|
43 |
+
{
|
44 |
+
"ename": "",
|
45 |
+
"evalue": "",
|
46 |
+
"output_type": "error",
|
47 |
+
"traceback": [
|
48 |
+
"\u001b[1;31mThe Kernel crashed while executing code in the current cell or a previous cell. \n",
|
49 |
+
"\u001b[1;31mPlease review the code in the cell(s) to identify a possible cause of the failure. \n",
|
50 |
+
"\u001b[1;31mClick <a href='https://aka.ms/vscodeJupyterKernelCrash'>here</a> for more info. \n",
|
51 |
+
"\u001b[1;31mView Jupyter <a href='command:jupyter.viewOutput'>log</a> for further details."
|
52 |
+
]
|
53 |
+
}
|
54 |
+
],
|
55 |
+
"source": [
|
56 |
+
"with open(\"prompt.txt\", \"r\") as file:\n",
|
57 |
+
" prompt = file.read()\n",
|
58 |
+
" compressed_prompt = llm_lingua.compress_prompt(prompt, rate=1)\n",
|
59 |
+
" [print(\": \".join(entry)) for entry in compressed_prompt.items()]"
|
60 |
+
]
|
61 |
+
}
|
62 |
+
],
|
63 |
+
"metadata": {
|
64 |
+
"kernelspec": {
|
65 |
+
"display_name": "venv",
|
66 |
+
"language": "python",
|
67 |
+
"name": "python3"
|
68 |
+
},
|
69 |
+
"language_info": {
|
70 |
+
"codemirror_mode": {
|
71 |
+
"name": "ipython",
|
72 |
+
"version": 3
|
73 |
+
},
|
74 |
+
"file_extension": ".py",
|
75 |
+
"mimetype": "text/x-python",
|
76 |
+
"name": "python",
|
77 |
+
"nbconvert_exporter": "python",
|
78 |
+
"pygments_lexer": "ipython3",
|
79 |
+
"version": "3.11.6"
|
80 |
+
}
|
81 |
+
},
|
82 |
+
"nbformat": 4,
|
83 |
+
"nbformat_minor": 2
|
84 |
+
}
|
test.py
DELETED
@@ -1,10 +0,0 @@
|
|
1 |
-
from llmlingua import PromptCompressor
|
2 |
-
|
3 |
-
llm_lingua = PromptCompressor(
|
4 |
-
model_name="microsoft/llmlingua-2-xlm-roberta-large-meetingbank",
|
5 |
-
use_llmlingua2=True,
|
6 |
-
device_map="mps"
|
7 |
-
)
|
8 |
-
compressed_prompt = llm_lingua.compress_prompt("The meeting was scheduled for 2:30 PM, but it started at 3:00 PM. Why?", rate=0.5)
|
9 |
-
|
10 |
-
print(compressed_prompt)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|