Spaces:
Running
Running
set max tokens to 2000
Browse files- __pycache__/main.cpython-312.pyc +0 -0
- main.py +28 -2
- services/__pycache__/cv_pipeline.cpython-312.pyc +0 -0
- services/cv_pipeline.py +10 -13
__pycache__/main.cpython-312.pyc
CHANGED
|
Binary files a/__pycache__/main.cpython-312.pyc and b/__pycache__/main.cpython-312.pyc differ
|
|
|
main.py
CHANGED
|
@@ -1,7 +1,8 @@
|
|
| 1 |
from typing import Any, Optional
|
| 2 |
|
| 3 |
-
from fastapi import FastAPI
|
| 4 |
from pydantic import BaseModel
|
|
|
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
|
|
@@ -17,4 +18,29 @@ def home():
|
|
| 17 |
message="Job Processor API is running",
|
| 18 |
statusCode=200,
|
| 19 |
payload=None
|
| 20 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from typing import Any, Optional
|
| 2 |
|
| 3 |
+
from fastapi import FastAPI, UploadFile, File, HTTPException
|
| 4 |
from pydantic import BaseModel
|
| 5 |
+
from services.cv_pipeline import process
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
|
|
|
|
| 18 |
message="Job Processor API is running",
|
| 19 |
statusCode=200,
|
| 20 |
payload=None
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
@app.post("/process-cv", response_model=APIResponse)
|
| 24 |
+
async def process_cv(file: UploadFile = File(...)):
|
| 25 |
+
if not file.filename:
|
| 26 |
+
raise HTTPException(status_code=400, detail="No file uploaded")
|
| 27 |
+
|
| 28 |
+
allowed_extensions = (".pdf", ".docx")
|
| 29 |
+
if not file.filename.lower().endswith(allowed_extensions):
|
| 30 |
+
raise HTTPException(
|
| 31 |
+
status_code=400,
|
| 32 |
+
detail=f"Invalid file format. Allowed formats are: {', '.join(allowed_extensions)}"
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
try:
|
| 36 |
+
file_bytes = await file.read()
|
| 37 |
+
result = process(file_bytes)
|
| 38 |
+
return APIResponse(
|
| 39 |
+
message="CV processed successfully",
|
| 40 |
+
statusCode=200,
|
| 41 |
+
payload=result
|
| 42 |
+
)
|
| 43 |
+
except ValueError as e:
|
| 44 |
+
raise HTTPException(status_code=400, detail=str(e))
|
| 45 |
+
except Exception as e:
|
| 46 |
+
raise HTTPException(status_code=500, detail=f"An error occurred during processing: {str(e)}")
|
services/__pycache__/cv_pipeline.cpython-312.pyc
ADDED
|
Binary file (6.26 kB). View file
|
|
|
services/cv_pipeline.py
CHANGED
|
@@ -3,6 +3,7 @@ pip install pdfplumber python-docx transformers torch accelerate
|
|
| 3 |
"""
|
| 4 |
|
| 5 |
import io
|
|
|
|
| 6 |
import re
|
| 7 |
import json
|
| 8 |
import pdfplumber
|
|
@@ -11,17 +12,14 @@ from docx.oxml.ns import qn
|
|
| 11 |
from transformers import pipeline
|
| 12 |
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
# Qwen2.5-1.5B-Instruct: ~3GB RAM, follows JSON instructions reliably
|
| 16 |
-
# Swap model= for anything larger if you have more RAM:
|
| 17 |
-
# "Qwen/Qwen2.5-3B-Instruct" (~6GB)
|
| 18 |
-
# "Qwen/Qwen2.5-7B-Instruct" (~14GB)
|
| 19 |
|
| 20 |
llm = pipeline(
|
| 21 |
"text-generation",
|
| 22 |
model="Qwen/Qwen2.5-1.5B-Instruct",
|
| 23 |
-
device_map="auto",
|
| 24 |
-
|
|
|
|
| 25 |
)
|
| 26 |
|
| 27 |
SYSTEM_PROMPT = """You are a CV parser. Extract information and return ONLY raw JSON β no markdown, no backticks, no explanation.
|
|
@@ -86,19 +84,18 @@ def clean(text: str) -> str:
|
|
| 86 |
|
| 87 |
def call_llm(text: str) -> dict:
|
| 88 |
messages = [
|
| 89 |
-
{"role": "system",
|
| 90 |
-
{"role": "user",
|
| 91 |
]
|
| 92 |
|
| 93 |
out = llm(
|
| 94 |
messages,
|
| 95 |
max_new_tokens=2000,
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
top_p=None,
|
| 99 |
)
|
| 100 |
|
| 101 |
-
raw = out[0]["generated_text"][-1]["content"]
|
| 102 |
raw = re.sub(r"^```(?:json)?\s*|\s*```$", "", raw.strip())
|
| 103 |
return json.loads(raw)
|
| 104 |
|
|
|
|
| 3 |
"""
|
| 4 |
|
| 5 |
import io
|
| 6 |
+
import os
|
| 7 |
import re
|
| 8 |
import json
|
| 9 |
import pdfplumber
|
|
|
|
| 12 |
from transformers import pipeline
|
| 13 |
|
| 14 |
|
| 15 |
+
HF_TOKEN = "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" # set in .env or environment
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
llm = pipeline(
|
| 18 |
"text-generation",
|
| 19 |
model="Qwen/Qwen2.5-1.5B-Instruct",
|
| 20 |
+
device_map="auto",
|
| 21 |
+
dtype="auto",
|
| 22 |
+
token=HF_TOKEN,
|
| 23 |
)
|
| 24 |
|
| 25 |
SYSTEM_PROMPT = """You are a CV parser. Extract information and return ONLY raw JSON β no markdown, no backticks, no explanation.
|
|
|
|
| 84 |
|
| 85 |
def call_llm(text: str) -> dict:
|
| 86 |
messages = [
|
| 87 |
+
{"role": "system", "content": SYSTEM_PROMPT},
|
| 88 |
+
{"role": "user", "content": text},
|
| 89 |
]
|
| 90 |
|
| 91 |
out = llm(
|
| 92 |
messages,
|
| 93 |
max_new_tokens=2000,
|
| 94 |
+
max_length=None, # suppresses max_length vs max_new_tokens warning
|
| 95 |
+
do_sample=False,
|
|
|
|
| 96 |
)
|
| 97 |
|
| 98 |
+
raw = out[0]["generated_text"][-1]["content"]
|
| 99 |
raw = re.sub(r"^```(?:json)?\s*|\s*```$", "", raw.strip())
|
| 100 |
return json.loads(raw)
|
| 101 |
|