Spaces:
Sleeping
Sleeping
Commit ·
9c64f70
1
Parent(s): 39bea23
v2 initial
Browse files- agents/analysis_phase/Introduction.py +12 -8
- routers/introduction_route.py +3 -7
- schemas/__init__.py +1 -5
- schemas/introduction_schema.py +3 -43
agents/analysis_phase/Introduction.py
CHANGED
|
@@ -97,15 +97,19 @@ intro_task = Task(
|
|
| 97 |
"• الإفراط في الجمل الطويلة.\n"
|
| 98 |
"• العبارات التسويقية.\n"
|
| 99 |
"• الكلمات اللامعة الخاوية.\n\n"
|
| 100 |
-
"## OUTPUT\n"
|
| 101 |
-
"Return JSON:\n"
|
| 102 |
-
"{\n"
|
| 103 |
-
' "
|
| 104 |
-
"}\n"
|
| 105 |
),
|
| 106 |
-
expected_output="A JSON object in Arabic containing introduction
|
| 107 |
-
|
| 108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
agent=intro_agent,
|
| 110 |
human_input=False,
|
| 111 |
)
|
|
|
|
| 97 |
"• الإفراط في الجمل الطويلة.\n"
|
| 98 |
"• العبارات التسويقية.\n"
|
| 99 |
"• الكلمات اللامعة الخاوية.\n\n"
|
| 100 |
+
# "## OUTPUT\n"
|
| 101 |
+
# "Return JSON:\n"
|
| 102 |
+
# "{\n"
|
| 103 |
+
# ' "introduction": "النص العربي الكامل"\n'
|
| 104 |
+
# "}\n"
|
| 105 |
),
|
| 106 |
+
expected_output="""A JSON object in Arabic containing introduction with this structure:
|
| 107 |
+
{
|
| 108 |
+
"introduction": "engaging introduction to the training program content, max 300 words."
|
| 109 |
+
}
|
| 110 |
+
""",
|
| 111 |
+
output_json=IntroductionOutput,
|
| 112 |
+
# output_parser=parsed_introduction,
|
| 113 |
agent=intro_agent,
|
| 114 |
human_input=False,
|
| 115 |
)
|
routers/introduction_route.py
CHANGED
|
@@ -15,8 +15,6 @@ from schemas import (
|
|
| 15 |
LearningOutcomesOutput,
|
| 16 |
DNAMetadata,
|
| 17 |
OutlineInput,
|
| 18 |
-
SafePydanticParser,
|
| 19 |
-
IntroductionOutput
|
| 20 |
)
|
| 21 |
|
| 22 |
|
|
@@ -32,7 +30,7 @@ def run_training(
|
|
| 32 |
data: OutlineInput,
|
| 33 |
outlines: ValidatedCurriculumOutput,
|
| 34 |
outcomes: LearningOutcomesOutput,
|
| 35 |
-
units:
|
| 36 |
):
|
| 37 |
training_crew = Crew(
|
| 38 |
agents=[intro_agent],
|
|
@@ -55,9 +53,7 @@ def run_training(
|
|
| 55 |
"outcomes": outcomes.dict(),
|
| 56 |
"units": units,
|
| 57 |
}
|
| 58 |
-
parser = SafePydanticParser(IntroductionOutput)
|
| 59 |
result = training_crew.kickoff(inputs=merged_inputs)
|
| 60 |
-
|
| 61 |
-
print(structured.json_dict)
|
| 62 |
|
| 63 |
-
return {"message": "Introduction Generated Well 🚀", "result":
|
|
|
|
| 15 |
LearningOutcomesOutput,
|
| 16 |
DNAMetadata,
|
| 17 |
OutlineInput,
|
|
|
|
|
|
|
| 18 |
)
|
| 19 |
|
| 20 |
|
|
|
|
| 30 |
data: OutlineInput,
|
| 31 |
outlines: ValidatedCurriculumOutput,
|
| 32 |
outcomes: LearningOutcomesOutput,
|
| 33 |
+
units:dict,
|
| 34 |
):
|
| 35 |
training_crew = Crew(
|
| 36 |
agents=[intro_agent],
|
|
|
|
| 53 |
"outcomes": outcomes.dict(),
|
| 54 |
"units": units,
|
| 55 |
}
|
|
|
|
| 56 |
result = training_crew.kickoff(inputs=merged_inputs)
|
| 57 |
+
print(result.json_dict)
|
|
|
|
| 58 |
|
| 59 |
+
return {"message": "Introduction Generated Well 🚀", "result": result}
|
schemas/__init__.py
CHANGED
|
@@ -15,9 +15,5 @@ from .outliner_schema import (
|
|
| 15 |
)
|
| 16 |
|
| 17 |
from .source_schema import AllResults, SingleResult
|
| 18 |
-
from .introduction_schema import
|
| 19 |
-
IntroductionOutput,
|
| 20 |
-
parsed_introduction,
|
| 21 |
-
SafePydanticParser,
|
| 22 |
-
)
|
| 23 |
from .inputs_schema import OutlineInput
|
|
|
|
| 15 |
)
|
| 16 |
|
| 17 |
from .source_schema import AllResults, SingleResult
|
| 18 |
+
from .introduction_schema import IntroductionOutput
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
from .inputs_schema import OutlineInput
|
schemas/introduction_schema.py
CHANGED
|
@@ -1,50 +1,10 @@
|
|
| 1 |
-
from pydantic import BaseModel, Field
|
| 2 |
from typing import List, Dict, Literal, Optional
|
| 3 |
-
import re
|
| 4 |
-
import json
|
| 5 |
-
|
| 6 |
-
CONTROL_CHARS = "".join(map(chr, range(0, 32)))
|
| 7 |
-
CONTROL_CHAR_RE = re.compile(f"[{re.escape(CONTROL_CHARS)}]")
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
import re
|
| 11 |
-
import json
|
| 12 |
-
from pydantic import ValidationError
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
class SafePydanticParser:
|
| 16 |
-
def __init__(self, model):
|
| 17 |
-
self.model = model
|
| 18 |
-
|
| 19 |
-
def parse(self, raw_output: str):
|
| 20 |
-
# 1. Remove control characters
|
| 21 |
-
cleaned = re.sub(r"[\x00-\x1F\x7F]", "", raw_output)
|
| 22 |
-
|
| 23 |
-
# 2. Extract JSON block
|
| 24 |
-
match = re.search(r"\{.*\}", cleaned, re.S)
|
| 25 |
-
if not match:
|
| 26 |
-
raise ValueError("❌ No JSON object found!")
|
| 27 |
-
|
| 28 |
-
json_block = match.group()
|
| 29 |
-
|
| 30 |
-
# 3. Validate JSON structure before sending to Pydantic
|
| 31 |
-
try:
|
| 32 |
-
json.loads(json_block)
|
| 33 |
-
except json.JSONDecodeError as e:
|
| 34 |
-
raise ValueError(f"❌ JSON is malformed: {e}")
|
| 35 |
-
|
| 36 |
-
# 4. Convert to Pydantic
|
| 37 |
-
try:
|
| 38 |
-
return self.model.model_validate_json(json_block)
|
| 39 |
-
except ValidationError as e:
|
| 40 |
-
raise ValueError(f"❌ Pydantic validation error: {e}")
|
| 41 |
|
| 42 |
|
| 43 |
class IntroductionOutput(BaseModel):
|
| 44 |
-
|
| 45 |
...,
|
| 46 |
-
title="
|
| 47 |
description="engaging introduction to the training program content, max 300 words.",
|
| 48 |
)
|
| 49 |
-
|
| 50 |
-
parsed_introduction = SafePydanticParser(IntroductionOutput)
|
|
|
|
| 1 |
+
from pydantic import BaseModel, Field
|
| 2 |
from typing import List, Dict, Literal, Optional
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
|
| 5 |
class IntroductionOutput(BaseModel):
|
| 6 |
+
introduction: str = Field(
|
| 7 |
...,
|
| 8 |
+
title="Introduction",
|
| 9 |
description="engaging introduction to the training program content, max 300 words.",
|
| 10 |
)
|
|
|
|
|
|