Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
!pip install gradio
|
| 3 |
+
from transformers import (
|
| 4 |
+
AutoModelForSequenceClassification,
|
| 5 |
+
AutoTokenizer,
|
| 6 |
+
AutoModelForTokenClassification,
|
| 7 |
+
pipeline
|
| 8 |
+
)
|
| 9 |
+
import gradio as gr
|
| 10 |
+
import torch
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
# Use a pipeline as a high-level helper
|
| 14 |
+
from transformers import pipeline
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
CLASS_MODEL_NAME = "AmandaCAI/resume-classifier"
|
| 18 |
+
NER_MODEL_NAME = "AmandaCAI/ner-keywords-extract"
|
| 19 |
+
|
| 20 |
+
# 初始化分类模型
|
| 21 |
+
class_tokenizer = AutoTokenizer.from_pretrained(CLASS_MODEL_NAME)
|
| 22 |
+
class_model = AutoModelForSequenceClassification.from_pretrained(CLASS_MODEL_NAME)
|
| 23 |
+
|
| 24 |
+
# 初始化NER模型
|
| 25 |
+
ner_tokenizer = AutoTokenizer.from_pretrained(NER_MODEL_NAME)
|
| 26 |
+
ner_model = AutoModelForTokenClassification.from_pretrained(NER_MODEL_NAME)
|
| 27 |
+
ner_pipeline = pipeline("ner", model=ner_model, tokenizer=ner_tokenizer, aggregation_strategy="simple")
|
| 28 |
+
|
| 29 |
+
# 岗位分类标签(根据你的训练数据调整)
|
| 30 |
+
job_categories = [
|
| 31 |
+
"Data Science", "Java Developer", "HR",
|
| 32 |
+
"Python Developer", "Web Designing", "Testing"
|
| 33 |
+
]
|
| 34 |
+
|
| 35 |
+
def analyze_resume(text):
|
| 36 |
+
"""处理简历分析的主函数"""
|
| 37 |
+
# 岗位分类
|
| 38 |
+
class_inputs = class_tokenizer(text, return_tensors="pt", truncation=True, max_length=512)
|
| 39 |
+
with torch.no_grad():
|
| 40 |
+
class_logits = class_model(**class_inputs).logits
|
| 41 |
+
predicted_class = torch.argmax(class_logits).item()
|
| 42 |
+
class_label = job_categories[predicted_class]
|
| 43 |
+
|
| 44 |
+
# 技能提取
|
| 45 |
+
ner_results = ner_pipeline(text)
|
| 46 |
+
skills = [entity["word"] for entity in ner_results if entity["entity_group"] == "SKILL"]
|
| 47 |
+
|
| 48 |
+
# 工作经验提取(示例)
|
| 49 |
+
experience = [entity["word"] for entity in ner_results if entity["entity_group"] == "EXPERIENCE"]
|
| 50 |
+
|
| 51 |
+
return {
|
| 52 |
+
"岗位类别": class_label,
|
| 53 |
+
"匹配度": f"{torch.softmax(class_logits, dim=1)[0][predicted_class].item()*100:.1f}%",
|
| 54 |
+
"核心技能": list(set(skills))[:5], # 取前5个不重复技能
|
| 55 |
+
"工作经验": list(set(experience))[:3]
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
# Gradio界面设计
|
| 59 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 60 |
+
gr.Markdown("# 🧠 AI Resume Analyzer")
|
| 61 |
+
|
| 62 |
+
with gr.Row():
|
| 63 |
+
with gr.Column():
|
| 64 |
+
input_text = gr.Textbox(label="📝 Paste Resume Text Here", lines=10,
|
| 65 |
+
placeholder="Enter your resume text here...")
|
| 66 |
+
submit_btn = gr.Button("Start the analysis", variant="primary")
|
| 67 |
+
|
| 68 |
+
with gr.Column():
|
| 69 |
+
output_json = gr.JSON(label="Analysis result")
|
| 70 |
+
|
| 71 |
+
# 示例数据
|
| 72 |
+
gr.Examples(
|
| 73 |
+
examples=[[
|
| 74 |
+
"""John Smith
|
| 75 |
+
Senior Python Developer
|
| 76 |
+
Skills: Python, Django, AWS, Machine Learning
|
| 77 |
+
Experience: 5+ years at Google, 3 years at Amazon
|
| 78 |
+
Education: MIT Computer Science"""
|
| 79 |
+
]],
|
| 80 |
+
inputs=[input_text]
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
submit_btn.click(
|
| 84 |
+
fn=analyze_resume,
|
| 85 |
+
inputs=[input_text],
|
| 86 |
+
outputs=output_json
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
# 启动应用
|
| 90 |
+
if __name__ == "__main__":
|
| 91 |
+
demo.launch()
|