soojeongcrystal commited on
Commit
7b0fd85
ยท
verified ยท
1 Parent(s): afea222

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -13
app.py CHANGED
@@ -16,15 +16,31 @@ plt.rc('font', family='NanumBarunGothic')
16
 
17
  # ์ „์—ญ ๋ณ€์ˆ˜๋กœ ๋ถ„์„ ๊ฒฐ๊ณผ ์ €์žฅ
18
  global_recommendations = None
 
19
 
20
  # CSV ๋ฌธ์ž์—ด ์ƒ์„ฑ ํ•จ์ˆ˜
21
- def recommendations_to_csv(recommendations):
22
  output = io.StringIO()
23
  writer = csv.writer(output)
24
  writer.writerow(["Employee ID", "Employee Name", "Recommended Programs"])
25
- writer.writerows(recommendations)
 
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
- plt.figure(figsize=(10, 8))
109
- pos = nx.spring_layout(G)
110
- nx.draw(G, pos, with_labels=True, node_color='lightblue', node_size=3000, font_size=10, font_weight='bold', edge_color='gray')
111
- plt.title("์ง์›๊ณผ ํ”„๋กœ๊ทธ๋žจ ๊ฐ„์˜ ๊ด€๊ณ„", fontsize=14, fontweight='bold')
112
- plt.tight_layout()
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, plt.gcf(), csv_output
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.Plot(label="์‹œ๊ฐํ™” ์ฐจํŠธ")
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)