-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
288 lines (258 loc) · 11.6 KB
/
app.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import streamlit as st
import numpy as np
import time
import torch
from torch import nn
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
def generate_text(model,itos,stoi, block_size, max_len, start_str = None):
g = torch.Generator()
g.manual_seed(42)
context = [0]*block_size
if start_str:
for char in start_str:
context = context[1:] + [stoi[char]]
text = start_str if start_str else ''
for _ in range(max_len):
x = torch.tensor(context).view(1,-1).to(device)
y_pred = model(x)
ix = torch.distributions.Categorical(logits=y_pred).sample().item()
ch = itos[ix]
text += ch
context = context[1:] + [ix]
return text
def type_text(text):
text_element = st.empty()
s = ''
for i in text:
s += i
text_element.write(s + '$I$')
time.sleep(0.005)
text_element.write(s)
class NextChar(nn.Module):
def __init__(self, block_size, vocab_size, emb_dim, hidden_size):
super().__init__()
self.emb = nn.Embedding(vocab_size, emb_dim)
self.lin1 = nn.Linear(block_size * emb_dim, hidden_size)
self.activation = nn.ReLU() # Adding ReLU activation function
self.dropout = nn.Dropout(0.2) # Adding dropout with 20% probability
self.lin2 = nn.Linear(hidden_size, vocab_size)
def forward(self, x):
x = self.emb(x)
x = x.view(x.shape[0], -1)
x = self.activation(self.lin1(x)) # Applying ReLU activation
x = self.dropout(x) # Applying dropout
x = self.lin2(x)
return x
st.write("## Neural Network based Text Generator using Next Token Prediction")
st.sidebar.title("Model Information")
st.sidebar.write("This is a simple Neural Network based text generator. It can predict next characters of the input text provided. The model uses lower case letters, punctuation marks and fullstops. The text is generated paragraph wise, because the model learnt this from the text corpus.")
corpus=st.selectbox(
'Select Corpus',
('Startup Funding', 'Shakespear','Algebra latex Report')
)
num_chars = st.slider("Number of characters to be generated", 100, 5000, 1000)
# model = torch.compile(model)
if(corpus == 'Startup Funding'):
with open("text files/startup_funding.txt", 'r') as file:
thefile = file.read()
sort_char = ''
for char in thefile:
if char in ['\x0c','\ufeff','»', '¿', 'â', 'ï', '”','€']:
continue
sort_char += char.lower()
characters = sorted(list(set(sort_char)))
stoi = {s: i+1 for i, s in enumerate(characters)}
stoi['~'] = 0 # for padding
stoi['—'] = 53
# print(stoi)
itos = {i : s for s,i in stoi.items()}
col1, col2 = st.columns(2)
with col1:
option1 = st.selectbox('Select Block Size', ['10', '100'])
with col2:
option2 = st.selectbox('Select Embedding', ['60', '150'])
if(option1 == '10' and option2 == '60'):
block_size = 10
emb_dim=60
elif(option1 == '10' and option2 == '150'):
block_size = 10
emb_dim=150
elif(option1 == '100' and option2 == '60'):
block_size = 100
emb_dim=60
else:
block_size = 100
emb_dim=150
input_text = st.text_input("Enter Text", placeholder="Enter a text (In Lower Case) or leave it empty")
# lower the text
model = NextChar(block_size, len(stoi), emb_dim, 512).to(device)
# if input text contains any character other than the ones in the model, then throw an error
for char in input_text:
if char not in characters:
st.error("Invalid Characters in the input text")
break
btn=st.button("Generate Text")
if btn:
st.subheader("Seed Text")
type_text(input_text)
if(option1 == '10' and option2 == '60'):
model.load_state_dict(torch.load("models/startup_funding model/model_story11.pth", map_location = device))
elif(option1 == '10' and option2 == '150'):
model.load_state_dict(torch.load("models/startup_funding model/model_story12.pth", map_location = device))
elif(option1 == '100' and option2 == '60'):
model.load_state_dict(torch.load("models/startup_funding model/model_story21.pth", map_location = device))
else:
model.load_state_dict(torch.load("models/startup_funding model/model_story22.pth", map_location =device))
gen_text = generate_text(model, itos, stoi, block_size, num_chars, input_text)
st.subheader("Generated Text")
# print(gen_text)
type_text(gen_text)
elif(corpus == 'Algebra latex Report'):
with open("text files/algerbra_latex_report.txt", 'r') as file:
thefile = file.read()
sort_char = ''
for char in thefile:
if char in ['\x0c','\ufeff','»', '¿', 'â', 'ï', '”','€']:
continue
sort_char += char.lower()
characters = sorted(list(set(sort_char)))
stoi = {s: i+1 for i, s in enumerate(characters)}
stoi['~'] = 0 # for padding
# stoi['—'] = 53
# print(stoi)
itos = {i : s for s,i in stoi.items()}
# side by side select box for block size and embedding size
col1, col2 = st.columns(2)
with col1:
option1 = st.selectbox('Select Block Size', ['10','25','50'])
with col2:
option2 = st.selectbox('Select Embedding', ['60', '150'])
if(option1 == '10' and option2 == '60'):
block_size = 10
emb_dim=60
elif(option1 == '10' and option2 == '150'):
block_size = 10
emb_dim=150
elif(option1 == '25' and option2 == '60'):
block_size = 25
emb_dim=60
elif(option1 == 25 and option2 == '150'):
block_size ='25'
emb_dim=150
elif(option1 == '50' and option2 == '60'):
block_size = 50
emb_dim=60
elif(option1 == '50' and option2 == '150'):
block_size = 50
emb_dim=150
model_option = st.radio("Select Model?", ("Trained", "Untrained"))
input_text = st.text_input("Enter Text", placeholder="Enter a text (In Lower Case) or leave it empty")
# lower the text
model = NextChar(block_size, len(stoi), emb_dim, 512).to(device)
# if input text contains any character other than the ones in the model, then throw an error
for char in input_text:
if char not in characters:
st.error("Invalid Characters in the input text")
break
btn=st.button("Generate Text")
if btn:
st.subheader("Seed Text")
type_text(input_text)
if model_option == 'Trained':
if(option1 == '10' and option2 == '60'):
model.load_state_dict(torch.load("models/algebra letex_code model/algebra10_60.pth", map_location = device))
elif(option1 == '10' and option2 == '150'):
model.load_state_dict(torch.load("models/algebra letex_code model/algebra10_150.pth", map_location = device))
elif(option1 == '25' and option2 == '60'):
model.load_state_dict(torch.load("models/algebra letex_code model/algebra25_60.pth", map_location = device))
elif(option1 == '25' and option2 == '150'):
model.load_state_dict(torch.load("models/algebra letex_code model/algebra25_150.pth", map_location = device))
elif(option1 == '50' and option2 == '60'):
model.load_state_dict(torch.load("models/algebra letex_code model/algebra50_60.pth", map_location = device))
else:
model.load_state_dict(torch.load("models/algebra letex_code model/algebra50_150.pth", map_location =device))
else:
model.load_state_dict(torch.load("models/algebra letex_code model/algebra_untrained.pth", map_location =device))
gen_text = generate_text(model, itos, stoi, block_size, num_chars, input_text)
st.subheader("Generated Text")
# print(gen_text)
type_text(gen_text)
elif(corpus == 'Shakespear'):
with open("text files/shakespear.txt", 'r') as file:
thefile = file.read()
sort_char = ''
for char in thefile:
if char in ['\x0c','\ufeff','»', '¿', 'â', 'ï', '”','€']:
continue
sort_char += char.lower()
characters = sorted(list(set(sort_char)))
stoi = {s: i+1 for i, s in enumerate(characters)}
stoi['~'] = 0 # for padding
# print(stoi)
itos = {i : s for s,i in stoi.items()}
col1, col2 = st.columns(2)
with col1:
option1 = st.selectbox('Select Block Size', ['10','25','50', '100'])
with col2:
option2 = st.selectbox('Select Embedding', ['60', '150'])
if(option1 == '10' and option2 == '60'):
block_size = 10
emb_dim=60
elif(option1 == '10' and option2 == '150'):
block_size = 10
emb_dim=150
elif(option1 == '25' and option2 == '60'):
block_size = 25
emb_dim=60
elif(option1 == '25' and option2 == '150'):
block_size = 25
emb_dim=150
elif(option1 == '50' and option2 == '60'):
block_size = 50
emb_dim=60
elif(option1 == '50' and option2 == '150'):
block_size = 50
emb_dim=150
elif(option1 == '100' and option2 == '60'):
block_size = 100
emb_dim=60
else:
block_size = 100
emb_dim=150
# Add radio button for selecting the model for trained and untrained model
model_option = st.radio("Select Model?", ("Trained", "Untrained"))
input_text = st.text_input("Enter Text", placeholder="Enter a text (In Lower Case) or leave it empty")
# lower the text
model = NextChar(block_size, len(stoi), emb_dim, 512).to(device)
# if input text contains any character other than the ones in the model, then throw an error
for char in input_text:
if char not in characters:
st.error("Invalid Characters in the input text")
break
btn=st.button("Generate Text")
if btn:
st.subheader("Seed Text")
type_text(input_text)
if model_option == 'Trained':
if(option1 == '10' and option2 == '60'):
model.load_state_dict(torch.load("models/shakespear model/shakespear10_60.pth", map_location = device))
elif(option1 == '10' and option2 == '150'):
model.load_state_dict(torch.load("models/shakespear model/shakespear10_150.pth", map_location = device))
elif(option1 == '25' and option2 == '60'):
model.load_state_dict(torch.load("models/shakespear model/shakespear25_60.pth", map_location = device))
elif(option1 == '25' and option2 == '150'):
model.load_state_dict(torch.load("models/shakespear model/shakespear25_150.pth", map_location = device))
elif(option1 == '50' and option2 == '60'):
model.load_state_dict(torch.load("models/shakespear model/shakespear50_60.pth", map_location = device))
elif(option1 == '50' and option2 == '150'):
model.load_state_dict(torch.load("models/shakespear model/shakespear50_150.pth", map_location = device))
elif(option1 == '100' and option2 == '60'):
model.load_state_dict(torch.load("models/shakespear model/shakespear100_60.pth", map_location = device))
else:
model.load_state_dict(torch.load("models/shakespear model/shakespear100_150.pth", map_location =device))
else:
model.load_state_dict(torch.load("models/shakespear model/shakespear_untrained.pth", map_location =device))
gen_text = generate_text(model, itos, stoi, block_size, num_chars, input_text)
st.subheader("Generated Text")
# print(gen_text)
type_text(gen_text)