LovnishVerma commited on
Commit
585c8d0
·
verified ·
1 Parent(s): b3c0469

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +27 -58
main.py CHANGED
@@ -1,58 +1,27 @@
1
- import logging
2
- from fastapi import FastAPI, UploadFile, File, HTTPException
3
- from fastapi.middleware.cors import CORSMiddleware
4
- from parser_logic import extract_text_from_stream, parse_resume_with_ai
5
-
6
- # Configure Logging
7
- logging.basicConfig(level=logging.INFO)
8
- logger = logging.getLogger(__name__)
9
-
10
- app = FastAPI(title="Resume Parser API", version="1.0.0")
11
-
12
- # CORS Middleware (Crucial for production when frontend/backend are on different ports/domains)
13
- app.add_middleware(
14
- CORSMiddleware,
15
- allow_origins=["*"], # In strict production, replace "*" with specific frontend domain
16
- allow_credentials=True,
17
- allow_methods=["POST"],
18
- allow_headers=["*"],
19
- )
20
-
21
- MAX_FILE_SIZE = 5 * 1024 * 1024 # 5 MB limit
22
-
23
- @app.post("/process-resume")
24
- async def process_resume(file: UploadFile = File(...)):
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(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)