Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
import torch
|
4 |
+
from peft import PeftModel, PeftConfig
|
5 |
+
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
6 |
+
|
7 |
+
def load_data(path):
|
8 |
+
"""
|
9 |
+
Load dataset
|
10 |
+
"""
|
11 |
+
input_path = os.path.join(path)
|
12 |
+
with open(input_path, "r") as f:
|
13 |
+
data = f.read()
|
14 |
+
|
15 |
+
return data
|
16 |
+
|
17 |
+
def preprocessing(data):
|
18 |
+
texts = list()
|
19 |
+
|
20 |
+
i = 0
|
21 |
+
if len(data) <= i+4000:
|
22 |
+
texts = data
|
23 |
+
else:
|
24 |
+
while len(data[i:]) != 0:
|
25 |
+
if len(data[i:]) > 4000:
|
26 |
+
string = str(data[i:i+4000])
|
27 |
+
texts.append(string)
|
28 |
+
i = i + 3800
|
29 |
+
else:
|
30 |
+
string = str(data[i:])
|
31 |
+
texts.append(string)
|
32 |
+
break
|
33 |
+
return texts
|
34 |
+
|
35 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
36 |
+
|
37 |
+
peft_model_id = "sooolee/flan-t5-base-cnn-samsum-lora"
|
38 |
+
config = PeftConfig.from_pretrained(peft_model_id)
|
39 |
+
tokenizer = AutoTokenizer.from_pretrained(config.base_model_name_or_path)
|
40 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(config.base_model_name_or_path, device_map='auto') # load_in_8bit=True,
|
41 |
+
model = PeftModel.from_pretrained(model, peft_model_id, device_map='auto')
|
42 |
+
|
43 |
+
def summarize(path):
|
44 |
+
transcript = load_data(path)
|
45 |
+
texts = preprocessing(transcript)
|
46 |
+
inputs = tokenizer(texts, return_tensors="pt", padding=True, )
|
47 |
+
|
48 |
+
with torch.no_grad():
|
49 |
+
output_tokens = model.generate(input_ids=inputs["input_ids"].to("cuda"), max_new_tokens=60, do_sample=True, top_p=0.9)
|
50 |
+
outputs = tokenizer.batch_decode(output_tokens.detach().cpu().numpy(), skip_special_tokens=True)
|
51 |
+
|
52 |
+
return outputs
|
53 |
+
|
54 |
+
gr.Interface(
|
55 |
+
fn=summarize,
|
56 |
+
title = 'Summarize Transcripts',
|
57 |
+
inputs = gr.File(file_types="text", label="Upload a text file.", interactive=True),
|
58 |
+
outputs = gr.Textbox(label="Summary", max_lines=120, interactive=False),
|
59 |
+
).launch()
|