update w3d1 space
Browse files- .chainlit/config.toml +1 -1
- Dockerfile +2 -3
- README.md +3 -3
- app.py +100 -24
- requirements.txt +2 -1
.chainlit/config.toml
CHANGED
@@ -35,7 +35,7 @@ multi_modal = true
|
|
35 |
|
36 |
[UI]
|
37 |
# Name of the app and chatbot.
|
38 |
-
name = "Chatbot"
|
39 |
|
40 |
# Show the readme while the conversation is empty.
|
41 |
show_readme_as_default = true
|
|
|
35 |
|
36 |
[UI]
|
37 |
# Name of the app and chatbot.
|
38 |
+
name = "Legal Summarizer Chatbot"
|
39 |
|
40 |
# Show the readme while the conversation is empty.
|
41 |
show_readme_as_default = true
|
Dockerfile
CHANGED
@@ -4,8 +4,7 @@ USER user
|
|
4 |
ENV HOME=/home/user \
|
5 |
PATH=/home/user/.local/bin:$PATH
|
6 |
WORKDIR $HOME/app
|
|
|
|
|
7 |
COPY --chown=user . $HOME/app
|
8 |
-
COPY ./requirements.txt ~/app/requirements.txt
|
9 |
-
RUN pip install -r requirements.txt
|
10 |
-
COPY . .
|
11 |
CMD ["chainlit", "run", "app.py", "--port", "7860"]
|
|
|
4 |
ENV HOME=/home/user \
|
5 |
PATH=/home/user/.local/bin:$PATH
|
6 |
WORKDIR $HOME/app
|
7 |
+
COPY --chown=user ./requirements.txt $HOME/app/requirements.txt
|
8 |
+
RUN pip install -r $HOME/app/requirements.txt
|
9 |
COPY --chown=user . $HOME/app
|
|
|
|
|
|
|
10 |
CMD ["chainlit", "run", "app.py", "--port", "7860"]
|
README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1 |
---
|
2 |
title: Legal Summarizer App
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: docker
|
7 |
pinned: false
|
8 |
app_port: 7860
|
|
|
1 |
---
|
2 |
title: Legal Summarizer App
|
3 |
+
emoji: ⚖️
|
4 |
+
colorFrom: red
|
5 |
+
colorTo: green
|
6 |
sdk: docker
|
7 |
pinned: false
|
8 |
app_port: 7860
|
app.py
CHANGED
@@ -1,42 +1,118 @@
|
|
1 |
import os
|
2 |
-
from peft import PeftModel, PeftConfig
|
3 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
4 |
-
import chainlit as cl # importing chainlit for our app
|
5 |
-
from chainlit.prompt import Prompt, PromptMessage # importing prompt tools
|
6 |
-
from dotenv import load_dotenv
|
7 |
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
config = PeftConfig.from_pretrained(model_name)
|
12 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
13 |
|
14 |
-
base_model = AutoModelForCausalLM.from_pretrained(
|
15 |
-
"NousResearch/Meta-Llama-3-8B-Instruct"
|
16 |
-
)
|
17 |
-
model = PeftModel.from_pretrained(base_model, model_name)
|
18 |
|
19 |
# Prompt Templates
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
|
25 |
@cl.on_message # marks a function that should be run each time the chatbot receives a message from a user
|
26 |
async def main(message: cl.Message):
|
27 |
-
|
28 |
-
|
29 |
-
return_tensors="pt",
|
30 |
-
)
|
31 |
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
)
|
36 |
|
37 |
-
|
|
|
|
|
|
|
|
|
38 |
|
39 |
-
|
40 |
|
41 |
|
42 |
if __name__ == "__main__":
|
|
|
1 |
import os
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
+
import chainlit as cl # importing chainlit for our app
|
4 |
+
import torch
|
5 |
+
from transformers import (
|
6 |
+
AutoTokenizer,
|
7 |
+
AutoConfig,
|
8 |
+
AutoModelForCausalLM,
|
9 |
+
BitsAndBytesConfig,
|
10 |
+
)
|
11 |
+
import bitsandbytes as bnb
|
12 |
|
13 |
+
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
|
|
|
|
|
14 |
|
|
|
|
|
|
|
|
|
15 |
|
16 |
# Prompt Templates
|
17 |
+
INSTRUCTION_PROMPT_TEMPLATE = """<|begin_of_text|><|start_header_id|>system<|end_header_id|>
|
18 |
+
|
19 |
+
Please convert the following legal content into a human-readable summary<|eot_id|><|start_header_id|>user<|end_header_id|>
|
20 |
+
|
21 |
+
[LEGAL_DOC]
|
22 |
+
{input}
|
23 |
+
[END_LEGAL_DOC]<|eot_id|><|start_header_id|>assistant<|end_header_id|>
|
24 |
+
"""
|
25 |
+
|
26 |
+
RESPONSE_TEMPLATE = """
|
27 |
+
{summary}<|eot_id|>
|
28 |
+
"""
|
29 |
+
|
30 |
+
|
31 |
+
def create_prompt(sample, include_response=False):
|
32 |
+
"""
|
33 |
+
Parameters:
|
34 |
+
- sample: dict representing row of dataset
|
35 |
+
- include_response: bool
|
36 |
+
|
37 |
+
Functionality:
|
38 |
+
This function should build the Python str `full_prompt`.
|
39 |
+
|
40 |
+
If `include_response` is true, it should include the summary -
|
41 |
+
else it should not contain the summary (useful for prompting) and testing
|
42 |
+
|
43 |
+
Returns:
|
44 |
+
- full_prompt: str
|
45 |
+
"""
|
46 |
+
|
47 |
+
full_prompt = INSTRUCTION_PROMPT_TEMPLATE.format(input=sample["original_text"])
|
48 |
+
|
49 |
+
if include_response:
|
50 |
+
full_prompt += RESPONSE_TEMPLATE.format(summary=sample["reference_summary"])
|
51 |
+
|
52 |
+
full_prompt += "<|end_of_text|>"
|
53 |
|
54 |
+
return full_prompt
|
55 |
+
|
56 |
+
|
57 |
+
@cl.on_chat_start
|
58 |
+
async def start_chat():
|
59 |
+
bnb_config = BitsAndBytesConfig(
|
60 |
+
load_in_4bit=True,
|
61 |
+
bnb_4bit_quant_type="nf4",
|
62 |
+
bnb_4bit_use_double_quant=True,
|
63 |
+
bnb_4bit_compute_dtype=torch.float16,
|
64 |
+
)
|
65 |
+
|
66 |
+
model_id = "lakshyaag/llama38binstruct_summarize"
|
67 |
+
|
68 |
+
model = AutoModelForCausalLM.from_pretrained(
|
69 |
+
model_id,
|
70 |
+
quantization_config=bnb_config,
|
71 |
+
device_map="auto",
|
72 |
+
cache_dir=os.path.join(os.getcwd(), ".cache"),
|
73 |
+
)
|
74 |
+
|
75 |
+
# Move model to GPU if available
|
76 |
+
if torch.cuda.is_available():
|
77 |
+
model = model.to("cuda")
|
78 |
+
|
79 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
80 |
+
model_id, cache_dir=os.path.join(os.getcwd(), ".cache")
|
81 |
+
)
|
82 |
+
|
83 |
+
tokenizer.pad_token = tokenizer.eos_token
|
84 |
+
tokenizer.padding_side = "right"
|
85 |
+
|
86 |
+
cl.user_session.set("model", model)
|
87 |
+
cl.user_session.set("tokenizer", tokenizer)
|
88 |
|
89 |
|
90 |
@cl.on_message # marks a function that should be run each time the chatbot receives a message from a user
|
91 |
async def main(message: cl.Message):
|
92 |
+
model = cl.user_session.get("model")
|
93 |
+
tokenizer = cl.user_session.get("tokenizer")
|
|
|
|
|
94 |
|
95 |
+
# convert str input into tokenized input
|
96 |
+
encoded_input = tokenizer(message, return_tensors="pt")
|
97 |
+
|
98 |
+
# send the tokenized inputs to our GPU
|
99 |
+
model_inputs = encoded_input.to("cuda")
|
100 |
+
|
101 |
+
# generate response and set desired generation parameters
|
102 |
+
generated_ids = model.generate(
|
103 |
+
**model_inputs,
|
104 |
+
max_new_tokens=256,
|
105 |
+
do_sample=True,
|
106 |
+
pad_token_id=tokenizer.eos_token_id,
|
107 |
)
|
108 |
|
109 |
+
# decode output from tokenized output to str output
|
110 |
+
decoded_output = tokenizer.batch_decode(generated_ids)
|
111 |
+
|
112 |
+
# return only the generated response (not the prompt) as output
|
113 |
+
response = decoded_output[0].split("<|end_header_id|>")[-1]
|
114 |
|
115 |
+
await message.reply(response)
|
116 |
|
117 |
|
118 |
if __name__ == "__main__":
|
requirements.txt
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
chainlit==0.7.700
|
2 |
transformers==4.41.2
|
3 |
-
|
|
|
4 |
tiktoken==0.5.1
|
5 |
python-dotenv==1.0.0
|
|
|
1 |
chainlit==0.7.700
|
2 |
transformers==4.41.2
|
3 |
+
bitsandbytes==0.43.1
|
4 |
+
accelerate==0.31.0
|
5 |
tiktoken==0.5.1
|
6 |
python-dotenv==1.0.0
|