Heewon Oh Claude Sonnet 4.5 commited on
Commit
e73a081
·
1 Parent(s): 5835058

feat(app): ZeroGPU 쿼터 소진 시 RunPod Serverless fallback 추가

Browse files

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>

Files changed (1) hide show
  1. app.py +38 -1
app.py CHANGED
@@ -39,6 +39,20 @@ def get_backend() -> str:
39
  return "zerogpu"
40
 
41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  BACKEND = get_backend()
43
  logger.info(f"LLM 백엔드: {BACKEND}")
44
 
@@ -76,6 +90,15 @@ else:
76
  return agent.research(query=query, options=options)
77
 
78
 
 
 
 
 
 
 
 
 
 
79
  def run_research(query: str, max_iterations: int):
80
  """리서치 실행 — 스트리밍 제너레이터.
81
 
@@ -94,7 +117,21 @@ def run_research(query: str, max_iterations: int):
94
 
95
  # 단일 GPU 컨텍스트에서 전체 research 실행
96
  # ZeroGPU pickle 제약: agent, callback 등은 _run_research_gpu 내부에서 생성
97
- result = _run_research_gpu(query.strip(), int(max_iterations))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98
 
99
  if not result:
100
  yield "리서치 결과가 없습니다.", "", ""
 
39
  return "zerogpu"
40
 
41
 
42
+ def _is_zerogpu_quota_error(e: Exception) -> bool:
43
+ """ZeroGPU 쿼터/할당 오류 여부 판별"""
44
+ msg = str(e).lower()
45
+ return any(kw in msg for kw in (
46
+ "quota", "zerogpu", "out of gpu", "no gpu", "gpu quota",
47
+ "exceeded", "gpu not available", "not enough gpu",
48
+ ))
49
+
50
+
51
+ def _runpod_available() -> bool:
52
+ """RunPod Serverless 환경변수 설정 여부 확인"""
53
+ return bool(os.environ.get("RUNPOD_API_KEY") and os.environ.get("RUNPOD_ENDPOINT_ID"))
54
+
55
+
56
  BACKEND = get_backend()
57
  logger.info(f"LLM 백엔드: {BACKEND}")
58
 
 
90
  return agent.research(query=query, options=options)
91
 
92
 
93
+ def _run_research_runpod(query: str, max_iterations: int):
94
+ """RunPod Serverless fallback (GPU 데코레이터 없음)"""
95
+ from vela import ResearchAgent
96
+ from vela.schemas import ResearchOptions
97
+ agent = ResearchAgent(llm_backend="runpod")
98
+ options = ResearchOptions(max_iterations=max_iterations, extract_content=True)
99
+ return agent.research(query=query, options=options)
100
+
101
+
102
  def run_research(query: str, max_iterations: int):
103
  """리서치 실행 — 스트리밍 제너레이터.
104
 
 
117
 
118
  # 단일 GPU 컨텍스트에서 전체 research 실행
119
  # ZeroGPU pickle 제약: agent, callback 등은 _run_research_gpu 내부에서 생성
120
+ result = None
121
+ try:
122
+ result = _run_research_gpu(query.strip(), int(max_iterations))
123
+ except Exception as gpu_err:
124
+ if _is_zerogpu_quota_error(gpu_err) and _runpod_available():
125
+ logger.warning(f"ZeroGPU 쿼터 소진, RunPod Serverless로 전환: {gpu_err}")
126
+ yield (
127
+ f"## 리서치 진행 중: {query.strip()}\n\n"
128
+ f"> ⚠️ ZeroGPU 쿼터 초과 — RunPod Serverless로 전환합니다...\n",
129
+ "",
130
+ "",
131
+ )
132
+ result = _run_research_runpod(query.strip(), int(max_iterations))
133
+ else:
134
+ raise
135
 
136
  if not result:
137
  yield "리서치 결과가 없습니다.", "", ""