Download utils.py from sp103107/NERA: direct link, hf CLI and curl.
- Browser
- Download file 2.72 kB
-
https://huggingface.co/spaces/sp103107/NERA/resolve/main/utils.py
- Command line
-
hf download hf://spaces/sp103107/NERA/utils.py
-
curl -L -o utils.py https://huggingface.co/spaces/sp103107/NERA/resolve/main/utils.py
2.72 kB
| """ | |
| utils.py | |
| -------- | |
| Utility functions for the NORA Admissions Agent project. | |
| Responsibilities: | |
| - parse_response: Safely parse JSON from Gemini’s response | |
| - rank_applicants: Sort applicants by total score | |
| - clean_json_text: Sanitize malformed JSON text (e.g., remove code fences) | |
| """ | |
| import json | |
| from typing import List, Dict | |
| def parse_response(response_text: str) -> Dict: | |
| """ | |
| Safely parse the JSON output from Gemini. | |
| The LLM is instructed to return strict JSON, but in case of formatting | |
| issues (extra text, markdown, etc.), this function attempts to handle errors | |
| gracefully. | |
| Parameters | |
| ---------- | |
| response_text : str | |
| The raw response text from Gemini. | |
| Returns | |
| ------- | |
| dict | |
| A parsed JSON dictionary, or an error dictionary if parsing fails. | |
| Example | |
| ------- | |
| >>> parse_response('{"name": "Anna", "total_score": 9.2}') | |
| {'name': 'Anna', 'total_score': 9.2} | |
| """ | |
| # Handle None or empty input | |
| if response_text is None: | |
| return {"error": "Could not parse JSON", "raw_response": None} | |
| if not response_text.strip(): | |
| return {"error": "Could not parse JSON", "raw_response": ""} | |
| try: | |
| return json.loads(response_text) | |
| except json.JSONDecodeError: | |
| cleaned = clean_json_text(response_text) | |
| try: | |
| return json.loads(cleaned) | |
| except json.JSONDecodeError: | |
| return {"error": "Could not parse JSON", "raw_response": response_text} | |
| def rank_applicants(reviews: List[Dict]) -> List[Dict]: | |
| """ | |
| Rank all applicants based on their total_score from highest to lowest. | |
| Parameters | |
| ---------- | |
| reviews : list of dict | |
| List of applicant review dictionaries with 'total_score' key. | |
| Returns | |
| ------- | |
| list of dict | |
| Sorted list of applicants. | |
| Example | |
| ------- | |
| >>> rank_applicants([{"name": "A", "total_score": 8.5}, {"name": "B", "total_score": 9.0}]) | |
| [{'name': 'B', 'total_score': 9.0}, {'name': 'A', 'total_score': 8.5}] | |
| """ | |
| return sorted(reviews, key=lambda x: x.get("total_score", 0), reverse=True) | |
| def clean_json_text(text: str) -> str: | |
| """ | |
| Attempt to clean up malformed JSON text by: | |
| - Removing markdown code fences | |
| - Stripping extraneous whitespace | |
| Parameters | |
| ---------- | |
| text : str | |
| The raw text from Gemini. | |
| Returns | |
| ------- | |
| str | |
| A cleaned string that is more likely to parse as JSON. | |
| """ | |
| cleaned = text.strip() | |
| # Remove triple backticks and 'json' code block indicators if present | |
| if cleaned.startswith("```"): | |
| cleaned = cleaned.replace("```json", "").replace("```", "") | |
| return cleaned.strip() | |