aiqtech commited on
Commit
c4ab828
ยท
verified ยท
1 Parent(s): 6511714

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -184
app.py CHANGED
@@ -1,24 +1,19 @@
1
  """
2
- Multi-Agent RAG-Enhanced LLM System
3
  ๊ฐ๋…์ž(Supervisor) -> ์ฐฝ์˜์„ฑ ์ƒ์„ฑ์ž(Creative) -> ๋น„ํ‰์ž(Critic) -> ๊ฐ๋…์ž(Final)
4
  4๋‹จ๊ณ„ ํŒŒ์ดํ”„๋ผ์ธ์„ ํ†ตํ•œ ๊ณ ํ’ˆ์งˆ ๋‹ต๋ณ€ ์ƒ์„ฑ ์‹œ์Šคํ…œ
5
  """
6
 
7
  import os
8
  import json
9
- import asyncio
10
  import time
11
  from typing import Optional, List, Dict, Any, Tuple
12
- from contextlib import asynccontextmanager
13
  from datetime import datetime
14
  from enum import Enum
15
 
16
  import requests
17
- import uvicorn
18
- from fastapi import FastAPI, HTTPException
19
- from fastapi.middleware.cors import CORSMiddleware
20
- from pydantic import BaseModel, Field
21
  import gradio as gr
 
22
  from dotenv import load_dotenv
23
 
24
  # ํ™˜๊ฒฝ๋ณ€์ˆ˜ ๋กœ๋“œ
@@ -42,16 +37,6 @@ class Message(BaseModel):
42
  content: str
43
 
44
 
45
- class ChatRequest(BaseModel):
46
- messages: List[Message]
47
- model: str = "accounts/fireworks/models/qwen3-235b-a22b-instruct-2507"
48
- max_tokens: int = Field(default=4096, ge=1, le=8192)
49
- temperature: float = Field(default=0.6, ge=0, le=2)
50
- top_p: float = Field(default=1.0, ge=0, le=1)
51
- top_k: int = Field(default=40, ge=1, le=100)
52
- use_search: bool = Field(default=True)
53
-
54
-
55
  class AgentResponse(BaseModel):
56
  role: AgentRole
57
  content: str
@@ -285,7 +270,7 @@ URL: {result.get('url', 'N/A')}
285
 
286
  return "\n".join(formatted)
287
 
288
- async def process_with_agents(
289
  self,
290
  query: str,
291
  search_results: List[Dict],
@@ -419,27 +404,39 @@ URL: {result.get('url', 'N/A')}
419
  # Gradio UI
420
  # ============================================================================
421
 
422
- def create_gradio_interface(multi_agent_system: MultiAgentSystem, search_client: BraveSearchClient):
423
  """Gradio ์ธํ„ฐํŽ˜์ด์Šค ์ƒ์„ฑ"""
424
 
425
- async def process_query(
 
 
 
 
 
 
 
 
 
 
 
 
426
  message: str,
427
- history: List[List[str]],
428
  use_search: bool,
429
  show_agent_thoughts: bool,
430
  search_count: int,
431
  temperature: float,
432
  max_tokens: int
433
- ):
434
  """์ฟผ๋ฆฌ ์ฒ˜๋ฆฌ ํ•จ์ˆ˜"""
435
 
436
- if not message:
437
- return "", history, "", ""
438
 
439
  try:
440
  # ๊ฒ€์ƒ‰ ์ˆ˜ํ–‰
441
  search_results = []
442
- if use_search and search_client.api_key:
443
  search_results = search_client.search(message, count=search_count)
444
 
445
  # ์„ค์ •
@@ -449,7 +446,7 @@ def create_gradio_interface(multi_agent_system: MultiAgentSystem, search_client:
449
  }
450
 
451
  # ๋ฉ€ํ‹ฐ ์—์ด์ „ํŠธ ์ฒ˜๋ฆฌ
452
- response = await multi_agent_system.process_with_agents(
453
  query=message,
454
  search_results=search_results,
455
  config=config
@@ -490,15 +487,17 @@ def create_gradio_interface(multi_agent_system: MultiAgentSystem, search_client:
490
  final_answer = response.final_answer
491
  final_answer += f"\n\n---\nโฑ๏ธ *์ฒ˜๋ฆฌ ์‹œ๊ฐ„: {response.processing_time:.2f}์ดˆ*"
492
 
493
- # ํžˆ์Šคํ† ๋ฆฌ ์—…๋ฐ์ดํŠธ
494
- history.append([message, final_answer])
 
495
 
496
- return "", history, agent_thoughts, search_display
497
 
498
  except Exception as e:
499
  error_msg = f"โŒ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}"
500
- history.append([message, error_msg])
501
- return "", history, "", ""
 
502
 
503
  # Gradio ์ธํ„ฐํŽ˜์ด์Šค
504
  with gr.Blocks(
@@ -509,9 +508,6 @@ def create_gradio_interface(multi_agent_system: MultiAgentSystem, search_client:
509
  max-width: 1400px !important;
510
  margin: auto !important;
511
  }
512
- #chatbot {
513
- height: 600px !important;
514
- }
515
  """
516
  ) as demo:
517
  gr.Markdown("""
@@ -521,13 +517,20 @@ def create_gradio_interface(multi_agent_system: MultiAgentSystem, search_client:
521
  **์ฒ˜๋ฆฌ ๊ณผ์ •:** ๊ฐ๋…์ž(๊ตฌ์กฐํ™”) โ†’ ์ฐฝ์˜์„ฑ ์ƒ์„ฑ์ž(์ฐฝ์˜์  ๋‹ต๋ณ€) โ†’ ๋น„ํ‰์ž(๊ฒ€์ฆ) โ†’ ์ตœ์ข… ๊ฐ๋…์ž(์ข…ํ•ฉ)
522
  """)
523
 
 
 
 
 
 
 
 
524
  with gr.Row():
525
  # ๋ฉ”์ธ ์ฑ„ํŒ… ์˜์—ญ
526
  with gr.Column(scale=3):
527
  chatbot = gr.Chatbot(
528
  height=500,
529
  label="๐Ÿ’ฌ ๋Œ€ํ™”",
530
- elem_id="chatbot"
531
  )
532
 
533
  msg = gr.Textbox(
@@ -614,18 +617,26 @@ def create_gradio_interface(multi_agent_system: MultiAgentSystem, search_client:
614
  process_query,
615
  inputs=[msg, chatbot, use_search, show_agent_thoughts,
616
  search_count, temperature, max_tokens],
617
- outputs=[msg, chatbot, agent_thoughts, search_sources]
 
 
 
 
618
  )
619
 
620
  msg.submit(
621
  process_query,
622
  inputs=[msg, chatbot, use_search, show_agent_thoughts,
623
  search_count, temperature, max_tokens],
624
- outputs=[msg, chatbot, agent_thoughts, search_sources]
 
 
 
 
625
  )
626
 
627
  clear.click(
628
- lambda: (None, None, None),
629
  None,
630
  [chatbot, agent_thoughts, search_sources]
631
  )
@@ -633,113 +644,6 @@ def create_gradio_interface(multi_agent_system: MultiAgentSystem, search_client:
633
  return demo
634
 
635
 
636
- # ============================================================================
637
- # FastAPI ์•ฑ
638
- # ============================================================================
639
-
640
- @asynccontextmanager
641
- async def lifespan(app: FastAPI):
642
- """์•ฑ ์ƒ๋ช…์ฃผ๊ธฐ ๊ด€๋ฆฌ"""
643
- port = int(os.getenv("PORT", 7860))
644
- print("\n" + "="*60)
645
- print("๐Ÿš€ Multi-Agent RAG System Starting...")
646
- print(f"๐Ÿ“ Port: {port}")
647
- print("="*60)
648
- yield
649
- print("\n๐Ÿ‘‹ Shutting down...")
650
-
651
-
652
- app = FastAPI(
653
- title="Multi-Agent RAG System API",
654
- description="4-Stage Agent Collaboration System with RAG",
655
- version="3.0.0",
656
- lifespan=lifespan
657
- )
658
-
659
- # CORS ์„ค์ •
660
- app.add_middleware(
661
- CORSMiddleware,
662
- allow_origins=["*"],
663
- allow_credentials=True,
664
- allow_methods=["*"],
665
- allow_headers=["*"]
666
- )
667
-
668
- # ํด๋ผ์ด์–ธํŠธ ์ดˆ๊ธฐํ™”
669
- try:
670
- llm_client = FireworksClient()
671
- search_client = BraveSearchClient()
672
- multi_agent_system = MultiAgentSystem(llm_client, search_client)
673
- except Exception as e:
674
- print(f"โš ๏ธ Initialization error: {e}")
675
- llm_client = None
676
- search_client = None
677
- multi_agent_system = None
678
-
679
-
680
- # API ์—”๋“œํฌ์ธํŠธ
681
- @app.get("/")
682
- async def root():
683
- """๋ฃจํŠธ ์—”๋“œํฌ์ธํŠธ"""
684
- port = int(os.getenv("PORT", 7860))
685
- return {
686
- "name": "Multi-Agent RAG System",
687
- "version": "3.0.0",
688
- "status": "running",
689
- "ui": f"http://localhost:{port}/ui",
690
- "docs": f"http://localhost:{port}/docs"
691
- }
692
-
693
-
694
- @app.post("/api/chat")
695
- async def chat_endpoint(request: ChatRequest):
696
- """๋ฉ€ํ‹ฐ ์—์ด์ „ํŠธ ์ฑ„ํŒ… API"""
697
- if not multi_agent_system:
698
- raise HTTPException(status_code=500, detail="System not initialized")
699
-
700
- try:
701
- # ๊ฒ€์ƒ‰ ์ˆ˜ํ–‰
702
- search_results = []
703
- if request.use_search and search_client.api_key:
704
- last_message = request.messages[-1].content if request.messages else ""
705
- search_results = search_client.search(last_message, count=5)
706
-
707
- # ๋ฉ€ํ‹ฐ ์—์ด์ „ํŠธ ์ฒ˜๋ฆฌ
708
- response = await multi_agent_system.process_with_agents(
709
- query=request.messages[-1].content,
710
- search_results=search_results,
711
- config={
712
- "temperature": request.temperature,
713
- "max_tokens": request.max_tokens
714
- }
715
- )
716
-
717
- return response
718
-
719
- except Exception as e:
720
- raise HTTPException(status_code=500, detail=str(e))
721
-
722
-
723
- @app.get("/health")
724
- async def health_check():
725
- """ํ—ฌ์Šค ์ฒดํฌ"""
726
- return {
727
- "status": "healthy",
728
- "timestamp": datetime.now().isoformat(),
729
- "services": {
730
- "llm": "ready" if llm_client else "not configured",
731
- "search": "ready" if search_client and search_client.api_key else "not configured",
732
- "multi_agent": "ready" if multi_agent_system else "not configured"
733
- }
734
- }
735
-
736
-
737
- # Gradio ๋งˆ์šดํŠธ
738
- if multi_agent_system:
739
- gradio_app = create_gradio_interface(multi_agent_system, search_client)
740
- app = gr.mount_gradio_app(app, gradio_app, path="/ui")
741
-
742
-
743
  # ============================================================================
744
  # ๋ฉ”์ธ ์‹คํ–‰
745
  # ============================================================================
@@ -757,53 +661,21 @@ if __name__ == "__main__":
757
  # API ํ‚ค ํ™•์ธ
758
  if not os.getenv("FIREWORKS_API_KEY"):
759
  print("\nโš ๏ธ FIREWORKS_API_KEY๊ฐ€ ์„ค์ •๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.")
760
- key = input("Fireworks API Key ์ž…๋ ฅ: ").strip()
761
- if key:
762
- os.environ["FIREWORKS_API_KEY"] = key
763
- llm_client = FireworksClient(key)
764
 
765
  if not os.getenv("BRAVE_SEARCH_API_KEY"):
766
  print("\nโš ๏ธ BRAVE_SEARCH_API_KEY๊ฐ€ ์„ค์ •๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.")
767
- print(" (์„ ํƒ์‚ฌํ•ญ: ๊ฒ€์ƒ‰ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ์ž…๋ ฅ)")
768
- key = input("Brave Search API Key ์ž…๋ ฅ (Enter=๊ฑด๋„ˆ๋›ฐ๊ธฐ): ").strip()
769
- if key:
770
- os.environ["BRAVE_SEARCH_API_KEY"] = key
771
- search_client = BraveSearchClient(key)
772
-
773
- # ์‹œ์Šคํ…œ ์žฌ์ดˆ๊ธฐํ™”
774
- if llm_client:
775
- multi_agent_system = MultiAgentSystem(llm_client, search_client)
776
- gradio_app = create_gradio_interface(multi_agent_system, search_client)
777
- app = gr.mount_gradio_app(app, gradio_app, path="/ui")
778
 
779
- print("\n" + "="*60)
780
- print("โœ… ์‹œ์Šคํ…œ ์ค€๋น„ ์™„๋ฃŒ!")
781
- print("="*60)
782
 
783
  # Hugging Face Spaces ํ™˜๊ฒฝ ํ™•์ธ
784
  is_hf_spaces = os.getenv("SPACE_ID") is not None
785
- port = int(os.getenv("PORT", 7860))
786
 
787
  if is_hf_spaces:
788
  print("\n๐Ÿค— Hugging Face Spaces์—์„œ ์‹คํ–‰ ์ค‘...")
789
- print(f" ํฌํŠธ: {port}")
790
  else:
791
- print("\n๐Ÿ“ ์ ‘์† ์ฃผ์†Œ:")
792
- print(f" ๐ŸŽจ Gradio UI: http://localhost:{port}/ui")
793
- print(f" ๐Ÿ“š API Docs: http://localhost:{port}/docs")
794
- print(f" ๐Ÿ”ง Chat API: POST http://localhost:{port}/api/chat")
795
-
796
- print("\n๐Ÿ’ก Ctrl+C๋ฅผ ๋ˆŒ๋Ÿฌ ์ข…๋ฃŒ")
797
- print("="*60 + "\n")
798
-
799
- # Hugging Face Spaces ํ™˜๊ฒฝ ๊ฐ์ง€
800
- port = int(os.getenv("PORT", 7860))
801
- host = "0.0.0.0"
802
-
803
- uvicorn.run(
804
- app,
805
- host=host,
806
- port=port,
807
- reload=False,
808
- log_level="info"
809
- )
 
1
  """
2
+ Multi-Agent RAG-Enhanced LLM System for Hugging Face Spaces
3
  ๊ฐ๋…์ž(Supervisor) -> ์ฐฝ์˜์„ฑ ์ƒ์„ฑ์ž(Creative) -> ๋น„ํ‰์ž(Critic) -> ๊ฐ๋…์ž(Final)
4
  4๋‹จ๊ณ„ ํŒŒ์ดํ”„๋ผ์ธ์„ ํ†ตํ•œ ๊ณ ํ’ˆ์งˆ ๋‹ต๋ณ€ ์ƒ์„ฑ ์‹œ์Šคํ…œ
5
  """
6
 
7
  import os
8
  import json
 
9
  import time
10
  from typing import Optional, List, Dict, Any, Tuple
 
11
  from datetime import datetime
12
  from enum import Enum
13
 
14
  import requests
 
 
 
 
15
  import gradio as gr
16
+ from pydantic import BaseModel, Field
17
  from dotenv import load_dotenv
18
 
19
  # ํ™˜๊ฒฝ๋ณ€์ˆ˜ ๋กœ๋“œ
 
37
  content: str
38
 
39
 
 
 
 
 
 
 
 
 
 
 
40
  class AgentResponse(BaseModel):
41
  role: AgentRole
42
  content: str
 
270
 
271
  return "\n".join(formatted)
272
 
273
+ def process_with_agents(
274
  self,
275
  query: str,
276
  search_results: List[Dict],
 
404
  # Gradio UI
405
  # ============================================================================
406
 
407
+ def create_gradio_interface():
408
  """Gradio ์ธํ„ฐํŽ˜์ด์Šค ์ƒ์„ฑ"""
409
 
410
+ # ํด๋ผ์ด์–ธํŠธ ์ดˆ๊ธฐํ™”
411
+ try:
412
+ llm_client = FireworksClient()
413
+ search_client = BraveSearchClient()
414
+ multi_agent_system = MultiAgentSystem(llm_client, search_client)
415
+ system_ready = True
416
+ except Exception as e:
417
+ print(f"โš ๏ธ System initialization error: {e}")
418
+ multi_agent_system = None
419
+ search_client = None
420
+ system_ready = False
421
+
422
+ def process_query(
423
  message: str,
424
+ history: List[Dict],
425
  use_search: bool,
426
  show_agent_thoughts: bool,
427
  search_count: int,
428
  temperature: float,
429
  max_tokens: int
430
+ ) -> Tuple[List[Dict], str, str]:
431
  """์ฟผ๋ฆฌ ์ฒ˜๋ฆฌ ํ•จ์ˆ˜"""
432
 
433
+ if not message or not system_ready:
434
+ return history, "", ""
435
 
436
  try:
437
  # ๊ฒ€์ƒ‰ ์ˆ˜ํ–‰
438
  search_results = []
439
+ if use_search and search_client and search_client.api_key:
440
  search_results = search_client.search(message, count=search_count)
441
 
442
  # ์„ค์ •
 
446
  }
447
 
448
  # ๋ฉ€ํ‹ฐ ์—์ด์ „ํŠธ ์ฒ˜๋ฆฌ
449
+ response = multi_agent_system.process_with_agents(
450
  query=message,
451
  search_results=search_results,
452
  config=config
 
487
  final_answer = response.final_answer
488
  final_answer += f"\n\n---\nโฑ๏ธ *์ฒ˜๋ฆฌ ์‹œ๊ฐ„: {response.processing_time:.2f}์ดˆ*"
489
 
490
+ # ํžˆ์Šคํ† ๋ฆฌ ์—…๋ฐ์ดํŠธ (OpenAI ํ˜•์‹)
491
+ history.append({"role": "user", "content": message})
492
+ history.append({"role": "assistant", "content": final_answer})
493
 
494
+ return history, agent_thoughts, search_display
495
 
496
  except Exception as e:
497
  error_msg = f"โŒ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}"
498
+ history.append({"role": "user", "content": message})
499
+ history.append({"role": "assistant", "content": error_msg})
500
+ return history, "", ""
501
 
502
  # Gradio ์ธํ„ฐํŽ˜์ด์Šค
503
  with gr.Blocks(
 
508
  max-width: 1400px !important;
509
  margin: auto !important;
510
  }
 
 
 
511
  """
512
  ) as demo:
513
  gr.Markdown("""
 
517
  **์ฒ˜๋ฆฌ ๊ณผ์ •:** ๊ฐ๋…์ž(๊ตฌ์กฐํ™”) โ†’ ์ฐฝ์˜์„ฑ ์ƒ์„ฑ์ž(์ฐฝ์˜์  ๋‹ต๋ณ€) โ†’ ๋น„ํ‰์ž(๊ฒ€์ฆ) โ†’ ์ตœ์ข… ๊ฐ๋…์ž(์ข…ํ•ฉ)
518
  """)
519
 
520
+ if not system_ready:
521
+ gr.Markdown("""
522
+ โš ๏ธ **์‹œ์Šคํ…œ ์ดˆ๊ธฐํ™” ์‹คํŒจ**: API ํ‚ค๋ฅผ ํ™•์ธํ•ด์ฃผ์„ธ์š”.
523
+ - FIREWORKS_API_KEY ํ•„์š”
524
+ - BRAVE_SEARCH_API_KEY (์„ ํƒ์‚ฌํ•ญ)
525
+ """)
526
+
527
  with gr.Row():
528
  # ๋ฉ”์ธ ์ฑ„ํŒ… ์˜์—ญ
529
  with gr.Column(scale=3):
530
  chatbot = gr.Chatbot(
531
  height=500,
532
  label="๐Ÿ’ฌ ๋Œ€ํ™”",
533
+ type="messages" # OpenAI ์Šคํƒ€์ผ ๋ฉ”์‹œ์ง€ ํ˜•์‹
534
  )
535
 
536
  msg = gr.Textbox(
 
617
  process_query,
618
  inputs=[msg, chatbot, use_search, show_agent_thoughts,
619
  search_count, temperature, max_tokens],
620
+ outputs=[chatbot, agent_thoughts, search_sources]
621
+ ).then(
622
+ lambda: "",
623
+ None,
624
+ msg
625
  )
626
 
627
  msg.submit(
628
  process_query,
629
  inputs=[msg, chatbot, use_search, show_agent_thoughts,
630
  search_count, temperature, max_tokens],
631
+ outputs=[chatbot, agent_thoughts, search_sources]
632
+ ).then(
633
+ lambda: "",
634
+ None,
635
+ msg
636
  )
637
 
638
  clear.click(
639
+ lambda: ([], None, None),
640
  None,
641
  [chatbot, agent_thoughts, search_sources]
642
  )
 
644
  return demo
645
 
646
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
647
  # ============================================================================
648
  # ๋ฉ”์ธ ์‹คํ–‰
649
  # ============================================================================
 
661
  # API ํ‚ค ํ™•์ธ
662
  if not os.getenv("FIREWORKS_API_KEY"):
663
  print("\nโš ๏ธ FIREWORKS_API_KEY๊ฐ€ ์„ค์ •๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.")
664
+ print("Hugging Face Spaces Settings์—์„œ ์„ค์ •ํ•ด์ฃผ์„ธ์š”.")
 
 
 
665
 
666
  if not os.getenv("BRAVE_SEARCH_API_KEY"):
667
  print("\nโš ๏ธ BRAVE_SEARCH_API_KEY๊ฐ€ ์„ค์ •๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.")
668
+ print("๊ฒ€์ƒ‰ ๊ธฐ๋Šฅ์ด ๋น„ํ™œ์„ฑํ™”๋ฉ๋‹ˆ๋‹ค.")
 
 
 
 
 
 
 
 
 
 
669
 
670
+ # Gradio ์•ฑ ์‹คํ–‰
671
+ demo = create_gradio_interface()
 
672
 
673
  # Hugging Face Spaces ํ™˜๊ฒฝ ํ™•์ธ
674
  is_hf_spaces = os.getenv("SPACE_ID") is not None
 
675
 
676
  if is_hf_spaces:
677
  print("\n๐Ÿค— Hugging Face Spaces์—์„œ ์‹คํ–‰ ์ค‘...")
678
+ demo.launch(server_name="0.0.0.0", server_port=7860)
679
  else:
680
+ print("\n๐Ÿ’ป ๋กœ์ปฌ ํ™˜๊ฒฝ์—์„œ ์‹คํ–‰ ์ค‘...")
681
+ demo.launch(server_name="0.0.0.0", server_port=7860, share=False)