Update app.py
Browse files
app.py
CHANGED
|
@@ -638,21 +638,44 @@ def create_optimized_gradio_interface():
|
|
| 638 |
# ์์คํ
์ด๊ธฐํ
|
| 639 |
system = SpeedOptimizedMultiAgentSystem()
|
| 640 |
|
| 641 |
-
|
| 642 |
message: str,
|
| 643 |
history: List[Dict],
|
| 644 |
use_search: bool,
|
| 645 |
show_agent_thoughts: bool,
|
| 646 |
search_count: int
|
| 647 |
):
|
| 648 |
-
"""์ต์ ํ๋ ์ฟผ๋ฆฌ ์ฒ๋ฆฌ"""
|
| 649 |
|
| 650 |
if not message:
|
| 651 |
yield history, "", ""
|
| 652 |
return
|
| 653 |
|
|
|
|
| 654 |
try:
|
| 655 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 656 |
search_results = []
|
| 657 |
search_display = ""
|
| 658 |
|
|
@@ -664,8 +687,10 @@ def create_optimized_gradio_interface():
|
|
| 664 |
]
|
| 665 |
yield history_with_message, "", ""
|
| 666 |
|
| 667 |
-
# ๋น๋๊ธฐ
|
| 668 |
-
search_results =
|
|
|
|
|
|
|
| 669 |
|
| 670 |
if search_results:
|
| 671 |
search_display = "## ๐ ์ฐธ๊ณ ์๋ฃ\n\n"
|
|
@@ -676,12 +701,22 @@ def create_optimized_gradio_interface():
|
|
| 676 |
# ์ฌ์ฉ์ ๋ฉ์์ง ์ถ๊ฐ
|
| 677 |
current_history = history + [{"role": "user", "content": message}]
|
| 678 |
|
| 679 |
-
# ๋ณ๋ ฌ ์ฒ๋ฆฌ
|
| 680 |
-
async
|
| 681 |
-
|
| 682 |
-
|
| 683 |
-
|
| 684 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 685 |
updated_history = current_history + [
|
| 686 |
{"role": "assistant", "content": response}
|
| 687 |
]
|
|
@@ -790,22 +825,8 @@ def create_optimized_gradio_interface():
|
|
| 790 |
)
|
| 791 |
|
| 792 |
# ์ด๋ฒคํธ ๋ฐ์ธ๋ฉ
|
| 793 |
-
def process_wrapper(message, history, use_search, show_thoughts, search_count):
|
| 794 |
-
"""๋๊ธฐ ๋ํผ for Gradio"""
|
| 795 |
-
loop = asyncio.new_event_loop()
|
| 796 |
-
asyncio.set_event_loop(loop)
|
| 797 |
-
|
| 798 |
-
async def run():
|
| 799 |
-
async for result in process_query_optimized(
|
| 800 |
-
message, history, use_search, show_thoughts, search_count
|
| 801 |
-
):
|
| 802 |
-
yield result
|
| 803 |
-
|
| 804 |
-
for result in loop.run_until_complete(run()):
|
| 805 |
-
yield result
|
| 806 |
-
|
| 807 |
submit.click(
|
| 808 |
-
|
| 809 |
inputs=[msg, chatbot, use_search, show_agent_thoughts, search_count],
|
| 810 |
outputs=[chatbot, agent_thoughts, search_sources]
|
| 811 |
).then(
|
|
@@ -815,7 +836,7 @@ def create_optimized_gradio_interface():
|
|
| 815 |
)
|
| 816 |
|
| 817 |
msg.submit(
|
| 818 |
-
|
| 819 |
inputs=[msg, chatbot, use_search, show_agent_thoughts, search_count],
|
| 820 |
outputs=[chatbot, agent_thoughts, search_sources]
|
| 821 |
).then(
|
|
|
|
| 638 |
# ์์คํ
์ด๊ธฐํ
|
| 639 |
system = SpeedOptimizedMultiAgentSystem()
|
| 640 |
|
| 641 |
+
def process_query_optimized(
|
| 642 |
message: str,
|
| 643 |
history: List[Dict],
|
| 644 |
use_search: bool,
|
| 645 |
show_agent_thoughts: bool,
|
| 646 |
search_count: int
|
| 647 |
):
|
| 648 |
+
"""์ต์ ํ๋ ์ฟผ๋ฆฌ ์ฒ๋ฆฌ - ๋๊ธฐ ๋ฒ์ """
|
| 649 |
|
| 650 |
if not message:
|
| 651 |
yield history, "", ""
|
| 652 |
return
|
| 653 |
|
| 654 |
+
# ๋น๋๊ธฐ ํจ์๋ฅผ ๋๊ธฐ์ ์ผ๋ก ์คํ
|
| 655 |
try:
|
| 656 |
+
import nest_asyncio
|
| 657 |
+
nest_asyncio.apply()
|
| 658 |
+
except ImportError:
|
| 659 |
+
pass # nest_asyncio๊ฐ ์์ด๋ ์งํ
|
| 660 |
+
|
| 661 |
+
def run_async_function(coro):
|
| 662 |
+
"""๋น๋๊ธฐ ํจ์๋ฅผ ๋๊ธฐ์ ์ผ๋ก ์คํํ๋ ํฌํผ"""
|
| 663 |
+
try:
|
| 664 |
+
loop = asyncio.get_event_loop()
|
| 665 |
+
if loop.is_running():
|
| 666 |
+
# ์ด๋ฏธ ์คํ ์ค์ธ ๋ฃจํ๊ฐ ์์ผ๋ฉด ์ ์ค๋ ๋์์ ์คํ
|
| 667 |
+
import concurrent.futures
|
| 668 |
+
with concurrent.futures.ThreadPoolExecutor() as executor:
|
| 669 |
+
future = executor.submit(asyncio.run, coro)
|
| 670 |
+
return future.result()
|
| 671 |
+
else:
|
| 672 |
+
return loop.run_until_complete(coro)
|
| 673 |
+
except RuntimeError:
|
| 674 |
+
# ๋ฃจํ๊ฐ ์์ผ๋ฉด ์๋ก ์์ฑ
|
| 675 |
+
return asyncio.run(coro)
|
| 676 |
+
|
| 677 |
+
try:
|
| 678 |
+
# ๊ฒ์ ์ํ (๋๊ธฐํ)
|
| 679 |
search_results = []
|
| 680 |
search_display = ""
|
| 681 |
|
|
|
|
| 687 |
]
|
| 688 |
yield history_with_message, "", ""
|
| 689 |
|
| 690 |
+
# ๋น๋๊ธฐ ๊ฒ์์ ๋๊ธฐ์ ์ผ๋ก ์คํ
|
| 691 |
+
search_results = run_async_function(
|
| 692 |
+
system.search.search_async(message, count=search_count)
|
| 693 |
+
)
|
| 694 |
|
| 695 |
if search_results:
|
| 696 |
search_display = "## ๐ ์ฐธ๊ณ ์๋ฃ\n\n"
|
|
|
|
| 701 |
# ์ฌ์ฉ์ ๋ฉ์์ง ์ถ๊ฐ
|
| 702 |
current_history = history + [{"role": "user", "content": message}]
|
| 703 |
|
| 704 |
+
# ๋ณ๋ ฌ ์ฒ๋ฆฌ ์คํ์ ๋๊ธฐ์ ์ผ๋ก ์์ง
|
| 705 |
+
async def collect_responses():
|
| 706 |
+
responses = []
|
| 707 |
+
async for response, thoughts in system.parallel_process_agents(
|
| 708 |
+
query=message,
|
| 709 |
+
search_results=search_results,
|
| 710 |
+
show_progress=show_agent_thoughts
|
| 711 |
+
):
|
| 712 |
+
responses.append((response, thoughts))
|
| 713 |
+
return responses
|
| 714 |
+
|
| 715 |
+
# ๋ชจ๋ ์๋ต ์์ง
|
| 716 |
+
all_responses = run_async_function(collect_responses())
|
| 717 |
+
|
| 718 |
+
# ์์ง๋ ์๋ต์ yield
|
| 719 |
+
for response, thoughts in all_responses:
|
| 720 |
updated_history = current_history + [
|
| 721 |
{"role": "assistant", "content": response}
|
| 722 |
]
|
|
|
|
| 825 |
)
|
| 826 |
|
| 827 |
# ์ด๋ฒคํธ ๋ฐ์ธ๋ฉ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 828 |
submit.click(
|
| 829 |
+
process_query_optimized,
|
| 830 |
inputs=[msg, chatbot, use_search, show_agent_thoughts, search_count],
|
| 831 |
outputs=[chatbot, agent_thoughts, search_sources]
|
| 832 |
).then(
|
|
|
|
| 836 |
)
|
| 837 |
|
| 838 |
msg.submit(
|
| 839 |
+
process_query_optimized,
|
| 840 |
inputs=[msg, chatbot, use_search, show_agent_thoughts, search_count],
|
| 841 |
outputs=[chatbot, agent_thoughts, search_sources]
|
| 842 |
).then(
|