File size: 6,147 Bytes
de99208 30a1b76 de99208 30a1b76 de99208 30a1b76 de99208 30a1b76 de99208 30a1b76 e2e5a5f de99208 30a1b76 de99208 30a1b76 de99208 30a1b76 de99208 30a1b76 de99208 30a1b76 de99208 30a1b76 de99208 30a1b76 de99208 30a1b76 de99208 30a1b76 de99208 30a1b76 de99208 30a1b76 de99208 30a1b76 de99208 30a1b76 de99208 30a1b76 de99208 30a1b76 d9d3195 30a1b76 de99208 30a1b76 de99208 30a1b76 de99208 30a1b76 de99208 30a1b76 de99208 30a1b76 de99208 30a1b76 de99208 30a1b76 de99208 30a1b76 de99208 30a1b76 de99208 30a1b76 de99208 30a1b76 de99208 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
"""
app.py (MULTI-LABEL V2 - English UI)
Gradio interface for the Entity Extraction Model
(SKILL, SOFT_SKILL, LANG, CERT, EXPERIENCE_DURATION)
Loads the trained model and provides a UI to compare CV and JD.
"""
import gradio as gr
import re
from typing import List, Dict, Set, Tuple
# Import the extractor we already created
from scripts.inference import EntityExtractor
# --- 1. Model Loading ---
# --- MODIFICATION ---
# Point to the local model you just trained
MODEL_PATH = "feliponi/hirly-ner-multi"
try:
extractor = EntityExtractor(MODEL_PATH)
print(f"Model loaded successfully from {MODEL_PATH}")
except Exception as e:
print(f"CRITICAL ERROR: Could not load model from {MODEL_PATH}.")
print("Ensure the trained model is in the correct directory.")
extractor = None
# --- 2. Business Logic (Unchanged) ---
def parse_and_sum_experience(entities: List[Dict]) -> float:
"""
Parses 'EXPERIENCE_DURATION' spans and sums them into years.
(This function remains the same)
"""
total_experience = 0.0
num_words = {
"one": 1,
"two": 2,
"three": 3,
"four": 4,
"five": 5,
"six": 6,
"seven": 7,
"eight": 8,
"nine": 9,
"ten": 10,
}
durations = [
e["entity"].lower() for e in entities if e["label"] == "EXPERIENCE_DURATION"
]
for text in durations:
found_number = None
match = re.search(r"(\d+[\.,]\d+|\d+)", text)
if match:
found_number = float(match.group(1).replace(",", "."))
else:
for word, number in num_words.items():
if word in text:
found_number = number
break
if found_number is not None:
if "month" in text or "mes" in text:
total_experience += found_number / 12
else:
total_experience += found_number
return round(total_experience, 1)
def extract_and_group_entities(
text: str, confidence_threshold: float
) -> Dict[str, Set[str]]:
"""
Extracts entities from text and groups them by label.
"""
grouped_entities = {
"SKILL": set(),
"SOFT_SKILL": set(),
"LANG": set(),
"CERT": set(),
"EXPERIENCE_DURATION": set(),
}
entities = extractor.extract_entities_with_details(text, confidence_threshold)
for entity in entities:
label = entity.get("label")
if label in grouped_entities:
grouped_entities[label].add(entity["entity"].lower())
return grouped_entities
def analyze_cv_and_jd(cv_text: str, jd_text: str) -> (str, str, str, Dict, Dict):
"""
Main function called by Gradio.
Processes CV and JD, finds all entities, sums experience, and compares.
"""
if not extractor:
return "ERROR: Model not loaded.", "", "", {}, {}
# 1. Process texts and group entities
cv_groups = extract_and_group_entities(cv_text, confidence_threshold=0.7)
jd_groups = extract_and_group_entities(jd_text, confidence_threshold=0.7)
# 2. Sum experience
cv_exp_entities = extractor.extract_entities_with_details(cv_text, 0.7)
jd_exp_entities = extractor.extract_entities_with_details(jd_text, 0.7)
cv_exp = parse_and_sum_experience(cv_exp_entities)
jd_exp = parse_and_sum_experience(jd_exp_entities)
# 3. Format Match Analysis output
match_output = "## π Match Analysis\n\n"
labels_to_match = ["SKILL", "SOFT_SKILL", "LANG", "CERT"]
for label in labels_to_match:
cv_set = cv_groups[label]
jd_set = jd_groups[label]
matching = cv_set.intersection(jd_set)
match_output += f"**Matching {label.replace('_', ' ')}S: {len(matching)}**\n"
if matching:
match_output += f"_{', '.join(sorted(list(matching)))}_\n"
else:
match_output += "_No matching items found._\n"
match_output += "---\n"
# 4. Format JSON outputs
cv_groups.pop("EXPERIENCE_DURATION")
jd_groups.pop("EXPERIENCE_DURATION")
cv_json_output = {k: sorted(list(v)) for k, v in cv_groups.items() if v}
jd_json_output = {k: sorted(list(v)) for k, v in jd_groups.items() if v}
cv_exp_str = f"{cv_exp} years"
jd_exp_str = f"{jd_exp} years (Requirement extracted from JD)"
return (match_output, cv_exp_str, jd_exp_str, cv_json_output, jd_json_output)
# --- 3. Gradio Interface Definition (All English) ---
with gr.Blocks(title="Hirly - Resume & JD Analyzer") as demo:
gr.Markdown("# π Resume vs. Job Description Analyzer")
gr.Markdown(
"Provide the text from a Resume (CV) and a Job Description (JD) to extract "
"skills, soft skills, languages, certifications, years of experience, and see their compatibility."
)
with gr.Row():
with gr.Column():
cv_input = gr.Textbox(lines=20, label="Resume (CV) Text")
with gr.Column():
jd_input = gr.Textbox(lines=20, label="Job Description (JD) Text")
analyze_button = gr.Button("Analyze Compatibility", variant="primary")
gr.Markdown("---")
with gr.Row():
with gr.Column(scale=2):
match_output = gr.Markdown(label="Match Analysis")
with gr.Column(scale=1):
cv_exp_output = gr.Textbox(label="Total Experience (CV)", interactive=False)
jd_exp_output = gr.Textbox(label="Total Experience (JD)", interactive=False)
with gr.Row():
cv_only_output = gr.JSON(label="Entities Found in CV")
jd_only_output = gr.JSON(label="Entities Required by JD")
# Connect button to function
analyze_button.click(
fn=analyze_cv_and_jd,
inputs=[cv_input, jd_input],
outputs=[
match_output,
cv_exp_output,
jd_exp_output,
cv_only_output,
jd_only_output,
],
)
if __name__ == "__main__":
demo.launch() |