-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathConstrained-Text-Generation-Studio.py
1166 lines (976 loc) · 63.3 KB
/
Constrained-Text-Generation-Studio.py
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import random
import re
import string
import time as time
from collections import Counter
import numpy as np
import dearpygui.dearpygui as dpg
import fasttext
import pronouncing
import torch
from huggingface_hub import hf_hub_download
from scipy import spatial
from torch.nn import functional as F
from transformers import (AutoModelForCausalLM, AutoModelForQuestionAnswering,
AutoModelForSeq2SeqLM,
AutoModelForSequenceClassification, AutoTokenizer,
GPT2Tokenizer, LogitsProcessor, LogitsProcessorList,
pipeline, top_k_top_p_filtering)
from screeninfo import get_monitors
##TODO: Allow more semantic, phonetic, lexical models (Maybe allow specification of fasttext weights, dropdown select some phonetic models)
##TODO: Loading Bars when loading model, and when generating text
##TODO: Allow the user to decide to regenerate new predictions with a right click?
##TODO: Button for loading Masked Language Models, and then displaying the tokenizer masking character, enabling the user to right click and to tab
##TODO: Proper layout somehow
##TODO: "novalty generation constraints" like pilish (specify list of word lengths) or pangram/perfect pangram generation
starttime = time.time()
fasttext_model = fasttext.load_model(hf_hub_download("osanseviero/fasttext_embedding", "model.bin"))
tokenizer = ""
model = ""
def add_and_load_image(image_path, parent=None):
width, height, channels, data = dpg.load_image(image_path)
with dpg.texture_registry() as reg_id:
texture_id = dpg.add_static_texture(width, height, data, parent=reg_id)
if parent is None:
return dpg.add_image(texture_id)
else:
return dpg.add_image(texture_id, parent=parent)
def _help(message):
last_item = dpg.last_item()
group = dpg.add_group(horizontal=True)
dpg.move_item(last_item, parent=group)
dpg.capture_next_item(lambda s: dpg.move_item(s, parent=group))
t = dpg.add_text("(?)", color=[0, 255, 0])
with dpg.tooltip(t):
dpg.add_text(message)
def load_model(the_name):
global tokenizer
global model
if the_name == "load_model":
model_name = dpg.get_value("model_name")
else:
model_name = the_name
tokenizer = AutoTokenizer.from_pretrained(model_name)
if dpg.get_value("percision") == "8bit":
model = AutoModelForCausalLM.from_pretrained(model_name, device_map='auto', load_in_8bit=True)
elif dpg.get_value("percision") == "16bit":
model = AutoModelForCausalLM.from_pretrained(model_name, device_map='auto', load_in_8bit=False, torch_dtype=torch.float16)
else:
model = AutoModelForCausalLM.from_pretrained(model_name, device_map='auto', load_in_8bit=False)
if torch.cuda.is_available():
loaded_model_string = model_name + " Loaded Succesfully onto the GPU" + " with " + dpg.get_value("percision") + " percision"
dpg.add_text(parent = "load_model_window", default_value = loaded_model_string)
else:
loaded_model_string = model_name + " Loaded Succesfully onto the CPU" + " with " + dpg.get_value("percision") + " percision"
dpg.add_text(parent = "load_model_window", default_value = loaded_model_string)
########--------------------------------------------------------------#### Filters ########--------------------------------------------------------------####
def all_letters_included(word, string_list):
return all(c in word[0] for c in string_list)
def any_letters_included(word, string_list):
return any(c in string_list for c in word[0])
def all_letters_not_included(word, string_list):
#any(c in letters for c in word)
return all(c not in word[0] for c in string_list)
def any_letters_not_included(word, string_list):
## "e.g. not words with both B and T in them!"
return any(c not in string_list for c in word[0])
def equal_to_length(word, word_length):
return len(word[0]) == word_length
def greater_than_length(word, word_length):
return len(word[0]) > word_length
def less_than_length(word, word_length):
return len(word[0]) < word_length
def ends_with(word, ending_string, start = None, end = None):
return word[0].endswith(ending_string, start, end)
def starts_with(word, starting_string, start = None, end = None):
return word[0].startswith(starting_string, start, end)
def string_in_position(word, a_string_list, position_index_list):
for idx, string in enumerate(a_string_list):
try:
if word[0][position_index_list[idx]] != string:
return False
except:
return False
return True
def phonetic_matching(word, phonetic_matching_string):
if pe.encode(word[0]) == pe.encode(phonetic_matching_string):
return True
else:
return False
def string_edit(word, string_edit_string, string_edit_threshold):
if cmp.dist_abs(word[0], string_edit_string) <= string_edit_threshold:
return True
else:
return False
def semantic_matching(word, semantic_string, semantic_threshold):
word_word_vector = fasttext_model.get_word_vector(word[0])
semantic_string_vector = fasttext_model.get_word_vector(semantic_string)
similarity = 1 - spatial.distance.cosine(word_word_vector, semantic_string_vector)
if similarity >= semantic_threshold:
return True
else:
return False
def rhyme(word, rhyme_string):
the_string = word[0]
if the_string in pronouncing.rhymes(rhyme_string):
return True
else:
return False
def meter(word, meter_string):
the_string = word[0]
phones_list = pronouncing.phones_for_word(meter_string)
meter_string_stress = pronouncing.stresses(phones_list[0])
word_phones_list = pronouncing.phones_for_word(the_string)
if len(word_phones_list) > 0:
word_stress = pronouncing.stresses(word_phones_list[0])
if word_stress == meter_string_stress:
return True
else:
return False
else:
return False
def syllable(word, syllable_number):
the_string = word[0]
phones_list = pronouncing.phones_for_word(the_string)
if len(phones_list) > 0:
syllable_count = pronouncing.syllable_count(phones_list[0])
if syllable_count == syllable_number:
return True
else:
return False
else:
return False
def palindrome(word):
the_string = word[0]
if the_string == the_string[::-1]:
return True
else:
return False
def partial_anagram(word, a_string):
###Like a full anagram but allows anagramic substrings
if Counter(word[0]) - Counter(a_string):
return False
return True
def full_anagram(word, a_string):
### Only true anagrams!
if Counter(word[0]) == Counter(a_string):
return True
else:
return False
def isogram(word, count = 1):
##Allow user to optionally specify list of characters to isolate
for char in word[0]:
if word[0].count(char) > count:
return False
return True
def reverse_isogram(word, count = 1):
##Allow user to optionally specify list of characters to isolate
for char in word[0]:
if word[0].count(char) < count:
return False
return True
"""
Ideas
1. Longest repeating and non overlapping substring in a string
2. List of strings to consume from, like a pangram but you specify the alphabet
3. Edit Distance of strings, user supplies string and max distance and it returns strings with that distance. I think this is fast enough to calculate for this to work!
Leetcode stuff like this
Find longest palindroming substring
def countSubstrings(self, s: str) -> int:
@cache
def isPalindrome(i, j):
return i >= j or s[i] == s[j] and isPalindrome(i + 1, j - 1)
return sum(isPalindrome(i, j) for i in range(len(s)) for j in range(i, len(s)))
"""
########--------------------------------------------------------------#### ########--------------------------------------------------------------####
lipogram_naughty_word_list = []
weak_lipogram_naughty_word_list = []
reverse_lipogram_nice_word_list = []
weak_reverse_lipogram_nice_word_list = []
string_in_positon_list = []
string_in_positon_index_list = []
starts_with_string = ""
ends_with_string = ""
phonetic_matching_string = ""
semantic_matching_string = ""
semantic_distance_threshold = 0.0
string_edit_string = ""
string_edit_distnace_threhold = 0
syllable_number = 0
meter_string = ""
rhyme_string = ""
constrained_length = 0
constrained_gt_length = 0
constrained_lt_length = 0
palindrome_enabled = False
anagram_string = ""
partial_anagram_string = ""
isogram_count = 0
reverse_isogram_count = 0
def get_next_word_without_e(sequence):
all_letters_filtered_list = []
with torch.no_grad():
input_ids = tokenizer.encode(sequence, return_tensors="pt")
# get logits of last hidden state
next_token_candidates_logits = model(input_ids)[0][:, -1, :]
temperature = dpg.get_value("temperature")
if temperature != 1.0:
next_token_candidates_logits = next_token_candidates_logits / temperature
# filter
top_p = dpg.get_value("top_p")
top_k = dpg.get_value("top_k")
if (top_p > 0 and top_k > 0):
filtered_next_token_candidates_logits = top_k_top_p_filtering(next_token_candidates_logits, top_k=top_k, top_p=top_p)
elif top_p > 0:
filtered_next_token_candidates_logits = top_k_top_p_filtering(next_token_candidates_logits, top_p=top_p)
elif top_k > 0:
filtered_next_token_candidates_logits = top_k_top_p_filtering(next_token_candidates_logits, top_k=top_k)
else:
filtered_next_token_candidates_logits = next_token_candidates_logits
# sample and get a probability distribution
probs = F.softmax(filtered_next_token_candidates_logits.float(), dim=-1).sort(descending = True)
word_list = []
resulting_strings = tokenizer.batch_decode(probs[1][0])
for iter, resulting_string in enumerate(resulting_strings):
probability = probs[0][0][iter].item()
if dpg.get_value("upper_case_transform"):
resulting_string = resulting_string.upper()
if dpg.get_value("lower_case_transform"):
resulting_string = resulting_string.lower()
if dpg.get_value("replace_spaces"):
resulting_string = resulting_string.replace(' ', '')
if dpg.get_value("lstrip_transform"):
resulting_string = resulting_string.lstrip()
if dpg.get_value("rstrip_transform"):
resulting_string = resulting_string.rstrip()
if dpg.get_value("strip_transform"):
resulting_string = resulting_string.strip()
if dpg.get_value("capitalize_first_letter_transform"):
resulting_string = resulting_string.capitalize()
if dpg.get_value("alpha_numaric_transform"):
resulting_string = ''.join(ch for ch in resulting_string if ch.isalnum())
if dpg.get_value("alpha_transform"):
resulting_string = ''.join(ch for ch in resulting_string if ch.isalpha())
if dpg.get_value("digit_transform"):
resulting_string = ''.join(ch for ch in resulting_string if ch.isdigit())
if dpg.get_value("ascii_transform"):
resulting_string = ''.join(ch for ch in resulting_string if ch.isascii())
if dpg.get_value("filter_blank_outputs"):
if resulting_string == "":
continue
word_list.append((resulting_string, probability))
#all_letters_filtered_list = [word for word in word_list if all_letters_not_included(word=word, string_list = lipogram_naughty_word_list)]
all_letters_filtered_list = [
word for word in word_list
if (
(not lipogram_naughty_word_list or all_letters_not_included(word=word, string_list=lipogram_naughty_word_list)) and
(not weak_lipogram_naughty_word_list or any_letters_not_included(word=word, string_list=weak_lipogram_naughty_word_list)) and
(not reverse_lipogram_nice_word_list or all_letters_included(word=word, string_list=reverse_lipogram_nice_word_list)) and
(not weak_reverse_lipogram_nice_word_list or any_letters_included(word=word, string_list=weak_reverse_lipogram_nice_word_list)) and
(not string_in_positon_list or string_in_position(word=word, a_string_list=string_in_positon_list, position_index_list=string_in_positon_index_list)) and
(not starts_with_string or starts_with(word=word, starting_string=starts_with_string)) and
(not ends_with_string or ends_with(word=word, ending_string=ends_with_string)) and
(not constrained_length or equal_to_length(word=word, word_length=constrained_length)) and
(not constrained_gt_length or greater_than_length(word=word, word_length=constrained_gt_length)) and
(not constrained_lt_length or less_than_length(word=word, word_length=constrained_lt_length)) and
(not palindrome_enabled or palindrome(word=word)) and
(not phonetic_matching_string or phonetic_matching(word=word, phonetic_matching_string=phonetic_matching_string)) and
(not semantic_matching_string or semantic_matching(word=word, semantic_string=semantic_matching_string, semantic_threshold=semantic_distance_threshold)) and
(not anagram_string or full_anagram(word=word, a_string=anagram_string)) and
(not partial_anagram_string or partial_anagram(word=word, a_string=partial_anagram_string)) and
(not rhyme_string or rhyme(word=word, rhyme_string=rhyme_string)) and
(not meter_string or meter(word=word, meter_string=meter_string)) and
(not string_edit_string or string_edit(word=word, string_edit_string=string_edit_string, string_edit_threshold=string_edit_distnace_threhold)) and
(not syllable_number or syllable(word=word, syllable_number=syllable_number)) and
(isogram_count < 1 or isogram(word=word, count=isogram_count)) and
(reverse_isogram_count < 1 or reverse_isogram(word=word, count=reverse_isogram_count))
)
]
#all_letters_filtered_list = [word for word in word_list if all_letters_not_included(word=word, starting_string= "EN")]
#list(filter(all_letters_included, word_list))
#print(probs)
#print(all_letters_filtered_list[0:50])
#print(probs)
return all_letters_filtered_list
def tab_key_generate_tokens_callback():
string_input = dpg.get_value("input_string")
generated_output = get_next_word_without_e(string_input)
if dpg.get_value("greedy_decoding"):
returned_word = generated_output[0][0]
else:
probability_weights = tuple(zip(*generated_output))[1]
returned_word = random.choices(generated_output, weights = probability_weights, k = 1)[0][0]
string_input += returned_word
dpg.set_value("input_string", string_input)
def generate_tokens_callback():
number_of_tokens = dpg.get_value("num_tokens_to_generate")
greedy_decoding = dpg.get_value("greedy_decoding")
string_input = dpg.get_value("input_string")
for _ in range(number_of_tokens):
generated_output = get_next_word_without_e(string_input)
if greedy_decoding:
returned_word = generated_output[0][0]
else:
words, probabilities = zip(*generated_output)
probabilities_normalized = np.array(probabilities) / np.sum(probabilities)
returned_word = np.random.choice(words, p=probabilities_normalized)
string_input += returned_word
dpg.set_value("input_string", string_input)
def add_generated_word_callback(sender, app_data, user_data):
current_value = dpg.get_value("input_string")
new_string = current_value + str(user_data)
dpg.set_value("input_string", new_string)
edit_string_callback()
def edit_string_callback():
string_input = dpg.get_value("input_string")
returned_words = get_next_word_without_e(string_input)
#print(returned_words)
with dpg.popup(parent = "input_string"):
dpg.add_text("Options")
dpg.add_separator()
if len(returned_words) >= 1:
for word in returned_words:
dpg.add_selectable(label=word, user_data = word[0], callback = add_generated_word_callback)
else:
dpg.add_text("No results with the current filters")
#print(dpg.get_value(word))
#print(dpg.get_value("yum"))
#dpg.log_debug(value)
def typed_calledback(sender, app_data, user_data):
dpg.set_value("pretty_input_string", str(app_data))
def lipogram_callback(sender, app_data, user_data):
if app_data == True:
dpg.show_item("Lipogram Options")
else:
dpg.hide_item("Lipogram Options")
def load_naughty_strings_callback():
global lipogram_naughty_word_list
string_input = dpg.get_value("lipogram_word_list")
lipogram_naughty_word_list = string_input.split(" ")
if not dpg.get_value("naughty_applied"):
dpg.add_text(tag = "naughty_applied", default_value= "Naughty Strings Applied!", parent = "Lipogram Options")
dpg.add_text(tag = "naughty_filter" , default_value = "Naughty Strings Filter: " + string_input, parent = "main_window", before = "lipogram")
else:
dpg.set_value(item = "naughty_filter", value = "Naughty Strings Filter: " + string_input)
edit_string_callback()
def weak_lipogram_callback(sender, app_data, user_data):
if app_data == True:
dpg.show_item("Weak Lipogram Options")
else:
dpg.hide_item("Weak Lipogram Options")
def load_weak_naughty_strings_callback():
global weak_lipogram_naughty_word_list
string_input = dpg.get_value("weak_lipogram_word_list")
weak_lipogram_naughty_word_list = string_input.split(" ")
if not dpg.get_value("weak_naughty_applied"):
dpg.add_text(tag = "weak_naughty_applied", default_value= "Weak Naughty Strings Applied!", parent = "Weak Lipogram Options")
dpg.add_text(tag = "weak_naughty_filter" , default_value = "Weak Naughty Strings Filter: " + string_input, parent = "main_window", before = "lipogram")
else:
dpg.set_value(item = "weak_naughty_filter", value = "Weak Naughty Strings Filter: " + string_input)
edit_string_callback()
def reverse_lipogram_callback(sender, app_data, user_data):
if app_data == True:
dpg.show_item("Reverse Lipogram Options")
else:
dpg.hide_item("Reverse Lipogram Options")
def load_reverse_naughty_strings_callback():
global reverse_lipogram_nice_word_list
string_input = dpg.get_value("reverse_lipogram_word_list")
reverse_lipogram_nice_word_list = string_input.split(" ")
if not dpg.get_value("reverse_nice_applied"):
dpg.add_text(tag = "reverse_nice_applied", default_value= "Nice Strings Applied!", parent = "Reverse Lipogram Options")
dpg.add_text(tag = "reverse_nice_filter" , default_value = "Nice Strings Filter: " + string_input, parent = "main_window", before = "lipogram")
else:
dpg.set_value(item = "reverse_nice_filter", value = "Nice Strings Filter: " + string_input)
edit_string_callback()
def weak_reverse_lipogram_callback(sender, app_data, user_data):
if app_data == True:
dpg.show_item("Weak Reverse Lipogram Options")
else:
dpg.hide_item("Weak Reverse Lipogram Options")
def load_weak_reverse_naughty_strings_callback():
global weak_reverse_lipogram_nice_word_list
string_input = dpg.get_value("weak_reverse_lipogram_word_list")
weak_reverse_lipogram_nice_word_list = string_input.split(" ")
if not dpg.get_value("weak_reverse_nice_applied"):
dpg.add_text(tag = "weak_reverse_nice_applied", default_value= "Weak Nice Strings Applied!", parent = "Weak Reverse Lipogram Options")
dpg.add_text(tag = "weak_reverse_nice_filter" , default_value = "Weak Nice Strings Filter: " + string_input, parent = "main_window", before = "lipogram")
else:
dpg.set_value(item = "weak_reverse_nice_filter", value = "Weak Nice Strings Filter: " + string_input)
edit_string_callback()
def string_position_callback(sender, app_data, user_data):
if app_data == True:
dpg.show_item("Letter Position Options")
else:
dpg.hide_item("Letter Position Options")
def load_string_positon_callback():
global string_in_positon_list
global string_in_positon_index_list
string_input = dpg.get_value("string_for_position")
int_input = dpg.get_value("string_position_int")
string_in_positon_list = string_input.split(" ")
string_in_positon_index_list = int_input.split(" ")
string_in_positon_index_list = [int(i) for i in string_in_positon_index_list]
if not dpg.get_value("string_postion_applied"):
dpg.add_text(tag = "string_postion_applied", default_value= "Strings in Position Applied!", parent = "Letter Position Options")
dpg.add_text(tag = "string_postion_filter" , default_value = "Strings in Position Applied: " + string_input + " " + str(int_input), parent = "main_window", before = "lipogram")
else:
dpg.set_value(item = "string_postion_filter", value = "Strings in Position Filter: " + string_input + " " + str(int_input))
edit_string_callback()
def string_starts_with_callback(sender, app_data, user_data):
if app_data == True:
dpg.show_item("Starting String Options")
else:
dpg.hide_item("Starting String Options")
def load_string_starts_with_callback():
global starts_with_string
starts_with_string = dpg.get_value("string_start_word")
if not dpg.get_value("string_starts_with_applied"):
dpg.add_text(tag = "string_starts_with_applied", default_value= "Starting String Applied!", parent = "Starting String Options")
dpg.add_text(tag = "string_starts_with_filter" , default_value = "Starting String Applied! " + starts_with_string, parent = "main_window", before = "lipogram")
else:
dpg.set_value(item = "string_starts_with_filter", value = "Starting String Filter: " + starts_with_string)
edit_string_callback()
def string_ends_with_callback(sender, app_data, user_data):
if app_data == True:
dpg.show_item("Ending String Options")
else:
dpg.hide_item("Ending String Options")
def load_string_ends_with_callback():
global ends_with_string
ends_with_string = dpg.get_value("string_end_word")
if not dpg.get_value("string_ends_with_applied"):
dpg.add_text(tag = "string_ends_with_applied", default_value= "Ending String Applied!", parent = "Ending String Options")
dpg.add_text(tag = "string_ends_with_filter" , default_value = "Ending String Applied! " + ends_with_string, parent = "main_window", before = "lipogram")
else:
dpg.set_value(item = "string_ends_with_filter", value = "Ending String Filter: " + ends_with_string)
edit_string_callback()
def string_length_constrained_callback(sender, app_data, user_data):
if app_data == True:
dpg.show_item("Length Constrained Options")
else:
dpg.hide_item("Length Constrained Options")
def load_string_length_constrained_callback():
global constrained_length
constrained_length = dpg.get_value("length_constrained_number")
constrained_length_str = str(constrained_length)
if not dpg.get_value("string_length_constrained_applied"):
dpg.add_text(tag = "string_length_constrained_applied", default_value= "String Length Constraint Applied!", parent = "Length Constrained Options")
dpg.add_text(tag = "string_length_constrained_filter" , default_value = "String Length Constraint Applied! " + constrained_length_str, parent = "main_window", before = "lipogram")
else:
dpg.set_value(item = "string_length_constrained_filter", value = "String Length Constraint Filter: " + constrained_length_str)
edit_string_callback()
def string_length_gt_constrained_callback(sender, app_data, user_data):
if app_data == True:
dpg.show_item("Length Greater Than Options")
else:
dpg.hide_item("Length Greater Than Options")
def load_string_length_gt_constrained_callback():
global constrained_gt_length
constrained_gt_length = dpg.get_value("length_gt_constrained_number")
constrained_gt_length_str = str(constrained_gt_length)
if not dpg.get_value("string_length_gt_constrained_applied"):
dpg.add_text(tag = "string_length_gt_constrained_applied", default_value= "String Length Greater Than Constraint Applied!", parent = "Length Greater Than Options")
dpg.add_text(tag = "string_length_gt_constrained_filter" , default_value = "String Length Greater Than Constraint Applied! " + constrained_gt_length_str, parent = "main_window", before = "lipogram")
else:
dpg.set_value(item = "string_length_gt_constrained_filter", value = "String Length Greater Than Constraint Filter: " + constrained_gt_length_str)
edit_string_callback()
def string_length_lt_constrained_callback(sender, app_data, user_data):
if app_data == True:
dpg.show_item("Length Lesser Than Options")
else:
dpg.hide_item("Length Lesser Than Options")
def load_string_length_lt_constrained_callback():
global constrained_lt_length
constrained_lt_length = dpg.get_value("length_lt_constrained_number")
constrained_lt_length_str = str(constrained_lt_length)
if not dpg.get_value("string_length_lt_constrained_applied"):
dpg.add_text(tag = "string_length_lt_constrained_applied", default_value= "String Length Lesser Than Constraint Applied!", parent = "Length Lesser Than Options")
dpg.add_text(tag = "string_length_lt_constrained_filter" , default_value = "String Length Lesser Than Constraint Applied! " + constrained_lt_length_str, parent = "main_window", before = "lipogram")
else:
dpg.set_value(item = "string_length_lt_constrained_filter", value = "String Length Lesser Than Constraint Filter: " + constrained_lt_length_str)
edit_string_callback()
def phonetic_callback(sender, app_data, user_data):
if app_data == True:
dpg.show_item("Phonetic Options")
else:
dpg.hide_item("Phonetic Options")
def load_phonetic_callback():
global phonetic_matching_string
phonetic_matching_string = dpg.get_value("phonetic_word")
if not dpg.get_value("phonetic_applied"):
dpg.add_text(tag = "phonetic_applied", default_value= "Phonetic Constraint Applied!", parent = "Phonetic Options")
dpg.add_text(tag = "phonetic_filter" , default_value = "Phonetic Constraint Applied! " + phonetic_matching_string, parent = "main_window", before = "lipogram")
else:
dpg.set_value(item = "phonetic_filter", value = "Phonetic Constraint Filter: " + phonetic_matching_string)
edit_string_callback()
def semantic_callback(sender, app_data, user_data):
if app_data == True:
dpg.show_item("Semantic Options")
else:
dpg.hide_item("Semantic Options")
def load_semantic_callback():
global semantic_matching_string
global semantic_distance_threshold
semantic_matching_string = dpg.get_value("semantic_word")
semantic_distance_threshold = dpg.get_value("semantic_distance")
semantic_distance_threshold_string = str(semantic_distance_threshold)
if not dpg.get_value("semantic_applied"):
dpg.add_text(tag = "semantic_applied", default_value= "Semantic Constraint Applied!", parent = "Semantic Options")
dpg.add_text(tag = "semantic_filter" , default_value = "Semantic Constraint Applied! " + semantic_matching_string + " Minimum Similarity: " + semantic_distance_threshold_string, parent = "main_window", before = "lipogram")
else:
dpg.set_value(item = "semantic_filter", value = "Semantic Constraint Filter: " + semantic_matching_string)
edit_string_callback()
def string_edit_callback(sender, app_data, user_data):
if app_data == True:
dpg.show_item("String Edit Options")
else:
dpg.hide_item("String Edit Options")
def load_string_edit_callback():
global string_edit_string
global string_edit_distnace_threhold
string_edit_string = dpg.get_value("string_edit_word")
string_edit_distnace_threhold = dpg.get_value("string_edit_distance")
string_edit_distnace_threhold_string = str(string_edit_distnace_threhold)
if not dpg.get_value("string_edit_applied"):
dpg.add_text(tag = "string_edit_applied", default_value= "String Edit Constraint Applied!", parent = "String Edit Options")
dpg.add_text(tag = "string_edit_filter" , default_value = "String Constraint Applied! " + string_edit_string + " Minimum Similarity: " + string_edit_distnace_threhold_string, parent = "main_window", before = "lipogram")
else:
dpg.set_value(item = "string_edit_filter", value = "String Constraint Filter: " + string_edit_string)
edit_string_callback()
def syllable_callback(sender, app_data, user_data):
if app_data == True:
dpg.show_item("Syllable Options")
else:
dpg.hide_item("Syllable Options")
def load_syllable_callback():
global syllable_number
syllable_number = dpg.get_value("syllable_number")
syllable_number_string = str(syllable_number)
if not dpg.get_value("syllable_applied"):
dpg.add_text(tag = "syllable_applied", default_value= "Syllable Constraint Applied!", parent = "Syllable Options")
dpg.add_text(tag = "syllable_filter" , default_value = "Syllable Constraint Applied! " + syllable_number_string, parent = "main_window", before = "lipogram")
else:
dpg.set_value(item = "syllable_filter", value = "Syllable Constraint Filter: " + syllable_number_string)
edit_string_callback()
def meter_callback(sender, app_data, user_data):
if app_data == True:
dpg.show_item("Meter Options")
else:
dpg.hide_item("Meter Options")
def load_meter_callback():
global meter_string
meter_string = dpg.get_value("meter_word")
if not dpg.get_value("meter_applied"):
dpg.add_text(tag = "meter_applied", default_value= "Meter Constraint Applied!", parent = "Meter Options")
dpg.add_text(tag = "meter_filter" , default_value = "Meter Constraint Applied! " + meter_string, parent = "main_window", before = "lipogram")
else:
dpg.set_value(item = "meter_filter", value = "Meter Constraint Filter: " + meter_string)
edit_string_callback()
def rhyme_callback(sender, app_data, user_data):
if app_data == True:
dpg.show_item("Rhyme Options")
else:
dpg.hide_item("Rhyme Options")
def load_rhyme_callback():
global rhyme_string
rhyme_string = dpg.get_value("rhyme_word")
if not dpg.get_value("rhyme_applied"):
dpg.add_text(tag = "rhyme_applied", default_value= "Rhyme Constraint Applied!", parent = "Rhyme Options")
dpg.add_text(tag = "rhyme_filter" , default_value = "Rhyme Constraint Applied! " + rhyme_string, parent = "main_window", before = "lipogram")
else:
dpg.set_value(item = "rhyme_filter", value = "Rhyme Constraint Filter: " + rhyme_string)
edit_string_callback()
def palindrome_callback(sender, app_data, user_data):
global palindrome_enabled
if app_data == True:
dpg.show_item("Palindrome Options")
palindrome_enabled = True
else:
dpg.hide_item("Palindrome Options")
palindrome_enabled = False
def load_palindrome_callback():
global palindrome_enabled
if not dpg.get_value("palindrome_applied"):
dpg.add_text(tag = "palindrome_applied", default_value= "String Palindromeic Constraint Applied!", parent = "Palindrome Options")
dpg.add_text(tag = "palindrome_filter" , default_value = "String Palindromeic Constraint Applied! ", parent = "main_window", before = "lipogram")
else:
dpg.set_value(item = "palindrome_filter", value = "String Palindromeic Constraint Applied! ")
edit_string_callback()
def anagram_callback(sender, app_data, user_data):
if app_data == True:
dpg.show_item("Anagram Options")
else:
dpg.hide_item("Anagram Options")
def load_anagram_callback():
global anagram_string
anagram_string = dpg.get_value("anagram_string")
if not dpg.get_value("anagram_applied"):
dpg.add_text(tag = "anagram_applied", default_value= "Anagram Applied!", parent = "Anagram Options")
dpg.add_text(tag = "anagram_filter" , default_value = "Anagram Applied! " + anagram_string, parent = "main_window", before = "lipogram")
else:
dpg.set_value(item = "anagram_filter", value = "Anagram Filter: " + anagram_string)
edit_string_callback()
def partial_anagram_callback(sender, app_data, user_data):
if app_data == True:
dpg.show_item("Partial Anagram Options")
else:
dpg.hide_item("Partial Anagram Options")
def load_partial_anagram_callback():
global partial_anagram_string
partial_anagram_string = dpg.get_value("partial_anagram_string")
if not dpg.get_value("partial_anagram_applied"):
dpg.add_text(tag = "partial_anagram_applied", default_value= "Partial Anagram Applied!", parent = "Partial Anagram Options")
dpg.add_text(tag = "partial_anagram_filter" , default_value = "Partial Anagram Applied! " + partial_anagram_string, parent = "main_window", before = "lipogram")
else:
dpg.set_value(item = "partial_anagram_filter", value = "Partial Anagram Filter: " + partial_anagram_string)
edit_string_callback()
def isogram_callback(sender, app_data, user_data):
if app_data == True:
dpg.show_item("Isogram Options")
else:
dpg.hide_item("Isogram Options")
def load_isogram_callback():
global isogram_count
isogram_count = dpg.get_value("isogram_number")
isogram_count_str = str(isogram_count)
if not dpg.get_value("isogram_applied"):
dpg.add_text(tag = "isogram_applied", default_value= "Isogram Applied!", parent = "Isogram Options")
dpg.add_text(tag = "isogram_filter" , default_value = "Isogram Applied! " + isogram_count_str, parent = "main_window", before = "lipogram")
else:
dpg.set_value(item = "isogram_filter", value = "Isogram Filter: " + isogram_count_str)
edit_string_callback()
def reverse_isogram_callback(sender, app_data, user_data):
if app_data == True:
dpg.show_item("Reverse Isogram Options")
else:
dpg.hide_item("Reverse Isogram Options")
def load_reverse_isogram_callback():
global reverse_isogram_count
reverse_isogram_count = dpg.get_value("reverse_isogram_number")
reverse_isogram_count_str = str(reverse_isogram_count)
if not dpg.get_value("reverse_isogram_applied"):
dpg.add_text(tag = "reverse_isogram_applied", default_value= "Reverse Isogram Applied!", parent = "Reverse Isogram Options")
dpg.add_text(tag = "reverse_isogram_filter" , default_value = "Reverse Isogram Applied! " + reverse_isogram_count_str, parent = "main_window", before = "lipogram")
else:
dpg.set_value(item = "reverse_isogram_filter", value = "Reverse Isogram Filter: " + reverse_isogram_count_str)
edit_string_callback()
def turn_filters_off_callback():
global lipogram_naughty_word_list
global weak_lipogram_naughty_word_list
global reverse_lipogram_nice_word_list
global weak_reverse_lipogram_nice_word_list
global string_in_positon_list
global string_in_positon_index_list
global starts_with_string
global ends_with_string
global phonetic_matching_string
global semantic_matching_string
global semantic_distance_threshold
global constrained_length
global constrained_gt_length
global constrained_lt_length
global palindrome_enabled
global anagram_string
global partial_anagram_string
global isogram_count
global reverse_isogram_count
global string_edit_string
global string_edit_distnace_threhold
global syllable_number
global meter_string
global rhyme_string
lipogram_naughty_word_list = []
weak_lipogram_naughty_word_list = []
reverse_lipogram_nice_word_list = []
weak_reverse_lipogram_nice_word_list = []
string_in_positon_list = []
string_in_positon_index_list = []
starts_with_string = ""
ends_with_string = ""
phonetic_matching_string = ""
semantic_matching_string = ""
semantic_distance_threshold = 0.0
string_edit_string = ""
string_edit_distnace_threhold = 0
syllable_number = 0
meter_string = ""
rhyme_string = ""
constrained_length = 0
constrained_gt_length = 0
constrained_lt_length = 0
palindrome_enabled = False
anagram_string = ""
partial_anagram_string = ""
isogram_count = 0
reverse_isogram_count = 0
filters = [
"partial_anagram_applied", "partial_anagram_filter",
"naughty_applied", "naughty_filter",
"weak_naughty_applied", "weak_naughty_filter",
"reverse_nice_applied", "reverse_nice_filter",
"weak_reverse_nice_applied", "weak_reverse_nice_filter",
"string_position_applied", "string_position_filter",
"string_starts_with_applied", "string_starts_with_filter",
"string_ends_with_applied", "string_ends_with_filter",
"string_length_constrained_applied", "string_length_constrained_filter",
"string_length_gt_constrained_applied", "string_length_gt_constrained_filter",
"string_length_lt_constrained_applied", "string_length_lt_constrained_filter",
"phonetic_applied", "phonetic_filter",
"semantic_applied", "semantic_filter",
"string_edit_applied", "string_edit_filter",
"syllable_applied", "syllable_filter",
"meter_applied", "meter_filter",
"rhyme_applied", "rhyme_filter",
"palindrome_applied", "palindrome_filter",
"anagram_applied", "anagram_filter",
"isogram_applied", "isogram_filter",
"reverse_isogram_applied", "reverse_isogram_filter"]
for filter_name in filters:
if dpg.get_value(filter_name):
dpg.delete_item(filter_name)
edit_string_callback()
#TODO: Finish this
dpg.create_context()
monitor = get_monitors()[0]
screen_width = monitor.width
screen_height = monitor.height
dpg.create_viewport(width=screen_width, height=screen_height)
dpg.setup_dearpygui()
dpg.toggle_viewport_fullscreen()
#dpg.configure_app(docking=True, dock_space = True)
dpg.enable_docking(dock_space=True)
with dpg.font_registry():
# Download font here: https://fonts.google.com/specimen/Open+Sans
font = dpg.add_font("OpenSans-VariableFont_wdth,wght.ttf", 15, tag="ttf-font"
)
dpg.bind_font(font)
readme_pos = ((screen_width) // 2, (screen_height) // 2)
window_size = (screen_width // 2, screen_height // 2.5)
window_positions = [
(0, 0),
(window_size[0], window_size[1]),
(window_size[0] * 2, 0),
(window_size[0], 0),
]
"""
Doesn't work...
def edited_call(sender, app_data, user_data):
global starttime
newtime = time.time()
if newtime - starttime > 2:
print(dpg.get_item_)
print(dpg.get_item_state("input_string"))
edit_string_callback()
starttime = newtime
"""
#edit_string_callback("This is an example")
with dpg.window(tag="main_window", label="CTGS - Contrained Text Generation Studio", no_close=True, width=window_size[0], height=window_size[1], pos=window_positions[0]) as window:
dpg.add_text("Main Text Box")
dpg.add_text("Right Click within the text box for LM recommended continuations with constraints applied!")
dpg.add_input_text(tag = "input_string", width = 900, height = 500, multiline=True, default_value = "Type something here!")
with dpg.window(tag="load_model_window", label="Model Settings", no_close=True, width=window_size[0], height=window_size[1], pos=window_positions[1]) as model_window:
dpg.add_text("Enter the name of the pre-trained model from transformers that we are using for Text Generation")
_help("Make sure torch.cuda.is_available returns True to get GPU support for your models, which significantly speeds them up!")
dpg.add_text("This will download a new model, so it may take awhile or even break if the model is too large")
dpg.add_input_text(tag = "model_name", width = 500, height = 500, default_value="EleutherAI/pythia-1b", label = "Huggingface Model Name")
dpg.add_button(tag="load_model", label="load_model", callback=load_model)
_help("If ran from the commnad line, you can see the downloading progress in the terminal. Be patient, it can take awhile to download a model!")
dpg.add_combo(items=["32bit", "16bit", "8bit"], tag = "percision", label = "Select the model percision", default_value ="32bit")
with dpg.window(tag="Filter Options", label="Filters", show=True, no_close=True, width=window_size[0], height=window_size[1], pos=window_positions[2]) as filter_options:
dpg.add_text("Select which filters you want to enable")
dpg.add_text("List of enabled filters: ")
dpg.add_checkbox(tag="lipogram", label = "All Strings Banned", callback=lipogram_callback)
_help("A lipogram is when a particular letter or group of strings is avoided.")
with dpg.child_window(tag="Lipogram Options", show = False, height = 100, width = 600) as lipogram_selection_window:
dpg.add_text("Add banned letters or strings seperated by a space!")
dpg.add_input_text(tag = "lipogram_word_list", width = 500, height = 500, label = "Banned Strings")
dpg.add_button(tag="lipogram_button", label="Load Banned Strings", callback=load_naughty_strings_callback)
dpg.add_checkbox(tag="weak_lipogram", label = "Any Strings Banned", callback=weak_lipogram_callback)
_help("A weak lipogram is when at least one of a particular letter or group of strings is avoided.")
with dpg.child_window(tag="Weak Lipogram Options", show = False, height = 100, width = 600) as weak_lipogram_selection_window:
dpg.add_text("Add banned letters or strings seperated by a space!")
dpg.add_input_text(tag = "weak_lipogram_word_list", width = 500, height = 500, label = "Banned Strings")
dpg.add_button(tag="weak_lipogram_button", label="Load Banned Strings", callback = load_weak_naughty_strings_callback)
dpg.add_checkbox(tag="reverse_lipogram", label = "All Strings Required", callback=reverse_lipogram_callback)
_help("A reverse lipogram is when a particular letter or group of strings is forced.")
with dpg.child_window(tag="Reverse Lipogram Options", show = False, height = 100, width = 600) as reverse_lipogram_selection_window:
dpg.add_text("Add forced letters or strings seperated by a space!")
dpg.add_input_text(tag = "reverse_lipogram_word_list", width = 500, height = 500, label = "Forced Strings")
dpg.add_button(tag="reverse_lipogram_button", label="Load Forced Strings", callback = load_reverse_naughty_strings_callback)
dpg.add_checkbox(tag="weak_reverse_lipogram", label = "Any Strings Required", callback=weak_reverse_lipogram_callback)
_help("A weak reverse lipogram is when at least one of a particular letter or group of strings is forced.")
with dpg.child_window(tag="Weak Reverse Lipogram Options", show = False, height = 100, width = 600) as weak_reverse_selection_window:
dpg.add_text("Add forced letters or strings seperated by a space!")
dpg.add_input_text(tag = "weak_reverse_lipogram_word_list", width = 500, height = 500, label = "Forced Strings")
dpg.add_button(tag="weak_reverse_lipogram_button", label="Load Forced Strings", callback = load_weak_reverse_naughty_strings_callback)
dpg.add_checkbox(tag="string_position", label = "String In Position", callback = string_position_callback)
_help("This allows one to force a particular letter in a particular position of a string\n" "NOTE: It's recommended to combine this with whitespace stripping")
with dpg.child_window(tag="Letter Position Options", show = False, height = 130, width = 600) as letter_position_selection_window:
dpg.add_text("Add the position that you want to force a particular letter to appear at! Give a list of characters seperated by a space")
dpg.add_input_text(tag = "string_for_position", width = 500, height = 500, label = "List of characters")
dpg.add_text("Corresponding list of indexes for each character. Must be the same length as the list of characters")
dpg.add_input_text(tag = "string_position_int", width = 500, height = 500, label = "List of indexes")
dpg.add_button(tag="string_position_button", label="Load Strings", callback = load_string_positon_callback)
dpg.add_checkbox(tag="string_starts", label = "String Starts With", callback = string_starts_with_callback)
_help("This allows one to guarantee that the string will start with a particular set of letters")
with dpg.child_window(tag="Starting String Options", show = False, height = 100, width = 600) as starting_string_selection_window:
dpg.add_text("Add the string that the word should start with")
dpg.add_input_text(tag = "string_start_word", width = 500, height = 500, label = "String for word to start with")
dpg.add_button(tag="string_start_button", label="Load Starting String", callback=load_string_starts_with_callback)
dpg.add_checkbox(tag="string_ends", label = "String Ends With", callback = string_ends_with_callback)
_help("This allows one to guarantee that the string will end with a particular set of letters")
with dpg.child_window(tag="Ending String Options", show = False, height = 100, width = 600) as ending_string_selection_window:
dpg.add_text("Add the string that the word should end with")
dpg.add_input_text(tag = "string_end_word", width = 500, height = 500, label = "String for word to end with")
dpg.add_button(tag="string_end_button", label="Load Ending String", callback=load_string_ends_with_callback)
dpg.add_checkbox(tag="string_edit_distance_check", label = "String Edit Distance Matching", callback = string_edit_callback)
_help("This uses Levenshtein distance to return all strings with lower edit distance then specified")
with dpg.child_window(tag="String Edit Options", show = False, height = 100, width = 600) as string_edit_window:
dpg.add_text("Specify the word you want to string edit distance match against")
dpg.add_input_text(tag = "string_edit_word", width = 500, height = 500, label = "String to match using Levenshtein distance")
dpg.add_input_int(tag = "string_edit_distance", label = "Similarity that the word has to be higher than")
dpg.add_button(tag="string_edit_constrained_button", label="Load String Edit Distance Matching Strings", callback=load_string_edit_callback)
dpg.add_checkbox(tag="length_constrained", label = "String Length Equal To", callback = string_length_constrained_callback)
_help("This allows one to guarantee that the string will be of a particular length\n" "NOTE: It's recommended to combine this filter with whitespace stripping")
with dpg.child_window(tag="Length Constrained Options", show = False, height = 100, width = 600) as length_constrained_selection_window:
dpg.add_text("Specify the length that you want your strings to be constrained to")
dpg.add_input_int(tag = "length_constrained_number", label = "Number to constrain the length with")
dpg.add_button(tag="length_constrained_button", label="Load Length Constrained String", callback=load_string_length_constrained_callback)
dpg.add_checkbox(tag="length_gt", label = "String Length Greater Than", callback = string_length_gt_constrained_callback)
_help("This allows one to guarantee that the string will be longar than a particular length\n" "NOTE: It's recommended to combine this filter with whitespace stripping")
with dpg.child_window(tag="Length Greater Than Options", show = False, height = 100, width = 600) as length_gt_selection_window:
dpg.add_text("Specify the length that you want your strings to be greater than")
dpg.add_input_int(tag = "length_gt_constrained_number", label = "Number to constrain the length to be greater than")
dpg.add_button(tag="length_gt_constrained_button", label="Load Length Constrained String", callback=load_string_length_gt_constrained_callback)