Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -16,15 +16,31 @@ plt.rc('font', family='NanumBarunGothic')
|
|
| 16 |
|
| 17 |
# ์ ์ญ ๋ณ์๋ก ๋ถ์ ๊ฒฐ๊ณผ ์ ์ฅ
|
| 18 |
global_recommendations = None
|
|
|
|
| 19 |
|
| 20 |
# CSV ๋ฌธ์์ด ์์ฑ ํจ์
|
| 21 |
-
def
|
| 22 |
output = io.StringIO()
|
| 23 |
writer = csv.writer(output)
|
| 24 |
writer.writerow(["Employee ID", "Employee Name", "Recommended Programs"])
|
| 25 |
-
|
|
|
|
| 26 |
return output.getvalue()
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
# ์ด ๋งค์นญ ํจ์
|
| 29 |
def auto_match_columns(df, required_cols):
|
| 30 |
matched_cols = {}
|
|
@@ -58,6 +74,7 @@ def validate_and_get_columns(employee_df, program_df):
|
|
| 58 |
# ์ง์ ๋ฐ์ดํฐ๋ฅผ ๋ถ์ํ์ฌ ๊ต์ก ํ๋ก๊ทธ๋จ์ ์ถ์ฒํ๊ณ , ํ
์ด๋ธ๊ณผ ๊ทธ๋ํ๋ฅผ ์์ฑํ๋ ํจ์
|
| 59 |
def hybrid_rag(employee_file, program_file):
|
| 60 |
global global_recommendations
|
|
|
|
| 61 |
|
| 62 |
# 1. VectorRAG: KoSentence-BERT๋ฅผ ์ด์ฉํ ์ ์ฌ๋ ๊ณ์ฐ
|
| 63 |
employee_df = pd.read_csv(employee_file.name)
|
|
@@ -105,19 +122,16 @@ def hybrid_rag(employee_file, program_file):
|
|
| 105 |
if similarities[i][j] > 0.5:
|
| 106 |
G.add_edge(employee[employee_cols['employee_name']], program[program_cols['program_name']])
|
| 107 |
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
# CSV ๋ฌธ์์ด๋ก ์ถ์ฒ ๊ฒฐ๊ณผ ๋ฐํ
|
| 115 |
-
csv_output = recommendations_to_csv(recommendation_rows)
|
| 116 |
|
| 117 |
# ๊ฒฐ๊ณผ ํ
์ด๋ธ ๋ฐ์ดํฐํ๋ ์ ์์ฑ
|
| 118 |
result_df = pd.DataFrame(recommendation_rows, columns=["Employee ID", "Employee Name", "Recommended Programs"])
|
| 119 |
|
| 120 |
-
return result_df,
|
| 121 |
|
| 122 |
# ์ฑํ
์๋ต ํจ์
|
| 123 |
def chat_response(message, history):
|
|
@@ -131,6 +145,13 @@ def chat_response(message, history):
|
|
| 131 |
|
| 132 |
return "์ฃ์กํฉ๋๋ค. ํด๋น ์ง์์ ์ ๋ณด๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค. ๋ค๋ฅธ ์ง์ ์ด๋ฆ์ ์
๋ ฅํด์ฃผ์ธ์."
|
| 133 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
# Gradio ๋ธ๋ก
|
| 135 |
with gr.Blocks(css=".gradio-button {background-color: #007bff; color: white;} .gradio-textbox {border-color: #6c757d;}") as demo:
|
| 136 |
gr.Markdown("<h1 style='text-align: center; color: #2c3e50;'>๐ผ HybridRAG ์์คํ
</h1>")
|
|
@@ -142,11 +163,11 @@ with gr.Blocks(css=".gradio-button {background-color: #007bff; color: white;} .g
|
|
| 142 |
program_file = gr.File(label="๊ต์ก ํ๋ก๊ทธ๋จ ๋ฐ์ดํฐ ์
๋ก๋", interactive=True)
|
| 143 |
analyze_button = gr.Button("๋ถ์ ์์", elem_classes="gradio-button")
|
| 144 |
output_table = gr.DataFrame(label="๋ถ์ ๊ฒฐ๊ณผ (ํ
์ด๋ธ)")
|
| 145 |
-
csv_download = gr.File(label="์ถ์ฒ ๊ฒฐ๊ณผ ๋ค์ด๋ก๋")
|
| 146 |
|
| 147 |
with gr.Column(scale=2, min_width=500):
|
| 148 |
gr.Markdown("<h3 style='color: #34495e;'>2. ๋ถ์ ๊ฒฐ๊ณผ ๋ฐ ์๊ฐํ</h3>")
|
| 149 |
-
chart_output = gr.
|
| 150 |
|
| 151 |
gr.Markdown("<h3 style='color: #34495e;'>3. ์ง์๋ณ ์ถ์ฒ ํ๋ก๊ทธ๋จ ํ์ธ</h3>")
|
| 152 |
chatbot = gr.Chatbot()
|
|
@@ -156,6 +177,9 @@ with gr.Blocks(css=".gradio-button {background-color: #007bff; color: white;} .g
|
|
| 156 |
# ๋ถ์ ๋ฒํผ ํด๋ฆญ ์ ํ
์ด๋ธ, ์ฐจํธ, ํ์ผ ๋ค์ด๋ก๋๋ฅผ ์
๋ฐ์ดํธ
|
| 157 |
analyze_button.click(hybrid_rag, inputs=[employee_file, program_file], outputs=[output_table, chart_output, csv_download])
|
| 158 |
|
|
|
|
|
|
|
|
|
|
| 159 |
# ์ฑํ
๊ธฐ๋ฅ
|
| 160 |
msg.submit(chat_response, [msg, chatbot], [chatbot])
|
| 161 |
clear.click(lambda: None, None, chatbot, queue=False)
|
|
|
|
| 16 |
|
| 17 |
# ์ ์ญ ๋ณ์๋ก ๋ถ์ ๊ฒฐ๊ณผ ์ ์ฅ
|
| 18 |
global_recommendations = None
|
| 19 |
+
global_csv_string = None
|
| 20 |
|
| 21 |
# CSV ๋ฌธ์์ด ์์ฑ ํจ์
|
| 22 |
+
def create_csv_string(recommendations):
|
| 23 |
output = io.StringIO()
|
| 24 |
writer = csv.writer(output)
|
| 25 |
writer.writerow(["Employee ID", "Employee Name", "Recommended Programs"])
|
| 26 |
+
for rec in recommendations:
|
| 27 |
+
writer.writerow(rec)
|
| 28 |
return output.getvalue()
|
| 29 |
|
| 30 |
+
# ์ฐจํธ ์์ฑ ํจ์
|
| 31 |
+
def create_chart(G):
|
| 32 |
+
plt.figure(figsize=(10, 8))
|
| 33 |
+
pos = nx.spring_layout(G)
|
| 34 |
+
nx.draw(G, pos, with_labels=True, node_color='lightblue', node_size=3000, font_size=10, font_weight='bold', edge_color='gray')
|
| 35 |
+
plt.title("์ง์๊ณผ ํ๋ก๊ทธ๋จ ๊ฐ์ ๊ด๊ณ", fontsize=14, fontweight='bold')
|
| 36 |
+
plt.tight_layout()
|
| 37 |
+
|
| 38 |
+
buf = io.BytesIO()
|
| 39 |
+
plt.savefig(buf, format='png')
|
| 40 |
+
buf.seek(0)
|
| 41 |
+
plt.close()
|
| 42 |
+
return buf
|
| 43 |
+
|
| 44 |
# ์ด ๋งค์นญ ํจ์
|
| 45 |
def auto_match_columns(df, required_cols):
|
| 46 |
matched_cols = {}
|
|
|
|
| 74 |
# ์ง์ ๋ฐ์ดํฐ๋ฅผ ๋ถ์ํ์ฌ ๊ต์ก ํ๋ก๊ทธ๋จ์ ์ถ์ฒํ๊ณ , ํ
์ด๋ธ๊ณผ ๊ทธ๋ํ๋ฅผ ์์ฑํ๋ ํจ์
|
| 75 |
def hybrid_rag(employee_file, program_file):
|
| 76 |
global global_recommendations
|
| 77 |
+
global global_csv_string
|
| 78 |
|
| 79 |
# 1. VectorRAG: KoSentence-BERT๋ฅผ ์ด์ฉํ ์ ์ฌ๋ ๊ณ์ฐ
|
| 80 |
employee_df = pd.read_csv(employee_file.name)
|
|
|
|
| 122 |
if similarities[i][j] > 0.5:
|
| 123 |
G.add_edge(employee[employee_cols['employee_name']], program[program_cols['program_name']])
|
| 124 |
|
| 125 |
+
# ์ฐจํธ ์์ฑ
|
| 126 |
+
chart_buffer = create_chart(G)
|
| 127 |
+
|
| 128 |
+
# CSV ๋ฌธ์์ด ์์ฑ
|
| 129 |
+
global_csv_string = create_csv_string(recommendation_rows)
|
|
|
|
|
|
|
|
|
|
| 130 |
|
| 131 |
# ๊ฒฐ๊ณผ ํ
์ด๋ธ ๋ฐ์ดํฐํ๋ ์ ์์ฑ
|
| 132 |
result_df = pd.DataFrame(recommendation_rows, columns=["Employee ID", "Employee Name", "Recommended Programs"])
|
| 133 |
|
| 134 |
+
return result_df, chart_buffer, gr.File.update(visible=True)
|
| 135 |
|
| 136 |
# ์ฑํ
์๋ต ํจ์
|
| 137 |
def chat_response(message, history):
|
|
|
|
| 145 |
|
| 146 |
return "์ฃ์กํฉ๋๋ค. ํด๋น ์ง์์ ์ ๋ณด๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค. ๋ค๋ฅธ ์ง์ ์ด๋ฆ์ ์
๋ ฅํด์ฃผ์ธ์."
|
| 147 |
|
| 148 |
+
# CSV ๋ค์ด๋ก๋ ํจ์
|
| 149 |
+
def download_csv():
|
| 150 |
+
global global_csv_string
|
| 151 |
+
if global_csv_string is None:
|
| 152 |
+
return gr.File.update(visible=False)
|
| 153 |
+
return gr.File.update(value=global_csv_string, visible=True, filename="recommendations.csv")
|
| 154 |
+
|
| 155 |
# Gradio ๋ธ๋ก
|
| 156 |
with gr.Blocks(css=".gradio-button {background-color: #007bff; color: white;} .gradio-textbox {border-color: #6c757d;}") as demo:
|
| 157 |
gr.Markdown("<h1 style='text-align: center; color: #2c3e50;'>๐ผ HybridRAG ์์คํ
</h1>")
|
|
|
|
| 163 |
program_file = gr.File(label="๊ต์ก ํ๋ก๊ทธ๋จ ๋ฐ์ดํฐ ์
๋ก๋", interactive=True)
|
| 164 |
analyze_button = gr.Button("๋ถ์ ์์", elem_classes="gradio-button")
|
| 165 |
output_table = gr.DataFrame(label="๋ถ์ ๊ฒฐ๊ณผ (ํ
์ด๋ธ)")
|
| 166 |
+
csv_download = gr.File(label="์ถ์ฒ ๊ฒฐ๊ณผ ๋ค์ด๋ก๋", visible=False)
|
| 167 |
|
| 168 |
with gr.Column(scale=2, min_width=500):
|
| 169 |
gr.Markdown("<h3 style='color: #34495e;'>2. ๋ถ์ ๊ฒฐ๊ณผ ๋ฐ ์๊ฐํ</h3>")
|
| 170 |
+
chart_output = gr.Image(label="์๊ฐํ ์ฐจํธ")
|
| 171 |
|
| 172 |
gr.Markdown("<h3 style='color: #34495e;'>3. ์ง์๋ณ ์ถ์ฒ ํ๋ก๊ทธ๋จ ํ์ธ</h3>")
|
| 173 |
chatbot = gr.Chatbot()
|
|
|
|
| 177 |
# ๋ถ์ ๋ฒํผ ํด๋ฆญ ์ ํ
์ด๋ธ, ์ฐจํธ, ํ์ผ ๋ค์ด๋ก๋๋ฅผ ์
๋ฐ์ดํธ
|
| 178 |
analyze_button.click(hybrid_rag, inputs=[employee_file, program_file], outputs=[output_table, chart_output, csv_download])
|
| 179 |
|
| 180 |
+
# CSV ๋ค์ด๋ก๋ ๋ฒํผ
|
| 181 |
+
csv_download.click(download_csv, inputs=[], outputs=[csv_download])
|
| 182 |
+
|
| 183 |
# ์ฑํ
๊ธฐ๋ฅ
|
| 184 |
msg.submit(chat_response, [msg, chatbot], [chatbot])
|
| 185 |
clear.click(lambda: None, None, chatbot, queue=False)
|