Spaces:
Runtime error
Runtime error
Sagar Desai commited on
Commit ·
7675310
1
Parent(s): 39a7e3d
corrected the page seq
Browse files- Name_Generator.py +59 -0
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 |
+
|