import streamlit as st import torch from torch import nn from torchtext.data.utils import get_tokenizer class LanguageModel(nn.Module): def __init__(self, vocab_size, embedding_dim, hidden_dim, num_layers, dropout_rnn=0.5, dropout_embd=0.5): super().__init__() self.emb = nn.Embedding(vocab_size, embedding_dim) self.emb.weight.data.uniform_(-0.1, 0.1) self.lstm = nn.LSTM(embedding_dim, hidden_dim, num_layers=num_layers, dropout=dropout_rnn, batch_first=True) self.fc = nn.Linear(hidden_dim, vocab_size) self.dropout = nn.Dropout(dropout_embd) def forward(self, src): embedding = self.dropout(self.emb(src)) output, _ = self.lstm(embedding) prediction = self.fc(output) return prediction embedding_dim = 300 num_layers = 3 hidden_dim = 1150 dropoute = 0.1 dropouti = 0.65 dropouth = 0.3 dropouto = 0.4 weight_drop = 0. device = 'cuda' if torch.cuda.is_available() else 'cpu' model = torch.load('model.pt', map_location=torch.device(device)) model.eval() tokenizer = get_tokenizer('basic_english') vocab = torch.load('vocab.pt') LanguageModel(len(vocab), 300, 512, 2) def generate(prompt, tokenizer=tokenizer, vocab=vocab, model=model, max_seq_len=6, temperature=0.5, num_pred=4, seed=None): if seed is not None: torch.manual_seed(seed) itos = vocab.get_itos() preds = [] for _ in range(num_pred): seq = prompt indices = vocab(tokenizer(seq)) itos = vocab.get_itos() for i in range(max_seq_len): src = torch.LongTensor(indices).to(device) with torch.no_grad(): prediction = model(src) probs = torch.softmax(prediction[-1]/temperature, dim=0) idx = vocab[''] while idx == vocab['']: idx = torch.multinomial(probs, num_samples=1).item() token = itos[idx] seq += ' ' + token if idx == vocab['.']: break indices.append(idx) preds.append(seq) return preds st.set_page_config( page_title="Language Modeling Web App", page_icon="🧩", ) st.title('Language Modeling Web App') st.markdown(""" Welcome to our Language modeling Web App! This application contains a language modeling model using PyTorch, trained on the WikiText-2 dataset, and deployed as an interactive web application using Streamlit. Enter a word or phrase then press ENTER and let our deep learning model predict the rest of the phrase. Experience the power of AI in language modeling! * **Python libraries:** pytorch, torchtext, streamlit * **Data source:** [github-repository](https://github.com/shgyg99/LanguageModeling). """) st.write('---') st.markdown( """ """, unsafe_allow_html=True ) if 'user_input' not in st.session_state: st.session_state['user_input'] = "" st.markdown('
', unsafe_allow_html=True) user_input = st.text_input("", placeholder="Enter a word or phrase...", label_visibility="collapsed") if user_input != st.session_state['user_input']: st.session_state['user_input'] = user_input if st.session_state['user_input']: suggestions = generate(st.session_state['user_input']) for suggestion in suggestions: st.markdown( f"""
{suggestion}
""", unsafe_allow_html=True ) st.markdown('', unsafe_allow_html=True) else: pass st.markdown('', unsafe_allow_html=True)