Spaces:
Paused
Paused
app
Browse files
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__":
|