aiqtech commited on
Commit
0fa7d12
ยท
verified ยท
1 Parent(s): 9dca33d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -27
app.py CHANGED
@@ -638,21 +638,44 @@ def create_optimized_gradio_interface():
638
  # ์‹œ์Šคํ…œ ์ดˆ๊ธฐํ™”
639
  system = SpeedOptimizedMultiAgentSystem()
640
 
641
- async 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
  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 = await system.search.search_async(message, count=search_count)
 
 
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 for response, thoughts in system.parallel_process_agents(
681
- query=message,
682
- search_results=search_results,
683
- show_progress=show_agent_thoughts
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
- process_wrapper,
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
- process_wrapper,
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(