LovnishVerma commited on
Commit
8f32986
·
verified ·
1 Parent(s): 29fc8f7

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +22 -41
main.py CHANGED
@@ -1,58 +1,39 @@
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, 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))