soojeongcrystal commited on
Commit
160f223
ยท
verified ยท
1 Parent(s): 54e68ac

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -60
app.py CHANGED
@@ -7,52 +7,22 @@ import matplotlib.pyplot as plt
7
  import csv
8
  import io
9
  import matplotlib.font_manager as fm
10
- from neo4j import GraphDatabase
11
 
12
  # ํ•œ๊ตญ์–ด ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ KoSentence-BERT ๋ชจ๋ธ ๋กœ๋“œ
13
  model = SentenceTransformer('jhgan/ko-sbert-sts')
14
 
15
- # ๋‚˜๋ˆ”๋ฐ”๋ฅธ๊ณ ๋”• ํฐํŠธ ์„ค์ •
16
- font_path = "NanumBarunGothic.ttf" # Hugging Face ๋ฃจํŠธ์— ์ €์žฅ๋œ ํฐํŠธ ๊ฒฝ๋กœ
17
- fontprop = fm.FontProperties(fname=font_path)
18
- plt.rc('font', family=fontprop.get_name())
19
-
20
- # Neo4j ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—ฐ๊ฒฐ ํด๋ž˜์Šค
21
- class Neo4jConnection:
22
- def __init__(self, uri, user, pwd):
23
- self.driver = GraphDatabase.driver(uri, auth=(user, pwd))
24
-
25
- def close(self):
26
- self.driver.close()
27
-
28
- def query(self, query, parameters=None, db=None):
29
- session = None
30
- response = None
31
- try:
32
- session = self.driver.session(database=db) if db else self.driver.session()
33
- response = list(session.run(query, parameters))
34
- except Exception as e:
35
- print("Query failed:", e)
36
- finally:
37
- if session:
38
- session.close()
39
- return response
40
-
41
- # Neo4j ์—ฐ๊ฒฐ ์„ค์ •
42
- conn = Neo4jConnection(uri="bolt://localhost:7687", user="neo4j", pwd="your_password")
43
-
44
- # ์ถ”์ฒœ ๊ฒฐ๊ณผ๋ฅผ ์‹ค์ œ ํŒŒ์ผ๋กœ ์ €์žฅํ•˜๋Š” ํ•จ์ˆ˜
45
- def save_recommendations_to_file(recommendations):
46
- file_path = "recommendations.csv"
47
- with open(file_path, mode='w', newline='', encoding='utf-8') as file:
48
- writer = csv.writer(file)
49
- writer.writerow(["Employee ID", "Employee Name", "Recommended Programs"])
50
-
51
- # ์ถ”์ฒœ ๊ฒฐ๊ณผ CSV ํŒŒ์ผ์— ๊ธฐ๋ก
52
- for rec in recommendations:
53
- writer.writerow(rec)
54
-
55
- return file_path
56
 
57
  # ์ž๋™์œผ๋กœ ์—ด์„ ๋งค์นญํ•˜๋Š” ํ•จ์ˆ˜
58
  def auto_match_columns(df, required_cols):
@@ -92,7 +62,7 @@ def hybrid_rag(employee_file, program_file):
92
 
93
  error_msg, employee_cols, program_cols = validate_and_get_columns(employee_df, program_df)
94
  if error_msg:
95
- return error_msg, None, None, None
96
 
97
  employee_skills = employee_df[employee_cols["current_skills"]].tolist()
98
  program_skills = program_df[program_cols["skills_acquired"]].tolist()
@@ -118,18 +88,7 @@ def hybrid_rag(employee_file, program_file):
118
 
119
  recommendations.append(recommendation)
120
 
121
- # 2. GraphRAG: Neo4j์—์„œ ํ”„๋กœ๊ทธ๋žจ ์ถ”์ฒœ์„ ๊ฐ€์ ธ์˜ด
122
- query = """
123
- MATCH (e:Employee)-[:HAS_SKILL]->(p:Program)
124
- RETURN e.name AS employee_name, p.name AS program_name, p.duration AS duration
125
- """
126
- graph_rag_results = conn.query(query)
127
-
128
- # GraphRAG ๊ฒฐ๊ณผ ์ถ”๊ฐ€
129
- for record in graph_rag_results:
130
- for row in recommendation_rows:
131
- if record['employee_name'] == row[1]:
132
- row[2] += f", {record['program_name']} (GraphRAG)"
133
 
134
  G = nx.Graph()
135
  for employee in employee_df[employee_cols['employee_name']]:
@@ -145,12 +104,12 @@ def hybrid_rag(employee_file, program_file):
145
 
146
  plt.figure(figsize=(10, 8))
147
  pos = nx.spring_layout(G)
148
- nx.draw(G, pos, with_labels=True, node_color='lightblue', node_size=3000, font_size=10, font_weight='bold', edge_color='gray', fontproperties=fontprop)
149
- plt.title("์ง์›๊ณผ ํ”„๋กœ๊ทธ๋žจ ๊ฐ„์˜ ๊ด€๊ณ„", fontsize=14, fontweight='bold', fontproperties=fontprop)
150
  plt.tight_layout()
151
 
152
- # CSV ํŒŒ์ผ๋กœ ์ถ”์ฒœ ๊ฒฐ๊ณผ ๋ฐ˜ํ™˜
153
- csv_output = save_recommendations_to_file(recommendation_rows)
154
 
155
  # ๊ฒฐ๊ณผ ํ…Œ์ด๋ธ” ๋ฐ์ดํ„ฐํ”„๋ ˆ์ž„ ์ƒ์„ฑ
156
  result_df = pd.DataFrame(recommendation_rows, columns=["Employee ID", "Employee Name", "Recommended Programs"])
@@ -177,5 +136,4 @@ with gr.Blocks(css=".gradio-button {background-color: #007bff; color: white;} .g
177
  # ๋ถ„์„ ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ ํ…Œ์ด๋ธ”, ์ฐจํŠธ, ํŒŒ์ผ ๋‹ค์šด๋กœ๋“œ๋ฅผ ์—…๋ฐ์ดํŠธ
178
  analyze_button.click(hybrid_rag, inputs=[employee_file, program_file], outputs=[output_table, chart_output, csv_download])
179
 
180
- # Gradio ์ธํ„ฐํŽ˜์ด์Šค ์‹คํ–‰
181
  demo.launch()
 
7
  import csv
8
  import io
9
  import matplotlib.font_manager as fm
 
10
 
11
  # ํ•œ๊ตญ์–ด ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ KoSentence-BERT ๋ชจ๋ธ ๋กœ๋“œ
12
  model = SentenceTransformer('jhgan/ko-sbert-sts')
13
 
14
+ # ๋‚˜๋ˆ”๋ฐ”๋ฅธ๊ณ ๋”• ํฐํŠธ ์„ค์ • (ํ—ˆ๊น…ํŽ˜์ด์Šค ํ™˜๊ฒฝ์— ๋งž๊ฒŒ ์ˆ˜์ •)
15
+ plt.rc('font', family='NanumBarunGothic')
16
+
17
+ # Neo4j ๊ด€๋ จ ์ฝ”๋“œ ์ œ๊ฑฐ (ํ—ˆ๊น…ํŽ˜์ด์Šค ํ™˜๊ฒฝ์—์„œ๋Š” ์‚ฌ์šฉํ•˜๊ธฐ ์–ด๋ ค์›€)
18
+
19
+ # ์ถ”์ฒœ ๊ฒฐ๊ณผ๋ฅผ CSV ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜
20
+ def recommendations_to_csv(recommendations):
21
+ output = io.StringIO()
22
+ writer = csv.writer(output)
23
+ writer.writerow(["Employee ID", "Employee Name", "Recommended Programs"])
24
+ writer.writerows(recommendations)
25
+ return output.getvalue()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
 
27
  # ์ž๋™์œผ๋กœ ์—ด์„ ๋งค์นญํ•˜๋Š” ํ•จ์ˆ˜
28
  def auto_match_columns(df, required_cols):
 
62
 
63
  error_msg, employee_cols, program_cols = validate_and_get_columns(employee_df, program_df)
64
  if error_msg:
65
+ return error_msg, None, None
66
 
67
  employee_skills = employee_df[employee_cols["current_skills"]].tolist()
68
  program_skills = program_df[program_cols["skills_acquired"]].tolist()
 
88
 
89
  recommendations.append(recommendation)
90
 
91
+ # GraphRAG ๋ถ€๋ถ„ ์ œ๊ฑฐ (Neo4j ์‚ฌ์šฉ ๋ถˆ๊ฐ€)
 
 
 
 
 
 
 
 
 
 
 
92
 
93
  G = nx.Graph()
94
  for employee in employee_df[employee_cols['employee_name']]:
 
104
 
105
  plt.figure(figsize=(10, 8))
106
  pos = nx.spring_layout(G)
107
+ nx.draw(G, pos, with_labels=True, node_color='lightblue', node_size=3000, font_size=10, font_weight='bold', edge_color='gray')
108
+ plt.title("์ง์›๊ณผ ํ”„๋กœ๊ทธ๋žจ ๊ฐ„์˜ ๊ด€๊ณ„", fontsize=14, fontweight='bold')
109
  plt.tight_layout()
110
 
111
+ # CSV ๋ฌธ์ž์—ด๋กœ ์ถ”์ฒœ ๊ฒฐ๊ณผ ๋ฐ˜ํ™˜
112
+ csv_output = recommendations_to_csv(recommendation_rows)
113
 
114
  # ๊ฒฐ๊ณผ ํ…Œ์ด๋ธ” ๋ฐ์ดํ„ฐํ”„๋ ˆ์ž„ ์ƒ์„ฑ
115
  result_df = pd.DataFrame(recommendation_rows, columns=["Employee ID", "Employee Name", "Recommended Programs"])
 
136
  # ๋ถ„์„ ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ ํ…Œ์ด๋ธ”, ์ฐจํŠธ, ํŒŒ์ผ ๋‹ค์šด๋กœ๋“œ๋ฅผ ์—…๋ฐ์ดํŠธ
137
  analyze_button.click(hybrid_rag, inputs=[employee_file, program_file], outputs=[output_table, chart_output, csv_download])
138
 
 
139
  demo.launch()