-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathapp.py
More file actions
133 lines (104 loc) · 3.87 KB
/
app.py
File metadata and controls
133 lines (104 loc) · 3.87 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#import dependencies
from dash import Dash, dcc, html, Input, Output, State, callback_context
import dash_bootstrap_components as dbc
import plotly.express as px
from transformers import AutoTokenizer, AutoModelForCausalLM
from readability import Readability
import nltk
tokenizer = AutoTokenizer.from_pretrained("gpt2")
model = AutoModelForCausalLM.from_pretrained("gpt2")
gen_text_list = []
exv = 0
#create an input field
def textareas():
return html.Div([
dbc.Textarea(id = 'my-input'
, size="lg"
, placeholder="Enter text for auto completion")
, dbc.Button("Submit"
, id="gen-button"
, className="me-2"
, n_clicks=0)
])
#instantiate dash
app = Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
#create layout
app.layout = html.Div([dbc.Container([
html.H1("Eric's Writing Assistant")
, html.Br()
, html.H3("Enter a prompt")
, textareas()
, html.Br()
, html.Br()
, html.H3("Generated Text")
, html.Div(id='readability-score')
, html.Div(id='my-output')
, dbc.Button("Expand", id="expand-button", className="me-2", n_clicks=0)
, dbc.Button("Clear", id="clear-button", className="me-2", n_clicks=0)
])
])
@app.callback(
Output(component_id='my-output', component_property='children'),
Output(component_id = 'readability-score', component_property='children'),
Input(component_id='gen-button', component_property='n_clicks'),
Input(component_id='expand-button', component_property='n_clicks'),
Input(component_id='clear-button', component_property='n_clicks'),
State(component_id='my-input', component_property='value')
)
def update_output_div(gen, ex, cl, input_value):
gen_text = ""
changed_id = [p['prop_id'] for p in callback_context.triggered][0]
global gen_text_list
global exv
score = ''
if 'gen-button' in changed_id:
if input_value is None or input_value == "":
input_value = ""
gen_text = ""
else:
input_ids = tokenizer(input_value, return_tensors="pt").input_ids
gen_tokens = model.generate(
input_ids,
do_sample=True,
temperature=0.9,
max_length=100,
)
gen_text = tokenizer.batch_decode(gen_tokens)[0]
gen_text_list.append(gen_text)
if len(gen_text.strip().split(" ")) >100:
print(len(gen_text))
r = Readability(gen_text)
fk = r.flesch_kincaid()
score = fk.score
else:
score = 'Not 100 tokens'
if 'expand-button' in changed_id:
if len(gen_text_list) > 0:
MAX_LENGTH = 100 + 100*(exv+1)
input_ids = tokenizer(gen_text_list[exv], return_tensors="pt").input_ids
gen_tokens = model.generate(
input_ids,
do_sample=True,
temperature=0.9,
max_length=MAX_LENGTH,
)
gen_text = tokenizer.batch_decode(gen_tokens)[0]
gen_text_list.append(gen_text)
exv+=1
if len(gen_text.strip().split(" ")) >100:
print(len(gen_text))
r = Readability(gen_text)
fk = r.flesch_kincaid()
score = fk.score
else:
score = 'Not 100 tokens'
else:
html.P("no text has been generated")
if 'clear-button' in changed_id:
gen_text = ''
exv = 0
gen_text_list = []
return html.P(gen_text), html.P(f"Readability Score: {score}")
#run app server
if __name__ == '__main__':
app.run_server(debug=True)