Download app/services/postprocessing.py from alaa3/TouRa: direct link, hf CLI and curl.
- Browser
- Download file 1.1 kB
-
https://huggingface.co/spaces/alaa3/TouRa/resolve/main/app/services/postprocessing.py
- Command line
-
hf download hf://spaces/alaa3/TouRa/app/services/postprocessing.py
-
curl -L -o postprocessing.py https://huggingface.co/spaces/alaa3/TouRa/resolve/main/app/services/postprocessing.py
1.1 kB
| """ | |
| Post-processing utilities for raw agent outputs. | |
| """ | |
| import json | |
| import logging | |
| from pathlib import Path | |
| from typing import Any, Dict | |
| from app.core.schemas import AgentResponse | |
| logger = logging.getLogger(__name__) | |
| def save_agent_response(response: AgentResponse, output_path: str) -> None: | |
| """ | |
| Persist an AgentResponse to a JSON file. | |
| Args: | |
| response: Parsed AgentResponse Pydantic model. | |
| output_path: Destination file path. | |
| """ | |
| path = Path(output_path) | |
| path.parent.mkdir(parents=True, exist_ok=True) | |
| response_dict = response.model_dump(mode="json") | |
| with open(path, "w", encoding="utf-8") as f: | |
| json.dump(response_dict, f, indent=4, ensure_ascii=False) | |
| logger.info("Saved structured agent response to %s", path.resolve()) | |
| def agent_response_to_dict(response: AgentResponse) -> Dict[str, Any]: | |
| """ | |
| Convert an AgentResponse Pydantic model to a plain dict for JSON serialisation. | |
| Args: | |
| response: Parsed AgentResponse. | |
| Returns: | |
| Plain dictionary. | |
| """ | |
| return response.model_dump(mode="json") | |