Spaces:
Runtime error
Runtime error
Update output_parser.py
Browse files- output_parser.py +151 -27
output_parser.py
CHANGED
@@ -1,40 +1,164 @@
|
|
1 |
-
|
2 |
-
|
|
|
|
|
|
|
3 |
|
4 |
class AttachmentStyle(BaseModel):
|
5 |
speaker: str
|
6 |
-
secured: float
|
7 |
-
anxious_preoccupied: float
|
8 |
-
dismissive_avoidant: float
|
9 |
-
fearful_avoidant: float
|
10 |
-
self_rating: int
|
11 |
-
others_rating: int
|
12 |
-
anxiety: int
|
13 |
-
avoidance: int
|
14 |
explanation: str
|
15 |
|
16 |
class BigFiveTraits(BaseModel):
|
17 |
speaker: str
|
18 |
-
extraversion: int
|
19 |
-
agreeableness: int
|
20 |
-
conscientiousness: int
|
21 |
-
neuroticism: int
|
22 |
-
openness: int
|
23 |
explanation: str
|
24 |
|
25 |
class PersonalityDisorder(BaseModel):
|
26 |
speaker: str
|
27 |
-
depressed: int
|
28 |
-
paranoid: int
|
29 |
-
schizoid_schizotypal: int
|
30 |
-
antisocial_psychopathic: int
|
31 |
-
borderline_dysregulated: int
|
32 |
-
narcissistic: int
|
33 |
-
anxious_avoidant: int
|
34 |
-
dependent_victimized: int
|
35 |
-
obsessional: int
|
36 |
explanation: str
|
37 |
|
38 |
-
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# output_parser.py
|
2 |
+
|
3 |
+
from typing import List
|
4 |
+
from pydantic import BaseModel, Field
|
5 |
+
import json
|
6 |
|
7 |
class AttachmentStyle(BaseModel):
|
8 |
speaker: str
|
9 |
+
secured: float = Field(..., ge=0, le=1)
|
10 |
+
anxious_preoccupied: float = Field(..., ge=0, le=1)
|
11 |
+
dismissive_avoidant: float = Field(..., ge=0, le=1)
|
12 |
+
fearful_avoidant: float = Field(..., ge=0, le=1)
|
13 |
+
self_rating: int = Field(..., ge=0, le=10)
|
14 |
+
others_rating: int = Field(..., ge=0, le=10)
|
15 |
+
anxiety: int = Field(..., ge=0, le=10)
|
16 |
+
avoidance: int = Field(..., ge=0, le=10)
|
17 |
explanation: str
|
18 |
|
19 |
class BigFiveTraits(BaseModel):
|
20 |
speaker: str
|
21 |
+
extraversion: int = Field(..., ge=-10, le=10)
|
22 |
+
agreeableness: int = Field(..., ge=-10, le=10)
|
23 |
+
conscientiousness: int = Field(..., ge=-10, le=10)
|
24 |
+
neuroticism: int = Field(..., ge=-10, le=10)
|
25 |
+
openness: int = Field(..., ge=-10, le=10)
|
26 |
explanation: str
|
27 |
|
28 |
class PersonalityDisorder(BaseModel):
|
29 |
speaker: str
|
30 |
+
depressed: int = Field(..., ge=0, le=5)
|
31 |
+
paranoid: int = Field(..., ge=0, le=5)
|
32 |
+
schizoid_schizotypal: int = Field(..., ge=0, le=5)
|
33 |
+
antisocial_psychopathic: int = Field(..., ge=0, le=5)
|
34 |
+
borderline_dysregulated: int = Field(..., ge=0, le=5)
|
35 |
+
narcissistic: int = Field(..., ge=0, le=5)
|
36 |
+
anxious_avoidant: int = Field(..., ge=0, le=5)
|
37 |
+
dependent_victimized: int = Field(..., ge=0, le=5)
|
38 |
+
obsessional: int = Field(..., ge=0, le=5)
|
39 |
explanation: str
|
40 |
|
41 |
+
class AttachmentStyleOutputParser:
|
42 |
+
def parse(self, text: str) -> List[AttachmentStyle]:
|
43 |
+
try:
|
44 |
+
data = json.loads(text)
|
45 |
+
if isinstance(data, list):
|
46 |
+
return [AttachmentStyle(**item) for item in data]
|
47 |
+
elif isinstance(data, dict):
|
48 |
+
return [AttachmentStyle(**data)]
|
49 |
+
else:
|
50 |
+
raise ValueError("Invalid JSON structure")
|
51 |
+
except json.JSONDecodeError:
|
52 |
+
raise ValueError(f"Invalid JSON: {text}")
|
53 |
+
except Exception as e:
|
54 |
+
raise ValueError(f"Error parsing AttachmentStyle: {str(e)}")
|
55 |
+
|
56 |
+
def parse_object(self, obj: dict) -> AttachmentStyle:
|
57 |
+
return AttachmentStyle(**obj)
|
58 |
+
|
59 |
+
def get_format_instructions(self) -> str:
|
60 |
+
return """
|
61 |
+
The output should be a JSON list of objects, where each object represents a speaker and has the following format:
|
62 |
+
[
|
63 |
+
{
|
64 |
+
"speaker": "Speaker 1",
|
65 |
+
"secured": 0.5,
|
66 |
+
"anxious_preoccupied": 0.2,
|
67 |
+
"dismissive_avoidant": 0.1,
|
68 |
+
"fearful_avoidant": 0.2,
|
69 |
+
"self_rating": 7,
|
70 |
+
"others_rating": 6,
|
71 |
+
"anxiety": 3,
|
72 |
+
"avoidance": 4,
|
73 |
+
"explanation": "Explanation of the attachment style for Speaker 1"
|
74 |
+
},
|
75 |
+
{
|
76 |
+
"speaker": "Speaker 2",
|
77 |
+
...
|
78 |
+
}
|
79 |
+
]
|
80 |
+
"""
|
81 |
+
|
82 |
+
class BigFiveTraitsOutputParser:
|
83 |
+
def parse(self, text: str) -> List[BigFiveTraits]:
|
84 |
+
try:
|
85 |
+
data = json.loads(text)
|
86 |
+
if isinstance(data, list):
|
87 |
+
return [BigFiveTraits(**item) for item in data]
|
88 |
+
elif isinstance(data, dict):
|
89 |
+
return [BigFiveTraits(**data)]
|
90 |
+
else:
|
91 |
+
raise ValueError("Invalid JSON structure")
|
92 |
+
except json.JSONDecodeError:
|
93 |
+
raise ValueError(f"Invalid JSON: {text}")
|
94 |
+
except Exception as e:
|
95 |
+
raise ValueError(f"Error parsing BigFiveTraits: {str(e)}")
|
96 |
+
|
97 |
+
def parse_object(self, obj: dict) -> BigFiveTraits:
|
98 |
+
return BigFiveTraits(**obj)
|
99 |
+
|
100 |
+
def get_format_instructions(self) -> str:
|
101 |
+
return """
|
102 |
+
The output should be a JSON list of objects, where each object represents a speaker and has the following format:
|
103 |
+
[
|
104 |
+
{
|
105 |
+
"speaker": "Speaker 1",
|
106 |
+
"extraversion": 5,
|
107 |
+
"agreeableness": 4,
|
108 |
+
"conscientiousness": 7,
|
109 |
+
"neuroticism": 3,
|
110 |
+
"openness": 6,
|
111 |
+
"explanation": "Explanation of the Big Five traits for Speaker 1"
|
112 |
+
},
|
113 |
+
{
|
114 |
+
"speaker": "Speaker 2",
|
115 |
+
...
|
116 |
+
}
|
117 |
+
]
|
118 |
+
"""
|
119 |
+
|
120 |
+
class PersonalityDisorderOutputParser:
|
121 |
+
def parse(self, text: str) -> List[PersonalityDisorder]:
|
122 |
+
try:
|
123 |
+
data = json.loads(text)
|
124 |
+
if isinstance(data, list):
|
125 |
+
return [PersonalityDisorder(**item) for item in data]
|
126 |
+
elif isinstance(data, dict):
|
127 |
+
return [PersonalityDisorder(**data)]
|
128 |
+
else:
|
129 |
+
raise ValueError("Invalid JSON structure")
|
130 |
+
except json.JSONDecodeError:
|
131 |
+
raise ValueError(f"Invalid JSON: {text}")
|
132 |
+
except Exception as e:
|
133 |
+
raise ValueError(f"Error parsing PersonalityDisorder: {str(e)}")
|
134 |
+
|
135 |
+
def parse_object(self, obj: dict) -> PersonalityDisorder:
|
136 |
+
return PersonalityDisorder(**obj)
|
137 |
+
|
138 |
+
def get_format_instructions(self) -> str:
|
139 |
+
return """
|
140 |
+
The output should be a JSON list of objects, where each object represents a speaker and has the following format:
|
141 |
+
[
|
142 |
+
{
|
143 |
+
"speaker": "Speaker 1",
|
144 |
+
"depressed": 1,
|
145 |
+
"paranoid": 0,
|
146 |
+
"schizoid_schizotypal": 0,
|
147 |
+
"antisocial_psychopathic": 0,
|
148 |
+
"borderline_dysregulated": 0,
|
149 |
+
"narcissistic": 2,
|
150 |
+
"anxious_avoidant": 3,
|
151 |
+
"dependent_victimized": 1,
|
152 |
+
"obsessional": 2,
|
153 |
+
"explanation": "Explanation of the personality disorders for Speaker 1"
|
154 |
+
},
|
155 |
+
{
|
156 |
+
"speaker": "Speaker 2",
|
157 |
+
...
|
158 |
+
}
|
159 |
+
]
|
160 |
+
"""
|
161 |
+
|
162 |
+
attachment_parser = AttachmentStyleOutputParser()
|
163 |
+
bigfive_parser = BigFiveTraitsOutputParser()
|
164 |
+
personality_parser = PersonalityDisorderOutputParser()
|