-
Notifications
You must be signed in to change notification settings - Fork 14
/
AppCallback.py
117 lines (98 loc) · 3.85 KB
/
AppCallback.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
from dash.dependencies import Input, Output
from dash.exceptions import PreventUpdate
from transformers import pipeline
from transformers import AutoTokenizer, AutoModelForSequenceClassification
from transformers import logging
from Pages import home
import dash
import base64
import io
import docx2txt
import PyPDF2
import os
# -----------------------------------
home_page_path = '/'
def parse_contents(content, filename):
content_type, content_string = content.split(',')
decoded = base64.b64decode(content_string)
try:
if '.docx' in filename:
text = docx2txt.process(io.BytesIO(decoded))
if '.txt' in filename:
text = decoded.decode()
if '.pdf' in filename:
reader = PyPDF2.PdfReader(io.BytesIO(decoded))
# get the number of pages in pdf file
num_of_pages = len(reader.pages)
text = ''
for n_page in range(num_of_pages):
# extract the text of the page
text = text + reader.pages[n_page].extract_text()
except Exception as e:
print(e)
raise PreventUpdate
return text
class AppCallback:
def __init__(self, app):
self.app = app
logging.set_verbosity_error()
absolute_path = os.path.dirname(__file__)
relative_path = "./roberta-base-openai-detector/"
full_path = os.path.join(absolute_path, relative_path)
tokenizer = AutoTokenizer.from_pretrained(full_path, local_files_only=True)
model = AutoModelForSequenceClassification.from_pretrained(full_path, local_files_only=True)
self.classifier = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)
self.app.callback([
Output("real_prog", "value"),
Output("real_prog", "label"),
Output("fake_prog", "value"),
Output("fake_prog", "label"),
Output('loading', 'children'),
],
Input('text', 'value')
)(self.classifyText)
# Main callback to decide which page to render
self.app.callback(
Output('page-content', 'children'),
[Input('url', 'pathname')]
)(self.display_page)
app.callback(
[Output('text', 'value')],
[Input('upload_file', 'contents'),
Input('upload_file', "filename")],
)(self.import_data)
def import_data(self, contents, filename):
if contents:
text = parse_contents(contents, filename)
return [text]
else:
raise PreventUpdate
def display_page(self, pathname):
if pathname == home_page_path:
return home.layout
else:
return dash.no_update
def classifyText(self, text):
if text != None and text != "":
res = self.classifier(text, truncation=True, max_length=510)
label = res[0]['label']
score = res[0]['score']
if label == 'Real':
real_score = score*100
fake_score = 100-real_score
real_score_lable = f"{real_score:.0f}%"
if int(fake_score) < 10:
fake_score_lable = ""
else:
fake_score_lable =f"{fake_score:.0f}%"
else:
fake_score = score*100
real_score = 100-fake_score
fake_score_lable = f"{fake_score:.0f}%"
if int(real_score) < 10:
real_score_lable = ""
else:
real_score_lable =f"{real_score:.0f}%"
return [real_score, real_score_lable, fake_score, fake_score_lable, '']
else:
return [50, "", 50, "", '']