Update main.py
Browse files
main.py
CHANGED
|
@@ -1,58 +1,39 @@
|
|
| 1 |
-
import
|
| 2 |
-
from fastapi import FastAPI, UploadFile, File, HTTPException
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
-
from parser_logic import extract_text_from_stream,
|
|
|
|
| 5 |
|
| 6 |
-
# Configure Logging
|
| 7 |
logging.basicConfig(level=logging.INFO)
|
| 8 |
logger = logging.getLogger(__name__)
|
| 9 |
|
| 10 |
-
app = FastAPI(
|
| 11 |
|
| 12 |
-
# CORS Middleware (Crucial for production when frontend/backend are on different ports/domains)
|
| 13 |
app.add_middleware(
|
| 14 |
CORSMiddleware,
|
| 15 |
-
allow_origins=["*"],
|
| 16 |
-
allow_credentials=True,
|
| 17 |
allow_methods=["POST"],
|
| 18 |
allow_headers=["*"],
|
| 19 |
)
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
Endpoint to process PDF resumes.
|
| 27 |
-
Validates file type and size, then processes in-memory.
|
| 28 |
-
"""
|
| 29 |
if file.content_type != "application/pdf":
|
| 30 |
-
raise HTTPException(
|
| 31 |
-
|
| 32 |
try:
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
# Validation: Check file size
|
| 37 |
-
if len(file_content) > MAX_FILE_SIZE:
|
| 38 |
-
raise HTTPException(status_code=413, detail="File too large. Max size is 5MB.")
|
| 39 |
-
|
| 40 |
-
logger.info(f"Processing file: {file.filename} ({len(file_content)} bytes)")
|
| 41 |
-
|
| 42 |
-
# Extract text (CPU bound task, but fast enough for small PDFs to run synchronously here)
|
| 43 |
-
# For very heavy loads, this should be offloaded to a background task (Celery/RQ)
|
| 44 |
-
raw_text = extract_text_from_stream(file_content)
|
| 45 |
-
|
| 46 |
-
if not raw_text.strip():
|
| 47 |
-
raise HTTPException(status_code=400, detail="Could not extract text. PDF might be image-based.")
|
| 48 |
-
|
| 49 |
-
# AI Processing
|
| 50 |
-
structured_result = parse_resume_with_ai(raw_text)
|
| 51 |
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
-
except HTTPException as he:
|
| 55 |
-
raise he
|
| 56 |
except Exception as e:
|
| 57 |
-
logger.error(f"
|
| 58 |
-
raise HTTPException(
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, File, Form, HTTPException
|
|
|
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
from parser_logic import extract_text_from_stream, analyze_resume
|
| 4 |
+
import logging
|
| 5 |
|
|
|
|
| 6 |
logging.basicConfig(level=logging.INFO)
|
| 7 |
logger = logging.getLogger(__name__)
|
| 8 |
|
| 9 |
+
app = FastAPI()
|
| 10 |
|
|
|
|
| 11 |
app.add_middleware(
|
| 12 |
CORSMiddleware,
|
| 13 |
+
allow_origins=["*"],
|
|
|
|
| 14 |
allow_methods=["POST"],
|
| 15 |
allow_headers=["*"],
|
| 16 |
)
|
| 17 |
|
| 18 |
+
@app.post("/analyze")
|
| 19 |
+
async def process_resume(
|
| 20 |
+
file: UploadFile = File(...),
|
| 21 |
+
job_description: str = Form(None) # Optional form field
|
| 22 |
+
):
|
|
|
|
|
|
|
|
|
|
| 23 |
if file.content_type != "application/pdf":
|
| 24 |
+
raise HTTPException(400, "Invalid file type. Only PDF allowed.")
|
| 25 |
+
|
| 26 |
try:
|
| 27 |
+
content = await file.read()
|
| 28 |
+
text = extract_text_from_stream(content)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
if not text.strip():
|
| 31 |
+
raise HTTPException(400, "PDF text extraction failed.")
|
| 32 |
+
|
| 33 |
+
# Pass both text and JD to the logic
|
| 34 |
+
result = analyze_resume(text, job_description)
|
| 35 |
+
return result
|
| 36 |
|
|
|
|
|
|
|
| 37 |
except Exception as e:
|
| 38 |
+
logger.error(f"Server Error: {e}")
|
| 39 |
+
raise HTTPException(500, str(e))
|