soojeongcrystal commited on
Commit
402b304
โ€ข
1 Parent(s): 6532346

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -29
app.py CHANGED
@@ -6,10 +6,24 @@ import networkx as nx
6
  import matplotlib.pyplot as plt
7
  import csv
8
  import datetime
 
9
 
10
  # Sentence-BERT ๋ชจ๋ธ ๋กœ๋“œ
11
  model = SentenceTransformer('all-MiniLM-L6-v2')
12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  # ์ถ”์ฒœ ๊ฒฐ๊ณผ์™€ ํ”ผ๋“œ๋ฐฑ์„ ๊ธฐ๋กํ•˜๋Š” ํ•จ์ˆ˜
14
  def log_recommendation(employee_name, recommended_programs, feedback=None):
15
  with open('recommendation_log.csv', mode='a', newline='') as file:
@@ -19,34 +33,41 @@ def log_recommendation(employee_name, recommended_programs, feedback=None):
19
 
20
  # ์ง์› ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„์„ํ•˜์—ฌ ๊ต์œก ํ”„๋กœ๊ทธ๋žจ์„ ์ถ”์ฒœํ•˜๊ณ  ๊ทธ๋ž˜ํ”„๋ฅผ ๊ทธ๋ฆฌ๋Š” ํ•จ์ˆ˜
21
  def analyze_data(employee_file, program_file, feedback=None):
 
22
  employee_df = pd.read_csv(employee_file.name)
23
  program_df = pd.read_csv(program_file.name)
24
 
 
25
  employee_skills = employee_df['current_skills'].tolist()
26
  program_skills = program_df['skills_acquired'].tolist()
27
  employee_embeddings = model.encode(employee_skills)
28
  program_embeddings = model.encode(program_skills)
29
 
 
30
  similarities = cosine_similarity(employee_embeddings, program_embeddings)
31
 
 
32
  recommendations = []
 
33
  for i, employee in employee_df.iterrows():
34
  recommended_programs = []
35
  for j, program in program_df.iterrows():
36
- if similarities[i][j] > 0.5:
37
  recommended_programs.append(f"{program['program_name']} ({program['duration']})")
38
 
39
  if recommended_programs:
40
  recommendation = f"์ง์› {employee['employee_name']}์˜ ์ถ”์ฒœ ํ”„๋กœ๊ทธ๋žจ: {', '.join(recommended_programs)}"
 
41
  else:
42
  recommendation = f"์ง์› {employee['employee_name']}์—๊ฒŒ ์ ํ•ฉํ•œ ํ”„๋กœ๊ทธ๋žจ์ด ์—†์Šต๋‹ˆ๋‹ค."
 
43
 
 
44
  log_recommendation(employee['employee_name'], recommended_programs, feedback)
45
 
46
  recommendations.append(recommendation)
47
 
48
- result_text = "\n".join(recommendations)
49
-
50
  G = nx.Graph()
51
  for employee in employee_df['employee_name']:
52
  G.add_node(employee, type='employee')
@@ -56,16 +77,20 @@ def analyze_data(employee_file, program_file, feedback=None):
56
 
57
  for i, employee in employee_df.iterrows():
58
  for j, program in program_df.iterrows():
59
- if similarities[i][j] > 0.5:
60
  G.add_edge(employee['employee_name'], program['program_name'])
61
 
 
62
  plt.figure(figsize=(10, 8))
63
  pos = nx.spring_layout(G)
64
  nx.draw(G, pos, with_labels=True, node_color='skyblue', node_size=2000, font_size=10, font_weight='bold')
65
  plt.title("์ง์›๊ณผ ํ”„๋กœ๊ทธ๋žจ ๊ฐ„์˜ ๊ด€๊ณ„")
66
  plt.tight_layout()
67
 
68
- return result_text, plt.gcf()
 
 
 
69
 
70
  # ์˜ˆ์‹œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ํ•จ์ˆ˜
71
  def show_example_data():
@@ -97,30 +122,17 @@ with gr.Blocks() as demo:
97
  with gr.Column(scale=1):
98
  gr.Markdown("# HybridRAG ์‹œ์Šคํ…œ")
99
 
100
- # ์—ญํ•  ์„ ํƒ (HR vs ์ง์›)
101
- role = gr.Radio(["HR ๊ด€๋ฆฌ์ž", "์ง์›"], label="์—ญํ• ์„ ์„ ํƒํ•˜์„ธ์š”")
102
-
103
- # ์—ญํ• ์— ๋”ฐ๋ฅธ ์กฐ๊ฑด ๋ถ„๊ธฐ
104
- with gr.Tabs() as tabs:
105
- with gr.Tab("HR ๊ด€๋ฆฌ์ž"):
106
- with gr.Box(visible=True) as hr_admin_box:
107
- gr.Markdown("## HR ๊ด€๋ฆฌ์ž ์ธํ„ฐํŽ˜์ด์Šค")
108
- employee_file = gr.File(label="์ง์› ๋ฐ์ดํ„ฐ ์—…๋กœ๋“œ")
109
- program_file = gr.File(label="๊ต์œก ํ”„๋กœ๊ทธ๋žจ ๋ฐ์ดํ„ฐ ์—…๋กœ๋“œ")
110
- analyze_button = gr.Button("๋ถ„์„ ์‹œ์ž‘")
111
- output_text = gr.Textbox(label="๋ถ„์„ ๊ฒฐ๊ณผ")
112
-
113
- # HR ๊ด€๋ฆฌ์ž๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€
114
- feedback_input = gr.Radio(choices=["๋งŒ์กฑ", "๋ถˆ๋งŒ์กฑ"], label="์ง์› ํ”ผ๋“œ๋ฐฑ ๊ธฐ๋ก")
115
-
116
- analyze_button.click(analyze_data, inputs=[employee_file, program_file, feedback_input], outputs=[output_text])
117
-
118
- with gr.Tab("์ง์›"):
119
- with gr.Box(visible=True) as employee_box:
120
- gr.Markdown("## ์ง์› ์ธํ„ฐํŽ˜์ด์Šค")
121
- gr.Markdown("์ง์›์—๊ฒŒ ๋งž์ถคํ˜• ๊ต์œก ํ”„๋กœ๊ทธ๋žจ ์ถ”์ฒœ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.")
122
- employee_data = gr.DataFrame(label="์ง์› ๊ฐœ์ธ ๋ฐ์ดํ„ฐ ์˜ˆ์‹œ")
123
- gr.Button("ํ”„๋กœ๊ทธ๋žจ ์ถ”์ฒœ ์‹œ์ž‘")
124
 
125
  # ์˜ˆ์‹œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฏธ๋ฆฌ๋ณด๊ธฐ๋กœ ์ œ๊ณต
126
  example_button = gr.Button("์˜ˆ์‹œ ๋ฐ์ดํ„ฐ ๋ณด๊ธฐ")
@@ -128,5 +140,16 @@ with gr.Blocks() as demo:
128
  program_example_output = gr.DataFrame(label="๊ต์œก ํ”„๋กœ๊ทธ๋žจ ๋ฐ์ดํ„ฐ ์˜ˆ์‹œ")
129
  example_button.click(show_example_data, outputs=[employee_example_output, program_example_output])
130
 
 
 
 
 
 
 
 
 
 
 
 
131
  # Gradio ์ธํ„ฐํŽ˜์ด์Šค ์‹คํ–‰
132
  demo.launch()
 
6
  import matplotlib.pyplot as plt
7
  import csv
8
  import datetime
9
+ import io
10
 
11
  # Sentence-BERT ๋ชจ๋ธ ๋กœ๋“œ
12
  model = SentenceTransformer('all-MiniLM-L6-v2')
13
 
14
+ # ์ถ”์ฒœ ๊ฒฐ๊ณผ๋ฅผ CSV ํŒŒ์ผ๋กœ ์ €์žฅํ•˜๋Š” ํ•จ์ˆ˜
15
+ def save_recommendations_to_csv(recommendations):
16
+ output = io.StringIO()
17
+ writer = csv.writer(output)
18
+ writer.writerow(["Employee ID", "Employee Name", "Recommended Programs"])
19
+
20
+ # ์ถ”์ฒœ ๊ฒฐ๊ณผ CSV ํŒŒ์ผ์— ๊ธฐ๋ก
21
+ for rec in recommendations:
22
+ writer.writerow(rec)
23
+
24
+ output.seek(0)
25
+ return output
26
+
27
  # ์ถ”์ฒœ ๊ฒฐ๊ณผ์™€ ํ”ผ๋“œ๋ฐฑ์„ ๊ธฐ๋กํ•˜๋Š” ํ•จ์ˆ˜
28
  def log_recommendation(employee_name, recommended_programs, feedback=None):
29
  with open('recommendation_log.csv', mode='a', newline='') as file:
 
33
 
34
  # ์ง์› ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„์„ํ•˜์—ฌ ๊ต์œก ํ”„๋กœ๊ทธ๋žจ์„ ์ถ”์ฒœํ•˜๊ณ  ๊ทธ๋ž˜ํ”„๋ฅผ ๊ทธ๋ฆฌ๋Š” ํ•จ์ˆ˜
35
  def analyze_data(employee_file, program_file, feedback=None):
36
+ # ์ง์› ๋ฐ์ดํ„ฐ์™€ ๊ต์œก ํ”„๋กœ๊ทธ๋žจ ๋ฐ์ดํ„ฐ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
37
  employee_df = pd.read_csv(employee_file.name)
38
  program_df = pd.read_csv(program_file.name)
39
 
40
+ # ์ง์› ์—ญ๋Ÿ‰๊ณผ ํ”„๋กœ๊ทธ๋žจ ํ•™์Šต ๋ชฉํ‘œ๋ฅผ ๋ฒกํ„ฐํ™”
41
  employee_skills = employee_df['current_skills'].tolist()
42
  program_skills = program_df['skills_acquired'].tolist()
43
  employee_embeddings = model.encode(employee_skills)
44
  program_embeddings = model.encode(program_skills)
45
 
46
+ # ์œ ์‚ฌ๋„ ๊ณ„์‚ฐ
47
  similarities = cosine_similarity(employee_embeddings, program_embeddings)
48
 
49
+ # ์ง์›๋ณ„ ์ถ”์ฒœ ํ”„๋กœ๊ทธ๋žจ ๋ฆฌ์ŠคํŠธ
50
  recommendations = []
51
+ recommendation_rows = [] # CSV ํŒŒ์ผ์— ์ €์žฅํ•  ๋ฐ์ดํ„ฐ๋ฅผ ์œ„ํ•œ ๋ฆฌ์ŠคํŠธ
52
  for i, employee in employee_df.iterrows():
53
  recommended_programs = []
54
  for j, program in program_df.iterrows():
55
+ if similarities[i][j] > 0.5: # ์œ ์‚ฌ๋„ ์ž„๊ณ„๊ฐ’ ๊ธฐ์ค€
56
  recommended_programs.append(f"{program['program_name']} ({program['duration']})")
57
 
58
  if recommended_programs:
59
  recommendation = f"์ง์› {employee['employee_name']}์˜ ์ถ”์ฒœ ํ”„๋กœ๊ทธ๋žจ: {', '.join(recommended_programs)}"
60
+ recommendation_rows.append([employee['employee_id'], employee['employee_name'], ", ".join(recommended_programs)])
61
  else:
62
  recommendation = f"์ง์› {employee['employee_name']}์—๊ฒŒ ์ ํ•ฉํ•œ ํ”„๋กœ๊ทธ๋žจ์ด ์—†์Šต๋‹ˆ๋‹ค."
63
+ recommendation_rows.append([employee['employee_id'], employee['employee_name'], "์ ํ•ฉํ•œ ํ”„๋กœ๊ทธ๋žจ ์—†์Œ"])
64
 
65
+ # ํ”ผ๋“œ๋ฐฑ ๋กœ๊ทธ ๊ธฐ๋ก
66
  log_recommendation(employee['employee_name'], recommended_programs, feedback)
67
 
68
  recommendations.append(recommendation)
69
 
70
+ # ๋„คํŠธ์›Œํฌ ๊ทธ๋ž˜ํ”„ ์ƒ์„ฑ
 
71
  G = nx.Graph()
72
  for employee in employee_df['employee_name']:
73
  G.add_node(employee, type='employee')
 
77
 
78
  for i, employee in employee_df.iterrows():
79
  for j, program in program_df.iterrows():
80
+ if similarities[i][j] > 0.5: # ์œ ์‚ฌ๋„ ์ž„๊ณ„๊ฐ’
81
  G.add_edge(employee['employee_name'], program['program_name'])
82
 
83
+ # ๊ทธ๋ž˜ํ”„ ์‹œ๊ฐํ™”
84
  plt.figure(figsize=(10, 8))
85
  pos = nx.spring_layout(G)
86
  nx.draw(G, pos, with_labels=True, node_color='skyblue', node_size=2000, font_size=10, font_weight='bold')
87
  plt.title("์ง์›๊ณผ ํ”„๋กœ๊ทธ๋žจ ๊ฐ„์˜ ๊ด€๊ณ„")
88
  plt.tight_layout()
89
 
90
+ # CSV ํŒŒ์ผ๋กœ ์ถ”์ฒœ ๊ฒฐ๊ณผ ๋ฐ˜ํ™˜
91
+ csv_output = save_recommendations_to_csv(recommendation_rows)
92
+
93
+ return "\n".join(recommendations), plt.gcf(), csv_output
94
 
95
  # ์˜ˆ์‹œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ํ•จ์ˆ˜
96
  def show_example_data():
 
122
  with gr.Column(scale=1):
123
  gr.Markdown("# HybridRAG ์‹œ์Šคํ…œ")
124
 
125
+ # ์ง์› ๋ฐ์ดํ„ฐ์™€ ํ”„๋กœ๊ทธ๋žจ ๋ฐ์ดํ„ฐ ์—…๋กœ๋“œ
126
+ employee_file = gr.File(label="์ง์› ๋ฐ์ดํ„ฐ ์—…๋กœ๋“œ")
127
+ program_file = gr.File(label="๊ต์œก ํ”„๋กœ๊ทธ๋žจ ๋ฐ์ดํ„ฐ ์—…๋กœ๋“œ")
128
+ analyze_button = gr.Button("๋ถ„์„ ์‹œ์ž‘")
129
+ output_text = gr.Textbox(label="๋ถ„์„ ๊ฒฐ๊ณผ")
130
+
131
+ # HR ๊ด€๋ฆฌ์ž๋Š” ์ง์› ํ”ผ๋“œ๋ฐฑ์„ ๊ธฐ๋กํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
132
+ feedback_input = gr.Radio(choices=["๋งŒ์กฑ", "๋ถˆ๋งŒ์กฑ"], label="์ง์› ํ”ผ๋“œ๋ฐฑ ๊ธฐ๋ก")
133
+
134
+ # ๋ถ„์„ ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ ์‹คํ–‰
135
+ analyze_button.click(analyze_data, inputs=[employee_file, program_file, feedback_input], outputs=[output_text])
 
 
 
 
 
 
 
 
 
 
 
 
 
136
 
137
  # ์˜ˆ์‹œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฏธ๋ฆฌ๋ณด๊ธฐ๋กœ ์ œ๊ณต
138
  example_button = gr.Button("์˜ˆ์‹œ ๋ฐ์ดํ„ฐ ๋ณด๊ธฐ")
 
140
  program_example_output = gr.DataFrame(label="๊ต์œก ํ”„๋กœ๊ทธ๋žจ ๋ฐ์ดํ„ฐ ์˜ˆ์‹œ")
141
  example_button.click(show_example_data, outputs=[employee_example_output, program_example_output])
142
 
143
+ with gr.Column(scale=2):
144
+ gr.Markdown("### ์ •๋ณด ํŒจ๋„")
145
+ gr.Markdown("์—…๋กœ๋“œ๋œ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ๋ถ„์„ ๋ฐ ๊ฒฐ๊ณผ๋ฅผ ์—ฌ๊ธฐ์— ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค.")
146
+
147
+ # ์‹œ๊ฐํ™” ์ฐจํŠธ ์ถœ๋ ฅ
148
+ chart_output = gr.Plot(label="์‹œ๊ฐํ™” ์ฐจํŠธ")
149
+
150
+ # ๋ถ„์„ ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ ์ฐจํŠธ ์—…๋ฐ์ดํŠธ ๋ฐ ์ถ”์ฒœ ๊ฒฐ๊ณผ ๋‹ค์šด๋กœ๋“œ ์ถ”๊ฐ€
151
+ csv_download = gr.File(label="์ถ”์ฒœ ๊ฒฐ๊ณผ ๋‹ค์šด๋กœ๋“œ")
152
+ analyze_button.click(analyze_data, inputs=[employee_file, program_file, feedback_input], outputs=[output_text, chart_output, csv_download])
153
+
154
  # Gradio ์ธํ„ฐํŽ˜์ด์Šค ์‹คํ–‰
155
  demo.launch()