ishanarang commited on
Commit
e9a9a67
1 Parent(s): 87dfd2e
Sapp.py ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import torch
3
+ import torch.nn.functional as F
4
+ from torch import nn
5
+ import pandas as pd
6
+ import matplotlib.pyplot as plt # for making figures
7
+ # %matplotlib inline
8
+ # %config InlineBackend.figure_format = 'retina'
9
+ from pprint import pprint
10
+
11
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
12
+ data = open('shakespeare2.txt', 'r').read()
13
+
14
+ unique_chars = list(set(''.join(data)))
15
+ unique_chars.sort()
16
+ to_string = {i:ch for i, ch in enumerate(unique_chars)}
17
+ to_int = {ch:i for i, ch in enumerate(unique_chars)}
18
+
19
+
20
+ class NextChar(nn.Module):
21
+ def __init__(self, block_size, vocab_size, emb_dim, hidden_dims):
22
+ super().__init__()
23
+ self.emb = nn.Embedding(vocab_size, emb_dim)
24
+ self.lin1 = nn.Linear(block_size * emb_dim, hidden_dims[0])
25
+ self.lin2 = nn.Linear(hidden_dims[0], hidden_dims[1])
26
+ self.lin3 = nn.Linear(hidden_dims[1], vocab_size)
27
+
28
+ def forward(self, x):
29
+ x = self.emb(x)
30
+ x = x.view(x.shape[0], -1)
31
+ x = torch.sin(self.lin1(x))
32
+ x = torch.sin(self.lin2(x))
33
+ x = self.lin3(x)
34
+ return x
35
+
36
+ # For context size 5 and embedding size 5
37
+ model_c5_e5 = NextChar(5, len(to_int), 5, [64, 64])
38
+ model_c5_e5.load_state_dict(torch.load("context_5_embedding_5.pth"))
39
+
40
+ # For context size 5 and embedding size 10
41
+ model_c5_e10 = NextChar(5, len(to_int), 10, [64, 64])
42
+ model_c5_e10.load_state_dict(torch.load("context_5_embedding_10.pth"))
43
+
44
+ # For context size 7 and embedding size 5
45
+ model_c7_e5 = NextChar(7, len(to_int), 5, [64, 64])
46
+ model_c7_e5.load_state_dict(torch.load("context_7_embedding_5.pth"))
47
+
48
+ # For context size 7 and embedding size 10
49
+ model_c7_e10 = NextChar(7, len(to_int), 10, [64, 64])
50
+ model_c7_e10.load_state_dict(torch.load("context_7_embedding_10.pth"))
51
+
52
+ random_seed = 3
53
+ g = torch.Generator()
54
+ g.manual_seed(random_seed)
55
+ torch.manual_seed(random_seed)
56
+ def generate_name(model,sentence, itos, stoi, block_size, max_len=10):
57
+ original_sentence = sentence
58
+ if len(sentence) < block_size:
59
+ sentence = " " * (block_size - len(sentence)) + sentence
60
+ using_for_predicction = sentence[-block_size:].lower()
61
+ context = [stoi[word] for word in using_for_predicction]
62
+ prediction = ""
63
+ for i in range(max_len):
64
+ x = torch.tensor(context).view(1, -1).to(device)
65
+ print(type(model))
66
+ y_pred = model(x)
67
+ ix = torch.distributions.categorical.Categorical(logits=y_pred).sample().item()
68
+ ch = itos[ix]
69
+ prediction += ch
70
+ context = context[1:] + [ix]
71
+
72
+ return original_sentence + prediction
73
+
74
+ # Streamlit app
75
+ st.title("Next K Text Generation with MLP")
76
+ st.sidebar.title("Settings")
77
+
78
+
79
+ input_string = st.sidebar.text_input("Input String")
80
+ nextk = st.sidebar.number_input("Next K Tokens", min_value=1, max_value=500, value=150)
81
+ block_size = st.select_slider("Block Size", options=[5,7], value=5)
82
+ embedding_size = st.select_slider("Embedding Size", options=[5,10], value=5)
83
+
84
+
85
+ if st.sidebar.button("Generate Text"):
86
+
87
+ if block_size == 5:
88
+ context = input_string
89
+ if embedding_size == 5:
90
+ generated_text = generate_name(model_c5_e5,context, to_string, to_int, 5, max_len=nextk)
91
+ else:
92
+ generated_text = generate_name(model_c5_e10,context, to_string, to_int, 5, max_len=nextk)
93
+ elif block_size == 7:
94
+ context = input_string
95
+ if embedding_size == 10:
96
+ generated_text = generate_name(model_c7_e10, context, to_string, to_int, 7 ,max_len=nextk)
97
+ else:
98
+ generated_text = generate_name(model_c7_e5, context, to_string, to_int, 7, max_len=nextk)
99
+ st.write("Generated Text:")
100
+ st.write(generated_text)
101
+
102
+
103
+
context_5_embedding_10.pth ADDED
Binary file (51.8 kB). View file
 
context_5_embedding_5.pth ADDED
Binary file (44 kB). View file
 
context_7_embedding_10.pth ADDED
Binary file (56.9 kB). View file
 
context_7_embedding_5.pth ADDED
Binary file (46.6 kB). View file