from fastapi import FastAPI, UploadFile, File, Form, HTTPException from fastapi.middleware.cors import CORSMiddleware from parser_logic import extract_text_from_stream, analyze_resume import logging logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) app = FastAPI() app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_methods=["POST"], allow_headers=["*"], ) @app.post("/analyze") async def process_resume( file: UploadFile = File(...), job_description: str = Form(None) # Optional form field ): if file.content_type != "application/pdf": raise HTTPException(400, "Invalid file type. Only PDF allowed.") try: content = await file.read() text = extract_text_from_stream(content) if not text.strip(): raise HTTPException(400, "PDF text extraction failed.") # Pass both text and JD to the logic result = analyze_resume(text, job_description) return result except Exception as e: logger.error(f"Server Error: {e}") raise HTTPException(500, str(e))