mahrooshishaq commited on
Commit
4ea7f74
·
1 Parent(s): 958fece

pdf extractor corrected

Browse files
Files changed (3) hide show
  1. Dockerfile +16 -0
  2. routers/extraction.py +9 -13
  3. services/pdf_extractor.py +3 -3
Dockerfile ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.11-slim
2
+
3
+ WORKDIR /app
4
+
5
+ # Install system dependencies for pdf extraction if needed
6
+ RUN apt-get update && apt-get install -y \
7
+ build-essential \
8
+ && rm -rf /var/lib/apt/lists/*
9
+
10
+ COPY requirements.txt .
11
+ RUN pip install --no-cache-dir -r requirements.txt
12
+
13
+ COPY . .
14
+
15
+ EXPOSE 8000
16
+ CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
routers/extraction.py CHANGED
@@ -1,24 +1,20 @@
1
- from fastapi import APIRouter, HTTPException
2
- from pydantic import BaseModel
3
- from services.pdf_extractor import extract_resume_from_path
4
 
5
  router = APIRouter(prefix="/extract-resume", tags=["extraction"])
6
 
7
 
8
- class ExtractRequest(BaseModel):
9
- file_path: str
10
-
11
-
12
  @router.post("")
13
- def extract_resume(request: ExtractRequest):
14
  """
15
- Receives the absolute path of an uploaded PDF (written by NestJS),
16
- extracts and structures its text, and returns it to NestJS.
17
  """
18
  try:
19
- result = extract_resume_from_path(request.file_path)
 
 
20
  return result
21
- except FileNotFoundError:
22
- raise HTTPException(status_code=404, detail="File not found at provided path.")
23
  except Exception as e:
24
  raise HTTPException(status_code=500, detail=f"Extraction failed: {str(e)}")
 
1
+ import io
2
+ from fastapi import APIRouter, HTTPException, File, UploadFile
3
+ from services.pdf_extractor import extract_resume_from_stream
4
 
5
  router = APIRouter(prefix="/extract-resume", tags=["extraction"])
6
 
7
 
 
 
 
 
8
  @router.post("")
9
+ async def extract_resume(file: UploadFile = File(...)):
10
  """
11
+ Receives an uploaded PDF file stream, extracts and structures its text,
12
+ and returns it to NestJS.
13
  """
14
  try:
15
+ content = await file.read()
16
+ pdf_stream = io.BytesIO(content)
17
+ result = extract_resume_from_stream(pdf_stream)
18
  return result
 
 
19
  except Exception as e:
20
  raise HTTPException(status_code=500, detail=f"Extraction failed: {str(e)}")
services/pdf_extractor.py CHANGED
@@ -14,9 +14,9 @@ SECTION_KEYWORDS = {
14
  }
15
 
16
 
17
- def extract_resume_from_path(file_path: str) -> dict:
18
  """
19
- Opens a PDF with pdfplumber, extracts text page by page,
20
  detects basic metadata, and identifies resume sections.
21
  """
22
  text_pages = []
@@ -24,7 +24,7 @@ def extract_resume_from_path(file_path: str) -> dict:
24
  has_images = False
25
  page_count = 0
26
 
27
- with pdfplumber.open(file_path) as pdf:
28
  page_count = len(pdf.pages)
29
 
30
  for page in pdf.pages:
 
14
  }
15
 
16
 
17
+ def extract_resume_from_stream(stream) -> dict:
18
  """
19
+ Opens a PDF with pdfplumber from a stream, extracts text page by page,
20
  detects basic metadata, and identifies resume sections.
21
  """
22
  text_pages = []
 
24
  has_images = False
25
  page_count = 0
26
 
27
+ with pdfplumber.open(stream) as pdf:
28
  page_count = len(pdf.pages)
29
 
30
  for page in pdf.pages: