soojeongcrystal commited on
Commit
558d69b
โ€ข
1 Parent(s): ca15903

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -49
app.py CHANGED
@@ -2,22 +2,12 @@ import gradio as gr
2
  import pandas as pd
3
  from sentence_transformers import SentenceTransformer
4
  from sklearn.metrics.pairwise import cosine_similarity
5
- import networkx as nx
6
- import matplotlib.pyplot as plt
7
  import csv
8
  import io
9
- import matplotlib.font_manager as fm
10
- from datetime import datetime, timedelta
11
 
12
  # ํ•œ๊ตญ์–ด ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ KoSentence-BERT ๋ชจ๋ธ ๋กœ๋“œ
13
  model = SentenceTransformer('jhgan/ko-sbert-sts')
14
 
15
- font_path = "./NanumBarunGothic.ttf"
16
- font_prop = fm.FontProperties(fname=font_path)
17
- plt.rcParams['font.family'] = 'NanumBarunGothic'
18
- plt.rcParams['font.sans-serif'] = ['NanumBarunGothic']
19
- fm.fontManager.addfont(font_path)
20
-
21
  # ์ „์—ญ ๋ณ€์ˆ˜
22
  global_recommendations = None
23
  global_csv_string = None
@@ -32,19 +22,6 @@ def create_csv_string(recommendations):
32
  writer.writerow(rec)
33
  return output.getvalue()
34
 
35
- def create_chart(G):
36
- plt.figure(figsize=(10, 8))
37
- pos = nx.spring_layout(G)
38
- nx.draw(G, pos, with_labels=True, node_color='lightblue', node_size=3000, font_size=10, font_weight='bold', edge_color='gray')
39
- plt.title("์ง์›๊ณผ ํ”„๋กœ๊ทธ๋žจ ๊ฐ„์˜ ๊ด€๊ณ„", fontsize=14, fontweight='bold')
40
- plt.tight_layout(pad=1.0)
41
-
42
- buf = io.BytesIO()
43
- plt.savefig(buf, format='png', bbox_inches='tight')
44
- buf.seek(0)
45
- plt.close()
46
- return buf
47
-
48
  # ์—ด ๋งค์นญ ํ•จ์ˆ˜
49
  def auto_match_columns(df, required_cols):
50
  matched_cols = {}
@@ -76,7 +53,6 @@ def validate_and_get_columns(employee_df, program_df):
76
  return None, employee_cols, program_cols
77
 
78
  # ์œ ํŠœ๋ธŒ ๋ฐ์ดํ„ฐ ์—ด ์„ ํƒ ํ•จ์ˆ˜
79
-
80
  def select_youtube_columns(youtube_file):
81
  global youtube_columns
82
  if youtube_file is None:
@@ -121,7 +97,7 @@ def match_youtube_content(program_skills, youtube_df, model):
121
  similarities = cosine_similarity(program_embeddings, youtube_embeddings)
122
  return similarities
123
 
124
- # ์ง์› ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„์„ํ•˜์—ฌ ๊ต์œก ํ”„๋กœ๊ทธ๋žจ์„ ์ถ”์ฒœํ•˜๊ณ , ํ…Œ์ด๋ธ”๊ณผ ๊ทธ๋ž˜ํ”„๋ฅผ ์ƒ์„ฑํ•˜๋Š” ํ•จ์ˆ˜
125
  def hybrid_rag(employee_file, program_file, youtube_file, title_col, description_col, url_col, upload_date_col):
126
  global global_recommendations
127
  global global_csv_string
@@ -132,7 +108,7 @@ def hybrid_rag(employee_file, program_file, youtube_file, title_col, description
132
 
133
  error_msg, employee_cols, program_cols = validate_and_get_columns(employee_df, program_df)
134
  if error_msg:
135
- return error_msg, None, None, None
136
 
137
  employee_skills = employee_df[employee_cols["current_skills"]].tolist()
138
  program_skills = program_df[program_cols["skills_acquired"]].tolist()
@@ -176,21 +152,6 @@ def hybrid_rag(employee_file, program_file, youtube_file, title_col, description
176
  recommendations.append(recommendation + "\n" + youtube_recommendation)
177
 
178
  global_recommendations = recommendation_rows
179
-
180
- G = nx.Graph()
181
- for employee in employee_df[employee_cols['employee_name']]:
182
- G.add_node(employee, type='employee')
183
-
184
- for program in program_df[program_cols['program_name']]:
185
- G.add_node(program, type='program')
186
-
187
- for i, employee in employee_df.iterrows():
188
- for j, program in program_df.iterrows():
189
- if similarities[i][j] > 0.5:
190
- G.add_edge(employee[employee_cols['employee_name']], program[program_cols['program_name']])
191
-
192
- # ์ฐจํŠธ ์ƒ์„ฑ
193
- chart_buffer = create_chart(G)
194
 
195
  # CSV ๋ฌธ์ž์—ด ์ƒ์„ฑ
196
  global_csv_string = create_csv_string(recommendation_rows)
@@ -198,7 +159,7 @@ def hybrid_rag(employee_file, program_file, youtube_file, title_col, description
198
  # ๊ฒฐ๊ณผ ํ…Œ์ด๋ธ” ๋ฐ์ดํ„ฐํ”„๋ ˆ์ž„ ์ƒ์„ฑ
199
  result_df = pd.DataFrame(recommendation_rows, columns=["Employee ID", "Employee Name", "Recommended Programs", "Recommended YouTube Content"])
200
 
201
- return result_df, chart_buffer, gr.File(value=global_csv_string, visible=True), gr.Button(value="CSV ๋‹ค์šด๋กœ๋“œ", visible=True)
202
 
203
  # ์ฑ„ํŒ… ์‘๋‹ต ํ•จ์ˆ˜
204
  def chat_response(message, history):
@@ -241,20 +202,16 @@ with gr.Blocks(css=".gradio-button {background-color: #007bff; color: white;} .g
241
  csv_download = gr.File(label="์ถ”์ฒœ ๊ฒฐ๊ณผ ๋‹ค์šด๋กœ๋“œ", visible=False)
242
  download_button = gr.Button("CSV ๋‹ค์šด๋กœ๋“œ", visible=False)
243
 
244
- with gr.Column(scale=2, min_width=500):
245
- gr.Markdown("<h3 style='color: #34495e;'>2. ๋ถ„์„ ๊ฒฐ๊ณผ ๋ฐ ์‹œ๊ฐํ™”</h3>")
246
- chart_output = gr.Image(label="์‹œ๊ฐํ™” ์ฐจํŠธ")
247
-
248
- gr.Markdown("<h3 style='color: #34495e;'>3. ์ง์›๋ณ„ ์ถ”์ฒœ ํ”„๋กœ๊ทธ๋žจ ๋ฐ ์œ ํŠœ๋ธŒ ์ฝ˜ํ…์ธ  ํ™•์ธ</h3>")
249
  chatbot = gr.Chatbot()
250
  msg = gr.Textbox(label="์ง์› ์ด๋ฆ„์„ ์ž…๋ ฅํ•˜์„ธ์š”")
251
  clear = gr.Button("๋Œ€ํ™” ๋‚ด์—ญ ์ง€์šฐ๊ธฐ")
252
 
253
- # ๋ถ„์„ ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ ํ…Œ์ด๋ธ”, ์ฐจํŠธ, ํŒŒ์ผ ๋‹ค์šด๋กœ๋“œ๋ฅผ ์—…๋ฐ์ดํŠธ
254
  analyze_button.click(
255
  hybrid_rag,
256
  inputs=[employee_file, program_file, youtube_file, title_col, description_col, url_col, upload_date_col],
257
- outputs=[output_table, chart_output, csv_download, download_button]
258
  )
259
 
260
  # CSV ๋‹ค์šด๋กœ๋“œ ๋ฒ„ํŠผ
 
2
  import pandas as pd
3
  from sentence_transformers import SentenceTransformer
4
  from sklearn.metrics.pairwise import cosine_similarity
 
 
5
  import csv
6
  import io
 
 
7
 
8
  # ํ•œ๊ตญ์–ด ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ KoSentence-BERT ๋ชจ๋ธ ๋กœ๋“œ
9
  model = SentenceTransformer('jhgan/ko-sbert-sts')
10
 
 
 
 
 
 
 
11
  # ์ „์—ญ ๋ณ€์ˆ˜
12
  global_recommendations = None
13
  global_csv_string = None
 
22
  writer.writerow(rec)
23
  return output.getvalue()
24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  # ์—ด ๋งค์นญ ํ•จ์ˆ˜
26
  def auto_match_columns(df, required_cols):
27
  matched_cols = {}
 
53
  return None, employee_cols, program_cols
54
 
55
  # ์œ ํŠœ๋ธŒ ๋ฐ์ดํ„ฐ ์—ด ์„ ํƒ ํ•จ์ˆ˜
 
56
  def select_youtube_columns(youtube_file):
57
  global youtube_columns
58
  if youtube_file is None:
 
97
  similarities = cosine_similarity(program_embeddings, youtube_embeddings)
98
  return similarities
99
 
100
+ # ์ง์› ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„์„ํ•˜์—ฌ ๊ต์œก ํ”„๋กœ๊ทธ๋žจ์„ ์ถ”์ฒœํ•˜๊ณ , ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•˜๋Š” ํ•จ์ˆ˜
101
  def hybrid_rag(employee_file, program_file, youtube_file, title_col, description_col, url_col, upload_date_col):
102
  global global_recommendations
103
  global global_csv_string
 
108
 
109
  error_msg, employee_cols, program_cols = validate_and_get_columns(employee_df, program_df)
110
  if error_msg:
111
+ return error_msg, None, None
112
 
113
  employee_skills = employee_df[employee_cols["current_skills"]].tolist()
114
  program_skills = program_df[program_cols["skills_acquired"]].tolist()
 
152
  recommendations.append(recommendation + "\n" + youtube_recommendation)
153
 
154
  global_recommendations = recommendation_rows
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
155
 
156
  # CSV ๋ฌธ์ž์—ด ์ƒ์„ฑ
157
  global_csv_string = create_csv_string(recommendation_rows)
 
159
  # ๊ฒฐ๊ณผ ํ…Œ์ด๋ธ” ๋ฐ์ดํ„ฐํ”„๋ ˆ์ž„ ์ƒ์„ฑ
160
  result_df = pd.DataFrame(recommendation_rows, columns=["Employee ID", "Employee Name", "Recommended Programs", "Recommended YouTube Content"])
161
 
162
+ return result_df, gr.File(value=global_csv_string, visible=True), gr.Button(value="CSV ๋‹ค์šด๋กœ๋“œ", visible=True)
163
 
164
  # ์ฑ„ํŒ… ์‘๋‹ต ํ•จ์ˆ˜
165
  def chat_response(message, history):
 
202
  csv_download = gr.File(label="์ถ”์ฒœ ๊ฒฐ๊ณผ ๋‹ค์šด๋กœ๋“œ", visible=False)
203
  download_button = gr.Button("CSV ๋‹ค์šด๋กœ๋“œ", visible=False)
204
 
205
+ gr.Markdown("<h3 style='color: #34495e;'>2. ์ง์›๋ณ„ ์ถ”์ฒœ ํ”„๋กœ๊ทธ๋žจ ๋ฐ ์œ ํŠœ๋ธŒ ์ฝ˜ํ…์ธ  ํ™•์ธ</h3>")
 
 
 
 
206
  chatbot = gr.Chatbot()
207
  msg = gr.Textbox(label="์ง์› ์ด๋ฆ„์„ ์ž…๋ ฅํ•˜์„ธ์š”")
208
  clear = gr.Button("๋Œ€ํ™” ๋‚ด์—ญ ์ง€์šฐ๊ธฐ")
209
 
210
+ # ๋ถ„์„ ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ ํ…Œ์ด๋ธ”, ํŒŒ์ผ ๋‹ค์šด๋กœ๋“œ๋ฅผ ์—…๋ฐ์ดํŠธ
211
  analyze_button.click(
212
  hybrid_rag,
213
  inputs=[employee_file, program_file, youtube_file, title_col, description_col, url_col, upload_date_col],
214
+ outputs=[output_table, csv_download, download_button]
215
  )
216
 
217
  # CSV ๋‹ค์šด๋กœ๋“œ ๋ฒ„ํŠผ