Update app.py
Browse files
app.py
CHANGED
@@ -42,7 +42,7 @@ def respond(
|
|
42 |
parquet_data: Dict = None
|
43 |
) -> str:
|
44 |
# μμ€ν
ν둬ννΈ μ€μ
|
45 |
-
system_prefix = """λ°λμ νκΈλ‘ λ΅λ³ν κ². λλ μ£Όμ΄μ§
|
46 |
|
47 |
# Parquet λ°μ΄ν° ν¬ν¨
|
48 |
if parquet_data:
|
@@ -54,7 +54,7 @@ def respond(
|
|
54 |
messages = [{"role": "system", "content": system_prefix}]
|
55 |
for chat in history:
|
56 |
messages.append({"role": chat['role'], "content": chat['content']})
|
57 |
-
|
58 |
|
59 |
try:
|
60 |
# λͺ¨λΈμ λ©μμ§ μ μ‘ λ° μλ΅ λ°κΈ°
|
@@ -155,38 +155,13 @@ textarea, input[type="text"] {
|
|
155 |
with gr.Blocks(css=css) as demo:
|
156 |
gr.Markdown("# My RAG: LLMμ΄ λλ§μ λ°μ΄ν°λ‘ νμ΅ν μ½ν
μΈ μμ±/λ΅λ³", elem_id="initial-description")
|
157 |
gr.Markdown(
|
158 |
-
"### 1) λλ§μ λ°μ΄ν°λ₯Ό ν
μ€νΈλ‘
|
159 |
-
"###
|
160 |
-
"### Tip) 'μμ 'λ₯Ό ν΅ν΄ λ€μν νμ© λ°©λ²μ 체ννκ³ μμ©ν΄ 보μΈμ.",
|
161 |
elem_id="initial-description"
|
162 |
)
|
163 |
|
164 |
# 첫 λ²μ§Έ ν: μ±λ΄ λ°μ΄ν° μ
λ‘λ (ν μ΄λ¦ λ³κ²½: "My λ°μ΄ν°μ
+LLM")
|
165 |
with gr.Tab("My λ°μ΄ν°μ
+LLM"):
|
166 |
-
gr.Markdown("### Parquet νμΌ μ
λ‘λ λ° μ§λ¬ΈνκΈ°")
|
167 |
-
with gr.Row():
|
168 |
-
with gr.Column():
|
169 |
-
parquet_upload = gr.File(
|
170 |
-
label="Parquet νμΌ μ
λ‘λ", type="filepath", elem_id="parquet-upload-area"
|
171 |
-
)
|
172 |
-
parquet_upload_button = gr.Button("μ
λ‘λ")
|
173 |
-
parquet_upload_status = gr.Textbox(label="μ
λ‘λ μν", interactive=False)
|
174 |
-
parquet_preview_chat = gr.Markdown(label="Parquet νμΌ λ―Έλ¦¬λ³΄κΈ°")
|
175 |
-
parquet_data_state = gr.State()
|
176 |
-
|
177 |
-
def handle_parquet_upload(file_path: str):
|
178 |
-
message, parquet_content, parquet_json = upload_parquet(file_path)
|
179 |
-
if parquet_json:
|
180 |
-
return message, parquet_content, parquet_json
|
181 |
-
else:
|
182 |
-
return message, "", ""
|
183 |
-
|
184 |
-
parquet_upload_button.click(
|
185 |
-
handle_parquet_upload,
|
186 |
-
inputs=parquet_upload,
|
187 |
-
outputs=[parquet_upload_status, parquet_preview_chat, parquet_data_state]
|
188 |
-
)
|
189 |
-
|
190 |
gr.Markdown("### LLMκ³Ό λννκΈ°")
|
191 |
chatbot_data_upload = gr.Chatbot(label="μ±λ΄", type="messages", elem_id="chatbot-data-upload")
|
192 |
msg_data_upload = gr.Textbox(label="λ©μμ§ μ
λ ₯", placeholder="μ¬κΈ°μ λ©μμ§λ₯Ό μ
λ ₯νμΈμ...")
|
@@ -209,6 +184,8 @@ with gr.Blocks(css=css) as demo:
|
|
209 |
):
|
210 |
history = history or []
|
211 |
try:
|
|
|
|
|
212 |
# μλ΅ μμ±
|
213 |
response_gen = respond(
|
214 |
message, history, system_message, max_tokens, temperature, top_p, parquet_data
|
@@ -218,16 +195,13 @@ with gr.Blocks(css=css) as demo:
|
|
218 |
partial_response = partial
|
219 |
# λν λ΄μ μ
λ°μ΄νΈ
|
220 |
display_history = history + [
|
221 |
-
{"role": "user", "content": message},
|
222 |
{"role": "assistant", "content": partial_response}
|
223 |
]
|
224 |
yield display_history, ""
|
225 |
-
#
|
226 |
-
history.append({"role": "user", "content": message})
|
227 |
history.append({"role": "assistant", "content": partial_response})
|
228 |
except Exception as e:
|
229 |
response = f"μΆλ‘ μ€ μ€λ₯κ° λ°μνμ΅λλ€: {str(e)}"
|
230 |
-
history.append({"role": "user", "content": message})
|
231 |
history.append({"role": "assistant", "content": response})
|
232 |
yield history, ""
|
233 |
|
@@ -240,7 +214,7 @@ with gr.Blocks(css=css) as demo:
|
|
240 |
max_tokens,
|
241 |
temperature,
|
242 |
top_p,
|
243 |
-
parquet_data_state
|
244 |
],
|
245 |
outputs=[chatbot_data_upload, msg_data_upload],
|
246 |
queue=True
|
@@ -251,16 +225,41 @@ with gr.Blocks(css=css) as demo:
|
|
251 |
gr.Examples(
|
252 |
examples=[
|
253 |
["μ
λ‘λλ λ°μ΄ν°μ
μ λν΄ μμ½ μ€λͺ
νλΌ."],
|
254 |
-
["μ
λ‘λλ λ°μ΄ν°μ
νμΌμ νμ΅ λ°μ΄ν°λ‘ νμ©νμ¬, λ³Έ μλΉμ€λ₯Ό SEO μ΅μ ννμ¬ λΈλ‘κ·Έ ν¬μ€νΈ(κ°μ, λ°°κ²½ λ° νμμ±, κΈ°μ‘΄ μ μ¬
|
255 |
["μ
λ‘λλ λ°μ΄ν°μ
νμΌμ νμ΅ λ°μ΄ν°λ‘ νμ©νμ¬, μ¬μ© λ°©λ²κ³Ό μ°¨λ³μ , νΉμ§, κ°μ μ μ€μ¬μΌλ‘ 4000 ν ν° μ΄μ μ νλΈ μμ μ€ν¬λ¦½νΈ ννλ‘ μμ±νλΌ"],
|
256 |
["μ
λ‘λλ λ°μ΄ν°μ
νμΌμ νμ΅ λ°μ΄ν°λ‘ νμ©νμ¬, μ ν μμΈ νμ΄μ§ νμμ λ΄μ©μ 4000 ν ν° μ΄μ μμΈν μ€λͺ
νλΌ"],
|
257 |
["μ
λ‘λλ λ°μ΄ν°μ
νμΌμ νμ΅ λ°μ΄ν°λ‘ νμ©νμ¬, FAQ 20건μ μμΈνκ² μμ±νλΌ. 4000ν ν° μ΄μ μ¬μ©νλΌ."],
|
258 |
-
["μ
λ‘λλ λ°μ΄ν°μ
νμΌμ νμ΅ λ°μ΄ν°λ‘ νμ©νμ¬, νΉν μΆμμ νμ©ν κΈ°μ λ°
|
259 |
],
|
260 |
inputs=msg_data_upload,
|
261 |
label="μμ μ ν",
|
262 |
)
|
263 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
264 |
# λ λ²μ§Έ ν: λ°μ΄ν° λ³ν (ν μ΄λ¦ λ³κ²½: "CSV to My λ°μ΄ν°μ
")
|
265 |
with gr.Tab("CSV to My λ°μ΄ν°μ
"):
|
266 |
gr.Markdown("### CSV νμΌ μ
λ‘λ λ° Parquet λ³ν")
|
@@ -318,4 +317,3 @@ with gr.Blocks(css=css) as demo:
|
|
318 |
|
319 |
if __name__ == "__main__":
|
320 |
demo.launch()
|
321 |
-
|
|
|
42 |
parquet_data: Dict = None
|
43 |
) -> str:
|
44 |
# μμ€ν
ν둬ννΈ μ€μ
|
45 |
+
system_prefix = """λ°λμ νκΈλ‘ λ΅λ³ν κ². λλ μ£Όμ΄μ§ λ°μ΄ν°λ₯Ό κΈ°λ°μΌλ‘ "μλΉμ€ μ¬μ© μ€λͺ
λ° μλ΄, Q&Aλ₯Ό νλ μν μ΄λ€". μμ£Ό μΉμ νκ³ μμΈνκ² Markdown νμμΌλ‘ μμ±νλΌ. λλ λ°μ΄ν°λ₯Ό κΈ°λ°μΌλ‘ μ¬μ© μ€λͺ
λ° μ§μ μλ΅μ μ§ννλ©°, μ΄μ©μμκ² λμμ μ£Όμ΄μΌ νλ€. μ΄μ©μκ° κΆκΈν΄ν λ§ν λ΄μ©μ μΉμ νκ² μλ €μ£Όλλ‘ νλΌ. λ°μ΄ν° μ 체 λ΄μ©μ λν΄μλ 보μμ μ μ§νκ³ , λ―Όκ°ν μ 보λ 곡κ°νμ§ λ§λΌ."""
|
46 |
|
47 |
# Parquet λ°μ΄ν° ν¬ν¨
|
48 |
if parquet_data:
|
|
|
54 |
messages = [{"role": "system", "content": system_prefix}]
|
55 |
for chat in history:
|
56 |
messages.append({"role": chat['role'], "content": chat['content']})
|
57 |
+
# λ§μ§λ§ λ©μμ§λ μ΄λ―Έ historyμ μμΌλ―λ‘ μΆκ°νμ§ μμ΅λλ€.
|
58 |
|
59 |
try:
|
60 |
# λͺ¨λΈμ λ©μμ§ μ μ‘ λ° μλ΅ λ°κΈ°
|
|
|
155 |
with gr.Blocks(css=css) as demo:
|
156 |
gr.Markdown("# My RAG: LLMμ΄ λλ§μ λ°μ΄ν°λ‘ νμ΅ν μ½ν
μΈ μμ±/λ΅λ³", elem_id="initial-description")
|
157 |
gr.Markdown(
|
158 |
+
"### 1) λλ§μ λ°μ΄ν°λ₯Ό ν
μ€νΈλ‘ μ
λ ₯ λλ CSV μ
λ‘λνμ¬ Parquet ν¬λ§· λ°μ΄ν°μ
μλ λ³ν 2) Parquet ν¬λ§· λ°μ΄ν°μ
μ μ
λ‘λνλ©΄, LLMμ΄ λ§μΆ€ νμ΅ λ°μ΄ν°λ‘ νμ©νμ¬ μλ΅\n"
|
159 |
+
"### Tip) 'μμ 'λ₯Ό ν΅ν΄ λ€μν νμ© λ°©λ²μ 체ννκ³ μμ©ν΄ 보μΈμ, λ°μ΄ν°μ
μ
λ‘λμ 미리보기λ 10κ±΄λ§ μΆλ ₯",
|
|
|
160 |
elem_id="initial-description"
|
161 |
)
|
162 |
|
163 |
# 첫 λ²μ§Έ ν: μ±λ΄ λ°μ΄ν° μ
λ‘λ (ν μ΄λ¦ λ³κ²½: "My λ°μ΄ν°μ
+LLM")
|
164 |
with gr.Tab("My λ°μ΄ν°μ
+LLM"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
165 |
gr.Markdown("### LLMκ³Ό λννκΈ°")
|
166 |
chatbot_data_upload = gr.Chatbot(label="μ±λ΄", type="messages", elem_id="chatbot-data-upload")
|
167 |
msg_data_upload = gr.Textbox(label="λ©μμ§ μ
λ ₯", placeholder="μ¬κΈ°μ λ©μμ§λ₯Ό μ
λ ₯νμΈμ...")
|
|
|
184 |
):
|
185 |
history = history or []
|
186 |
try:
|
187 |
+
# μ¬μ©μμ λ©μμ§λ₯Ό νμ€ν 리μ μΆκ°
|
188 |
+
history.append({"role": "user", "content": message})
|
189 |
# μλ΅ μμ±
|
190 |
response_gen = respond(
|
191 |
message, history, system_message, max_tokens, temperature, top_p, parquet_data
|
|
|
195 |
partial_response = partial
|
196 |
# λν λ΄μ μ
λ°μ΄νΈ
|
197 |
display_history = history + [
|
|
|
198 |
{"role": "assistant", "content": partial_response}
|
199 |
]
|
200 |
yield display_history, ""
|
201 |
+
# μ΄μμ€ν΄νΈμ μλ΅μ νμ€ν 리μ μΆκ°
|
|
|
202 |
history.append({"role": "assistant", "content": partial_response})
|
203 |
except Exception as e:
|
204 |
response = f"μΆλ‘ μ€ μ€λ₯κ° λ°μνμ΅λλ€: {str(e)}"
|
|
|
205 |
history.append({"role": "assistant", "content": response})
|
206 |
yield history, ""
|
207 |
|
|
|
214 |
max_tokens,
|
215 |
temperature,
|
216 |
top_p,
|
217 |
+
gr.State(), # parquet_data_stateλ μλμμ μ
λ°μ΄νΈλ©λλ€.
|
218 |
],
|
219 |
outputs=[chatbot_data_upload, msg_data_upload],
|
220 |
queue=True
|
|
|
225 |
gr.Examples(
|
226 |
examples=[
|
227 |
["μ
λ‘λλ λ°μ΄ν°μ
μ λν΄ μμ½ μ€λͺ
νλΌ."],
|
228 |
+
["μ
λ‘λλ λ°μ΄ν°μ
νμΌμ νμ΅ λ°μ΄ν°λ‘ νμ©νμ¬, λ³Έ μλΉμ€λ₯Ό SEO μ΅μ ννμ¬ λΈλ‘κ·Έ ν¬μ€νΈ(κ°μ, λ°°κ²½ λ° νμμ±, κΈ°μ‘΄ μ μ¬ μ ν/μλΉμ€μ λΉκ΅νμ¬ νΉμ₯μ , νμ©μ², κ°μΉ, κΈ°λν¨κ³Ό, κ²°λ‘ μ ν¬ν¨)λ‘ 4000 ν ν° μ΄μ μμ±νλΌ"],
|
229 |
["μ
λ‘λλ λ°μ΄ν°μ
νμΌμ νμ΅ λ°μ΄ν°λ‘ νμ©νμ¬, μ¬μ© λ°©λ²κ³Ό μ°¨λ³μ , νΉμ§, κ°μ μ μ€μ¬μΌλ‘ 4000 ν ν° μ΄μ μ νλΈ μμ μ€ν¬λ¦½νΈ ννλ‘ μμ±νλΌ"],
|
230 |
["μ
λ‘λλ λ°μ΄ν°μ
νμΌμ νμ΅ λ°μ΄ν°λ‘ νμ©νμ¬, μ ν μμΈ νμ΄μ§ νμμ λ΄μ©μ 4000 ν ν° μ΄μ μμΈν μ€λͺ
νλΌ"],
|
231 |
["μ
λ‘λλ λ°μ΄ν°μ
νμΌμ νμ΅ λ°μ΄ν°λ‘ νμ©νμ¬, FAQ 20건μ μμΈνκ² μμ±νλΌ. 4000ν ν° μ΄μ μ¬μ©νλΌ."],
|
232 |
+
["μ
λ‘λλ λ°μ΄ν°μ
νμΌμ νμ΅ λ°μ΄ν°λ‘ νμ©νμ¬, νΉν μΆμμ νμ©ν κΈ°μ λ° λΉμ¦λμ€ λͺ¨λΈ μΈ‘λ©΄μ ν¬ν¨νμ¬ νΉν μΆμμ ꡬμ±μ λ§κ² νμ μ μΈ μ°½μ λ°λͺ
λ΄μ©μ μ€μ¬μΌλ‘ 4000 ν ν° μ΄μ μμ±νλΌ."],
|
233 |
],
|
234 |
inputs=msg_data_upload,
|
235 |
label="μμ μ ν",
|
236 |
)
|
237 |
|
238 |
+
# Parquet νμΌ μ
λ‘λλ₯Ό νλ©΄ νλ¨μΌλ‘ μ΄λ
|
239 |
+
gr.Markdown("### Parquet νμΌ μ
λ‘λ")
|
240 |
+
with gr.Row():
|
241 |
+
with gr.Column():
|
242 |
+
parquet_upload = gr.File(
|
243 |
+
label="Parquet νμΌ μ
λ‘λ", type="filepath", elem_id="parquet-upload-area"
|
244 |
+
)
|
245 |
+
parquet_upload_button = gr.Button("μ
λ‘λ")
|
246 |
+
parquet_upload_status = gr.Textbox(label="μ
λ‘λ μν", interactive=False)
|
247 |
+
parquet_preview_chat = gr.Markdown(label="Parquet νμΌ λ―Έλ¦¬λ³΄κΈ°")
|
248 |
+
parquet_data_state = gr.State()
|
249 |
+
|
250 |
+
def handle_parquet_upload(file_path: str):
|
251 |
+
message, parquet_content, parquet_json = upload_parquet(file_path)
|
252 |
+
if parquet_json:
|
253 |
+
return message, parquet_content, parquet_json
|
254 |
+
else:
|
255 |
+
return message, "", ""
|
256 |
+
|
257 |
+
parquet_upload_button.click(
|
258 |
+
handle_parquet_upload,
|
259 |
+
inputs=parquet_upload,
|
260 |
+
outputs=[parquet_upload_status, parquet_preview_chat, parquet_data_state]
|
261 |
+
)
|
262 |
+
|
263 |
# λ λ²μ§Έ ν: λ°μ΄ν° λ³ν (ν μ΄λ¦ λ³κ²½: "CSV to My λ°μ΄ν°μ
")
|
264 |
with gr.Tab("CSV to My λ°μ΄ν°μ
"):
|
265 |
gr.Markdown("### CSV νμΌ μ
λ‘λ λ° Parquet λ³ν")
|
|
|
317 |
|
318 |
if __name__ == "__main__":
|
319 |
demo.launch()
|
|