Update app.py
Browse files
app.py
CHANGED
|
@@ -53,23 +53,22 @@ async def end_session(session_id: str, user_email: str) -> str:
|
|
| 53 |
|
| 54 |
def chat_streaming(message: str, user_email: str, session_id: str,
|
| 55 |
web_search: bool, self_learning: bool, history: list) -> Generator:
|
| 56 |
-
"""์คํธ๋ฆฌ๋ฐ ์ฑํ
"""
|
| 57 |
if not user_email:
|
| 58 |
history = history or []
|
| 59 |
-
history.append(
|
| 60 |
yield history
|
| 61 |
return
|
| 62 |
|
| 63 |
if not session_id:
|
| 64 |
history = history or []
|
| 65 |
-
history.append(
|
| 66 |
yield history
|
| 67 |
return
|
| 68 |
|
| 69 |
# ์ฌ์ฉ์ ๋ฉ์์ง ์ถ๊ฐ
|
| 70 |
history = history or []
|
| 71 |
-
history.append(
|
| 72 |
-
history.append({"role": "assistant", "content": ""})
|
| 73 |
yield history
|
| 74 |
|
| 75 |
# ์คํธ๋ฆฌ๋ฐ ์๋ต
|
|
@@ -96,12 +95,12 @@ def chat_streaming(message: str, user_email: str, session_id: str,
|
|
| 96 |
chunk = json.loads(data)
|
| 97 |
content = chunk.get("content", "")
|
| 98 |
full_response += content
|
| 99 |
-
history[-1][
|
| 100 |
yield history
|
| 101 |
except json.JSONDecodeError:
|
| 102 |
continue
|
| 103 |
except Exception as e:
|
| 104 |
-
history[-1][
|
| 105 |
yield history
|
| 106 |
|
| 107 |
|
|
@@ -240,27 +239,6 @@ async def get_storage_info() -> str:
|
|
| 240 |
return f"โ ์ค๋ฅ: {str(e)}"
|
| 241 |
|
| 242 |
|
| 243 |
-
async def upload_file(file, session_id: str, user_email: str) -> str:
|
| 244 |
-
"""ํ์ผ ์
๋ก๋"""
|
| 245 |
-
if not file or not session_id or not user_email:
|
| 246 |
-
return "โ ํ์ผ, ์ธ์
ID, ์ด๋ฉ์ผ์ด ๋ชจ๋ ํ์ํฉ๋๋ค."
|
| 247 |
-
|
| 248 |
-
try:
|
| 249 |
-
async with httpx.AsyncClient(timeout=60.0) as client:
|
| 250 |
-
with open(file.name, "rb") as f:
|
| 251 |
-
files = {"file": (file.name.split("/")[-1], f)}
|
| 252 |
-
data = {"session_id": session_id, "user_email": user_email}
|
| 253 |
-
response = await client.post(
|
| 254 |
-
f"{API_BASE_URL}/upload/file",
|
| 255 |
-
files=files,
|
| 256 |
-
data=data
|
| 257 |
-
)
|
| 258 |
-
result = response.json()
|
| 259 |
-
return json.dumps(result, ensure_ascii=False, indent=2)
|
| 260 |
-
except Exception as e:
|
| 261 |
-
return f"โ ์ค๋ฅ: {str(e)}"
|
| 262 |
-
|
| 263 |
-
|
| 264 |
# Gradio UI ๊ตฌ์ฑ
|
| 265 |
with gr.Blocks(title="KT API ํ
์คํธ") as demo:
|
| 266 |
gr.Markdown("# ๐งช KT ์๋์ด AI ๋น์ API ํ
์คํธ")
|
|
@@ -284,7 +262,7 @@ with gr.Blocks(title="KT API ํ
์คํธ") as demo:
|
|
| 284 |
self_learning_check = gr.Checkbox(label="์๊ฐ ํ์ต ํ์ฑํ", value=True)
|
| 285 |
|
| 286 |
with gr.Column(scale=2):
|
| 287 |
-
chatbot = gr.Chatbot(label="๋ํ", height=400
|
| 288 |
msg_input = gr.Textbox(label="๋ฉ์์ง ์
๋ ฅ", placeholder="๋ฉ์์ง๋ฅผ ์
๋ ฅํ์ธ์...")
|
| 289 |
send_btn = gr.Button("์ ์ก", variant="primary")
|
| 290 |
|
|
@@ -377,17 +355,7 @@ with gr.Blocks(title="KT API ํ
์คํธ") as demo:
|
|
| 377 |
web_search_btn.click(web_search, inputs=[web_query, web_count], outputs=[web_result])
|
| 378 |
crawl_btn.click(crawl_url, inputs=[crawl_url_input], outputs=[web_result])
|
| 379 |
|
| 380 |
-
# ํญ 5:
|
| 381 |
-
with gr.TabItem("๐ ํ์ผ ์
๋ก๋"):
|
| 382 |
-
upload_email = gr.Textbox(label="์ด๋ฉ์ผ", placeholder="test@example.com")
|
| 383 |
-
upload_session = gr.Textbox(label="์ธ์
ID")
|
| 384 |
-
upload_file_input = gr.File(label="ํ์ผ ์ ํ (PDF, CSV, TXT)", file_types=[".pdf", ".csv", ".txt"])
|
| 385 |
-
upload_btn = gr.Button("์
๋ก๋", variant="primary")
|
| 386 |
-
upload_result = gr.Textbox(label="๊ฒฐ๊ณผ", lines=10)
|
| 387 |
-
|
| 388 |
-
upload_btn.click(upload_file, inputs=[upload_file_input, upload_session, upload_email], outputs=[upload_result])
|
| 389 |
-
|
| 390 |
-
# ํญ 6: ์์คํ
์ ๋ณด
|
| 391 |
with gr.TabItem("โ๏ธ ์์คํ
"):
|
| 392 |
storage_btn = gr.Button("์คํ ๋ฆฌ์ง ์ ๋ณด ์กฐํ", variant="primary")
|
| 393 |
storage_result = gr.Textbox(label="๊ฒฐ๊ณผ", lines=15)
|
|
|
|
| 53 |
|
| 54 |
def chat_streaming(message: str, user_email: str, session_id: str,
|
| 55 |
web_search: bool, self_learning: bool, history: list) -> Generator:
|
| 56 |
+
"""์คํธ๋ฆฌ๋ฐ ์ฑํ
- ํํ ํ์ ์ฌ์ฉ"""
|
| 57 |
if not user_email:
|
| 58 |
history = history or []
|
| 59 |
+
history.append(["", "โ ์ด๋ฉ์ผ์ ๋จผ์ ์
๋ ฅํด์ฃผ์ธ์."])
|
| 60 |
yield history
|
| 61 |
return
|
| 62 |
|
| 63 |
if not session_id:
|
| 64 |
history = history or []
|
| 65 |
+
history.append(["", "โ ์ธ์
์ ๋จผ์ ์์ฑํด์ฃผ์ธ์."])
|
| 66 |
yield history
|
| 67 |
return
|
| 68 |
|
| 69 |
# ์ฌ์ฉ์ ๋ฉ์์ง ์ถ๊ฐ
|
| 70 |
history = history or []
|
| 71 |
+
history.append([message, ""])
|
|
|
|
| 72 |
yield history
|
| 73 |
|
| 74 |
# ์คํธ๋ฆฌ๋ฐ ์๋ต
|
|
|
|
| 95 |
chunk = json.loads(data)
|
| 96 |
content = chunk.get("content", "")
|
| 97 |
full_response += content
|
| 98 |
+
history[-1][1] = full_response
|
| 99 |
yield history
|
| 100 |
except json.JSONDecodeError:
|
| 101 |
continue
|
| 102 |
except Exception as e:
|
| 103 |
+
history[-1][1] = f"โ ์ค๋ฅ: {str(e)}"
|
| 104 |
yield history
|
| 105 |
|
| 106 |
|
|
|
|
| 239 |
return f"โ ์ค๋ฅ: {str(e)}"
|
| 240 |
|
| 241 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 242 |
# Gradio UI ๊ตฌ์ฑ
|
| 243 |
with gr.Blocks(title="KT API ํ
์คํธ") as demo:
|
| 244 |
gr.Markdown("# ๐งช KT ์๋์ด AI ๋น์ API ํ
์คํธ")
|
|
|
|
| 262 |
self_learning_check = gr.Checkbox(label="์๊ฐ ํ์ต ํ์ฑํ", value=True)
|
| 263 |
|
| 264 |
with gr.Column(scale=2):
|
| 265 |
+
chatbot = gr.Chatbot(label="๋ํ", height=400)
|
| 266 |
msg_input = gr.Textbox(label="๋ฉ์์ง ์
๋ ฅ", placeholder="๋ฉ์์ง๋ฅผ ์
๋ ฅํ์ธ์...")
|
| 267 |
send_btn = gr.Button("์ ์ก", variant="primary")
|
| 268 |
|
|
|
|
| 355 |
web_search_btn.click(web_search, inputs=[web_query, web_count], outputs=[web_result])
|
| 356 |
crawl_btn.click(crawl_url, inputs=[crawl_url_input], outputs=[web_result])
|
| 357 |
|
| 358 |
+
# ํญ 5: ์์คํ
์ ๋ณด
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 359 |
with gr.TabItem("โ๏ธ ์์คํ
"):
|
| 360 |
storage_btn = gr.Button("์คํ ๋ฆฌ์ง ์ ๋ณด ์กฐํ", variant="primary")
|
| 361 |
storage_result = gr.Textbox(label="๊ฒฐ๊ณผ", lines=15)
|