Reflect changes to gradio_app.py
Browse files- api_server.py +94 -7
- llm/generator.py +3 -2
- persona/make_persona.py +3 -3
- persona/persona_loader.py +4 -0
api_server.py
CHANGED
|
@@ -83,23 +83,110 @@ sys.stdout = _stdout_proxy
|
|
| 83 |
|
| 84 |
class PersonaRequest(BaseModel):
|
| 85 |
info: str
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
|
| 88 |
@app.post("/persona/")
|
| 89 |
async def create_persona(request: PersonaRequest):
|
| 90 |
info = (request.info or "").strip()
|
|
|
|
|
|
|
| 91 |
if not info:
|
| 92 |
return JSONResponse(status_code=400, content={"error": "info ํ๋๊ฐ ๋น์ด ์์ต๋๋ค."})
|
| 93 |
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
class QueryRequest(BaseModel):
|
| 105 |
query: str
|
|
|
|
| 83 |
|
| 84 |
class PersonaRequest(BaseModel):
|
| 85 |
info: str
|
| 86 |
+
stream: bool = True
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
PERSONA_STATUS_MESSAGES = [
|
| 90 |
+
"์ธ๋ฌผ ์ ๋ณด ์์ง ์ค...",
|
| 91 |
+
"์น ๊ฒ์์ ํตํด ๋ฐฐ๊ฒฝ ์กฐ์ฌ ์ค...",
|
| 92 |
+
"๊ธ์ต ์ฌ๊ณ ๋ฐฉ์ ๋ถ์ ์ค...",
|
| 93 |
+
"๋ฐ์ดํฐ ๋ถ์ ์ ๊ทผ๋ฒ ํ๊ฐ ์ค...",
|
| 94 |
+
"๋ต๋ณ ์คํ์ผ ํน์ฑ ํ์
์ค...",
|
| 95 |
+
"ํต์ฌ ํฌ์ ์์น ์ถ์ถ ์ค...",
|
| 96 |
+
"๋ํ ์ด๋ก ์ ๋ฆฌ ์ค...",
|
| 97 |
+
"ํ๋ฅด์๋ ํ๋กํ ๊ตฌ์ฑ ์ค...",
|
| 98 |
+
"์ต์ข
๊ฒ์ฆ ๋ฐ ์ ์ฅ ์ค๋น ์ค...",
|
| 99 |
+
]
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
def _build_persona_payload(persona) -> dict:
|
| 103 |
+
return {
|
| 104 |
+
"type": "result",
|
| 105 |
+
"name": persona.name,
|
| 106 |
+
"full_name": persona.full_name,
|
| 107 |
+
"summary": persona.summary,
|
| 108 |
+
"financial_mindset": persona.financial_mindset,
|
| 109 |
+
"data_analysis_approach": persona.data_analysis_approach,
|
| 110 |
+
"response_style": persona.response_style,
|
| 111 |
+
"key_principles": persona.key_principles,
|
| 112 |
+
"famous_quotes": getattr(persona, "famous_quotes", None),
|
| 113 |
+
}
|
| 114 |
|
| 115 |
|
| 116 |
@app.post("/persona/")
|
| 117 |
async def create_persona(request: PersonaRequest):
|
| 118 |
info = (request.info or "").strip()
|
| 119 |
+
stream = request.stream
|
| 120 |
+
|
| 121 |
if not info:
|
| 122 |
return JSONResponse(status_code=400, content={"error": "info ํ๋๊ฐ ๋น์ด ์์ต๋๋ค."})
|
| 123 |
|
| 124 |
+
if not stream:
|
| 125 |
+
try:
|
| 126 |
+
persona = make_persona(info)
|
| 127 |
+
except Exception as exc:
|
| 128 |
+
return JSONResponse(status_code=500, content={"error": str(exc)})
|
| 129 |
+
|
| 130 |
+
if persona is None:
|
| 131 |
+
return JSONResponse(status_code=500, content={"error": "ํ๋ฅด์๋ ์์ฑ์ ์คํจํ์ต๋๋ค."})
|
| 132 |
+
|
| 133 |
+
return JSONResponse(content=persona.model_dump())
|
| 134 |
+
|
| 135 |
+
def event_stream():
|
| 136 |
+
event_queue: Queue = Queue()
|
| 137 |
+
|
| 138 |
+
def status_sender():
|
| 139 |
+
import asyncio
|
| 140 |
+
|
| 141 |
+
async def send_status():
|
| 142 |
+
for i, message in enumerate(PERSONA_STATUS_MESSAGES[:-1]): # ๋ง์ง๋ง ๋ฉ์์ง๋ ์๋ฃ ์์ ์ ์ฌ์ฉ
|
| 143 |
+
event_queue.put({"type": "status", "message": message})
|
| 144 |
+
await asyncio.sleep(7)
|
| 145 |
+
|
| 146 |
+
# ๋น๋๊ธฐ ์ด๋ฒคํธ ๋ฃจํ์์ ์คํ
|
| 147 |
+
loop = asyncio.new_event_loop()
|
| 148 |
+
asyncio.set_event_loop(loop)
|
| 149 |
+
loop.run_until_complete(send_status())
|
| 150 |
+
|
| 151 |
+
def worker():
|
| 152 |
+
thread_id = threading.get_ident()
|
| 153 |
+
_stdout_proxy.register(thread_id, _QueueingStdoutTee(_stdout_proxy._target, event_queue))
|
| 154 |
+
try:
|
| 155 |
+
# status ๋ฉ์์ง ์ ์ก ์ค๋ ๋ ์์
|
| 156 |
+
status_thread = Thread(target=status_sender, daemon=True)
|
| 157 |
+
status_thread.start()
|
| 158 |
+
|
| 159 |
+
persona = make_persona(info)
|
| 160 |
+
|
| 161 |
+
if persona is None:
|
| 162 |
+
event_queue.put({"type": "error", "message": "ํ๋ฅด์๋ ์์ฑ์ ์คํจํ์ต๋๋ค."})
|
| 163 |
+
else:
|
| 164 |
+
event_queue.put(_build_persona_payload(persona))
|
| 165 |
+
except Exception as exc:
|
| 166 |
+
event_queue.put({"type": "error", "message": str(exc)})
|
| 167 |
+
finally:
|
| 168 |
+
_stdout_proxy.unregister(thread_id)
|
| 169 |
+
event_queue.put({"type": "done"})
|
| 170 |
+
|
| 171 |
+
yield _sse({"type": "status", "message": "ํ๋ฅด์๋ ์์ฑ ์ค๋น ์ค..."})
|
| 172 |
+
Thread(target=worker, daemon=True).start()
|
| 173 |
|
| 174 |
+
done = False
|
| 175 |
+
while not done:
|
| 176 |
+
try:
|
| 177 |
+
event = event_queue.get(timeout=0.2)
|
| 178 |
+
except Empty:
|
| 179 |
+
continue
|
| 180 |
+
yield _sse(jsonable_encoder(event))
|
| 181 |
+
if event.get("type") == "done":
|
| 182 |
+
done = True
|
| 183 |
|
| 184 |
+
headers = {
|
| 185 |
+
"Cache-Control": "no-cache",
|
| 186 |
+
"Connection": "keep-alive",
|
| 187 |
+
"X-Accel-Buffering": "no",
|
| 188 |
+
}
|
| 189 |
+
return StreamingResponse(event_stream(), media_type="text/event-stream", headers=headers)
|
| 190 |
|
| 191 |
class QueryRequest(BaseModel):
|
| 192 |
query: str
|
llm/generator.py
CHANGED
|
@@ -62,7 +62,7 @@ def generate_news_info(client, user_query, intent):
|
|
| 62 |
class Persona(BaseModel):
|
| 63 |
name: str # ์ธ๋ฌผ ์ด๋ฆ
|
| 64 |
full_name: str # ์ธ๋ฌผ ์ด๋ฆ
|
| 65 |
-
|
| 66 |
financial_mindset: str # ๊ธ์ต ์ฌ๊ณ ๋ฐฉ์
|
| 67 |
data_analysis_approach: str # ๋ฐ์ดํฐ ๋ถ์ ๋ฐฉ์
|
| 68 |
response_style: str # ์ง๋ฌธ์ ๋ํ ๋ต๋ณ ์คํ์ผ
|
|
@@ -76,6 +76,7 @@ def generate_persona(client, user_query):
|
|
| 76 |
"- full_name: ์ธ๋ฌผ์ ์๋ฌธ/์ ์ ์ ์ฒด ์ด๋ฆ.\n"
|
| 77 |
"- name: ์ฌ์ฉ์๊ฐ ์ดํดํ๊ธฐ ์ฌ์ด ํ์ ์ด๋ฆ(ํ๊ตญ์ด ํต์ฉ๋ช
์ฐ์ , ์์ผ๋ฉด ๊ฐ๊ฒฐํ ์์ด).\n"
|
| 78 |
"- ๊ดํธ ๋ณ์นญ/์๋ฌธ ๋ณ๊ธฐ๋ full_name์๋ง ํฌํจํ๊ณ name์๋ ๋ฃ์ง ๋ง ๊ฒ.\n"
|
|
|
|
| 79 |
"๋๋จธ์ง ํ๋๋ ์ฌ์ค ๊ธฐ๋ฐ์ผ๋ก ์ถฉ์คํ ์์ฑํ์์ค."
|
| 80 |
)
|
| 81 |
|
|
@@ -123,7 +124,7 @@ def build_full_prompt(user_query, context, intent, persona=None):
|
|
| 123 |
|
| 124 |
[์ ํ๋ ํ๋ฅด์๋]
|
| 125 |
์ด๋ฆ: {persona.name}
|
| 126 |
-
|
| 127 |
๊ธ์ต ์ฌ๊ณ ๋ฐฉ์: {persona.financial_mindset}
|
| 128 |
๋ฐ์ดํฐ ๋ถ์ ๋ฐฉ์: {persona.data_analysis_approach}
|
| 129 |
๋ต๋ณ ์คํ์ผ: {persona.response_style}
|
|
|
|
| 62 |
class Persona(BaseModel):
|
| 63 |
name: str # ์ธ๋ฌผ ์ด๋ฆ
|
| 64 |
full_name: str # ์ธ๋ฌผ ์ด๋ฆ
|
| 65 |
+
summary: str # ์ธ๋ฌผ ์์ฝ (๊ฐ๋จํ ์๊ฐ)
|
| 66 |
financial_mindset: str # ๊ธ์ต ์ฌ๊ณ ๋ฐฉ์
|
| 67 |
data_analysis_approach: str # ๋ฐ์ดํฐ ๋ถ์ ๋ฐฉ์
|
| 68 |
response_style: str # ์ง๋ฌธ์ ๋ํ ๋ต๋ณ ์คํ์ผ
|
|
|
|
| 76 |
"- full_name: ์ธ๋ฌผ์ ์๋ฌธ/์ ์ ์ ์ฒด ์ด๋ฆ.\n"
|
| 77 |
"- name: ์ฌ์ฉ์๊ฐ ์ดํดํ๊ธฐ ์ฌ์ด ํ์ ์ด๋ฆ(ํ๊ตญ์ด ํต์ฉ๋ช
์ฐ์ , ์์ผ๋ฉด ๊ฐ๊ฒฐํ ์์ด).\n"
|
| 78 |
"- ๊ดํธ ๋ณ์นญ/์๋ฌธ ๋ณ๊ธฐ๋ full_name์๋ง ํฌํจํ๊ณ name์๋ ๋ฃ์ง ๋ง ๊ฒ.\n"
|
| 79 |
+
"- summary: ์ธ๋ฌผ์ ๊ฐ๋จํ ์๊ฐ ์์ฝ (2-3๋ฌธ์ฅ ์ ๋๋ก ๊ฐ๊ฒฐํ๊ฒ).\n"
|
| 80 |
"๋๋จธ์ง ํ๋๋ ์ฌ์ค ๊ธฐ๋ฐ์ผ๋ก ์ถฉ์คํ ์์ฑํ์์ค."
|
| 81 |
)
|
| 82 |
|
|
|
|
| 124 |
|
| 125 |
[์ ํ๋ ํ๋ฅด์๋]
|
| 126 |
์ด๋ฆ: {persona.name}
|
| 127 |
+
์์ฝ: {persona.summary}
|
| 128 |
๊ธ์ต ์ฌ๊ณ ๋ฐฉ์: {persona.financial_mindset}
|
| 129 |
๋ฐ์ดํฐ ๋ถ์ ๋ฐฉ์: {persona.data_analysis_approach}
|
| 130 |
๋ต๋ณ ์คํ์ผ: {persona.response_style}
|
persona/make_persona.py
CHANGED
|
@@ -68,13 +68,13 @@ def save_persona_jsonl(persona, query, file_name=None):
|
|
| 68 |
def print_persona(persona):
|
| 69 |
print("\n[Persona ์์ฑ ๊ฒฐ๊ณผ]")
|
| 70 |
print(f"- ์ด๋ฆ: {persona.name}")
|
| 71 |
-
print(f"-
|
| 72 |
print(f"- ๊ธ์ต ์ฌ๊ณ ๋ฐฉ์: {persona.financial_mindset}")
|
| 73 |
print(f"- ๋ฐ์ดํฐ ๋ถ์ ๋ฐฉ์: {persona.data_analysis_approach}")
|
| 74 |
print(f"- ๋ต๋ณ ์คํ์ผ: {persona.response_style}")
|
| 75 |
-
print(f"- ํต์ฌ ์์น: {', '.join(persona.key_principles)}")
|
| 76 |
if getattr(persona, "famous_quotes", None):
|
| 77 |
-
print(f"- ์ด๋ก: {', '.join(persona.famous_quotes)}")
|
| 78 |
|
| 79 |
|
| 80 |
def make_persona(info):
|
|
|
|
| 68 |
def print_persona(persona):
|
| 69 |
print("\n[Persona ์์ฑ ๊ฒฐ๊ณผ]")
|
| 70 |
print(f"- ์ด๋ฆ: {persona.name}")
|
| 71 |
+
print(f"- ์์ฝ: {persona.summary}")
|
| 72 |
print(f"- ๊ธ์ต ์ฌ๊ณ ๋ฐฉ์: {persona.financial_mindset}")
|
| 73 |
print(f"- ๋ฐ์ดํฐ ๋ถ์ ๋ฐฉ์: {persona.data_analysis_approach}")
|
| 74 |
print(f"- ๋ต๋ณ ์คํ์ผ: {persona.response_style}")
|
| 75 |
+
print(f"- ํต์ฌ ์์น: {', '.join(persona.key_principles)}", flush=True)
|
| 76 |
if getattr(persona, "famous_quotes", None):
|
| 77 |
+
print(f"- ์ด๋ก: {', '.join(persona.famous_quotes)}", flush=True)
|
| 78 |
|
| 79 |
|
| 80 |
def make_persona(info):
|
persona/persona_loader.py
CHANGED
|
@@ -13,6 +13,10 @@ def load_personas():
|
|
| 13 |
line = line.strip()
|
| 14 |
if line:
|
| 15 |
data = json.loads(line)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
# full_name ์์ผ๋ฉด name ์ฌ์ฉํ์ฌ ์ฑ์
|
| 17 |
if isinstance(data, dict) and not data.get("full_name"):
|
| 18 |
data["full_name"] = data.get("name", "")
|
|
|
|
| 13 |
line = line.strip()
|
| 14 |
if line:
|
| 15 |
data = json.loads(line)
|
| 16 |
+
# ํธํ์ฑ: background ํ๋๊ฐ ์์ผ๋ฉด summary๋ก ๋งคํ
|
| 17 |
+
if isinstance(data, dict) and "background" in data and "summary" not in data:
|
| 18 |
+
data["summary"] = data["background"]
|
| 19 |
+
# background ํ๋๋ ์ ๊ฑฐํ์ง ์๊ณ ์ ์ง (ํธํ์ฑ)
|
| 20 |
# full_name ์์ผ๋ฉด name ์ฌ์ฉํ์ฌ ์ฑ์
|
| 21 |
if isinstance(data, dict) and not data.get("full_name"):
|
| 22 |
data["full_name"] = data.get("name", "")
|