jeongsoo commited on
Commit
71e4abb
ยท
1 Parent(s): 65c366f
Files changed (1) hide show
  1. app.py +0 -194
app.py CHANGED
@@ -1413,200 +1413,6 @@ class AutoRAGChatApp:
1413
 
1414
  return file_info
1415
 
1416
- def launch_app(self) -> None:
1417
- """
1418
- Gradio ์•ฑ ์‹คํ–‰
1419
- """
1420
- try:
1421
- import gradio as gr
1422
- except ImportError:
1423
- logger.error("Gradio ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. pip install gradio๋กœ ์„ค์น˜ํ•˜์„ธ์š”.")
1424
- print("Gradio ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. pip install gradio๋กœ ์„ค์น˜ํ•˜์„ธ์š”.")
1425
- return
1426
-
1427
- try:
1428
- with gr.Blocks(title="PDF ๋ฌธ์„œ ๊ธฐ๋ฐ˜ RAG ์ฑ—๋ด‡") as app:
1429
- gr.Markdown("# PDF ๋ฌธ์„œ ๊ธฐ๋ฐ˜ RAG ์ฑ—๋ด‡")
1430
-
1431
- # LLM ๋ชจ๋ธ ์ •๋ณด ๊ฐ€์ ธ์˜ค๊ธฐ
1432
- from config import LLM_MODEL, USE_OPENAI, USE_DEEPSEEK
1433
-
1434
- # ์‚ฌ์šฉ ์ค‘์ธ ๋ชจ๋ธ ํ‘œ์‹œ
1435
- model_type = "DeepSeek" if USE_DEEPSEEK else "OpenAI" if USE_OPENAI else "Ollama"
1436
- gr.Markdown(f"* ์‚ฌ์šฉ ์ค‘์ธ LLM ๋ชจ๋ธ: **{model_type} - {LLM_MODEL}**")
1437
-
1438
- # ์—ฌ๊ธฐ๋ฅผ ์ˆ˜์ •: ์‹ค์ œ ๊ฒฝ๋กœ ํ‘œ์‹œ
1439
- actual_pdf_dir = self.pdf_directory.replace('\\', '\\\\') if os.name == 'nt' else self.pdf_directory
1440
- gr.Markdown(f"* PDF ๋ฌธ์„œ ํด๋”: **{actual_pdf_dir}**")
1441
- with gr.Row():
1442
- with gr.Column(scale=1):
1443
- # ๋ฌธ์„œ ์ƒํƒœ ์„น์…˜
1444
- status_box = gr.Textbox(
1445
- label="๋ฌธ์„œ ์ฒ˜๋ฆฌ ์ƒํƒœ",
1446
- value=self._get_status_message(),
1447
- lines=5,
1448
- interactive=False
1449
- )
1450
-
1451
- # ์บ์‹œ ๊ด€๋ฆฌ ๋ฒ„ํŠผ
1452
- refresh_button = gr.Button("๋ฌธ์„œ ์ƒˆ๋กœ ์ฝ๊ธฐ", variant="primary")
1453
- reset_button = gr.Button("์บ์‹œ ์ดˆ๊ธฐํ™”", variant="stop")
1454
-
1455
- # DeepSeek API ์ƒํƒœ ํ™•์ธ ๋ฒ„ํŠผ
1456
- from config import USE_DEEPSEEK
1457
- if USE_DEEPSEEK:
1458
- deepseek_button = gr.Button("DeepSeek API ์ƒํƒœ ํ™•์ธ", variant="secondary")
1459
-
1460
- # ์ƒํƒœ ๋ฐ ์˜ค๋ฅ˜ ํ‘œ์‹œ
1461
- status_info = gr.Markdown(
1462
- value=f"์‹œ์Šคํ…œ ์ƒํƒœ: {'์ดˆ๊ธฐํ™”๋จ' if self.is_initialized else '์ดˆ๊ธฐํ™”๋˜์ง€ ์•Š์Œ'}"
1463
- )
1464
-
1465
- # ์ฒ˜๋ฆฌ๋œ ํŒŒ์ผ ์ •๋ณด
1466
- with gr.Accordion("์บ์‹œ ์„ธ๋ถ€ ์ •๋ณด", open=False):
1467
- cache_info = gr.Textbox(
1468
- label="์บ์‹œ๋œ ํŒŒ์ผ ์ •๋ณด",
1469
- value=self._get_cache_info(),
1470
- lines=5,
1471
- interactive=False
1472
- )
1473
-
1474
- with gr.Column(scale=2):
1475
- # ์ฑ„ํŒ… ์ธํ„ฐํŽ˜์ด์Šค
1476
- chatbot = gr.Chatbot(
1477
- label="๋Œ€ํ™” ๋‚ด์šฉ",
1478
- bubble_full_width=False,
1479
- height=500,
1480
- show_copy_button=True
1481
- )
1482
-
1483
- # ์งˆ๋ฌธ ์ž…๋ ฅ๊ณผ ์ „์†ก ๋ฒ„ํŠผ์„ ์ˆ˜ํ‰์œผ๋กœ ๋ฐฐ์น˜
1484
- with gr.Row():
1485
- query_box = gr.Textbox(
1486
- label="์งˆ๋ฌธ",
1487
- placeholder="์ฒ˜๋ฆฌ๋œ ๋ฌธ์„œ ๋‚ด์šฉ์— ๋Œ€ํ•ด ์งˆ๋ฌธํ•˜์„ธ์š”...",
1488
- lines=2,
1489
- scale=4
1490
- )
1491
- submit_btn = gr.Button("์ „์†ก", variant="primary", scale=1)
1492
-
1493
- clear_chat_button = gr.Button("๋Œ€ํ™” ์ดˆ๊ธฐํ™”")
1494
-
1495
- # ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ ์„ค์ •
1496
- def update_ui_after_refresh(result):
1497
- return (
1498
- result, # ์ƒํƒœ ๋ฉ”์‹œ์ง€
1499
- self._get_status_message(), # ์ƒํƒœ ๋ฐ•์Šค ์—…๋ฐ์ดํŠธ
1500
- f"์‹œ์Šคํ…œ ์ƒํƒœ: {'์ดˆ๊ธฐํ™”๋จ' if self.is_initialized else '์ดˆ๊ธฐํ™”๋˜์ง€ ์•Š์Œ'}", # ์ƒํƒœ ์ •๋ณด ์—…๋ฐ์ดํŠธ
1501
- self._get_cache_info() # ์บ์‹œ ์ •๋ณด ์—…๋ฐ์ดํŠธ
1502
- )
1503
-
1504
- # ๋ฌธ์„œ ์ƒˆ๋กœ ์ฝ๊ธฐ ๋ฒ„ํŠผ
1505
- refresh_button.click(
1506
- fn=lambda: update_ui_after_refresh(self.auto_process_documents()),
1507
- inputs=[],
1508
- outputs=[status_box, status_box, status_info, cache_info]
1509
- )
1510
-
1511
- # ์บ์‹œ ์ดˆ๊ธฐํ™” ๋ฒ„ํŠผ
1512
- def reset_and_process():
1513
- reset_result = self.reset_cache()
1514
- process_result = self.auto_process_documents()
1515
- return update_ui_after_refresh(f"{reset_result}\n\n{process_result}")
1516
-
1517
- reset_button.click(
1518
- fn=reset_and_process,
1519
- inputs=[],
1520
- outputs=[status_box, status_box, status_info, cache_info]
1521
- )
1522
-
1523
- # DeepSeek API ์ƒํƒœ ํ™•์ธ ๋ฒ„ํŠผ (์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ)
1524
- if USE_DEEPSEEK:
1525
- def check_deepseek_api():
1526
- # DeepSeek API ์ƒํƒœ ํ™•์ธ
1527
- try:
1528
- from config import DEEPSEEK_API_KEY, DEEPSEEK_ENDPOINT, DEEPSEEK_MODEL
1529
-
1530
- try:
1531
- from deepseek_utils import test_deepseek_api
1532
- test_result = test_deepseek_api(DEEPSEEK_API_KEY, DEEPSEEK_ENDPOINT, DEEPSEEK_MODEL)
1533
-
1534
- if test_result["success"]:
1535
- return f"DeepSeek API ์ƒํƒœ: ์ •์ƒ\n๋ชจ๋ธ: {DEEPSEEK_MODEL}\n์‘๋‹ต: {test_result.get('response', '(์‘๋‹ต ๋‚ด์šฉ ์—†์Œ)')}"
1536
- else:
1537
- return f"DeepSeek API ์˜ค๋ฅ˜: {test_result['message']}\n์ƒํƒœ ์ฝ”๋“œ: {test_result.get('status_code', 'N/A')}"
1538
- except ImportError:
1539
- # ์ง์ ‘ ํ…Œ์ŠคํŠธ
1540
- import requests
1541
- import json
1542
-
1543
- # ํ…Œ์ŠคํŠธ์šฉ ๊ฐ„๋‹จํ•œ ํ”„๋กฌํ”„ํŠธ
1544
- test_prompt = "Hello, please respond with a short greeting."
1545
-
1546
- # API ์š”์ฒญ ํ—ค๋” ๋ฐ ๋ฐ์ดํ„ฐ
1547
- headers = {
1548
- "Content-Type": "application/json",
1549
- "Authorization": f"Bearer {DEEPSEEK_API_KEY}"
1550
- }
1551
-
1552
- payload = {
1553
- "model": DEEPSEEK_MODEL,
1554
- "messages": [{"role": "user", "content": test_prompt}],
1555
- "temperature": 0.7,
1556
- "max_tokens": 50
1557
- }
1558
-
1559
- # API ์š”์ฒญ ์ „์†ก
1560
- response = requests.post(
1561
- DEEPSEEK_ENDPOINT,
1562
- headers=headers,
1563
- data=json.dumps(payload),
1564
- timeout=10
1565
- )
1566
-
1567
- # ์‘๋‹ต ํ™•์ธ
1568
- if response.status_code == 200:
1569
- response_data = response.json()
1570
- message_content = response_data.get("choices", [{}])[0].get("message", {}).get(
1571
- "content", "")
1572
- return f"DeepSeek API ์ƒํƒœ: ์ •์ƒ\n๋ชจ๋ธ: {DEEPSEEK_MODEL}\n์‘๋‹ต: {message_content[:200]}..."
1573
- else:
1574
- return f"DeepSeek API ์˜ค๋ฅ˜\n์ƒํƒœ ์ฝ”๋“œ: {response.status_code}\n์‘๋‹ต: {response.text[:200]}..."
1575
-
1576
- except Exception as e:
1577
- return f"DeepSeek API ํ…Œ์ŠคํŠธ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}"
1578
-
1579
- deepseek_button.click(
1580
- fn=check_deepseek_api,
1581
- inputs=[],
1582
- outputs=[status_box]
1583
- )
1584
-
1585
- # ์ „์†ก ๋ฒ„ํŠผ ํด๋ฆญ ์ด๋ฒคํŠธ
1586
- submit_btn.click(
1587
- fn=self.process_query,
1588
- inputs=[query_box, chatbot],
1589
- outputs=[query_box, chatbot]
1590
- )
1591
-
1592
- # ์—”ํ„ฐํ‚ค ์ž…๋ ฅ ์ด๋ฒคํŠธ
1593
- query_box.submit(
1594
- fn=self.process_query,
1595
- inputs=[query_box, chatbot],
1596
- outputs=[query_box, chatbot]
1597
- )
1598
-
1599
- # ๋Œ€ํ™” ์ดˆ๊ธฐํ™” ๋ฒ„ํŠผ
1600
- clear_chat_button.click(
1601
- fn=lambda: [],
1602
- outputs=[chatbot]
1603
- )
1604
-
1605
- # ์•ฑ ์‹คํ–‰
1606
- app.launch(share=False)
1607
- except Exception as e:
1608
- logger.error(f"Gradio ์•ฑ ์‹คํ–‰ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {e}", exc_info=True)
1609
- print(f"Gradio ์•ฑ ์‹คํ–‰ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {e}")
1610
 
1611
 
1612
  if __name__ == "__main__":
 
1413
 
1414
  return file_info
1415
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1416
 
1417
 
1418
  if __name__ == "__main__":