Commit
·
ef797fb
1
Parent(s):
07d2559
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,6 +11,7 @@ from transformers import (AutoModelForCausalLM, AutoModelForQuestionAnswering,
|
|
| 11 |
pipeline, top_k_top_p_filtering)
|
| 12 |
|
| 13 |
|
|
|
|
| 14 |
st.set_page_config(page_title="Gadsby")
|
| 15 |
st.title("Gadsby - Constrained Text Generation with Transformers")
|
| 16 |
st.caption("By Allen Roush")
|
|
@@ -25,7 +26,7 @@ form.header("Main Settings")
|
|
| 25 |
|
| 26 |
model_name = form.text_area("Enter the name of the pre-trained model from transformers that we are using for Text Generation", value = "gpt2")
|
| 27 |
form.caption("This will download a new model, so it may take awhile or even break if the model is too large")
|
| 28 |
-
mode = form.selectbox("What kind of constrained generation are we doing?", ["lipogram", "reverse_lipogram", "e-prime", "rhopalism", "length_constrained", "greater_than_length"])
|
| 29 |
form.caption("Lipograms mean that a letter (or substring) is not allowed in the generated string, reverse lipograms force a letter to be in the generated string")
|
| 30 |
|
| 31 |
if mode == "lipogram":
|
|
@@ -42,12 +43,17 @@ elif mode == "reverse_lipogram":
|
|
| 42 |
elif mode == "rhopalism":
|
| 43 |
length_constraint = form.number_input("Enter the length that the Rhopalism shoud start with", value = 1)
|
| 44 |
st.caption("Rhopalisms are usually reliable but sometimes you need to try generating two or three times for a perfect one")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
else:
|
| 46 |
length_constraint = form.number_input("Enter the length should each word be restricted to (or greater/less than)", value = 5) + 1
|
| 47 |
|
| 48 |
|
| 49 |
length = form.number_input("Select how long you want the generated text to be", value = 100)
|
| 50 |
-
number_of_tokens_to_sample = form.number_input("Select how many tokens we want to search through when we do the filtering", value =
|
| 51 |
form.caption("Settings this to higher numbers will improve the experience but will cause generating to slow. Low numbers may cause lots of blank or failed generations")
|
| 52 |
temperature = form.number_input("How spicy/interesting do we want our models output to be", value = 1.05, min_value = 0.0)
|
| 53 |
form.caption("Setting this higher decreases the likelihood of high probability words and increases the likelihood of low probability (and presumably more interesting) words")
|
|
@@ -68,9 +74,13 @@ def isPalindrome(s):
|
|
| 68 |
return s == s[::-1]
|
| 69 |
|
| 70 |
|
| 71 |
-
if mode == "rhopalism":
|
| 72 |
rhopalism_len = length_constraint
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
|
| 76 |
def get_next_word_without_e(input_sequence):
|
|
@@ -110,19 +120,38 @@ def get_next_word_without_e(input_sequence):
|
|
| 110 |
##Mostly works
|
| 111 |
if len(resulting_string) == rhopalism_len:
|
| 112 |
return resulting_string
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
return " "
|
| 114 |
|
| 115 |
|
| 116 |
|
|
|
|
|
|
|
| 117 |
i = length
|
| 118 |
while i > 0:
|
|
|
|
| 119 |
new_word = get_next_word_without_e(input_sequence= sequence)
|
| 120 |
sequence = sequence + new_word
|
| 121 |
-
if mode == "rhopalism":
|
| 122 |
rhopalism_len += 1
|
| 123 |
i = i-1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
st.write("GENERATED SEQUENCE: ")
|
| 126 |
st.write(sequence)
|
|
|
|
| 127 |
|
| 128 |
|
|
|
|
| 11 |
pipeline, top_k_top_p_filtering)
|
| 12 |
|
| 13 |
|
| 14 |
+
|
| 15 |
st.set_page_config(page_title="Gadsby")
|
| 16 |
st.title("Gadsby - Constrained Text Generation with Transformers")
|
| 17 |
st.caption("By Allen Roush")
|
|
|
|
| 26 |
|
| 27 |
model_name = form.text_area("Enter the name of the pre-trained model from transformers that we are using for Text Generation", value = "gpt2")
|
| 28 |
form.caption("This will download a new model, so it may take awhile or even break if the model is too large")
|
| 29 |
+
mode = form.selectbox("What kind of constrained generation are we doing?", ["lipogram", "reverse_lipogram", "e-prime", "rhopalism", "length_constrained", "greater_than_length", "Pangram", "rhopalism-lipogram"])
|
| 30 |
form.caption("Lipograms mean that a letter (or substring) is not allowed in the generated string, reverse lipograms force a letter to be in the generated string")
|
| 31 |
|
| 32 |
if mode == "lipogram":
|
|
|
|
| 43 |
elif mode == "rhopalism":
|
| 44 |
length_constraint = form.number_input("Enter the length that the Rhopalism shoud start with", value = 1)
|
| 45 |
st.caption("Rhopalisms are usually reliable but sometimes you need to try generating two or three times for a perfect one")
|
| 46 |
+
elif mode == "rhopalism-lipogram":
|
| 47 |
+
naughty_strings_list = st.text_area("Enter the list of strings that you don't want in each word seperated by a space", value = "E e")
|
| 48 |
+
naughty_strings = naughty_strings_list.split(" ")
|
| 49 |
+
length_constraint = form.number_input("Enter the length that the Rhopalism shoud start with", value = 1)
|
| 50 |
+
st.caption("Rhopalisms are usually reliable but sometimes you need to try generating two or three times for a perfect one")
|
| 51 |
else:
|
| 52 |
length_constraint = form.number_input("Enter the length should each word be restricted to (or greater/less than)", value = 5) + 1
|
| 53 |
|
| 54 |
|
| 55 |
length = form.number_input("Select how long you want the generated text to be", value = 100)
|
| 56 |
+
number_of_tokens_to_sample = form.number_input("Select how many tokens we want to search through when we do the filtering", value = 25000)
|
| 57 |
form.caption("Settings this to higher numbers will improve the experience but will cause generating to slow. Low numbers may cause lots of blank or failed generations")
|
| 58 |
temperature = form.number_input("How spicy/interesting do we want our models output to be", value = 1.05, min_value = 0.0)
|
| 59 |
form.caption("Setting this higher decreases the likelihood of high probability words and increases the likelihood of low probability (and presumably more interesting) words")
|
|
|
|
| 74 |
return s == s[::-1]
|
| 75 |
|
| 76 |
|
| 77 |
+
if mode == "rhopalism" or mode == "rhopalism-lipogram":
|
| 78 |
rhopalism_len = length_constraint
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
nice_strings_pangram = list(string.ascii_lowercase)
|
| 83 |
+
|
| 84 |
|
| 85 |
|
| 86 |
def get_next_word_without_e(input_sequence):
|
|
|
|
| 120 |
##Mostly works
|
| 121 |
if len(resulting_string) == rhopalism_len:
|
| 122 |
return resulting_string
|
| 123 |
+
elif mode == "Pangram":
|
| 124 |
+
if any(c in nice_strings_pangram for c in resulting_string):
|
| 125 |
+
return resulting_string
|
| 126 |
+
elif mode == "rhopalism-lipogram":
|
| 127 |
+
if len(resulting_string) == rhopalism_len:
|
| 128 |
+
if all(nauty_string not in resulting_string for nauty_string in naughty_strings):
|
| 129 |
+
return resulting_string
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
|
| 133 |
return " "
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
+
|
| 138 |
+
j = 0
|
| 139 |
i = length
|
| 140 |
while i > 0:
|
| 141 |
+
current_digit = int(digits_of_pi[j])
|
| 142 |
new_word = get_next_word_without_e(input_sequence= sequence)
|
| 143 |
sequence = sequence + new_word
|
| 144 |
+
if mode == "rhopalism" or mode == "rhopalism-lipogram":
|
| 145 |
rhopalism_len += 1
|
| 146 |
i = i-1
|
| 147 |
+
if mode == "Pangram":
|
| 148 |
+
for character in sequence:
|
| 149 |
+
if character in nice_strings_pangram:
|
| 150 |
+
nice_strings_pangram.remove(character)
|
| 151 |
+
j += 1
|
| 152 |
|
| 153 |
st.write("GENERATED SEQUENCE: ")
|
| 154 |
st.write(sequence)
|
| 155 |
+
#st.write(nice_strings_pangram)
|
| 156 |
|
| 157 |
|