Spaces:
Runtime error
Runtime error
File size: 4,846 Bytes
9355b63 ce01fb6 9e68e0a b72c37c d6096c9 1cca70d d6096c9 1cca70d d6096c9 1cca70d 24f5e39 d6096c9 24f5e39 b72c37c ce01fb6 b72c37c ce01fb6 b72c37c ce01fb6 b72c37c ce01fb6 b72c37c de3778f b72c37c d6096c9 de3778f b72c37c ce01fb6 5c1ca89 |
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 |
from typing import List, Optional
from pydantic import BaseModel, Field
import json
class AttachmentStyle(BaseModel):
secured: float = 0.0
anxious_preoccupied: float = Field(0.0, alias="Anxious-Preoccupied")
dismissive_avoidant: float = Field(0.0, alias="Dismissive-Avoidant")
fearful_avoidant: float = Field(0.0, alias="Fearful-Avoidant")
self_model: int = Field(0, alias="Self")
others_model: int = Field(0, alias="Others")
anxiety: int = 0
avoidance: int = 0
explanation: str = "No explanation provided"
class BigFiveTraits(BaseModel):
extraversion: int = 0
agreeableness: int = 0
conscientiousness: int = 0
neuroticism: int = 0
openness: int = 0
explanation: str = "No explanation provided"
class PersonalityDisorder(BaseModel):
depressed: int = 0
paranoid: int = 0
schizoid_schizotypal: int = Field(0, alias="Schizoid-Schizotypal")
antisocial_psychopathic: int = Field(0, alias="Antisocial-Psychopathic")
borderline_dysregulated: int = Field(0, alias="Borderline-Dysregulated")
narcissistic: int = 0
anxious_avoidant: int = Field(0, alias="Anxious-Avoidant")
dependent_victimized: int = Field(0, alias="Dependent-Victimized")
obsessional: int = 0
explanation: str = "No explanation provided"
class SpeakerAnalysis(BaseModel):
speaker: str = "Unknown Speaker"
general_impression: str = "No general impression provided"
attachment_style: AttachmentStyle = AttachmentStyle()
big_five_traits: BigFiveTraits = BigFiveTraits()
personality_disorder: PersonalityDisorder = PersonalityDisorder()
class OutputParser:
def parse(self, text: str) -> List[SpeakerAnalysis]:
try:
data = json.loads(text)
if "speaker_analyses" in data and isinstance(data["speaker_analyses"], list):
return [self.parse_speaker_analysis(item) for item in data["speaker_analyses"]]
else:
raise ValueError("Invalid JSON structure: missing or invalid 'speaker_analyses' key")
except json.JSONDecodeError:
raise ValueError(f"Invalid JSON: {text}")
except Exception as e:
raise ValueError(f"Error parsing output: {str(e)}")
def parse_speaker_analysis(self, obj: dict) -> SpeakerAnalysis:
attachment_styles = obj.get("Attachment Styles", {})
big_five_traits = obj.get("Big Five Traits", {})
personality_disorders = obj.get("Personality Disorders", {})
return SpeakerAnalysis(
speaker=obj.get("Speaker", "Unknown Speaker"),
general_impression=obj.get("General Impression", "No general impression provided"),
attachment_style=AttachmentStyle(
secured=attachment_styles.get("Secured", 0.0),
anxious_preoccupied=attachment_styles.get("Anxious-Preoccupied", 0.0),
dismissive_avoidant=attachment_styles.get("Dismissive-Avoidant", 0.0),
fearful_avoidant=attachment_styles.get("Fearful-Avoidant", 0.0),
self_model=attachment_styles.get("Self", 0),
others_model=attachment_styles.get("Others", 0),
anxiety=attachment_styles.get("Anxiety", 0),
avoidance=attachment_styles.get("Avoidance", 0),
explanation=attachment_styles.get("Explanation", "No explanation provided")
),
big_five_traits=BigFiveTraits(
extraversion=big_five_traits.get("Extraversion", 0),
agreeableness=big_five_traits.get("Agreeableness", 0),
conscientiousness=big_five_traits.get("Conscientiousness", 0),
neuroticism=big_five_traits.get("Neuroticism", 0),
openness=big_five_traits.get("Openness", 0),
explanation=big_five_traits.get("Explanation", "No explanation provided")
),
personality_disorder=PersonalityDisorder(
depressed=personality_disorders.get("Depressed", 0),
paranoid=personality_disorders.get("Paranoid", 0),
schizoid_schizotypal=personality_disorders.get("Schizoid-Schizotypal", 0),
antisocial_psychopathic=personality_disorders.get("Antisocial-Psychopathic", 0),
borderline_dysregulated=personality_disorders.get("Borderline-Dysregulated", 0),
narcissistic=personality_disorders.get("Narcissistic", 0),
anxious_avoidant=personality_disorders.get("Anxious-Avoidant", 0),
dependent_victimized=personality_disorders.get("Dependent-Victimized", 0),
obsessional=personality_disorders.get("Obsessional", 0),
explanation=personality_disorders.get("Explanation", "No explanation provided")
)
)
output_parser = OutputParser()
|