Update main.py
Browse files
main.py
CHANGED
|
@@ -1,58 +1,27 @@
|
|
| 1 |
-
import
|
| 2 |
-
from fastapi import
|
| 3 |
-
from
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
)
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
"""
|
| 29 |
-
if file.content_type != "application/pdf":
|
| 30 |
-
raise HTTPException(status_code=400, detail="Invalid file type. Only PDF is allowed.")
|
| 31 |
-
|
| 32 |
-
try:
|
| 33 |
-
# Read file into memory (Async read)
|
| 34 |
-
file_content = await file.read()
|
| 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 |
-
return structured_result
|
| 53 |
-
|
| 54 |
-
except HTTPException as he:
|
| 55 |
-
raise he
|
| 56 |
-
except Exception as e:
|
| 57 |
-
logger.error(f"Unexpected Error: {e}")
|
| 58 |
-
raise HTTPException(status_code=500, detail="Internal Server Error")
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, File, HTTPException
|
| 2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
from parser_logic import extract_text_from_stream, parse_resume_with_ai
|
| 4 |
+
|
| 5 |
+
app = FastAPI()
|
| 6 |
+
|
| 7 |
+
app.add_middleware(
|
| 8 |
+
CORSMiddleware,
|
| 9 |
+
allow_origins=["*"],
|
| 10 |
+
allow_methods=["POST"],
|
| 11 |
+
allow_headers=["*"],
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
@app.post("/process-resume")
|
| 15 |
+
async def process_resume(file: UploadFile = File(...)):
|
| 16 |
+
if file.content_type != "application/pdf":
|
| 17 |
+
raise HTTPException(400, "Invalid file type.")
|
| 18 |
+
|
| 19 |
+
content = await file.read()
|
| 20 |
+
if len(content) > 5 * 1024 * 1024:
|
| 21 |
+
raise HTTPException(413, "File too large.")
|
| 22 |
+
|
| 23 |
+
text = extract_text_from_stream(content)
|
| 24 |
+
if not text.strip():
|
| 25 |
+
raise HTTPException(400, "PDF is empty or image-based.")
|
| 26 |
+
|
| 27 |
+
return parse_resume_with_ai(text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|