Spaces:
Sleeping
Sleeping
soojeongcrystal
commited on
Commit
β’
2f93eb1
1
Parent(s):
558d69b
Update app.py
Browse files
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 |
-
|
14 |
youtube_columns = None
|
15 |
|
16 |
-
# CSV
|
17 |
-
def
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
return
|
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
|
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 |
-
|
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=
|
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
|
179 |
-
return gr.File(value=
|
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()
|