Sagar Desai commited on
Commit
8cee544
1 Parent(s): 3e3134e

Add application file

Browse files
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ /env
Intro.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import streamlit as st
3
+
4
+ st.title("Intro")
5
+
6
+ st.write("")
7
+ st.write("")
8
+
9
+ st.subheader("Available Models")
10
+ st.markdown(
11
+ "Random Model - generates the name randomly"
12
+ )
13
+ st.markdown(
14
+ "Bigram model - character level model, 2 character understanding"
15
+ )
16
+
17
+ st.markdown(
18
+ "[repo link](https://github.com/SDcodehub/make_over.git)"
19
+ )
20
+
21
+ st.markdown(
22
+ "[Dataset](https://github.com/SDcodehub/make_over/blob/main/data/names.txt)"
23
+ )
24
+
25
+
26
+ st.subheader("Ref")
27
+ st.markdown(
28
+ "https://youtu.be/PaCmpygFfXo?si=MjyUM2oBykhJNuy1"
29
+ )
30
+
31
+ st.subheader("Credit to")
32
+ st.markdown(
33
+ "Andrej Karpathy"
34
+ )
README.md CHANGED
@@ -1,13 +1,10 @@
1
- ---
2
- title: Make Over
3
- emoji: 🦀
4
- colorFrom: red
5
- colorTo: yellow
6
- sdk: streamlit
7
- sdk_version: 1.25.0
8
- app_file: app.py
9
- pinned: false
10
- license: mit
11
- ---
12
-
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
+ # make_over
2
+
3
+ Available Models
4
+
5
+ Random Model - generates the name randomly
6
+ Bigram model - character level model, 2 character understanding
7
+
8
+ Ref - https://youtu.be/PaCmpygFfXo?si=MjyUM2oBykhJNuy1
9
+
10
+ Credit to - Andrej Karpathy
 
 
 
data/__init__.py ADDED
File without changes
data/names.txt ADDED
The diff for this file is too large to render. See raw diff
 
models/bigram-USA.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a5fd93e301e5d453f7afe0306907a083174608c4ad104b539d8381b8ce738570
3
+ size 3636
network/__init__.py ADDED
File without changes
network/__pycache__/__init__.cpython-311.pyc ADDED
Binary file (166 Bytes). View file
 
network/__pycache__/network.cpython-311.pyc ADDED
Binary file (5.6 kB). View file
 
network/example_gen.py ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ import torch
2
+
network/network.py ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from pathlib import Path
3
+ import string
4
+ import torch
5
+ import torch.nn.functional as F
6
+
7
+
8
+ class NeuralNetwork:
9
+ def __init__(self, lr, manual_seed) -> None:
10
+
11
+ self.lr = lr
12
+ chars = list(string.ascii_lowercase)
13
+ chars.insert(0, '.')
14
+ # string to index mapping we will call it as stoi
15
+ self.stoi = {s:i for i,s in enumerate(chars)}
16
+ self.itos = {i:s for s,i in self.stoi.items()}
17
+
18
+ # For character level model, 26 character and +1 for start and end special character
19
+ self.num_classes=27
20
+
21
+ self.g = torch.Generator().manual_seed(manual_seed)
22
+
23
+ def initialise_weights(self):
24
+ # initialise the 'network'
25
+ w = torch.randn((27,27), generator=self.g, requires_grad=True)
26
+ return w
27
+
28
+ def forward_pass(self, xs, w):
29
+ # forward pass
30
+ xenc = F.one_hot(xs, num_classes=self.num_classes).float() # input to the network one hot encodding
31
+ logits = xenc @ w
32
+ counts = logits.exp()
33
+ probs = counts / counts.sum(1, keepdims=True)
34
+ return probs
35
+
36
+ def loss(self, probs, ys, num):
37
+
38
+ loss = -probs[torch.arange(num), ys].log().mean()
39
+ print(f'{loss.item()=}')
40
+ return loss
41
+
42
+ def backword_pass(self, loss, w):
43
+ # backward pass
44
+ w.grad = None
45
+ loss.backward()
46
+
47
+ def update_weights(self,w):
48
+ # update
49
+ w.data += -self.lr * w.grad
50
+
51
+ def example_gen(self, words):
52
+ xs, ys = [], []
53
+ for w in words:
54
+ chs = '.' + w+ '.'
55
+ for ch1, ch2 in zip(chs, chs[1:]):
56
+ ix1 = self.stoi[ch1]
57
+ ix2 = self.stoi[ch2]
58
+ xs.append(ix1)
59
+ ys.append(ix2)
60
+ xs = torch.tensor(xs)
61
+ ys = torch.tensor(ys)
62
+ num=xs.nelement()
63
+ print(f'number of eaxmples: {num}')
64
+ return xs, ys, num
65
+
66
+
67
+ if __name__ == '__main__':
68
+
69
+ get_cwd = os.getcwd()
70
+ project_dir = Path(get_cwd).parents[0]
71
+ data_path = os.path.join(project_dir, 'data')
72
+ words = open(os.path.join(data_path, "names.txt"), 'r').read().splitlines()
73
+
74
+ nn = NeuralNetwork(1,2147483647)
75
+
76
+ # Gen examples
77
+ xs, ys, num = nn.example_gen(words)
78
+
79
+ w = nn.initialise_weights()
80
+
81
+
82
+ for k in range(500):
83
+
84
+ # forward pass
85
+ probs = nn.forward_pass(xs,w)
86
+
87
+ # loss
88
+ loss = nn.loss(probs, ys, num)
89
+
90
+ # backward pass
91
+ nn.backword_pass(loss, w)
92
+
93
+ #update
94
+ nn.update_weights(w)
95
+
96
+
97
+
notebook/bigram-USA.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a5fd93e301e5d453f7afe0306907a083174608c4ad104b539d8381b8ce738570
3
+ size 3636
notebook/build_makemore.ipynb ADDED
The diff for this file is too large to render. See raw diff
 
pages/Name_Generator.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from pathlib import Path
3
+ import streamlit as st
4
+ import torch
5
+ from network.network import NeuralNetwork
6
+ import torch.nn.functional as F
7
+
8
+ # Page title
9
+ st.set_page_config(page_title='Name Generator')
10
+ st.title('Name Generator')
11
+
12
+ # Select Model - drop down
13
+ model_list = [
14
+ 'Random model',
15
+ 'Bigram model'
16
+ ]
17
+ model_name = st.selectbox('Select an example query:', model_list)
18
+
19
+ # Number of outputs - input field
20
+ num_results = st.number_input("Number of Names to be Generated", min_value=1, max_value=50)
21
+
22
+ # Process
23
+ # get weights
24
+ with st.form('myform', clear_on_submit=True):
25
+
26
+ submitted = st.form_submit_button('Submit')
27
+
28
+ if submitted:
29
+ # get current path
30
+ get_cwd = os.getcwd()
31
+ project_dir = get_cwd
32
+ models_path = os.path.join(project_dir, 'models')
33
+
34
+ if model_name == 'Bigram model':
35
+ w = torch.load(os.path.join(models_path, 'bigram-USA.pt'))
36
+ elif model_name == 'Random model':
37
+ w = torch.ones(27,27) * 0.01
38
+
39
+ for i in range(num_results):
40
+ ix = 0
41
+ name=""
42
+ y = torch.Generator().manual_seed(2147483647)
43
+ while True:
44
+ nn = NeuralNetwork(50, 2147483647)
45
+
46
+ xenc = F.one_hot(torch.tensor([ix]), num_classes=27).float() # input to the network one hot encodding
47
+ logits = xenc @ w
48
+ counts = logits.exp()
49
+ probs = counts / counts.sum(1, keepdims=True)
50
+
51
+ ix = torch.multinomial(probs, num_samples=1, replacement=True).item()
52
+ name += nn.itos[ix]
53
+ if nn.itos[ix] ==".":
54
+ break
55
+ st.write(name)
56
+
57
+
58
+
59
+
pages/__init__.py ADDED
File without changes
requirements.txt ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ altair==5.0.1
2
+ asttokens==2.2.1
3
+ attrs==23.1.0
4
+ backcall==0.2.0
5
+ blinker==1.6.2
6
+ cachetools==5.3.1
7
+ certifi==2023.7.22
8
+ charset-normalizer==3.2.0
9
+ click==8.1.6
10
+ colorama==0.4.6
11
+ comm==0.1.4
12
+ contourpy==1.1.0
13
+ cycler==0.11.0
14
+ debugpy==1.6.7.post1
15
+ decorator==5.1.1
16
+ executing==1.2.0
17
+ filelock==3.12.2
18
+ fonttools==4.42.0
19
+ gitdb==4.0.10
20
+ GitPython==3.1.32
21
+ idna==3.4
22
+ importlib-metadata==6.8.0
23
+ ipykernel==6.25.1
24
+ ipython==8.14.0
25
+ jedi==0.19.0
26
+ Jinja2==3.1.2
27
+ jsonschema==4.19.0
28
+ jsonschema-specifications==2023.7.1
29
+ jupyter_client==8.3.0
30
+ jupyter_core==5.3.1
31
+ kiwisolver==1.4.4
32
+ markdown-it-py==3.0.0
33
+ MarkupSafe==2.1.3
34
+ matplotlib==3.7.2
35
+ matplotlib-inline==0.1.6
36
+ mdurl==0.1.2
37
+ mpmath==1.3.0
38
+ nest-asyncio==1.5.7
39
+ networkx==3.1
40
+ numpy==1.25.2
41
+ packaging==23.1
42
+ pandas==2.0.3
43
+ parso==0.8.3
44
+ pickleshare==0.7.5
45
+ Pillow==9.5.0
46
+ platformdirs==3.10.0
47
+ prompt-toolkit==3.0.39
48
+ protobuf==4.24.0
49
+ psutil==5.9.5
50
+ pure-eval==0.2.2
51
+ pyarrow==12.0.1
52
+ pydeck==0.8.0
53
+ Pygments==2.16.1
54
+ Pympler==1.0.1
55
+ pyparsing==3.0.9
56
+ python-dateutil==2.8.2
57
+ pytz==2023.3
58
+ pytz-deprecation-shim==0.1.0.post0
59
+ pywin32==306
60
+ pyzmq==25.1.1
61
+ referencing==0.30.2
62
+ requests==2.31.0
63
+ rich==13.5.2
64
+ rpds-py==0.9.2
65
+ six==1.16.0
66
+ smmap==5.0.0
67
+ stack-data==0.6.2
68
+ streamlit==1.25.0
69
+ sympy==1.12
70
+ tenacity==8.2.3
71
+ toml==0.10.2
72
+ toolz==0.12.0
73
+ torch==2.0.1
74
+ torchvision==0.15.2
75
+ tornado==6.3.3
76
+ traitlets==5.9.0
77
+ typing_extensions==4.7.1
78
+ tzdata==2023.3
79
+ tzlocal==4.3.1
80
+ urllib3==2.0.4
81
+ validators==0.21.2
82
+ watchdog==3.0.0
83
+ wcwidth==0.2.6
84
+ zipp==3.16.2