Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,213 +1,160 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
## 1. FastAPI λ°±μλ (backend.py)
|
| 4 |
-
|
| 5 |
-
```python
|
| 6 |
-
from fastapi import FastAPI, HTTPException
|
| 7 |
-
from fastapi.middleware.cors import CORSMiddleware
|
| 8 |
-
from pydantic import BaseModel
|
| 9 |
-
from typing import List, Optional, Dict, Any
|
| 10 |
import os
|
| 11 |
-
import httpx
|
| 12 |
-
import asyncio
|
| 13 |
import json
|
|
|
|
| 14 |
from datetime import datetime
|
|
|
|
|
|
|
|
|
|
| 15 |
import logging
|
| 16 |
|
| 17 |
# λ‘κΉ
μ€μ
|
| 18 |
logging.basicConfig(level=logging.INFO)
|
| 19 |
logger = logging.getLogger(__name__)
|
| 20 |
|
| 21 |
-
app = FastAPI(title="Collaborative LLM System")
|
| 22 |
-
|
| 23 |
-
# CORS μ€μ
|
| 24 |
-
app.add_middleware(
|
| 25 |
-
CORSMiddleware,
|
| 26 |
-
allow_origins=["*"],
|
| 27 |
-
allow_credentials=True,
|
| 28 |
-
allow_methods=["*"],
|
| 29 |
-
allow_headers=["*"],
|
| 30 |
-
)
|
| 31 |
-
|
| 32 |
# νκ²½ λ³μμμ ν ν° κ°μ Έμ€κΈ°
|
| 33 |
-
FRIENDLI_TOKEN = os.getenv("FRIENDLI_TOKEN")
|
| 34 |
-
if not FRIENDLI_TOKEN:
|
| 35 |
-
raise ValueError("FRIENDLI_TOKEN νκ²½ λ³μκ° μ€μ λμ§ μμμ΅λλ€.")
|
| 36 |
-
|
| 37 |
API_URL = "https://api.friendli.ai/dedicated/v1/chat/completions"
|
| 38 |
MODEL_ID = "dep89a2fld32mcm"
|
| 39 |
|
| 40 |
-
#
|
| 41 |
-
|
| 42 |
-
role: str
|
| 43 |
-
content: str
|
| 44 |
|
| 45 |
-
class
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
|
| 51 |
-
user_query: str
|
| 52 |
-
max_iterations: int = 3
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
role: str
|
| 57 |
-
timestamp: str
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
- μ 체μ μΈ λ°©ν₯κ³Ό νλ μμν¬λ₯Ό μ μν©λλ€.
|
| 67 |
-
- μ€νμ AIμ λ΅λ³μ κ²ν νκ³ κ°μ μ μ μ μν©λλ€.
|
| 68 |
-
- ꡬ쑰μ μ΄κ³ 체κ³μ μΈ μ κ·Όμ μΆκ΅¬ν©λλ€.""",
|
| 69 |
-
|
| 70 |
-
"executor": """λΉμ μ μΈλΆμ μΈ λ΄μ©μ ꡬννλ μ€νμ AIμ
λλ€.
|
| 71 |
-
- κ°λ
μ AIμ μ§μΉ¨μ λ°λΌ ꡬ체μ μΈ λ΄μ©μ μμ±ν©λλ€.
|
| 72 |
-
- μ€μ©μ μ΄κ³ μ€ν κ°λ₯ν μΈλΆ μ¬νμ μ§μ€ν©λλ€.
|
| 73 |
-
- νμμ κ°λ
μμκ² μΆκ° μ§μΉ¨μ μμ²ν©λλ€."""
|
| 74 |
-
}
|
| 75 |
-
|
| 76 |
-
# λ©μμ§ κ΅¬μ±
|
| 77 |
-
full_messages = [
|
| 78 |
-
{"role": "system", "content": system_prompts.get(role, "")},
|
| 79 |
-
*messages
|
| 80 |
-
]
|
| 81 |
-
|
| 82 |
-
headers = {
|
| 83 |
-
"Authorization": f"Bearer {FRIENDLI_TOKEN}",
|
| 84 |
-
"Content-Type": "application/json"
|
| 85 |
-
}
|
| 86 |
|
| 87 |
-
|
| 88 |
-
"
|
| 89 |
-
"
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
|
|
|
| 96 |
|
| 97 |
-
|
|
|
|
| 98 |
try:
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
data = response.json()
|
| 120 |
return data["choices"][0]["message"]["content"]
|
|
|
|
|
|
|
|
|
|
| 121 |
|
| 122 |
except Exception as e:
|
| 123 |
-
logger.error(f"LLM
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
# API μλν¬μΈνΈ
|
| 127 |
-
@app.post("/api/llm/single")
|
| 128 |
-
async def single_llm_call(request: LLMRequest):
|
| 129 |
-
"""λ¨μΌ LLM νΈμΆ"""
|
| 130 |
-
messages = [{"role": msg.role, "content": msg.content} for msg in request.messages]
|
| 131 |
-
|
| 132 |
-
if request.stream:
|
| 133 |
-
async def generate():
|
| 134 |
-
async for chunk in call_llm(messages, request.role, stream=True):
|
| 135 |
-
yield chunk
|
| 136 |
-
return generate()
|
| 137 |
-
else:
|
| 138 |
-
content = await call_llm(messages, request.role, stream=False)
|
| 139 |
-
return LLMResponse(
|
| 140 |
-
content=content,
|
| 141 |
-
role=request.role,
|
| 142 |
-
timestamp=datetime.now().isoformat()
|
| 143 |
-
)
|
| 144 |
-
|
| 145 |
-
@app.post("/api/llm/collaborate")
|
| 146 |
-
async def collaborative_process(request: CollaborativeRequest):
|
| 147 |
-
"""νλ ₯μ LLM μ²λ¦¬"""
|
| 148 |
-
user_query = request.user_query
|
| 149 |
-
conversation_log = []
|
| 150 |
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
μ΄ μ§λ¬Έμ λν μ 체μ μΈ μ κ·Ό λ°©ν₯κ³Ό νλ μμν¬λ₯Ό μ μν΄μ£ΌμΈμ.
|
| 156 |
-
ν΅μ¬ μμμ κ³ λ €μ¬νμ ꡬ쑰ννμ¬ μ€λͺ
ν΄μ£ΌμΈμ."""
|
| 157 |
-
|
| 158 |
-
supervisor_response = ""
|
| 159 |
-
async for chunk in call_llm([{"role": "user", "content": supervisor_prompt}], "supervisor"):
|
| 160 |
-
supervisor_response += chunk
|
| 161 |
-
|
| 162 |
-
conversation_log.append({
|
| 163 |
-
"role": "supervisor",
|
| 164 |
-
"stage": "initial_analysis",
|
| 165 |
-
"content": supervisor_response,
|
| 166 |
-
"timestamp": datetime.now().isoformat()
|
| 167 |
-
})
|
| 168 |
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
executor_response = ""
|
| 178 |
-
async for chunk in call_llm([{"role": "user", "content": executor_prompt}], "executor"):
|
| 179 |
-
executor_response += chunk
|
| 180 |
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
# 3λ¨κ³: κ°λ
μ AIμ κ²ν λ° νΌλλ°±
|
| 189 |
-
review_prompt = f"""μ¬μ©μ μ§λ¬Έ: {user_query}
|
| 190 |
-
|
| 191 |
-
μ€νμ AIμ λ΅λ³:
|
| 192 |
-
{executor_response}
|
| 193 |
-
|
| 194 |
-
μ΄ λ΅λ³μ κ²ν νκ³ κ°μ μ κ³Ό μΆκ° κ³ λ €μ¬νμ μ μν΄μ£ΌμΈμ."""
|
| 195 |
-
|
| 196 |
-
review_response = ""
|
| 197 |
-
async for chunk in call_llm([{"role": "user", "content": review_prompt}], "supervisor"):
|
| 198 |
-
review_response += chunk
|
| 199 |
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 209 |
|
| 210 |
-
###
|
| 211 |
{user_query}
|
| 212 |
|
| 213 |
### π κ±°μμ λΆμ (κ°λ
μ AI)
|
|
@@ -221,348 +168,168 @@ async def collaborative_process(request: CollaborativeRequest):
|
|
| 221 |
|
| 222 |
---
|
| 223 |
*μ΄ λ΅λ³μ κ±°μμ κ΄μ κ³Ό λ―Έμμ μΈλΆμ¬νμ μ’
ν©νμ¬ μμ±λμμ΅λλ€.*"""
|
| 224 |
-
|
| 225 |
-
return {
|
| 226 |
-
"final_result": final_summary,
|
| 227 |
-
"conversation_log": conversation_log,
|
| 228 |
-
"timestamp": datetime.now().isoformat()
|
| 229 |
-
}
|
| 230 |
-
|
| 231 |
-
except Exception as e:
|
| 232 |
-
logger.error(f"νλ ₯ μ²λ¦¬ μ€λ₯: {str(e)}")
|
| 233 |
-
raise HTTPException(status_code=500, detail=str(e))
|
| 234 |
-
|
| 235 |
-
@app.get("/health")
|
| 236 |
-
async def health_check():
|
| 237 |
-
return {"status": "healthy", "timestamp": datetime.now().isoformat()}
|
| 238 |
-
|
| 239 |
-
if __name__ == "__main__":
|
| 240 |
-
import uvicorn
|
| 241 |
-
uvicorn.run(app, host="0.0.0.0", port=8000)
|
| 242 |
-
```
|
| 243 |
-
|
| 244 |
-
## 2. Streamlit νλ‘ νΈμλ (streamlit_app.py)
|
| 245 |
-
|
| 246 |
-
```python
|
| 247 |
-
import streamlit as st
|
| 248 |
-
import requests
|
| 249 |
-
import json
|
| 250 |
-
from datetime import datetime
|
| 251 |
-
import time
|
| 252 |
-
import os
|
| 253 |
-
|
| 254 |
-
# νμ΄μ§ μ€μ
|
| 255 |
-
st.set_page_config(
|
| 256 |
-
page_title="νλ ₯μ LLM μμ€ν
",
|
| 257 |
-
page_icon="π€",
|
| 258 |
-
layout="wide",
|
| 259 |
-
initial_sidebar_state="expanded"
|
| 260 |
-
)
|
| 261 |
-
|
| 262 |
-
# μ€νμΌ μ€μ
|
| 263 |
-
st.markdown("""
|
| 264 |
-
<style>
|
| 265 |
-
.main-header {
|
| 266 |
-
text-align: center;
|
| 267 |
-
background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
|
| 268 |
-
-webkit-background-clip: text;
|
| 269 |
-
-webkit-text-fill-color: transparent;
|
| 270 |
-
font-size: 3rem;
|
| 271 |
-
font-weight: bold;
|
| 272 |
-
margin-bottom: 2rem;
|
| 273 |
-
}
|
| 274 |
-
.llm-box {
|
| 275 |
-
background-color: #f0f2f6;
|
| 276 |
-
border-radius: 10px;
|
| 277 |
-
padding: 1rem;
|
| 278 |
-
margin-bottom: 1rem;
|
| 279 |
-
border-left: 4px solid;
|
| 280 |
-
}
|
| 281 |
-
.supervisor-box {
|
| 282 |
-
border-left-color: #667eea;
|
| 283 |
-
}
|
| 284 |
-
.executor-box {
|
| 285 |
-
border-left-color: #764ba2;
|
| 286 |
-
}
|
| 287 |
-
.timestamp {
|
| 288 |
-
font-size: 0.8rem;
|
| 289 |
-
color: #666;
|
| 290 |
-
}
|
| 291 |
-
</style>
|
| 292 |
-
""", unsafe_allow_html=True)
|
| 293 |
-
|
| 294 |
-
# λ°±μλ URL
|
| 295 |
-
BACKEND_URL = os.getenv("BACKEND_URL", "http://localhost:8000")
|
| 296 |
-
|
| 297 |
-
# μΈμ
μν μ΄κΈ°ν
|
| 298 |
-
if "conversation_history" not in st.session_state:
|
| 299 |
-
st.session_state.conversation_history = []
|
| 300 |
-
if "current_conversation" not in st.session_state:
|
| 301 |
-
st.session_state.current_conversation = None
|
| 302 |
-
|
| 303 |
-
# ν€λ
|
| 304 |
-
st.markdown('<h1 class="main-header">π€ νλ ₯μ LLM μμ€ν
</h1>', unsafe_allow_html=True)
|
| 305 |
-
st.markdown("κ±°μμ κ°λ
μ AIμ λ―Έμμ μ€νμ AIκ° νλ ₯νμ¬ μ΅μμ λ΅λ³μ λ§λ€μ΄λ
λλ€.")
|
| 306 |
-
|
| 307 |
-
# μ¬μ΄λλ°
|
| 308 |
-
with st.sidebar:
|
| 309 |
-
st.header("βοΈ μ€μ ")
|
| 310 |
-
|
| 311 |
-
# λ°±μλ μν νμΈ
|
| 312 |
-
try:
|
| 313 |
-
response = requests.get(f"{BACKEND_URL}/health")
|
| 314 |
-
if response.status_code == 200:
|
| 315 |
-
st.success("β
λ°±μλ μ°κ²°λ¨")
|
| 316 |
-
else:
|
| 317 |
-
st.error("β λ°±μλ μ°κ²° μ€ν¨")
|
| 318 |
-
except:
|
| 319 |
-
st.error("β λ°±μλ μλ²λ₯Ό μμν΄μ£ΌμΈμ")
|
| 320 |
-
|
| 321 |
-
st.divider()
|
| 322 |
-
|
| 323 |
-
# λν κΈ°λ‘
|
| 324 |
-
st.header("π λν κΈ°λ‘")
|
| 325 |
-
if st.session_state.conversation_history:
|
| 326 |
-
for idx, conv in enumerate(reversed(st.session_state.conversation_history)):
|
| 327 |
-
if st.button(f"π¬ {conv['query'][:30]}...", key=f"hist_{idx}"):
|
| 328 |
-
st.session_state.current_conversation = conv
|
| 329 |
-
else:
|
| 330 |
-
st.info("λν κΈ°λ‘μ΄ μμ΅λλ€.")
|
| 331 |
-
|
| 332 |
-
if st.button("ποΈ κΈ°λ‘ μ΄κΈ°ν"):
|
| 333 |
-
st.session_state.conversation_history = []
|
| 334 |
-
st.session_state.current_conversation = None
|
| 335 |
-
st.rerun()
|
| 336 |
-
|
| 337 |
-
# λ©μΈ 컨ν
μΈ
|
| 338 |
-
main_container = st.container()
|
| 339 |
-
|
| 340 |
-
# μ§λ¬Έ μ
λ ₯
|
| 341 |
-
with main_container:
|
| 342 |
-
col1, col2 = st.columns([5, 1])
|
| 343 |
-
with col1:
|
| 344 |
-
user_query = st.text_input(
|
| 345 |
-
"μ§λ¬Έμ μ
λ ₯νμΈμ:",
|
| 346 |
-
placeholder="μ: κΈ°κ³νμ΅ λͺ¨λΈμ μ±λ₯μ ν₯μμν€λ λ°©λ²μ?",
|
| 347 |
-
key="user_input"
|
| 348 |
-
)
|
| 349 |
-
with col2:
|
| 350 |
-
process_button = st.button("π λΆμ μμ", type="primary", use_container_width=True)
|
| 351 |
-
|
| 352 |
-
# μ²λ¦¬ λ‘μ§
|
| 353 |
-
if process_button and user_query:
|
| 354 |
-
with st.spinner("AIλ€μ΄ νλ ₯νμ¬ λ΅λ³μ μμ±νκ³ μμ΅λλ€..."):
|
| 355 |
-
try:
|
| 356 |
-
# λ°±μλμ μμ²
|
| 357 |
-
response = requests.post(
|
| 358 |
-
f"{BACKEND_URL}/api/llm/collaborate",
|
| 359 |
-
json={"user_query": user_query, "max_iterations": 3}
|
| 360 |
-
)
|
| 361 |
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
"timestamp": datetime.now().isoformat()
|
| 370 |
-
}
|
| 371 |
-
st.session_state.conversation_history.append(conversation)
|
| 372 |
-
st.session_state.current_conversation = conversation
|
| 373 |
-
|
| 374 |
-
st.success("β
λΆμ μλ£!")
|
| 375 |
-
else:
|
| 376 |
-
st.error(f"μ€λ₯ λ°μ: {response.status_code}")
|
| 377 |
-
|
| 378 |
except Exception as e:
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
st.markdown("### π μ΅μ’
μ’
ν© κ²°κ³Ό")
|
| 387 |
-
with st.expander("νΌμΉκΈ°", expanded=True):
|
| 388 |
-
st.markdown(conv["result"]["final_result"])
|
| 389 |
-
|
| 390 |
-
# λν κ³Όμ
|
| 391 |
-
st.markdown("### π AI νλ ₯ κ³Όμ ")
|
| 392 |
-
|
| 393 |
-
# λ κ°μ 컬λΌμΌλ‘ λΆν
|
| 394 |
-
col1, col2 = st.columns(2)
|
| 395 |
-
|
| 396 |
-
supervisor_logs = [log for log in conv["result"]["conversation_log"] if log["role"] == "supervisor"]
|
| 397 |
-
executor_logs = [log for log in conv["result"]["conversation_log"] if log["role"] == "executor"]
|
| 398 |
-
|
| 399 |
-
with col1:
|
| 400 |
-
st.markdown("#### π§ κ°λ
μ AI (κ±°μμ λΆμ)")
|
| 401 |
-
for log in supervisor_logs:
|
| 402 |
-
with st.container():
|
| 403 |
-
st.markdown(f'<div class="llm-box supervisor-box">', unsafe_allow_html=True)
|
| 404 |
-
st.markdown(f"**{log['stage']}**")
|
| 405 |
-
st.text(log["content"][:500] + "..." if len(log["content"]) > 500 else log["content"])
|
| 406 |
-
st.markdown(f'<span class="timestamp">{log["timestamp"]}</span>', unsafe_allow_html=True)
|
| 407 |
-
st.markdown('</div>', unsafe_allow_html=True)
|
| 408 |
-
|
| 409 |
-
with col2:
|
| 410 |
-
st.markdown("#### ποΈ μ€νμ AI (λ―Έμμ ꡬν)")
|
| 411 |
-
for log in executor_logs:
|
| 412 |
-
with st.container():
|
| 413 |
-
st.markdown(f'<div class="llm-box executor-box">', unsafe_allow_html=True)
|
| 414 |
-
st.markdown(f"**{log['stage']}**")
|
| 415 |
-
st.text(log["content"][:500] + "..." if len(log["content"]) > 500 else log["content"])
|
| 416 |
-
st.markdown(f'<span class="timestamp">{log["timestamp"]}</span>', unsafe_allow_html=True)
|
| 417 |
-
st.markdown('</div>', unsafe_allow_html=True)
|
| 418 |
-
|
| 419 |
-
# νλ¨ μ 보
|
| 420 |
-
st.divider()
|
| 421 |
-
st.markdown("""
|
| 422 |
-
<div style="text-align: center; color: #666;">
|
| 423 |
-
π‘ μ΄ μμ€ν
μ λ AIκ° μλ‘ λ€λ₯Έ κ΄μ μμ μ κ·Όνμ¬ λ λμ λ΅λ³μ λ§λ€μ΄λ
λλ€.<br>
|
| 424 |
-
κ°λ
μ AIλ μ 체μ μΈ λ°©ν₯μ, μ€νμ AIλ ꡬ체μ μΈ μΈλΆμ¬νμ λ΄λΉν©λλ€.
|
| 425 |
-
</div>
|
| 426 |
-
""", unsafe_allow_html=True)
|
| 427 |
-
```
|
| 428 |
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
```python
|
| 432 |
-
import gradio as gr
|
| 433 |
-
import requests
|
| 434 |
-
import json
|
| 435 |
-
from datetime import datetime
|
| 436 |
-
import os
|
| 437 |
-
import asyncio
|
| 438 |
-
|
| 439 |
-
# λ°±μλ URL
|
| 440 |
-
BACKEND_URL = os.getenv("BACKEND_URL", "http://localhost:8000")
|
| 441 |
-
|
| 442 |
-
# κΈλ‘λ² λ³μ
|
| 443 |
-
conversation_history = []
|
| 444 |
-
|
| 445 |
-
def check_backend_status():
|
| 446 |
-
"""λ°±μλ μν νμΈ"""
|
| 447 |
-
try:
|
| 448 |
-
response = requests.get(f"{BACKEND_URL}/health")
|
| 449 |
-
return response.status_code == 200
|
| 450 |
-
except:
|
| 451 |
-
return False
|
| 452 |
|
| 453 |
def process_query(user_query, history):
|
| 454 |
-
"""
|
| 455 |
if not user_query:
|
| 456 |
-
return history, "", "", "", "μ§λ¬Έμ μ
λ ₯ν΄μ£ΌμΈμ."
|
|
|
|
|
|
|
|
|
|
| 457 |
|
| 458 |
try:
|
| 459 |
-
#
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 463 |
)
|
| 464 |
|
| 465 |
-
if response.status_code == 200:
|
| 466 |
-
result = response.json()
|
| 467 |
-
|
| 468 |
-
# λν λ‘κ·Έ νμ±
|
| 469 |
-
supervisor_text = ""
|
| 470 |
-
executor_text = ""
|
| 471 |
-
|
| 472 |
-
for log in result["conversation_log"]:
|
| 473 |
-
if log["role"] == "supervisor":
|
| 474 |
-
supervisor_text += f"[{log['stage']}]\n{log['content']}\n\n"
|
| 475 |
-
else:
|
| 476 |
-
executor_text += f"[{log['stage']}]\n{log['content']}\n\n"
|
| 477 |
-
|
| 478 |
-
# νμ€ν 리 μ
λ°μ΄νΈ
|
| 479 |
-
new_history = history + [(user_query, result["final_result"])]
|
| 480 |
-
|
| 481 |
-
return (
|
| 482 |
-
new_history,
|
| 483 |
-
supervisor_text,
|
| 484 |
-
executor_text,
|
| 485 |
-
result["final_result"],
|
| 486 |
-
"β
λΆμ μλ£!"
|
| 487 |
-
)
|
| 488 |
-
else:
|
| 489 |
-
return history, "", "", "", f"β μ€λ₯ λ°μ: {response.status_code}"
|
| 490 |
-
|
| 491 |
except Exception as e:
|
| 492 |
-
|
|
|
|
| 493 |
|
| 494 |
def clear_all():
|
| 495 |
"""λͺ¨λ λ΄μ© μ΄κΈ°ν"""
|
| 496 |
-
return [], "", "", "", ""
|
| 497 |
-
|
| 498 |
-
# Gradio μΈν°νμ΄μ€
|
| 499 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 500 |
gr.Markdown(
|
| 501 |
"""
|
| 502 |
# π€ νλ ₯μ LLM μμ€ν
|
| 503 |
|
| 504 |
-
κ±°μμ κ°λ
μ AIμ λ―Έμμ μ€νμ AIκ° νλ ₯νμ¬ μ΅μμ λ΅λ³μ λ§λ€μ΄λ
λλ€.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 505 |
"""
|
| 506 |
)
|
| 507 |
|
| 508 |
-
# λ°±μλ μν νμ
|
| 509 |
-
with gr.Row():
|
| 510 |
-
backend_status = gr.Markdown(
|
| 511 |
-
"π’ λ°±μλ μ°κ²°λ¨" if check_backend_status() else "π΄ λ°±μλ μ°κ²° μλ¨"
|
| 512 |
-
)
|
| 513 |
-
|
| 514 |
with gr.Row():
|
| 515 |
# μΌμͺ½: μ
λ ₯ λ° μ±ν
κΈ°λ‘
|
| 516 |
with gr.Column(scale=1):
|
| 517 |
chatbot = gr.Chatbot(
|
| 518 |
label="π¬ λν κΈ°λ‘",
|
| 519 |
-
height=
|
| 520 |
-
show_copy_button=True
|
|
|
|
| 521 |
)
|
| 522 |
|
| 523 |
user_input = gr.Textbox(
|
| 524 |
label="μ§λ¬Έ μ
λ ₯",
|
| 525 |
placeholder="μ: κΈ°κ³νμ΅ λͺ¨λΈμ μ±λ₯μ ν₯μμν€λ λ°©λ²μ?",
|
| 526 |
-
lines=
|
| 527 |
)
|
| 528 |
|
| 529 |
with gr.Row():
|
| 530 |
-
submit_btn = gr.Button("π λΆμ μμ", variant="primary")
|
| 531 |
-
clear_btn = gr.Button("ποΈ μ΄κΈ°ν")
|
| 532 |
|
| 533 |
status_text = gr.Textbox(
|
| 534 |
label="μν",
|
| 535 |
interactive=False,
|
| 536 |
-
value="λκΈ° μ€..."
|
|
|
|
| 537 |
)
|
| 538 |
|
| 539 |
# μ€λ₯Έμͺ½: AI μΆλ ₯
|
| 540 |
with gr.Column(scale=2):
|
| 541 |
# μ΅μ’
κ²°κ³Ό
|
| 542 |
-
|
| 543 |
-
|
| 544 |
-
|
| 545 |
-
|
| 546 |
|
| 547 |
with gr.Row():
|
| 548 |
# κ°λ
μ AI μΆλ ₯
|
| 549 |
with gr.Column():
|
|
|
|
| 550 |
supervisor_output = gr.Textbox(
|
| 551 |
-
label="
|
| 552 |
lines=15,
|
| 553 |
max_lines=20,
|
| 554 |
-
interactive=False
|
|
|
|
| 555 |
)
|
| 556 |
|
| 557 |
# μ€νμ AI μΆλ ₯
|
| 558 |
with gr.Column():
|
|
|
|
| 559 |
executor_output = gr.Textbox(
|
| 560 |
-
label="
|
| 561 |
lines=15,
|
| 562 |
max_lines=20,
|
| 563 |
-
interactive=False
|
|
|
|
| 564 |
)
|
| 565 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 566 |
# μ΄λ²€νΈ νΈλ€λ¬
|
| 567 |
submit_btn.click(
|
| 568 |
fn=process_query,
|
|
@@ -587,24 +354,17 @@ with gr.Blocks(title="νλ ₯μ LLM μμ€ν
", theme=gr.themes.Soft()) as app:
|
|
| 587 |
outputs=[chatbot, supervisor_output, executor_output, final_output, status_text]
|
| 588 |
)
|
| 589 |
|
| 590 |
-
# μμ
|
| 591 |
-
gr.Examples(
|
| 592 |
-
examples=[
|
| 593 |
-
"κΈ°κ³νμ΅ λͺ¨λΈμ μ±λ₯μ ν₯μμν€λ λ°©λ²μ?",
|
| 594 |
-
"ν¨κ³Όμ μΈ νλ‘μ νΈ κ΄λ¦¬ μ λ΅μ μ립νλ λ°©λ²μ?",
|
| 595 |
-
"μ§μ κ°λ₯ν λΉμ¦λμ€ λͺ¨λΈμ ꡬμΆνλ λ°©λ²μ?",
|
| 596 |
-
"볡μ‘ν λ°μ΄ν°λ₯Ό μκ°ννλ μ΅μ μ λ°©λ²μ?"
|
| 597 |
-
],
|
| 598 |
-
inputs=user_input
|
| 599 |
-
)
|
| 600 |
-
|
| 601 |
gr.Markdown(
|
| 602 |
"""
|
| 603 |
---
|
| 604 |
-
|
| 605 |
-
|
| 606 |
-
|
| 607 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 608 |
"""
|
| 609 |
)
|
| 610 |
|
|
@@ -612,6 +372,6 @@ if __name__ == "__main__":
|
|
| 612 |
app.launch(
|
| 613 |
server_name="0.0.0.0",
|
| 614 |
server_port=7860,
|
| 615 |
-
share=
|
| 616 |
show_error=True
|
| 617 |
-
)
|
|
|
|
| 1 |
+
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import os
|
|
|
|
|
|
|
| 3 |
import json
|
| 4 |
+
import requests
|
| 5 |
from datetime import datetime
|
| 6 |
+
import asyncio
|
| 7 |
+
import aiohttp
|
| 8 |
+
from typing import List, Dict, Any
|
| 9 |
import logging
|
| 10 |
|
| 11 |
# λ‘κΉ
μ€μ
|
| 12 |
logging.basicConfig(level=logging.INFO)
|
| 13 |
logger = logging.getLogger(__name__)
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
# νκ²½ λ³μμμ ν ν° κ°μ Έμ€κΈ°
|
| 16 |
+
FRIENDLI_TOKEN = os.getenv("FRIENDLI_TOKEN", "YOUR_FRIENDLI_TOKEN")
|
|
|
|
|
|
|
|
|
|
| 17 |
API_URL = "https://api.friendli.ai/dedicated/v1/chat/completions"
|
| 18 |
MODEL_ID = "dep89a2fld32mcm"
|
| 19 |
|
| 20 |
+
# μ μ λ³μ
|
| 21 |
+
conversation_history = []
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
class LLMCollaborativeSystem:
|
| 24 |
+
def __init__(self):
|
| 25 |
+
self.token = FRIENDLI_TOKEN
|
| 26 |
+
self.api_url = API_URL
|
| 27 |
+
self.model_id = MODEL_ID
|
| 28 |
+
|
| 29 |
+
def create_headers(self):
|
| 30 |
+
"""API ν€λ μμ±"""
|
| 31 |
+
return {
|
| 32 |
+
"Authorization": f"Bearer {self.token}",
|
| 33 |
+
"Content-Type": "application/json"
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
def create_supervisor_prompt(self, user_query: str, executor_response: str = None) -> str:
|
| 37 |
+
"""κ°λ
μ AI ν둬ννΈ μμ±"""
|
| 38 |
+
if executor_response:
|
| 39 |
+
return f"""λΉμ μ κ±°μμ κ΄μ μμ λΆμνκ³ μ§λνλ κ°λ
μ AIμ
λλ€.
|
| 40 |
|
| 41 |
+
μ¬μ©μ μ§λ¬Έ: {user_query}
|
|
|
|
|
|
|
| 42 |
|
| 43 |
+
μ€νμ AIμ λ΅λ³:
|
| 44 |
+
{executor_response}
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
μ΄ λ΅λ³μ κ²ν νκ³ κ°μ μ κ³Ό μΆκ° κ³ λ €μ¬νμ μ μν΄μ£ΌμΈμ. ꡬ쑰μ μ΄κ³ 체κ³μ μΈ νΌλλ°±μ μ 곡νμΈμ."""
|
| 47 |
+
else:
|
| 48 |
+
return f"""λΉμ μ κ±°μμ κ΄μ μμ λΆμνκ³ μ§λνλ κ°λ
μ AIμ
λλ€.
|
| 49 |
+
|
| 50 |
+
μ¬μ©μ μ§λ¬Έ: {user_query}
|
| 51 |
+
|
| 52 |
+
μ΄ μ§λ¬Έμ λν μ 체μ μΈ μ κ·Ό λ°©ν₯κ³Ό νλ μμν¬λ₯Ό μ μν΄μ£ΌμΈμ. ν΅μ¬ μμμ κ³ λ €μ¬νμ ꡬ쑰ννμ¬ μ€λͺ
ν΄μ£ΌμΈμ."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
+
def create_executor_prompt(self, user_query: str, supervisor_guidance: str) -> str:
|
| 55 |
+
"""μ€νμ AI ν둬ννΈ μμ±"""
|
| 56 |
+
return f"""λΉμ μ μΈλΆμ μΈ λ΄μ©μ ꡬννλ μ€νμ AIμ
λλ€.
|
| 57 |
+
|
| 58 |
+
κ°λ
μ AIμ μ§μΉ¨:
|
| 59 |
+
{supervisor_guidance}
|
| 60 |
+
|
| 61 |
+
μ¬μ©μ μ§λ¬Έ: {user_query}
|
| 62 |
+
|
| 63 |
+
μ μ§μΉ¨μ λ°νμΌλ‘ ꡬ체μ μ΄κ³ μ€ν κ°λ₯ν μΈλΆ λ΄μ©μ μμ±ν΄μ£ΌμΈμ. μ€μ©μ μ΄κ³ ꡬν κ°λ₯ν ν΄κ²°μ±
μ μ§μ€νμΈμ."""
|
| 64 |
|
| 65 |
+
def call_llm_sync(self, messages: List[Dict[str, str]], role: str) -> str:
|
| 66 |
+
"""λκΈ°μ LLM API νΈμΆ"""
|
| 67 |
try:
|
| 68 |
+
# μν μ λ°λ₯Έ μμ€ν
ν둬ννΈ
|
| 69 |
+
system_prompts = {
|
| 70 |
+
"supervisor": "λΉμ μ κ±°μμ κ΄μ μμ λΆμνκ³ μ§λνλ κ°λ
μ AIμ
λλ€.",
|
| 71 |
+
"executor": "λΉμ μ μΈλΆμ μΈ λ΄μ©μ ꡬννλ μ€νμ AIμ
λλ€."
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
# λ©μμ§ κ΅¬μ±
|
| 75 |
+
full_messages = [
|
| 76 |
+
{"role": "system", "content": system_prompts.get(role, "")},
|
| 77 |
+
*messages
|
| 78 |
+
]
|
| 79 |
+
|
| 80 |
+
payload = {
|
| 81 |
+
"model": self.model_id,
|
| 82 |
+
"messages": full_messages,
|
| 83 |
+
"max_tokens": 2048,
|
| 84 |
+
"temperature": 0.7,
|
| 85 |
+
"top_p": 0.8,
|
| 86 |
+
"stream": False
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
response = requests.post(
|
| 90 |
+
self.api_url,
|
| 91 |
+
headers=self.create_headers(),
|
| 92 |
+
json=payload,
|
| 93 |
+
timeout=60
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
if response.status_code == 200:
|
| 97 |
data = response.json()
|
| 98 |
return data["choices"][0]["message"]["content"]
|
| 99 |
+
else:
|
| 100 |
+
logger.error(f"API μ€λ₯: {response.status_code}")
|
| 101 |
+
return f"API νΈμΆ μ€λ₯: {response.status_code}"
|
| 102 |
|
| 103 |
except Exception as e:
|
| 104 |
+
logger.error(f"LLM νΈμΆ μ€ μ€λ₯: {str(e)}")
|
| 105 |
+
return f"μ€λ₯ λ°μ: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
+
def process_collaborative(self, user_query: str) -> Dict[str, Any]:
|
| 108 |
+
"""νλ ₯μ μ²λ¦¬ νλ‘μΈμ€"""
|
| 109 |
+
conversation_log = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
+
try:
|
| 112 |
+
# 1λ¨κ³: κ°λ
μ AIμ μ΄κΈ° λΆμ
|
| 113 |
+
supervisor_prompt = self.create_supervisor_prompt(user_query)
|
| 114 |
+
supervisor_response = self.call_llm_sync(
|
| 115 |
+
[{"role": "user", "content": supervisor_prompt}],
|
| 116 |
+
"supervisor"
|
| 117 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
+
conversation_log.append({
|
| 120 |
+
"role": "supervisor",
|
| 121 |
+
"stage": "μ΄κΈ° λΆμ",
|
| 122 |
+
"content": supervisor_response,
|
| 123 |
+
"timestamp": datetime.now().strftime("%H:%M:%S")
|
| 124 |
+
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
+
# 2λ¨κ³: μ€νμ AIμ μΈλΆ ꡬν
|
| 127 |
+
executor_prompt = self.create_executor_prompt(user_query, supervisor_response)
|
| 128 |
+
executor_response = self.call_llm_sync(
|
| 129 |
+
[{"role": "user", "content": executor_prompt}],
|
| 130 |
+
"executor"
|
| 131 |
+
)
|
| 132 |
+
|
| 133 |
+
conversation_log.append({
|
| 134 |
+
"role": "executor",
|
| 135 |
+
"stage": "μΈλΆ ꡬν",
|
| 136 |
+
"content": executor_response,
|
| 137 |
+
"timestamp": datetime.now().strftime("%H:%M:%S")
|
| 138 |
+
})
|
| 139 |
+
|
| 140 |
+
# 3λ¨κ³: κ°λ
μ AIμ κ²ν λ° νΌλλ°±
|
| 141 |
+
review_prompt = self.create_supervisor_prompt(user_query, executor_response)
|
| 142 |
+
review_response = self.call_llm_sync(
|
| 143 |
+
[{"role": "user", "content": review_prompt}],
|
| 144 |
+
"supervisor"
|
| 145 |
+
)
|
| 146 |
+
|
| 147 |
+
conversation_log.append({
|
| 148 |
+
"role": "supervisor",
|
| 149 |
+
"stage": "κ²ν λ° νΌλλ°±",
|
| 150 |
+
"content": review_response,
|
| 151 |
+
"timestamp": datetime.now().strftime("%H:%M:%S")
|
| 152 |
+
})
|
| 153 |
+
|
| 154 |
+
# 4λ¨κ³: μ΅μ’
μ’
ν©
|
| 155 |
+
final_summary = f"""## π€ νλ ₯μ AI μμ€ν
μ’
ν© λ΅λ³
|
| 156 |
|
| 157 |
+
### π μ¬μ©μ μ§λ¬Έ
|
| 158 |
{user_query}
|
| 159 |
|
| 160 |
### π κ±°μμ λΆμ (κ°λ
μ AI)
|
|
|
|
| 168 |
|
| 169 |
---
|
| 170 |
*μ΄ λ΅λ³μ κ±°μμ κ΄μ κ³Ό λ―Έμμ μΈλΆμ¬νμ μ’
ν©νμ¬ μμ±λμμ΅λλ€.*"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
|
| 172 |
+
return {
|
| 173 |
+
"final_result": final_summary,
|
| 174 |
+
"conversation_log": conversation_log,
|
| 175 |
+
"supervisor_texts": [log["content"] for log in conversation_log if log["role"] == "supervisor"],
|
| 176 |
+
"executor_texts": [log["content"] for log in conversation_log if log["role"] == "executor"]
|
| 177 |
+
}
|
| 178 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
except Exception as e:
|
| 180 |
+
logger.error(f"νλ ₯ μ²λ¦¬ μ€λ₯: {str(e)}")
|
| 181 |
+
return {
|
| 182 |
+
"final_result": f"μ²λ¦¬ μ€ μ€λ₯κ° λ°μνμ΅λλ€: {str(e)}",
|
| 183 |
+
"conversation_log": [],
|
| 184 |
+
"supervisor_texts": [],
|
| 185 |
+
"executor_texts": []
|
| 186 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
|
| 188 |
+
# μμ€ν
μΈμ€ν΄μ€ μμ±
|
| 189 |
+
llm_system = LLMCollaborativeSystem()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
|
| 191 |
def process_query(user_query, history):
|
| 192 |
+
"""Gradio μΈν°νμ΄μ€λ₯Ό μν 쿼리 μ²λ¦¬ ν¨μ"""
|
| 193 |
if not user_query:
|
| 194 |
+
return history, "", "", "", "β μ§λ¬Έμ μ
λ ₯ν΄μ£ΌμΈμ."
|
| 195 |
+
|
| 196 |
+
# μ²λ¦¬ μμ
|
| 197 |
+
status = "π AIλ€μ΄ νλ ₯νμ¬ λ΅λ³μ μμ±νκ³ μμ΅λλ€..."
|
| 198 |
|
| 199 |
try:
|
| 200 |
+
# νλ ₯μ μ²λ¦¬
|
| 201 |
+
result = llm_system.process_collaborative(user_query)
|
| 202 |
+
|
| 203 |
+
# κ°λ
μμ μ€νμ ν
μ€νΈ ν¬λ§·ν
|
| 204 |
+
supervisor_text = "\n\n---\n\n".join([
|
| 205 |
+
f"[{log['stage']}] - {log['timestamp']}\n{log['content']}"
|
| 206 |
+
for log in result["conversation_log"] if log["role"] == "supervisor"
|
| 207 |
+
])
|
| 208 |
+
|
| 209 |
+
executor_text = "\n\n---\n\n".join([
|
| 210 |
+
f"[{log['stage']}] - {log['timestamp']}\n{log['content']}"
|
| 211 |
+
for log in result["conversation_log"] if log["role"] == "executor"
|
| 212 |
+
])
|
| 213 |
+
|
| 214 |
+
# νμ€ν 리 μ
λ°μ΄νΈ
|
| 215 |
+
new_history = history + [(user_query, result["final_result"])]
|
| 216 |
+
|
| 217 |
+
return (
|
| 218 |
+
new_history,
|
| 219 |
+
supervisor_text,
|
| 220 |
+
executor_text,
|
| 221 |
+
result["final_result"],
|
| 222 |
+
"β
λΆμ μλ£!"
|
| 223 |
)
|
| 224 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 225 |
except Exception as e:
|
| 226 |
+
error_msg = f"β μ²λ¦¬ μ€ μ€λ₯: {str(e)}"
|
| 227 |
+
return history, "", "", error_msg, error_msg
|
| 228 |
|
| 229 |
def clear_all():
|
| 230 |
"""λͺ¨λ λ΄μ© μ΄κΈ°ν"""
|
| 231 |
+
return [], "", "", "", "π μ΄κΈ°νλμμ΅λλ€."
|
| 232 |
+
|
| 233 |
+
# Gradio μΈν°νμ΄μ€
|
| 234 |
+
css = """
|
| 235 |
+
.gradio-container {
|
| 236 |
+
font-family: 'Arial', sans-serif;
|
| 237 |
+
}
|
| 238 |
+
.supervisor-box {
|
| 239 |
+
border-left: 4px solid #667eea;
|
| 240 |
+
padding-left: 10px;
|
| 241 |
+
}
|
| 242 |
+
.executor-box {
|
| 243 |
+
border-left: 4px solid #764ba2;
|
| 244 |
+
padding-left: 10px;
|
| 245 |
+
}
|
| 246 |
+
"""
|
| 247 |
+
|
| 248 |
+
with gr.Blocks(title="νλ ₯μ LLM μμ€ν
", theme=gr.themes.Soft(), css=css) as app:
|
| 249 |
gr.Markdown(
|
| 250 |
"""
|
| 251 |
# π€ νλ ₯μ LLM μμ€ν
|
| 252 |
|
| 253 |
+
> κ±°μμ κ°λ
μ AIμ λ―Έμμ μ€νμ AIκ° νλ ₯νμ¬ μ΅μμ λ΅λ³μ λ§λ€μ΄λ
λλ€.
|
| 254 |
+
|
| 255 |
+
**μμ€ν
ꡬ쑰:**
|
| 256 |
+
- π§ **κ°λ
μ AI**: μ 체μ μΈ λ°©ν₯κ³Ό νλ μμν¬λ₯Ό μ μ
|
| 257 |
+
- ποΈ **μ€νμ AI**: ꡬ체μ μ΄κ³ μ€ν κ°λ₯ν μΈλΆμ¬νμ μμ±
|
| 258 |
+
- π **νλ ₯ νλ‘μΈμ€**: μνΈ νΌλλ°±μ ν΅ν λ΅λ³ κ°μ
|
| 259 |
"""
|
| 260 |
)
|
| 261 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 262 |
with gr.Row():
|
| 263 |
# μΌμͺ½: μ
λ ₯ λ° μ±ν
κΈ°λ‘
|
| 264 |
with gr.Column(scale=1):
|
| 265 |
chatbot = gr.Chatbot(
|
| 266 |
label="π¬ λν κΈ°λ‘",
|
| 267 |
+
height=500,
|
| 268 |
+
show_copy_button=True,
|
| 269 |
+
bubble_full_width=False
|
| 270 |
)
|
| 271 |
|
| 272 |
user_input = gr.Textbox(
|
| 273 |
label="μ§λ¬Έ μ
λ ₯",
|
| 274 |
placeholder="μ: κΈ°κ³νμ΅ λͺ¨λΈμ μ±λ₯μ ν₯μμν€λ λ°©λ²μ?",
|
| 275 |
+
lines=3
|
| 276 |
)
|
| 277 |
|
| 278 |
with gr.Row():
|
| 279 |
+
submit_btn = gr.Button("π λΆμ μμ", variant="primary", scale=2)
|
| 280 |
+
clear_btn = gr.Button("ποΈ μ΄κΈ°ν", scale=1)
|
| 281 |
|
| 282 |
status_text = gr.Textbox(
|
| 283 |
label="μν",
|
| 284 |
interactive=False,
|
| 285 |
+
value="λκΈ° μ€...",
|
| 286 |
+
max_lines=1
|
| 287 |
)
|
| 288 |
|
| 289 |
# μ€λ₯Έμͺ½: AI μΆλ ₯
|
| 290 |
with gr.Column(scale=2):
|
| 291 |
# μ΅μ’
κ²°κ³Ό
|
| 292 |
+
with gr.Accordion("π μ΅μ’
μ’
ν© κ²°κ³Ό", open=True):
|
| 293 |
+
final_output = gr.Markdown(
|
| 294 |
+
value="*μ§λ¬Έμ μ
λ ₯νλ©΄ κ²°κ³Όκ° μ¬κΈ°μ νμλ©λλ€.*"
|
| 295 |
+
)
|
| 296 |
|
| 297 |
with gr.Row():
|
| 298 |
# κ°λ
μ AI μΆλ ₯
|
| 299 |
with gr.Column():
|
| 300 |
+
gr.Markdown("### π§ κ°λ
μ AI (κ±°μμ λΆμ)")
|
| 301 |
supervisor_output = gr.Textbox(
|
| 302 |
+
label="",
|
| 303 |
lines=15,
|
| 304 |
max_lines=20,
|
| 305 |
+
interactive=False,
|
| 306 |
+
elem_classes=["supervisor-box"]
|
| 307 |
)
|
| 308 |
|
| 309 |
# μ€νμ AI μΆλ ₯
|
| 310 |
with gr.Column():
|
| 311 |
+
gr.Markdown("### ποΈ μ€νμ AI (λ―Έμμ ꡬν)")
|
| 312 |
executor_output = gr.Textbox(
|
| 313 |
+
label="",
|
| 314 |
lines=15,
|
| 315 |
max_lines=20,
|
| 316 |
+
interactive=False,
|
| 317 |
+
elem_classes=["executor-box"]
|
| 318 |
)
|
| 319 |
|
| 320 |
+
# μμ
|
| 321 |
+
gr.Examples(
|
| 322 |
+
examples=[
|
| 323 |
+
"κΈ°κ³νμ΅ λͺ¨λΈμ μ±λ₯μ ν₯μμν€λ λ°©λ²μ?",
|
| 324 |
+
"ν¨κ³Όμ μΈ νλ‘μ νΈ κ΄λ¦¬ μ λ΅μ μ립νλ λ°©λ²μ?",
|
| 325 |
+
"μ§μ κ°λ₯ν λΉμ¦λμ€ λͺ¨λΈμ ꡬμΆνλ λ°©λ²μ?",
|
| 326 |
+
"볡μ‘ν λ°μ΄ν°λ₯Ό μκ°ννλ μ΅μ μ λ°©λ²μ?",
|
| 327 |
+
"νμ μμ°μ±μ λμ΄λ λ°©λ²μ?"
|
| 328 |
+
],
|
| 329 |
+
inputs=user_input,
|
| 330 |
+
label="π‘ μμ μ§λ¬Έ"
|
| 331 |
+
)
|
| 332 |
+
|
| 333 |
# μ΄λ²€νΈ νΈλ€λ¬
|
| 334 |
submit_btn.click(
|
| 335 |
fn=process_query,
|
|
|
|
| 354 |
outputs=[chatbot, supervisor_output, executor_output, final_output, status_text]
|
| 355 |
)
|
| 356 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 357 |
gr.Markdown(
|
| 358 |
"""
|
| 359 |
---
|
| 360 |
+
### π μ¬μ© λ°©λ²
|
| 361 |
+
1. μ§λ¬Έμ μ
λ ₯νκ³ Enter λλ 'λΆμ μμ' λ²νΌμ ν΄λ¦νμΈμ.
|
| 362 |
+
2. λ AIκ° νλ ₯νμ¬ λ΅λ³μ μμ±νλ κ³Όμ μ μ€μκ°μΌλ‘ νμΈν μ μμ΅λλ€.
|
| 363 |
+
3. μ΅μ’
μ’
ν© κ²°κ³Όλ μλ¨μ νμλ©λλ€.
|
| 364 |
+
|
| 365 |
+
### βοΈ νκ²½ μ€μ
|
| 366 |
+
- `FRIENDLI_TOKEN` νκ²½ λ³μλ₯Ό μ€μ νμΈμ: `export FRIENDLI_TOKEN="your_token"`
|
| 367 |
+
- λλ μ½λμ `FRIENDLI_TOKEN` λ³μλ₯Ό μ§μ μμ νμΈμ.
|
| 368 |
"""
|
| 369 |
)
|
| 370 |
|
|
|
|
| 372 |
app.launch(
|
| 373 |
server_name="0.0.0.0",
|
| 374 |
server_port=7860,
|
| 375 |
+
share=True,
|
| 376 |
show_error=True
|
| 377 |
+
)
|