"""율봇 WebUI — youlbot REST API를 호출하는 Gradio 프론트엔드. 실행: python app.py 환경변수 (.env): YOULBOT_API_URL=http://localhost:8000 YOULBOT_API_TOKEN= ← api.py에 API_TOKEN 설정 시 동일 값 """ import html as _html import logging import os import gradio as gr from dotenv import load_dotenv load_dotenv() logging.basicConfig( level=os.getenv("LOG_LEVEL", "INFO").upper(), format="%(asctime)s %(levelname)-8s %(name)s — %(message)s", datefmt="%Y-%m-%d %H:%M:%S", ) logger = logging.getLogger(__name__) from container import Container container = Container() USER_LABELS = ["아록", "근혜", "도율", "하율"] DEFAULT_USER = "아록" # ── STT (Whisper) — 로컬 실행 유지 ────────────────────────────── _whisper_model = None def _get_whisper(): global _whisper_model if _whisper_model is None: import whisper _whisper_model = whisper.load_model(container.config().whisper_model_size) return _whisper_model def transcribe_audio(filepath: str) -> str: if not filepath: return "" model = _get_whisper() result = model.transcribe(filepath, language="ko") return result["text"].strip() # ── 채팅 ───────────────────────────────────────────────────────── async def respond(message, history, show_thinking, user_id, use_tts, run_ids, image_path): if not message.strip() and not image_path: yield history, "", None, run_ids, "", "", None return history = list(history) run_ids = list(run_ids) display_msg = message if image_path: display_msg = f"🖼️ [이미지 첨부]\n{message}" if message.strip() else "🖼️ [이미지 첨부]" history.append({"role": "user", "content": display_msg}) history.append({"role": "assistant", "content": ""}) yield history, "", None, run_ids, "", "", None # boxes 초기화 + 이미지 초기화 collected_run_id: str | None = None tts_text = "" thinking_acc = "" thinking_text = "" thinking_finalized = False source_box_html = "" try: async for token, run_id in container.chat_service().chat( message or "이 이미지를 분석해줘.", user_id, show_thinking, image_path=image_path ): if run_id is not None: collected_run_id = run_id break # 즉시 상태 — thinking_acc에 누적 안 함 if isinstance(token, dict) and "__status" in token: if not thinking_acc: yield history, "", None, run_ids, _status_html(token["__status"]), gr.update(), gr.update() continue # 사고 과정(LLM thinking) — 현재 줄만 live_html로 표시 if isinstance(token, dict) and "__thinking" in token: thinking_text += token["__thinking"] thinking_acc += token["__thinking"] yield history, "", None, run_ids, _live_html(_last_line(thinking_text)), gr.update(), gr.update() continue # 진행 로그(LangGraph, 검색 등) — 메시지 전체를 live_html로 표시 if isinstance(token, dict) and "__meta" in token: thinking_acc += token["__meta"] live = token["__meta"].strip() if live: yield history, "", None, run_ids, _live_html(live), gr.update(), gr.update() continue # RAG 출처 — 별도 source_box로 표시 if isinstance(token, dict) and "__sources" in token: source_box_html = _sources_html(token["__sources"]) yield history, "", None, run_ids, gr.update(), source_box_html, gr.update() continue # 첫 답변 토큰 도착 — 전체를 details로 전환 (접힌 상태) if thinking_acc and not thinking_finalized: thinking_finalized = True yield history, "", None, run_ids, _thinking_html(thinking_acc), gr.update(), gr.update() tts_text += token history[-1]["content"] += token yield history, "", None, run_ids, gr.update(), gr.update(), gr.update() except Exception as e: history[-1]["content"] += f"\n\n[오류: {e}]" yield history, "", None, run_ids, gr.update(), gr.update(), gr.update() return run_ids.append(collected_run_id) if use_tts: audio_path = await container.tts_service().speak(tts_text) yield history, "", audio_path, run_ids, gr.update(), gr.update(), gr.update() else: yield history, "", None, run_ids, gr.update(), gr.update(), gr.update() async def handle_feedback(like_data: gr.LikeData, history, run_ids, user_id): idx = like_data.index if isinstance(idx, (list, tuple)): idx = idx[0] if not isinstance(idx, int) or idx < 0 or idx >= len(history): return if history[idx].get("role") != "assistant": return asst_turn = sum(1 for m in history[:idx] if m.get("role") == "assistant") run_id = run_ids[asst_turn] if run_ids and asst_turn < len(run_ids) else None user_msg = str(history[idx - 1]["content"]) if idx > 0 else "" asst_msg = str(history[idx]["content"]) rating = 1 if like_data.liked else -1 try: await container.chat_service().save_feedback(user_id, user_msg, asst_msg, rating, run_id) except Exception as e: logger.error("피드백 저장 실패: %s", e) def switch_user(user_id): return [], [] async def reset_chat(user_id): try: await container.chat_service().reset(user_id) except Exception as e: logger.error("대화 초기화 실패: %s", e) return [], [] # ── 문서 관리 ───────────────────────────────────────────────────── async def ingest_files(files): if not files: return gr.update(value="파일을 선택해주세요.", visible=True) paths = [f if isinstance(f, str) else f.name for f in files] results = [] for path in paths: try: result = await container.document_service().ingest(path) name = os.path.basename(path) results.append(f"{name} → {result.get('chunks', '?')}개 청크") except Exception as e: results.append(f"{os.path.basename(path)} 오류: {e}") return gr.update(value="\n".join(results), visible=True) async def list_docs(): try: sources = await container.document_service().list_documents() return [[os.path.basename(s), s] for s in sources] except Exception as e: return [[f"오류: {e}", ""]] async def delete_doc(source): if not source.strip(): return "삭제할 파일 경로를 입력하세요.", await list_docs() try: await container.document_service().delete_document(source.strip()) return f"삭제 완료: {os.path.basename(source.strip())}", await list_docs() except Exception as e: return f"오류: {e}", await list_docs() # ── UI 구성 ────────────────────────────────────────────────────── _BOX_STYLE = ( "background:#f9f9f9;border-left:3px solid #bbb;border-radius:6px;" "padding:8px 14px;margin-bottom:6px;" ) _CONTENT_STYLE = ( "margin-top:6px;white-space:pre-wrap;font-size:0.85em;" "color:#555;max-height:160px;overflow-y:auto;" ) def _last_line(text: str) -> str: """현재 진행 중인 마지막 비어있지 않은 줄 반환.""" lines = [l for l in text.split("\n") if l.strip()] return lines[-1] if lines else text.strip() def _live_html(text: str) -> str: """스트리밍 중 현재 줄만 보여주는 단순 div (details 미사용 → 닫힘 현상 없음).""" return ( f'