Spaces:
Sleeping
Sleeping
Create parser.py
Browse files
parser.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 2 |
+
import json
|
| 3 |
+
|
| 4 |
+
model_name = "Qwen/Qwen3-0.6B"
|
| 5 |
+
|
| 6 |
+
# load the tokenizer and the model
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 9 |
+
model_name,
|
| 10 |
+
torch_dtype="auto",
|
| 11 |
+
device_map="auto"
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
parsing = [
|
| 15 |
+
{"name":"education", "type":"List[str]","description":"attended school, university, and other education programs"},
|
| 16 |
+
{"name":"experience", "type":"float", "description":"years of experience"},
|
| 17 |
+
{"name":"skills", "type":"List[str]", "description":"list of skills"},
|
| 18 |
+
{"name":"name", "type":"str", "description":"name of the person"},
|
| 19 |
+
{"name":"location", "type":"str", "description":"location of the person"},
|
| 20 |
+
{"name":"email", "type":"str", "description":"email of the person"},
|
| 21 |
+
{"name":"websites", "type":"List[str]", "description":"urls related of the person"},
|
| 22 |
+
{"name":"certifications", "type":"List[str]", "description":"list of certifications"},
|
| 23 |
+
{"name":"languages", "type":"List[str]", "description":"list of languages"},
|
| 24 |
+
{"name":"projects", "type":"List[str]", "description":"list of projects"},
|
| 25 |
+
{"name":"note", "type":"str", "description":"additional note which highlight the best or uniqueness of the person"}
|
| 26 |
+
]
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def parse_resume(parsing, resume):
|
| 30 |
+
format_parsing = [f"{x['name']} : {x['type']} = {x['description']}\n" for x in parsing]
|
| 31 |
+
|
| 32 |
+
prompt = f"""Based on the below resume, tell me the summary details of skills, name, experience years, education, etc in short
|
| 33 |
+
The Output must be the JSON object with the following format:
|
| 34 |
+
{format_parsing}
|
| 35 |
+
|
| 36 |
+
RESUME:\n""" + resume
|
| 37 |
+
|
| 38 |
+
messages = [
|
| 39 |
+
{"role": "user", "content": prompt}
|
| 40 |
+
]
|
| 41 |
+
|
| 42 |
+
text = tokenizer.apply_chat_template(
|
| 43 |
+
messages,
|
| 44 |
+
tokenize=False,
|
| 45 |
+
add_generation_prompt=True,
|
| 46 |
+
enable_thinking=True # Switches between thinking and non-thinking modes. Default is True.
|
| 47 |
+
)
|
| 48 |
+
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
|
| 49 |
+
|
| 50 |
+
# conduct text completion
|
| 51 |
+
generated_ids = model.generate(
|
| 52 |
+
**model_inputs,
|
| 53 |
+
max_new_tokens=32768
|
| 54 |
+
)
|
| 55 |
+
output_ids = generated_ids[0][len(model_inputs.input_ids[0]):].tolist()
|
| 56 |
+
|
| 57 |
+
# parsing thinking content
|
| 58 |
+
try:
|
| 59 |
+
# rindex finding 151668 (</think>)
|
| 60 |
+
index = len(output_ids) - output_ids[::-1].index(151668)
|
| 61 |
+
except ValueError:
|
| 62 |
+
index = 0
|
| 63 |
+
|
| 64 |
+
thinking_content = tokenizer.decode(output_ids[:index], skip_special_tokens=True).strip("\n")
|
| 65 |
+
content = tokenizer.decode(output_ids[index:], skip_special_tokens=True).strip("\n")
|
| 66 |
+
return thinking_content, json.loads(content)
|