Spaces:
Sleeping
Sleeping
Download app/api/routes/jd.py from AnishDev/coderound-task-backend: direct link, hf CLI and curl.
- Browser
- Download file 3.52 kB
-
https://huggingface.co/spaces/AnishDev/coderound-task-backend/resolve/main/app/api/routes/jd.py
- Command line
-
hf download hf://spaces/AnishDev/coderound-task-backend/app/api/routes/jd.py
-
curl -L -o jd.py https://huggingface.co/spaces/AnishDev/coderound-task-backend/resolve/main/app/api/routes/jd.py
3.52 kB
| import uuid | |
| import json | |
| import logging | |
| from datetime import datetime, timezone | |
| from fastapi import APIRouter, UploadFile, File, HTTPException | |
| from app.services import storage, file_parser | |
| from app.pipeline.jd_parser import parse_jd | |
| from app.models.schemas import JDUploadResponse, JDListItem, JDDetailResponse | |
| router = APIRouter(prefix="/api/v1/jd", tags=["jd"]) | |
| logger = logging.getLogger(__name__) | |
| async def upload_jd(file: UploadFile = File(...)): | |
| if not file.filename or not file.filename.lower().endswith(".pdf"): | |
| raise HTTPException(400, "Only PDF files accepted for JDs") | |
| content = await file.read() | |
| if len(content) > 10 * 1024 * 1024: | |
| raise HTTPException(413, "PDF too large (max 10 MB)") | |
| try: | |
| text = file_parser.parse_pdf(content) | |
| except ValueError as e: | |
| raise HTTPException(400, str(e)) | |
| jd_id = str(uuid.uuid4())[:8] | |
| jd = parse_jd(text, jd_id=jd_id) | |
| now = datetime.now(timezone.utc).isoformat() | |
| data = { | |
| "jd_id": jd_id, | |
| "title": jd.title, | |
| "engineer_type": jd.engineer_type or "", | |
| "min_years": jd.min_years, | |
| "required_skills": json.dumps([ | |
| {"name": s.name, "level": s.level, "raw": s.raw} | |
| for s in jd.required_skills | |
| ]), | |
| "preferred_skills": json.dumps([ | |
| {"name": s.name, "level": s.level, "raw": s.raw} | |
| for s in jd.preferred_skills | |
| ]), | |
| "responsibilities": json.dumps(jd.responsibilities), | |
| "full_text": jd.full_text[:3000], | |
| "uploaded_at": now, | |
| } | |
| storage.save_jd(jd_id, data) | |
| logger.info(f"JD uploaded: {jd_id} '{jd.title}'") | |
| return JDUploadResponse( | |
| jd_id=jd_id, | |
| title=jd.title, | |
| engineer_type=jd.engineer_type, | |
| min_years=jd.min_years, | |
| required_skills_count=len(jd.required_skills), | |
| preferred_skills_count=len(jd.preferred_skills), | |
| uploaded_at=now, | |
| ) | |
| def list_jds(): | |
| items = storage.list_jds() | |
| result = [] | |
| for d in items: | |
| try: | |
| req = json.loads(d.get("required_skills", "[]")) | |
| except Exception: | |
| req = [] | |
| result.append(JDListItem( | |
| jd_id=str(d.get("jd_id", "")), | |
| title=d.get("title", ""), | |
| engineer_type=d.get("engineer_type") or None, | |
| min_years=float(d.get("min_years", 0)), | |
| required_skills_count=len(req), | |
| uploaded_at=d.get("uploaded_at", ""), | |
| )) | |
| return result | |
| def get_jd(jd_id: str): | |
| data = storage.get_jd(jd_id) | |
| if not data: | |
| raise HTTPException(404, f"JD {jd_id} not found") | |
| req = data.get("required_skills", []) | |
| pref = data.get("preferred_skills", []) | |
| resp = data.get("responsibilities", []) | |
| if isinstance(req, str): | |
| req = json.loads(req) | |
| if isinstance(pref, str): | |
| pref = json.loads(pref) | |
| if isinstance(resp, str): | |
| resp = json.loads(resp) | |
| return JDDetailResponse( | |
| jd_id=str(data.get("jd_id", jd_id)), | |
| title=data.get("title", ""), | |
| engineer_type=data.get("engineer_type") or None, | |
| min_years=float(data.get("min_years", 0)), | |
| required_skills_count=len(req), | |
| uploaded_at=data.get("uploaded_at", ""), | |
| required_skills=req, | |
| preferred_skills=pref, | |
| responsibilities=resp, | |
| ) | |