cdcvd commited on
Commit
7b059c6
·
verified ·
1 Parent(s): 4a2164f

Upload 5 files

Browse files
N_F_skill_output.xlsx ADDED
Binary file (20.4 kB). View file
 
app.py ADDED
@@ -0,0 +1,161 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import re
3
+ import json
4
+ from transformers import AutoTokenizer, AutoModelForTokenClassification, pipeline
5
+ import gradio as gr
6
+
7
+ def read_from_excel(file_path):
8
+ df = pd.read_excel(file_path)
9
+ items = df['object'].astype(str).tolist() # تبدیل همه مقادیر به رشته
10
+ return items
11
+
12
+ def preprocess_text(text):
13
+ # حذف کاراکترهای غیرضروری و نرمال‌سازی متن
14
+ text = text.replace('\u200c', ' ').strip() # حذف نیم‌فاصله و فاصله‌های اضافی
15
+ text = re.sub(r'\s+', ' ', text) # حذف فاصله‌های تکراری
16
+ return text
17
+
18
+ def extract_items_in_text(text, items):
19
+ text = preprocess_text(text)
20
+ found_items = set() # استفاده از مجموعه برای جلوگیری از تکرار
21
+ for item in items:
22
+ item_normalized = preprocess_text(item)
23
+ if item_normalized.lower() in text.lower():
24
+ found_items.add(item_normalized)
25
+ return list(found_items)
26
+
27
+ def compare_items(items_1, items_2):
28
+ common_items = set()
29
+ score = 0 # مقدار پیش‌فرض برای score
30
+ for item1 in items_1:
31
+ for item2 in items_2:
32
+ words1 = set(item1.lower().split())
33
+ words2 = set(item2.lower().split())
34
+ common_words = words1.intersection(words2)
35
+ num_common = len(common_words)
36
+
37
+ if num_common >= 3:
38
+ common_items.add((item1, item2))
39
+ score = 100
40
+ elif num_common == 2:
41
+ common_items.add((item1, item2))
42
+ score = 75
43
+ elif num_common == 1:
44
+ common_items.add((item1, item2))
45
+ score = 50
46
+
47
+ return score, common_items
48
+
49
+ def compare_skills(skill_1, skill_2):
50
+ common_skill = set(skill_1).intersection(set(skill_2))
51
+ num_common = len(common_skill)
52
+
53
+ if num_common >= 10:
54
+ score = 100
55
+ elif num_common == 7:
56
+ score = 75
57
+ elif num_common == 5:
58
+ score = 50
59
+ else:
60
+ score = 25
61
+
62
+ return score, common_skill
63
+
64
+ def extract_ner_info(text, nlp):
65
+ ner_results = nlp(text)
66
+ full_name = ''
67
+ loc = ''
68
+ age = None
69
+
70
+ for i in range(len(ner_results)):
71
+ if ner_results[i]['entity'] == 'B-PER':
72
+ full_name = ner_results[i]['word']
73
+ for j in range(i+1, len(ner_results)):
74
+ if ner_results[j]['entity'].startswith('I-PER'):
75
+ full_name += ner_results[j]['word'].replace('##', '')
76
+ else:
77
+ break
78
+
79
+ if ner_results[i]['entity'] == 'B-LOC' and not loc:
80
+ loc = ner_results[i]['word']
81
+
82
+ age_match = re.search(r'سن\s*:\s*(\d+)', text)
83
+ if age_match:
84
+ age = int(age_match.group(1))
85
+
86
+ return full_name, loc, age
87
+
88
+ def process_text(input_text):
89
+ # مسیر فایل اکسل‌ها را وارد کنید
90
+ job_excel_file_path = 'jobs_output.xlsx'
91
+ education_excel_file_path = 'education_output.xlsx'
92
+ skills_excel_file_path = 'N_F_skill_output.xlsx'
93
+ # خواندن شغل‌ها، تحصیلات و مهارت‌ها از فایل‌های اکسل
94
+ jobs = read_from_excel(job_excel_file_path)
95
+ education = read_from_excel(education_excel_file_path)
96
+ skills = read_from_excel(skills_excel_file_path)
97
+
98
+ # متن ثابت
99
+ fixed_text = """استخدام کارآموز هوش مصنوعی (AI-شیراز)"""
100
+
101
+ input_text = input_text.replace("آدرس", "")
102
+ # استخراج شغل‌ها، تحصیلات و مهارت‌ها از متن‌ها
103
+ jobs_in_fixed_text = extract_items_in_text(fixed_text, jobs)
104
+ jobs_in_input_text = extract_items_in_text(input_text, jobs)
105
+ education_in_fixed_text = extract_items_in_text(fixed_text, education)
106
+ education_in_input_text = extract_items_in_text(input_text, education)
107
+ skills_in_fixed_text = extract_items_in_text(fixed_text, skills)
108
+ skills_in_input_text = extract_items_in_text(input_text, skills)
109
+
110
+ # مقایسه و نمره‌دهی
111
+ job_score, common_jobs = compare_items(jobs_in_fixed_text, jobs_in_input_text)
112
+ education_score, common_education = compare_items(education_in_fixed_text, education_in_input_text)
113
+ skill_score, common_skills = compare_skills(skills_in_fixed_text, skills_in_input_text)
114
+
115
+ # تنظیم و آماده‌سازی مدل NER
116
+ model_name_or_path = "HooshvareLab/distilbert-fa-zwnj-base-ner"
117
+ tokenizer = AutoTokenizer.from_pretrained(model_name_or_path)
118
+ model = AutoModelForTokenClassification.from_pretrained(model_name_or_path) # Pytorch
119
+ nlp = pipeline("ner", model=model, tokenizer=tokenizer)
120
+
121
+ # استخراج اطلاعات NER
122
+ full_name, loc, age = extract_ner_info(input_text, nlp)
123
+
124
+ # نمره‌دهی لوکیشن
125
+ fixed_loc = "شیراز"
126
+ loc_score = 100 if loc == fixed_loc else 0
127
+
128
+ # نمره‌دهی سن
129
+ age_score = 100 if age and 18 <= age <= 30 else 0
130
+
131
+ # محاسبه و نمایش میانگین نمرات
132
+ average_score = (job_score + education_score + skill_score + loc_score + age_score) / 5
133
+
134
+ # ساخت خروجی JSON
135
+ output = {
136
+ "average_score": average_score,
137
+ "full_name": full_name,
138
+ "age": age,
139
+ "location": loc,
140
+ "job_score": job_score,
141
+ "education_score": education_score,
142
+ "skill_score": skill_score,
143
+ "loc_score": loc_score,
144
+ "age_score": age_score,
145
+ "common_jobs": list(common_jobs),
146
+ "common_education": list(common_education),
147
+ "common_skills": list(common_skills)
148
+ }
149
+
150
+ return json.dumps(output, ensure_ascii=False, indent=4)
151
+
152
+ iface = gr.Interface(
153
+ fn=process_text,
154
+ inputs=gr.inputs.Textbox(lines=10, placeholder="لطفاً متن خود را وارد کنید..."),
155
+ outputs="json",
156
+ title="متن پرداز",
157
+ description="این ابزار متن شما را پردازش کرده و امتیازات مشابهت را محاسبه می‌کند."
158
+ )
159
+
160
+ if __name__ == "__main__":
161
+ iface.launch()
education_output.xlsx ADDED
Binary file (11 kB). View file
 
jobs_output.xlsx ADDED
Binary file (14.3 kB). View file
 
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ gradio
2
+ transformers
3
+ pandas
4
+ openpyxl