soojeongcrystal commited on
Commit
2f93eb1
β€’
1 Parent(s): 558d69b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -15
app.py CHANGED
@@ -4,23 +4,25 @@ 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
14
  youtube_columns = None
15
 
16
- # CSV λ¬Έμžμ—΄ 생성 ν•¨μˆ˜
17
- def create_csv_string(recommendations):
18
- output = io.StringIO()
19
- writer = csv.writer(output)
20
- writer.writerow(["Employee ID", "Employee Name", "Recommended Programs", "Recommended YouTube Content"])
21
- for rec in recommendations:
22
- writer.writerow(rec)
23
- return output.getvalue()
24
 
25
  # μ—΄ 맀칭 ν•¨μˆ˜
26
  def auto_match_columns(df, required_cols):
@@ -100,7 +102,7 @@ def match_youtube_content(program_skills, youtube_df, model):
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
104
 
105
  # 직원 및 ν”„λ‘œκ·Έλž¨ 데이터 λ‘œλ“œ
106
  employee_df = pd.read_csv(employee_file.name)
@@ -138,6 +140,10 @@ def hybrid_rag(employee_file, program_file, youtube_file, title_col, description
138
  if 'title' in youtube_df.columns and 'url' in youtube_df.columns:
139
  recommended_youtube.append(f"{youtube_df.iloc[idx]['title']} (URL: {youtube_df.iloc[idx]['url']})")
140
 
 
 
 
 
141
  if recommended_programs:
142
  recommendation = f"직원 {employee[employee_cols['employee_name']]}의 μΆ”μ²œ ν”„λ‘œκ·Έλž¨: {', '.join(recommended_programs)}"
143
  youtube_recommendation = f"μΆ”μ²œ 유튜브 μ½˜ν…μΈ : {', '.join(recommended_youtube)}" if recommended_youtube else "μΆ”μ²œν•  유튜브 μ½˜ν…μΈ κ°€ μ—†μŠ΅λ‹ˆλ‹€."
@@ -153,13 +159,13 @@ def hybrid_rag(employee_file, program_file, youtube_file, title_col, description
153
 
154
  global_recommendations = recommendation_rows
155
 
156
- # CSV λ¬Έμžμ—΄ 생성
157
- global_csv_string = create_csv_string(recommendation_rows)
158
 
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):
@@ -175,8 +181,8 @@ def chat_response(message, history):
175
 
176
  # CSV λ‹€μš΄λ‘œλ“œ ν•¨μˆ˜
177
  def download_csv():
178
- global global_csv_string
179
- return gr.File(value=global_csv_string, visible=True)
180
 
181
  # Gradio 블둝
182
  with gr.Blocks(css=".gradio-button {background-color: #007bff; color: white;} .gradio-textbox {border-color: #6c757d;}") as demo:
@@ -221,5 +227,14 @@ with gr.Blocks(css=".gradio-button {background-color: #007bff; color: white;} .g
221
  msg.submit(chat_response, [msg, chatbot], [chatbot])
222
  clear.click(lambda: None, None, chatbot, queue=False)
223
 
 
 
 
 
 
 
 
 
 
224
  # Gradio μΈν„°νŽ˜μ΄μŠ€ μ‹€ν–‰
225
  demo.launch()
 
4
  from sklearn.metrics.pairwise import cosine_similarity
5
  import csv
6
  import io
7
+ import tempfile
8
+ import os
9
 
10
  # ν•œκ΅­μ–΄ 처리λ₯Ό μœ„ν•œ KoSentence-BERT λͺ¨λΈ λ‘œλ“œ
11
  model = SentenceTransformer('jhgan/ko-sbert-sts')
12
 
13
  # μ „μ—­ λ³€μˆ˜
14
  global_recommendations = None
15
+ global_csv_file = None
16
  youtube_columns = None
17
 
18
+ # CSV 파일 생성 ν•¨μˆ˜
19
+ def create_csv_file(recommendations):
20
+ with tempfile.NamedTemporaryFile(mode='w+', delete=False, suffix='.csv', encoding='utf-8') as temp_file:
21
+ writer = csv.writer(temp_file)
22
+ writer.writerow(["Employee ID", "Employee Name", "Recommended Programs", "Recommended YouTube Content"])
23
+ for rec in recommendations:
24
+ writer.writerow(rec)
25
+ return temp_file.name
26
 
27
  # μ—΄ 맀칭 ν•¨μˆ˜
28
  def auto_match_columns(df, required_cols):
 
102
  # 직원 데이터λ₯Ό λΆ„μ„ν•˜μ—¬ ꡐ윑 ν”„λ‘œκ·Έλž¨μ„ μΆ”μ²œν•˜κ³ , ν…Œμ΄λΈ”μ„ μƒμ„±ν•˜λŠ” ν•¨μˆ˜
103
  def hybrid_rag(employee_file, program_file, youtube_file, title_col, description_col, url_col, upload_date_col):
104
  global global_recommendations
105
+ global global_csv_file
106
 
107
  # 직원 및 ν”„λ‘œκ·Έλž¨ 데이터 λ‘œλ“œ
108
  employee_df = pd.read_csv(employee_file.name)
 
140
  if 'title' in youtube_df.columns and 'url' in youtube_df.columns:
141
  recommended_youtube.append(f"{youtube_df.iloc[idx]['title']} (URL: {youtube_df.iloc[idx]['url']})")
142
 
143
+ # μΆ”μ²œ ν”„λ‘œκ·Έλž¨ 및 유튜브 μ½˜ν…μΈ  개수 μ œν•œ
144
+ recommended_programs = recommended_programs[:5] # μ΅œλŒ€ 5개 ν”„λ‘œκ·Έλž¨λ§Œ μΆ”μ²œ
145
+ recommended_youtube = recommended_youtube[:3] # μ΅œλŒ€ 3개 유튜브 μ½˜ν…μΈ λ§Œ μΆ”μ²œ
146
+
147
  if recommended_programs:
148
  recommendation = f"직원 {employee[employee_cols['employee_name']]}의 μΆ”μ²œ ν”„λ‘œκ·Έλž¨: {', '.join(recommended_programs)}"
149
  youtube_recommendation = f"μΆ”μ²œ 유튜브 μ½˜ν…μΈ : {', '.join(recommended_youtube)}" if recommended_youtube else "μΆ”μ²œν•  유튜브 μ½˜ν…μΈ κ°€ μ—†μŠ΅λ‹ˆλ‹€."
 
159
 
160
  global_recommendations = recommendation_rows
161
 
162
+ # CSV 파일 생성
163
+ global_csv_file = create_csv_file(recommendation_rows)
164
 
165
  # κ²°κ³Ό ν…Œμ΄λΈ” λ°μ΄ν„°ν”„λ ˆμž„ 생성
166
  result_df = pd.DataFrame(recommendation_rows, columns=["Employee ID", "Employee Name", "Recommended Programs", "Recommended YouTube Content"])
167
 
168
+ return result_df, gr.File(value=global_csv_file, visible=True), gr.Button(value="CSV λ‹€μš΄λ‘œλ“œ", visible=True)
169
 
170
  # μ±„νŒ… 응닡 ν•¨μˆ˜
171
  def chat_response(message, history):
 
181
 
182
  # CSV λ‹€μš΄λ‘œλ“œ ν•¨μˆ˜
183
  def download_csv():
184
+ global global_csv_file
185
+ return gr.File(value=global_csv_file, visible=True)
186
 
187
  # Gradio 블둝
188
  with gr.Blocks(css=".gradio-button {background-color: #007bff; color: white;} .gradio-textbox {border-color: #6c757d;}") as demo:
 
227
  msg.submit(chat_response, [msg, chatbot], [chatbot])
228
  clear.click(lambda: None, None, chatbot, queue=False)
229
 
230
+ # ν”„λ‘œκ·Έλž¨ μ’…λ£Œ μ‹œ μž„μ‹œ 파일 μ‚­μ œ
231
+ import atexit
232
+
233
+ @atexit.register
234
+ def cleanup():
235
+ global global_csv_file
236
+ if global_csv_file and os.path.exists(global_csv_file):
237
+ os.remove(global_csv_file)
238
+
239
  # Gradio μΈν„°νŽ˜μ΄μŠ€ μ‹€ν–‰
240
  demo.launch()