SHSH0819 commited on
Commit
e9b963a
1 Parent(s): 6ef99d1

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -0
app.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import sys
3
+ sys.path.insert(0, os.path.abspath('./'))
4
+
5
+ import nltk
6
+ nltk.download('punkt')
7
+
8
+ import numpy as np
9
+ from tqdm.auto import tqdm
10
+ from transformers import T5Tokenizer
11
+ from transformers import AutoModelForSeq2SeqLM
12
+ from summarization_dataset import *
13
+ from post_process import *
14
+
15
+ import gradio as gr
16
+
17
+
18
+ def predict(data):
19
+ data=[data]
20
+ device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
21
+
22
+ """Load Tokenizer"""
23
+ tokenizer = T5Tokenizer.from_pretrained("t5-small", model_max_length=512)
24
+
25
+ """Tokenized Inputs"""
26
+ tokenized_inputs = tokenizer(
27
+ data,
28
+ add_special_tokens=True,
29
+ padding="max_length",
30
+ return_token_type_ids=True,
31
+ truncation=True)
32
+
33
+ ids = torch.tensor(tokenized_inputs['input_ids']).to(device)
34
+ mask = torch.tensor(tokenized_inputs['attention_mask']).to(device)
35
+
36
+ """Load Model"""
37
+ model_path = "./"
38
+
39
+
40
+ model = AutoModelForSeq2SeqLM.from_pretrained(model_path + "/summarization_final",device_map=device)
41
+ model.to(device)
42
+
43
+
44
+ """Make Prediction"""
45
+ model.eval()
46
+
47
+ with torch.no_grad():
48
+ generate_token = model.generate(ids, attention_mask=mask, max_new_tokens=50)
49
+
50
+ decoded_generate = tokenizer.batch_decode(generate_token.cpu(), skip_special_tokens=True)
51
+ generated = ["\n".join(nltk.sent_tokenize(gen)) for gen in decoded_generate]
52
+
53
+ return generated[0]
54
+
55
+
56
+ title="Financial News Summarization"
57
+ description=""
58
+ article="Domain adaptation and fine-tunning dataset from Zhou, Z., Ma, L., & Liu, H. (2021)."
59
+ example_list=["EWC provides guests with a first class, professional waxing experience by the most highly trained estheticians in the industry, within the privacy of clean, individual waxing suites. They're so certain everyone will love the EWC experience, European Wax Center offers a free complimentary wax to each new guest. EWC continues to revolutionize the waxing category with their innovative, signatureComfort Wax. This proprietary blend is formulated with the highest quality ingredients to leave skin feeling smooth and make waxing a more pleasant, virtually painless experience.To help enhance and extend waxing services after leaving the center, EWC offers a full collection of proprietary products in the skincare, body, and brow categories."]
60
+
61
+
62
+ # Create the Gradio demo
63
+ demo = gr.Interface(fn=predict, # mapping function from input to output
64
+ inputs="text", # what are the inputs?
65
+ outputs="text", # our fn has two outputs, therefore we have two outputs
66
+ examples=example_list,
67
+ title=title,
68
+ description=description,
69
+ article=article)
70
+
71
+ # Launch the demo!
72
+ demo.launch(debug=False, share=False)