Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import streamlit as st
|
3 |
+
import numpy as np
|
4 |
+
import pandas as pd
|
5 |
+
import os
|
6 |
+
import torch
|
7 |
+
import torch.nn as nn
|
8 |
+
from transformers import ElectraModel, AutoConfig, GPT2LMHeadModel
|
9 |
+
from transformers.activations import get_activation
|
10 |
+
from transformers import AutoTokenizer
|
11 |
+
|
12 |
+
|
13 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
14 |
+
|
15 |
+
from transformers import AutoTokenizer, AutoModelForMaskedLM
|
16 |
+
artist_name = st.text_input("Model", "roberta-base")
|
17 |
+
tokenizer = AutoTokenizer.from_pretrained("roberta-base")
|
18 |
+
model = AutoModelForMaskedLM.from_pretrained(artist_name)
|
19 |
+
|
20 |
+
first = "Boston is a <mask> place to live."
|
21 |
+
|
22 |
+
with st.form(key='my_form'):
|
23 |
+
prompt = st.text_area(label='Enter Text. Put <mask> where you want the model to fill in the blank. You can use more than one at a time.', value=first)
|
24 |
+
submit_button = st.form_submit_button(label='Submit')
|
25 |
+
|
26 |
+
if submit_button:
|
27 |
+
a_list = []
|
28 |
+
token_ids = tokenizer.encode(prompt, return_tensors='pt')
|
29 |
+
token_ids_tk = tokenizer.tokenize(prompt, return_tensors='pt')
|
30 |
+
masked_position = (token_ids.squeeze() == tokenizer.mask_token_id).nonzero()
|
31 |
+
masked_pos = [mask.item() for mask in masked_position ]
|
32 |
+
with torch.no_grad():
|
33 |
+
output = model(token_ids)
|
34 |
+
last_hidden_state = output[0].squeeze()
|
35 |
+
for mask_index in masked_pos:
|
36 |
+
mask_hidden_state = last_hidden_state[mask_index]
|
37 |
+
idx = torch.topk(mask_hidden_state, k=100, dim=0)[1]
|
38 |
+
words = [tokenizer.decode(i.item()).strip() for i in idx]
|
39 |
+
st.text_area(label = 'Infill:', value=words)
|