Spaces:
Runtime error
Runtime error
File size: 10,012 Bytes
fc757ce a37ef4f 5e48936 a37ef4f 5e48936 d6cc25a 77de468 23c047e 52eb3b0 a6a5c53 23a0752 77de468 a08c530 23a0752 4b92f55 39e9427 48d2c46 2158ccf 5307bf5 3619d45 67c005c a08c530 599c53c 67c005c 4c3bc8a f474d4a 880a9a9 0e34b1b df1a01f 51cb87a 599c53c 7671981 599c53c 6c030df df7f0f8 a37ef4f 4e7c469 a37ef4f 6c030df a37ef4f cf031ee a37ef4f cf031ee a37ef4f 1f2cac2 a37ef4f a8a914a 1fef982 a8a914a 1fef982 a8a914a a37ef4f a8a914a a37ef4f a8a914a a37ef4f a8a914a a37ef4f a8a914a a37ef4f a8a914a a37ef4f a8a914a a37ef4f a8a914a a37ef4f a8a914a a37ef4f 5e48936 a37ef4f 5e48936 a37ef4f 5e48936 a37ef4f 5e48936 a37ef4f e0e7abc a37ef4f 1f2cac2 7eaf946 a37ef4f a8a914a a37ef4f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
import streamlit as st
from transformers import AutoTokenizer, AutoModelForCausalLM, AutoModel
import torch
first = """informal english: corn fields are all across illinois, visible once you leave chicago.\nTranslated into the Style of Abraham Lincoln: corn fields ( permeate illinois / span the state of illinois / ( occupy / persist in ) all corners of illinois / line the horizon of illinois / envelop the landscape of illinois ), manifesting themselves visibly as one ventures beyond chicago.\n\ninformal english: """
@st.cache(allow_output_mutation=True)
def get_model():
#tokenizer = AutoTokenizer.from_pretrained("BigSalmon/InformalToFormalLincoln86Paraphrase")
#model = AutoModelForCausalLM.from_pretrained("BigSalmon/InformalToFormalLincoln86Paraphrase")
#tokenizer = AutoTokenizer.from_pretrained("BigSalmon/InformalToFormalLincoln82Paraphrase")
#model = AutoModelForCausalLM.from_pretrained("BigSalmon/InformalToFormalLincoln82Paraphrase")
#tokenizer = AutoTokenizer.from_pretrained("BigSalmon/InformalToFormalLincoln79Paraphrase")
#model = AutoModelForCausalLM.from_pretrained("BigSalmon/InformalToFormalLincoln79Paraphrase")
#tokenizer = AutoTokenizer.from_pretrained("BigSalmon/InformalToFormalLincoln74Paraphrase")
#model = AutoModelForCausalLM.from_pretrained("BigSalmon/InformalToFormalLincoln74Paraphrase")
#tokenizer = AutoTokenizer.from_pretrained("BigSalmon/InformalToFormalLincoln72Paraphrase")
#model = AutoModelForCausalLM.from_pretrained("BigSalmon/InformalToFormalLincoln72Paraphrase")
#tokenizer = AutoTokenizer.from_pretrained("BigSalmon/InformalToFormalLincoln64Paraphrase")
#model = AutoModelForCausalLM.from_pretrained("BigSalmon/InformalToFormalLincoln64Paraphrase")
#tokenizer = AutoTokenizer.from_pretrained("BigSalmon/InformalToFormalLincoln60Paraphrase")
#model = AutoModelForCausalLM.from_pretrained("BigSalmon/InformalToFormalLincoln60Paraphrase")
#tokenizer = AutoTokenizer.from_pretrained("BigSalmon/GPTNeo1.3BInformalToFormal")
#model = AutoModelForCausalLM.from_pretrained("BigSalmon/GPTNeo1.3BInformalToFormal")
#tokenizer = AutoTokenizer.from_pretrained("BigSalmon/InformalToFormalLincoln55")
#model = AutoModelForCausalLM.from_pretrained("BigSalmon/InformalToFormalLincoln55")
#tokenizer = AutoTokenizer.from_pretrained("BigSalmon/InformalToFormalLincoln51")
#model = AutoModelForCausalLM.from_pretrained("BigSalmon/InformalToFormalLincoln51")
#tokenizer = AutoTokenizer.from_pretrained("BigSalmon/InformalToFormalLincoln45")
#model = AutoModelForCausalLM.from_pretrained("BigSalmon/InformalToFormalLincoln49")
#tokenizer = AutoTokenizer.from_pretrained("BigSalmon/InformalToFormalLincoln43")
#model = AutoModelForCausalLM.from_pretrained("BigSalmon/InformalToFormalLincoln43")
#tokenizer = AutoTokenizer.from_pretrained("BigSalmon/InformalToFormalLincoln41")
#model = AutoModelForCausalLM.from_pretrained("BigSalmon/InformalToFormalLincoln41")
#tokenizer = AutoTokenizer.from_pretrained("BigSalmon/InformalToFormalLincoln38")
#model = AutoModelForCausalLM.from_pretrained("BigSalmon/InformalToFormalLincoln38")
#tokenizer = AutoTokenizer.from_pretrained("BigSalmon/InformalToFormalLincoln37")
#model = AutoModelForCausalLM.from_pretrained("BigSalmon/InformalToFormalLincoln37")
#tokenizer = AutoTokenizer.from_pretrained("BigSalmon/InformalToFormalLincoln36")
#model = AutoModelForCausalLM.from_pretrained("BigSalmon/InformalToFormalLincoln36")
#tokenizer = AutoTokenizer.from_pretrained("BigSalmon/MediumInformalToFormalLincoln")
#model = AutoModelForCausalLM.from_pretrained("BigSalmon/MediumInformalToFormalLincoln")
#tokenizer = AutoTokenizer.from_pretrained("BigSalmon/InformalToFormalLincoln35")
#model = AutoModelForCausalLM.from_pretrained("BigSalmon/InformalToFormalLincoln35")
#tokenizer = AutoTokenizer.from_pretrained("BigSalmon/InformalToFormalLincoln31")
#model = AutoModelForCausalLM.from_pretrained("BigSalmon/InformalToFormalLincoln31")
#tokenizer = AutoTokenizer.from_pretrained("BigSalmon/InformalToFormalLincoln21")
#model = AutoModelForCausalLM.from_pretrained("BigSalmon/InformalToFormalLincoln21")
#tokenizer = AutoTokenizer.from_pretrained("BigSalmon/PointsOneSent")
#model = AutoModelForCausalLM.from_pretrained("BigSalmon/PointsOneSent")
#tokenizer = AutoTokenizer.from_pretrained("BigSalmon/PointsToSentence")
#model = AutoModelForCausalLM.from_pretrained("BigSalmon/PointsToSentence")
tokenizer = AutoTokenizer.from_pretrained("BigSalmon/InformalToFormalLincoln89Paraphrase")
model = AutoModelForCausalLM.from_pretrained("BigSalmon/InformalToFormalLincoln89Paraphrase")
tokenizer2 = AutoTokenizer.from_pretrained("BigSalmon/InformalToFormalLincolnMedium")
model2 = AutoModelForCausalLM.from_pretrained("BigSalmon/InformalToFormalLincolnMedium")
return model, model2, tokenizer, tokenizer2
model, model2, tokenizer, tokenizer2 = get_model()
st.text('''For Prompt Templates: https://huggingface.co/BigSalmon/InformalToFormalLincoln82Paraphrase''')
temp = st.sidebar.slider("Temperature", 0.7, 1.5)
number_of_outputs = st.sidebar.slider("Number of Outputs", 5, 50)
lengths = st.sidebar.slider("Length", 3, 500)
bad_words = st.text_input("Words You Do Not Want Generated", " core lemon height time ")
logs_outputs = st.sidebar.slider("Logit Outputs", 50, 300)
def run_generate(text, bad_words):
yo = []
input_ids = tokenizer.encode(text, return_tensors='pt')
res = len(tokenizer.encode(text))
bad_words = bad_words.split()
bad_word_ids = []
for bad_word in bad_words:
bad_word = " " + bad_word
ids = tokenizer(bad_word).input_ids
bad_word_ids.append(ids)
sample_outputs = model.generate(
input_ids,
do_sample=True,
max_length= res + lengths,
min_length = res + lengths,
top_k=50,
temperature=temp,
num_return_sequences=number_of_outputs,
bad_words_ids=bad_word_ids
)
for i in range(number_of_outputs):
e = tokenizer.decode(sample_outputs[i])
e = e.replace(text, "")
yo.append(e)
return yo
def BestProbs5(prompt):
prompt = prompt.strip()
text = tokenizer.encode(prompt)
myinput, past_key_values = torch.tensor([text]), None
myinput = myinput
logits, past_key_values = model(myinput, past_key_values = past_key_values, return_dict=False)
logits = logits[0,-1]
probabilities = torch.nn.functional.softmax(logits)
best_logits, best_indices = logits.topk(number_of_outputs)
best_words = [tokenizer.decode([idx.item()]) for idx in best_indices]
for i in best_words[0:number_of_outputs]:
#print(i)
print("\n")
g = (prompt + i)
st.write(g)
l = run_generate(g, "hey")
st.write(l)
def run_generate2(text, bad_words):
yo = []
input_ids = tokenizer2.encode(text, return_tensors='pt')
res = len(tokenizer2.encode(text))
bad_words = bad_words.split()
bad_word_ids = []
for bad_word in bad_words:
bad_word = " " + bad_word
ids = tokenizer2(bad_word).input_ids
bad_word_ids.append(ids)
sample_outputs = model2.generate(
input_ids,
do_sample=True,
max_length= res + lengths,
min_length = res + lengths,
top_k=50,
temperature=temp,
num_return_sequences=number_of_outputs,
bad_words_ids=bad_word_ids
)
for i in range(number_of_outputs):
e = tokenizer2.decode(sample_outputs[i])
e = e.replace(text, "")
yo.append(e)
return yo
def prefix_format(sentence):
words = sentence.split()
if "[MASK]" in sentence:
words2 = words.index("[MASK]")
#print(words2)
output = ("<Prefix> " + ' '.join(words[:words2]) + " <Prefix> " + "<Suffix> " + ' '.join(words[words2+1:]) + " <Suffix>" + " <Middle>")
st.write(output)
else:
st.write("Add [MASK] to sentence")
with st.form(key='my_form'):
text = st.text_area(label='Enter sentence', value=first)
submit_button = st.form_submit_button(label='Submit')
submit_button2 = st.form_submit_button(label='Submit Log Probs')
submit_button3 = st.form_submit_button(label='Submit Other Model')
submit_button4 = st.form_submit_button(label='Submit Log Probs Other Model')
submit_button5 = st.form_submit_button(label='Most Prob')
submit_button6 = st.form_submit_button(label='Turn Sentence with [MASK] into <Prefix> Format')
if submit_button:
translated_text = run_generate(text, bad_words)
st.write(translated_text if translated_text else "No translation found")
if submit_button2:
with torch.no_grad():
text2 = str(text)
print(text2)
text3 = tokenizer.encode(text2)
myinput, past_key_values = torch.tensor([text3]), None
myinput = myinput
logits, past_key_values = model(myinput, past_key_values = past_key_values, return_dict=False)
logits = logits[0,-1]
probabilities = torch.nn.functional.softmax(logits)
best_logits, best_indices = logits.topk(logs_outputs)
best_words = [tokenizer.decode([idx.item()]) for idx in best_indices]
st.write(best_words)
if submit_button3:
translated_text = run_generate2(text, bad_words)
st.write(translated_text if translated_text else "No translation found")
if submit_button4:
text2 = str(text)
print(text2)
text3 = tokenizer2.encode(text2)
myinput, past_key_values = torch.tensor([text3]), None
myinput = myinput
logits, past_key_values = model2(myinput, past_key_values = past_key_values, return_dict=False)
logits = logits[0,-1]
probabilities = torch.nn.functional.softmax(logits)
best_logits, best_indices = logits.topk(logs_outputs)
best_words = [tokenizer2.decode([idx.item()]) for idx in best_indices]
st.write(best_words)
if submit_button5:
BestProbs5(text)
if submit_button6:
text2 = str(text)
prefix_format(text2) |