-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse_parser.py
124 lines (107 loc) · 3.24 KB
/
response_parser.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
import re
from typing import Optional
from models import *
# REGEX LAIR!!!!
# ...
# As in like, yknow, to parse LLM Strings and such into Data Class
# Yeah, I think that's it...
# Imma go and make a universal function that captures all the [] first
# By that I mean, I'm gonna ask AI to make one for me.
#
def extract_bracketed_content(text:str):
pattern = r'\[(.*?)\]'
matches = re.findall(pattern, text, re.DOTALL)
return [match.strip() for match in matches]
# Aight, done, now I just have to format everything to use Bracket for anything relevant
# Oh The Beauty of GBNF!!!
# Okay, first, Creature Response
def extract_generated_creature(text:str)->Optional[Creature]:
extraction =extract_bracketed_content(text)
try:
name = extraction[0]
description = extraction[1]
appearance = extraction[2]
behavior = extraction[3]
creature = Creature(
name=name,
desc=description,
appearance=appearance,
behavior=behavior
)
return creature
except(IndexError):
print("Generation Failed, Try Again~")
return None
def extract_material(text:str)->Optional[Material]:
ex = extract_bracketed_content(text)
try:
name = ex[0]
desc = ex[1]
mat = Material(
name = name,
desc = desc
)
return mat
except(IndexError):
print("Generation Failed, Try Again~")
return None
# Hmmm... How do I do this?
# I don't want to separate each of these...
# Should I just do a 'join' and then Regex it?
# No... I need this... Hmm...
# Init Everything with None??? That sounds Dangerous, but might work
example_waifu_gen1 = """
Name: [Train Arachne]
Desc: [A shockingly beautiful woman wearing a train conductor uniform with the lower body
of a giant spider.]
"""
def extract_waifu_gen1(text:str)->Optional[Waifu]:
ex = extract_bracketed_content(text)
try:
name = ex[0]
desc = ex[1]
appearance = ex[2]
waifu = Waifu(
name = name,
desc = desc,
appearance=appearance
)
return waifu
except(IndexError):
print("Generation Failed, Try Again~")
return None
def extract_waifu_gen2(text:str,waifu:Waifu)->Optional[Waifu]:
ex = extract_bracketed_content(text)
try:
waifu.face = ex[0]
waifu.body = ex[1]
waifu.clothing = ex[2]
waifu.ero = ex[3]
return waifu
except(IndexError):
print("Generation Failed, Try Again~")
return None
def extract_waifu_gen3(text:str,waifu:Waifu,arch1:Archetype,arch2:Archetype)->Optional[OwnedWaifu]:
ex = extract_bracketed_content(text)
try:
owned_waifu = OwnedWaifu(
str(uuid4()),
waifu.name,
waifu.name + "-chan",
waifu.desc,
waifu.appearance,
waifu.face,
waifu.body,
waifu.clothing,
waifu.ero,
[arch1.name,arch2.name],
ex[0],
ex[1],
ex[2],
None,
None
)
return owned_waifu
except(IndexError):
print("Generation Failed, Try Again~")
return None