Spaces:
Sleeping
Sleeping
Update app.py
Browse files3 unmodified lines456789351 unmodified lines36136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945 unmodified lines4954964974984995005015025035043 unmodified linesfrom sklearn.linear_model import LogisticRegressionfrom xgboost import XGBClassifierimport time# ============================================================# 0. ๊ณตํต ์ค์ 351 unmodified lines</div>"""def render_gen5(amount, hour, new_payee_bin, ratio, prior_avg):"""5์ธ๋ LLM ์๋ฎฌ๋ ์ด์ (์์ ์ฑ์ ์ํด API ํธ์ถ ์์ด ํ๋์ฝ๋ฉ)"""is_high_risk = (new_payee_bin == 1 and (hour <= 6 or hour >= 22) and amount >= 500)prob = 0.95 if is_high_risk else min(prior_avg + 0.02, 0.98)dec, bg, fg = decide(prob)payee_str = "์ ๊ท ์์ทจ์ธ" if new_payee_bin == 1 else "๊ธฐ์กด ์์ทจ์ธ"time_str = "์๋ฒฝ" if (hour <= 6 or hour >= 22) else "์ผ๋ฐ"context_html = (f"[SYS] ๋น์ ์ FDS ๋ถ์๊ฐ์ ๋๋ค. ๊ฑฐ๋ ํจํด์ ๋ถ์ํด ์ฌ๊ธฐ ์ฌ๋ถ๋ฅผ ํ์ ํ์ธ์.<br>"f"[INPUT] amount={amount:.0f}๋ง, hour={int(hour):02d}, "f"new_payee={'true' if new_payee_bin==1 else 'false'}, ratio={ratio:.1f}ร<br>"f"[PRIOR] 1-4์ธ๋ ํฉ์: {dec} (avg {prior_avg*100:.1f}%)<br>"f"[TASK] ์์ฌ ์ฌ์ ์ ๊ถ๊ณ ์กฐ์น๋ฅผ ํ๊ตญ์ด๋ก.")cot_rows = (f"<tr><td style='padding:6px 4px;'>1</td>"f"<td style='padding:6px 4px; color:#666;'>์๊ฐ {int(hour):02d}์ + {payee_str} โ ๋ณด์ด์คํผ์ฑ ์ ํ ํจํด ๋งค์นญ</td>"f"<td style='text-align:right; padding:6px 4px; font-family:monospace; color:#666;'>0.42</td></tr>"f"<tr><td style='padding:6px 4px;'>2</td>"f"<td style='padding:6px 4px; color:#666;'>ํ์ {ratio:.1f}๋ฐฐ ๊ธ์ก โ ์ก๊ธ์ธ ํ์ ํ๋ ์ดํ๋ ์ธก์ </td>"f"<td style='text-align:right; padding:6px 4px; font-family:monospace; color:#666;'>0.28</td></tr>"f"<tr><td style='padding:6px 4px;'>3</td>"f"<td style='padding:6px 4px; color:#666;'>4์ธ๋ GNN ์ ํธ + {amount:.0f}๋ง์ ๊ณ ์ก โ ์ํ ๊ฐ์ค</td>"f"<td style='text-align:right; padding:6px 4px; font-family:monospace; color:#666;'>0.21</td></tr>"f"<tr><td style='padding:6px 4px;'>4</td>"f"<td style='padding:6px 4px; color:#666;'>์ต๊ทผ ๋ณด์ด์คํผ์ฑ ํธ๋ ๋ ์ฌ์ ์ง์ ํ์ฉ</td>"f"<td style='text-align:right; padding:6px 4px; font-family:monospace; color:#666;'>0.09</td></tr>")if is_high_risk:nl_judgment = (f"<b style='color:#633806;'>์ฐจ๋จ ๊ถ๊ณ (์์ฌ๋ {prob*100:.0f}%)</b><br><br>"f"{int(hour):02d}์ {time_str} ์๊ฐ๋์ ํ์๋ณด๋ค {ratio:.1f}๋ฐฐ ๊ธ์ฆํ {amount:.0f}๋ง์์ด "f"<u>{payee_str}</u>์๊ฒ ์ด์ฒด๋๋ ๊ฒ์ ์ ํ์ ์ธ ๋ณด์ด์คํผ์ฑ ํจํด์ ๋๋ค. "f"ํนํ ์์ทจ์ธ ๊ณ์ข๊ฐ ์ต๊ทผ ์ฌ๊ธฐ ์์ฌ ๊ณ์ข์ 2-hop ๊ฑฐ๋ฆฌ์ ์๋ค๋ GNN ์ ํธ๊น์ง ๋ํด์ ธ ์ํ๋๊ฐ ๋งค์ฐ ๋์ต๋๋ค.<br><br>"f"<b>๊ถ๊ณ ์กฐ์น:</b> โ ์ฆ์ ๊ฑฐ๋ ๋ณด๋ฅ, โก ๋ฑ๋ก๋ ์ ํ๋ฒํธ๋ก ๋ณธ์ธ ์ง์ ํ์ธ, โข ํ์ธ ์ ์๊ธ ๋๊ฒฐ 24์๊ฐ ์ ์ง")elif prob >= 0.5:nl_judgment = (f"<b style='color:#633806;'>์ถ๊ฐ ์ธ์ฆ ๊ถ๊ณ (์์ฌ๋ {prob*100:.0f}%)</b><br><br>"f"{int(hour):02d}์ ๊ฑฐ๋์์ ์ผ๋ถ ์ด์ ์ ํธ({ratio:.1f}๋ฐฐ ๊ธ์ก, {payee_str})๊ฐ ๊ฐ์ง๋์์ผ๋ "f"๊ฒฐ์ ์ ์ํ ํจํด์ ์๋๋๋ค. ์ฐจ๋จ๋ณด๋ค๋ ์ถ๊ฐ ์ธ์ฆ์ผ๋ก ๋ณธ์ธ ์์ฌ๋ฅผ ํ์ธํ๋ ๊ฒ์ด ์ ์ ํฉ๋๋ค.<br><br>"f"<b>๊ถ๊ณ ์กฐ์น:</b> โ ARS ๋๋ OTP ์ถ๊ฐ ์ธ์ฆ, โก ์ก๊ธ ์๋ ์ฌํ์ธ ๋ฉ์์ง ๋ฐ์ก")else:nl_judgment = (f"<b style='color:#3B6D11;'>ํต๊ณผ ๊ถ๊ณ (์์ฌ๋ {prob*100:.0f}%)</b><br><br>"f"{int(hour):02d}์ ๊ฑฐ๋์ ํจํด์ด ์ก๊ธ์ธ์ ํ์ ํ๋ ๋ฒ์ ๋ด์ ์์ผ๋ฉฐ, "f"1-4์ธ๋ ๋ชจ๋ธ ๋ชจ๋ ์ํ ์ ํธ๋ฅผ ๊ฐํ๊ฒ ๋ณด๋ด์ง ์์์ต๋๋ค. ์ ์ ๊ฑฐ๋๋ก ํ๋จ๋ฉ๋๋ค.<br><br>"f"<b>๊ถ๊ณ ์กฐ์น:</b> ๋ณ๋ ์กฐ์น ์์ด ๊ฑฐ๋ ์งํ")return f"""<div style="{CARD_STYLE}">{card_header("GEN 5 ยท FOUNDATION MODEL (LLM)", "์ด๊ฑฐ๋ ์ถ๋ก ๋ชจ๋ธ (์๋ฎฌ๋ ์ด์ )", dec, bg, fg, f"์์ฌ๋ {prob*100:.0f}%")}<p style="font-size:13px; color:#666; margin:4px 0 6px;">์ปจํ ์คํธ ํ ํฐํ</p><div style="background:#f5f5f0; padding:10px 12px; border-radius:6px; font-family:monospace; font-size:11px; line-height:1.7; margin-bottom:12px;">{context_html}</div><p style="font-size:13px; color:#666; margin:4px 0 6px;">์ถ๋ก ์ฒด์ธ (Chain-of-Thought)</p><table style="width:100%; font-size:13px; border-collapse:collapse; margin-bottom:12px;"><thead><tr style="border-bottom:0.5px solid rgba(0,0,0,0.15);"><th style="text-align:left; padding:6px 4px; font-weight:500; color:#666; width:12%;">๋จ๊ณ</th><th style="text-align:left; padding:6px 4px; font-weight:500; color:#666; width:58%;">์ถ๋ก ๋ด์ฉ</th><th style="text-align:right; padding:6px 4px; font-weight:500; color:#666; width:30%;">Attention</th></tr></thead><tbody>{cot_rows}</tbody></table><p style="font-size:13px; color:#666; margin:4px 0 6px;">์์ฑ๋ ์์ฐ์ด ํ๋จ (Token ํ๋ฅ )</p><div style="background:#FAEEDA; padding:12px 14px; border-radius:6px; font-size:13px; line-height:1.7; color:#412402;">{nl_judgment}</div><details style="margin-top:10px;"><summary style="font-size:12px; color:#666; cursor:pointer;">์์ฑ ํ๋ผ๋ฏธํฐ ๋ณด๊ธฐ</summary><div style="background:#f5f5f0; padding:8px 12px; border-radius:6px; font-family:monospace; font-size:11px; margin-top:6px; line-height:1.6;">model: claude-sonnet-4-5 / temperature: 0.2 / top_p: 0.9 / max_tokens: 512<br>input_tokens: 142 / output_tokens: 98 / latency: 1.83s (์๋ฎฌ๋ ์ด์ )</div></details><p style="font-size:12px; color:#888; margin:10px 0 0; font-style:italic;">๊ฐ์ : ์ฌ์ ํ์ต๋ ๋๋ฉ์ธ ์ง์์ผ๋ก '์ ์ฌ๊ธฐ์ธ์ง' ์์ฐ์ด ์ค๋ช + ๊ถ๊ณ ์กฐ์น๊น์ง ์๋ ์์ฑ</p></div>"""# ============================================================# 4. ๋ฉ์ธ ๋ถ์ ํจ์# ============================================================45 unmodified lines# 5. Gradio UI# ============================================================with gr.Blocks(theme=gr.themes.Default(), title="FDS 1-5์ธ๋ ๋น๊ต ๋ฐ๋ชจ") as demo:gr.HTML("""<div style="text-align:center; padding:10px 0;"><h1 style="margin:0;">๐ก๏ธ FDS 1-5์ธ๋ ๋น๊ต ๋ฐ๋ชจ</h1><p style="color:#666; margin:6px 0 0;">์ค๋ฌด ์ด์ ๋ด๋น์ ์์ฐ์ฉ ยท ํ๋จ ์์ยท๊ฐ์ค์นยท๊ทผ๊ฑฐ ์ ์ฒด ๋ ธ์ถ ๋ชจ๋</p></div>""")3 unmodified lines4567891011121314151617181920212223242526272829303132351 unmodified lines38438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262345 unmodified lines6696706716726736746756766776786796806816826836846856863 unmodified linesfrom sklearn.linear_model import LogisticRegressionfrom xgboost import XGBClassifierimport timeimport osimport reimport json# ============================================================# 0-A. Claude API ํด๋ผ์ด์ธํธ ์ด๊ธฐํ (5์ธ๋์ฉ)# ============================================================# ํ๊ฒฝ๋ณ์ ANTHROPIC_API_KEY ๊ฐ ์ค์ ๋์ด ์์ผ๋ฉด ์ค์ API ํธ์ถ ๋ชจ๋# ์๊ฑฐ๋ ํธ์ถ ์คํจ ์ ์๋์ผ๋ก ์๋ฎฌ๋ ์ด์ ๋ชจ๋๋ก fallbacktry:from anthropic import Anthropic_api_key = os.environ.get("ANTHROPIC_API_KEY")if _api_key:claude_client = Anthropic(api_key=_api_key)CLAUDE_AVAILABLE = Trueelse:claude_client = NoneCLAUDE_AVAILABLE = Falseexcept ImportError:claude_client = NoneCLAUDE_AVAILABLE = FalseCLAUDE_MODEL = "claude-sonnet-4-6" # 2026๋ 5์ ํ์ฌ ๊ถ์ฅ ๋ชจ๋ธ# ============================================================# 0. ๊ณตํต ์ค์ 351 unmodified lines</div>"""# ============================================================# 5์ธ๋ โ Claude API ์ค์ ํธ์ถ# ============================================================def build_claude_prompt(amount, hour, new_payee_bin, ratio, prior_avg, prior_dec):"""Claude์๊ฒ ๋ณด๋ผ ์์คํ ํ๋กฌํํธ์ ์ฌ์ฉ์ ๋ฉ์์ง ๊ตฌ์ฑ"""system_prompt = ("๋น์ ์ ํ๊ตญ ์ํ์ FDS(์ด์๊ธ์ต๊ฑฐ๋ํ์ง์์คํ ) ๋ถ์ ์ ๋ฌธ๊ฐ์ ๋๋ค. ""์ฃผ์ด์ง ๊ฑฐ๋ ์ ๋ณด์ 1-4์ธ๋ ๋ชจ๋ธ์ ์ฌ์ ํ๋จ์ ๋ฐํ์ผ๋ก ์ฌ๊ธฐ ์ฌ๋ถ๋ฅผ ์ข ํฉ ํ์ ํ์ธ์. ""ํนํ ๋ถ๋์ฐ ์๊ธ ์ก๊ธ, ์ฌ์ ์ ๋๊ธ ๊ฒฐ์ ๊ฐ์ ์ ์ ๊ฑฐ๋ ํจํด๊ณผ ๋ณด์ด์คํผ์ฑยท๋ํฌํต์ฅ ํจํด์ ๊ตฌ๋ถํด์ผ ํฉ๋๋ค. ""๋ฐ๋์ ๋ค์ JSON ํ์์ผ๋ก๋ง ์๋ตํ์ธ์(๋ค๋ฅธ ํ ์คํธ ๊ธ์ง):\n"'{\n'' "risk_score": <0.0~1.0 ์ฌ์ด ์ฌ๊ธฐ ์์ฌ๋>,\n'' "decision": "์ฐจ๋จ" | "์ถ๊ฐ ์ธ์ฆ" | "ํต๊ณผ",\n'' "reasoning_steps": [\n'' {"step": "<์ถ๋ก ๋จ๊ณ>", "attention": <0.0~1.0>},\n'' ... (3~5๊ฐ)\n'' ],\n'' "judgment": "<์ต์ข ํ๋จ ์์ฐ์ด (์ ๊ทธ๋ ๊ฒ ํ๋จํ๋์ง + ๊ถ๊ณ ์กฐ์น)>"\n''}')payee_str = "์ ๊ท" if new_payee_bin == 1 else "๊ธฐ์กด"time_period = "์๋ฒฝ" if (hour <= 6 or hour >= 22) else ("์ฃผ๊ฐ" if 9 <= hour <= 18 else "์ ๋ ")user_message = (f"[๊ฑฐ๋ ์ ๋ณด]\n"f"- ๊ธ์ก: {amount:.0f}๋ง์\n"f"- ๊ฑฐ๋ ์๊ฐ: {int(hour):02d}์ ({time_period})\n"f"- ์์ทจ์ธ: {payee_str} ์์ทจ์ธ\n"f"- ๊ณผ๊ฑฐ ๋๋น ๋ฐฐ์จ: {ratio:.1f}๋ฐฐ (์ก๊ธ์ธ์ ํ๊ท ๊ฑฐ๋์ก ๋๋น)\n\n"f"[1-4์ธ๋ ๋ชจ๋ธ ์ฌ์ ํ๋จ]\n"f"- ํ๊ท ์ฌ๊ธฐ ํ๋ฅ : {prior_avg*100:.1f}%\n"f"- ์ข ํฉ ํ์ : {prior_dec}\n\n"f"์ ๊ฑฐ๋์ ๋ํด FDS ์ ๋ฌธ๊ฐ ๊ด์ ์์ ์ข ํฉ ํ์ ํด์ฃผ์ธ์.")return system_prompt, user_messagedef call_claude_api(amount, hour, new_payee_bin, ratio, prior_avg, prior_dec):"""์ค์ Claude API ํธ์ถ. ์ฑ๊ณต ์ (parsed_dict, raw_meta) ๋ฐํ, ์คํจ ์ (None, error_msg)"""if not CLAUDE_AVAILABLE:return None, "API ํค ๋ฏธ์ค์ (ANTHROPIC_API_KEY ํ๊ฒฝ๋ณ์ ์์)"system_prompt, user_message = build_claude_prompt(amount, hour, new_payee_bin, ratio, prior_avg, prior_dec)try:t0 = time.time()response = claude_client.messages.create(model=CLAUDE_MODEL,max_tokens=1024,temperature=0.2,system=system_prompt,messages=[{"role": "user", "content": user_message}])latency = time.time() - t0raw_text = response.content[0].text.strip()# JSON ์ถ์ถ: ์๋ต ์ค ์ฒซ ๋ฒ์งธ { ๋ถํฐ ๋ง์ง๋ง } ๊น์งjson_match = re.search(r'\{.*\}', raw_text, re.DOTALL)if not json_match:return None, f"JSON ํ์ฑ ์คํจ: {raw_text[:200]}"parsed = json.loads(json_match.group(0))meta = {"input_tokens": response.usage.input_tokens,"output_tokens": response.usage.output_tokens,"latency": latency,"model": CLAUDE_MODEL,"raw_text": raw_text,}return parsed, metaexcept Exception as e:return None, f"API ํธ์ถ ์คํจ: {type(e).__name__}: {str(e)[:200]}"def render_gen5_card(amount, hour, new_payee_bin, ratio, prior_avg, prior_dec,prob, decision_text, reasoning_steps, judgment_text,meta_html, source_label):"""5์ธ๋ ์นด๋ HTML ๋ ๋๋ง (API/์๋ฎฌ๋ ์ด์ ๊ณต์ฉ)"""dec, bg, fg = decide(prob)context_html = (f"[SYS] ๋น์ ์ ํ๊ตญ ์ํ์ FDS ๋ถ์ ์ ๋ฌธ๊ฐ์ ๋๋ค. ๊ฑฐ๋ ์ ๋ณด์ 1-4์ธ๋ ์ฌ์ ํ๋จ์ ํ ๋๋ก ์ข ํฉ ํ์ ํ์ธ์.<br>"f"[INPUT] amount={amount:.0f}๋ง, hour={int(hour):02d}, "f"new_payee={'true' if new_payee_bin==1 else 'false'}, ratio={ratio:.1f}ร<br>"f"[PRIOR] 1-4์ธ๋ ํ๊ท : {prior_avg*100:.1f}% / ์ข ํฉ: {prior_dec}<br>"f"[TASK] JSON ํ์์ผ๋ก risk_score, decision, reasoning_steps, judgment ์ถ๋ ฅ")cot_rows = ""for i, step in enumerate(reasoning_steps, 1):step_text = step.get("step", "") if isinstance(step, dict) else str(step)attn = step.get("attention", 0.0) if isinstance(step, dict) else 0.0try:attn = float(attn)except (ValueError, TypeError):attn = 0.0cot_rows += (f"<tr><td style='padding:6px 4px;'>{i}</td>"f"<td style='padding:6px 4px; color:#666;'>{step_text}</td>"f"<td style='text-align:right; padding:6px 4px; font-family:monospace; color:#666;'>{attn:.2f}</td></tr>")judg_color = "#3B6D11" if prob < 0.5 else "#633806"judgment_html = (f"<b style='color:{judg_color};'>{decision_text} ๊ถ๊ณ (์์ฌ๋ {prob*100:.0f}%)</b><br><br>"f"{judgment_text}")return f"""<div style="{CARD_STYLE}">{card_header("GEN 5 ยท FOUNDATION MODEL (LLM)", f"์ด๊ฑฐ๋ ์ถ๋ก ๋ชจ๏ฟฝ๏ฟฝ๏ฟฝ ({source_label})", dec, bg, fg, f"์์ฌ๋ {prob*100:.0f}%")}<p style="font-size:13px; color:#666; margin:4px 0 6px;">์ปจํ ์คํธ ํ ํฐํ</p><div style="background:#f5f5f0; padding:10px 12px; border-radius:6px; font-family:monospace; font-size:11px; line-height:1.7; margin-bottom:12px;">{context_html}</div><p style="font-size:13px; color:#666; margin:4px 0 6px;">์ถ๋ก ์ฒด์ธ (Chain-of-Thought)</p><table style="width:100%; font-size:13px; border-collapse:collapse; margin-bottom:12px;"><thead><tr style="border-bottom:0.5px solid rgba(0,0,0,0.15);"><th style="text-align:left; padding:6px 4px; font-weight:500; color:#666; width:8%;">๋จ๊ณ</th><th style="text-align:left; padding:6px 4px; font-weight:500; color:#666; width:72%;">์ถ๋ก ๋ด์ฉ</th><th style="text-align:right; padding:6px 4px; font-weight:500; color:#666; width:20%;">Attention</th></tr></thead><tbody>{cot_rows}</tbody></table><p style="font-size:13px; color:#666; margin:4px 0 6px;">์์ฑ๋ ์์ฐ์ด ํ๋จ</p><div style="background:#FAEEDA; padding:12px 14px; border-radius:6px; font-size:13px; line-height:1.7; color:#412402;">{judgment_html}</div><details style="margin-top:10px;"><summary style="font-size:12px; color:#666; cursor:pointer;">์์ฑ ํ๋ผ๋ฏธํฐ ๋ณด๊ธฐ</summary><div style="background:#f5f5f0; padding:8px 12px; border-radius:6px; font-family:monospace; font-size:11px; margin-top:6px; line-height:1.6;">{meta_html}</div></details><p style="font-size:12px; color:#888; margin:10px 0 0; font-style:italic;">๊ฐ์ : ์ฌ์ ํ์ต๋ ๋๋ฉ์ธ ์ง์์ผ๋ก '์ ์ฌ๊ธฐ์ธ์ง' ์์ฐ์ด ์ค๋ช + ๊ถ๊ณ ์กฐ์น๊น์ง ์๋ ์์ฑ</p></div>"""def render_gen5_simulation(amount, hour, new_payee_bin, ratio, prior_avg, prior_dec):"""API ํธ์ถ ์คํจ/๋ฏธ์ค์ ์ fallback์ฉ ์๋ฎฌ๋ ์ด์ """is_high_risk = (new_payee_bin == 1 and (hour <= 6 or hour >= 22) and amount >= 500)prob = 0.95 if is_high_risk else min(prior_avg + 0.02, 0.98)payee_str = "์ ๊ท ์์ทจ์ธ" if new_payee_bin == 1 else "๊ธฐ์กด ์์ทจ์ธ"time_str = "์๋ฒฝ" if (hour <= 6 or hour >= 22) else "์ผ๋ฐ"reasoning_steps = [{"step": f"์๊ฐ {int(hour):02d}์ + {payee_str} โ ๋ณด์ด์คํผ์ฑ ์ ํ ํจํด ๋งค์นญ", "attention": 0.42},{"step": f"ํ์ {ratio:.1f}๋ฐฐ ๊ธ์ก โ ์ก๊ธ์ธ ํ์ ํ๋ ์ดํ๋ ์ธก์ ", "attention": 0.28},{"step": f"4์ธ๋ GNN ์ ํธ + {amount:.0f}๋ง์ ๊ณ ์ก โ ์ํ ๊ฐ์ค", "attention": 0.21},{"step": "์ต๊ทผ ๋ณด์ด์คํผ์ฑ ํธ๋ ๋ ์ฌ์ ์ง์ ํ์ฉ", "attention": 0.09},]if is_high_risk:decision_text = "์ฐจ๋จ"judgment_text = (f"{int(hour):02d}์ {time_str} ์๊ฐ๋์ ํ์๋ณด๋ค {ratio:.1f}๋ฐฐ ๊ธ์ฆํ {amount:.0f}๋ง์์ด "f"<u>{payee_str}</u>์๊ฒ ์ด์ฒด๋๋ ๊ฒ์ ์ ํ์ ์ธ ๋ณด์ด์คํผ์ฑ ํจํด์ ๋๋ค. "f"์์ทจ์ธ ๊ณ์ข๊ฐ ์ฌ๊ธฐ ์์ฌ ๊ณ์ข์ 2-hop ๊ฑฐ๋ฆฌ์ ์๋ค๋ GNN ์ ํธ๊น์ง ๋ํด์ ธ ์ํ๋๊ฐ ๋งค์ฐ ๋์ต๋๋ค.<br><br>"f"<b>๊ถ๊ณ ์กฐ์น:</b> โ ์ฆ์ ๊ฑฐ๋ ๋ณด๋ฅ, โก ๋ฑ๋ก๋ ์ ํ๋ฒํธ๋ก ๋ณธ์ธ ์ง์ ํ์ธ, โข ํ์ธ ์ ์๊ธ ๋๊ฒฐ 24์๊ฐ ์ ์ง")elif prob >= 0.5:decision_text = "์ถ๊ฐ ์ธ์ฆ"judgment_text = (f"{int(hour):02d}์ ๊ฑฐ๋์์ ์ผ๋ถ ์ด์ ์ ํธ({ratio:.1f}๋ฐฐ ๊ธ์ก, {payee_str})๊ฐ ๊ฐ์ง๋์์ผ๋ "f"๊ฒฐ์ ์ ์ํ ํจํด์ ์๋๋๋ค. ์ฐจ๋จ๋ณด๋ค๋ ์ถ๊ฐ ์ธ์ฆ์ผ๋ก ๋ณธ์ธ ์์ฌ๋ฅผ ํ์ธํ๋ ๊ฒ์ด ์ ์ ํฉ๋๋ค.<br><br>"f"<b>๊ถ๊ณ ์กฐ์น:</b> โ ARS ๋๋ OTP ์ถ๊ฐ ์ธ์ฆ, โก ์ก๊ธ ์๋ ์ฌํ์ธ ๋ฉ์์ง ๋ฐ์ก")else:decision_text = "ํต๊ณผ"judgment_text = (f"{int(hour):02d}์ ๊ฑฐ๋์ ํจํด์ด ์ก๊ธ์ธ์ ํ์ ํ๋ ๋ฒ์ ๋ด์ ์์ผ๋ฉฐ, "f"1-4์ธ๋ ๋ชจ๋ธ ๋ชจ๋ ์ํ ์ ํธ๋ฅผ ๊ฐํ๊ฒ ๋ณด๋ด์ง ์์์ต๋๋ค. ์ ์ ๊ฑฐ๋๋ก ํ๋จ๋ฉ๋๋ค.<br><br>"f"<b>๊ถ๊ณ ์กฐ์น:</b> ๋ณ๋ ์กฐ์น ์์ด ๊ฑฐ๋ ์งํ")meta_html = ("mode: <b>SIMULATION</b> (API ๋ฏธ์ฐ๊ฒฐ)<br>""์ด ๊ฒฐ๊ณผ๋ if-else ํ๋์ฝ๋ฉ์ผ๋ก ์์ฑ๋ ์๋ฎฌ๋ ์ด์ ์ ๋๋ค.<br>""์ค์ Claude ํธ์ถ์ ํ์ฑํํ๋ ค๋ฉด ANTHROPIC_API_KEY ํ๊ฒฝ๋ณ์๋ฅผ ์ค์ ํ์ธ์.")return render_gen5_card(amount, hour, new_payee_bin, ratio, prior_avg, prior_dec,prob, decision_text, reasoning_steps, judgment_text,meta_html, source_label="์๋ฎฌ๋ ์ด์ ๋ชจ๋")def render_gen5(amount, hour, new_payee_bin, ratio, prior_avg):"""5์ธ๋ ์ง์ ์ : API ๊ฐ๋ฅ ์ Claude ํธ์ถ, ์คํจ ์ ์๋ฎฌ๋ ์ด์ fallback"""prior_dec, _, _ = decide(prior_avg)if not CLAUDE_AVAILABLE:return render_gen5_simulation(amount, hour, new_payee_bin, ratio, prior_avg, prior_dec)parsed, meta_or_err = call_claude_api(amount, hour, new_payee_bin, ratio, prior_avg, prior_dec)if parsed is None:fallback_html = render_gen5_simulation(amount, hour, new_payee_bin, ratio, prior_avg, prior_dec)warning = (f'<div style="background:#FCEBEB; border-left:3px solid #A32D2D; padding:10px 14px; 'f'border-radius:6px; margin-bottom:10px; font-size:13px; color:#4A1B0C;">'f'โ ๏ธ Claude API ํธ์ถ ์คํจ. ์๋ฎฌ๋ ์ด์ ๋ชจ๋๋ก ๋์ฒดํฉ๋๋ค.<br>'f'<span style="font-family:monospace; font-size:11px; color:#712B13;">์ฌ์ : {meta_or_err}</span>'f'</div>')return warning + fallback_htmltry:prob = float(parsed.get("risk_score", 0.5))prob = max(0.0, min(1.0, prob))decision_text = parsed.get("decision", "์ถ๊ฐ ์ธ์ฆ")reasoning_steps = parsed.get("reasoning_steps", [])judgment_text = parsed.get("judgment", "(ํ๋จ ๋ด์ฉ ๋๋ฝ)")meta = meta_or_errmeta_html = (f"model: <b>{meta['model']}</b> / temperature: 0.2 / max_tokens: 1024<br>"f"input_tokens: {meta['input_tokens']} / output_tokens: {meta['output_tokens']} / "f"latency: {meta['latency']:.2f}s<br>"f"mode: <b style='color:#3B6D11;'>LIVE API CALL โ</b>")return render_gen5_card(amount, hour, new_payee_bin, ratio, prior_avg, prior_dec,prob, decision_text, reasoning_steps, judgment_text,meta_html, source_label=f"์ค์ {CLAUDE_MODEL}")except (KeyError, ValueError, TypeError) as e:fallback_html = render_gen5_simulation(amount, hour, new_payee_bin, ratio, prior_avg, prior_dec)warning = (f'<div style="background:#FCEBEB; border-left:3px solid #A32D2D; padding:10px 14px; 'f'border-radius:6px; margin-bottom:10px; font-size:13px; color:#4A1B0C;">'f'โ ๏ธ Claude ์๋ต ํ์ฑ ์คํจ. ์๋ฎฌ๋ ์ด์ ๋ชจ๋๋ก ๋์ฒดํฉ๋๋ค.<br>'f'<span style="font-family:monospace; font-size:11px; color:#712B13;">'f'{type(e).__name__}: {str(e)[:150]}</span></div>')return warning + fallback_html# ============================================================# 4. ๋ฉ์ธ ๋ถ์ ํจ์# ============================================================45 unmodified lines# 5. Gradio UI# ============================================================with gr.Blocks(theme=gr.themes.Default(), title="FDS 1-5์ธ๋ ๋น๊ต ๋ฐ๋ชจ") as demo:_api_badge = (f'<span style="background:#EAF3DE; color:#3B6D11; padding:3px 10px; border-radius:8px; font-size:12px; font-weight:500;">'f'โ Claude API ์ฐ๊ฒฐ๋จ ({CLAUDE_MODEL})</span>'if CLAUDE_AVAILABLE else'<span style="background:#FAEEDA; color:#854F0B; padding:3px 10px; border-radius:8px; font-size:12px; font-weight:500;">''โ API ๋ฏธ์ฐ๊ฒฐ (5์ธ๋ ์๋ฎฌ๋ ์ด์ ๋ชจ๋)</span>')gr.HTML(f"""<div style="text-align:center; padding:10px 0;"><h1 style="margin:0;">๐ก๏ธ FDS 1-5์ธ๋ ๋น๊ต ๋ฐ๋ชจ</h1><p style="color:#666; margin:6px 0 0;">์ค๋ฌด ์ด์ ๋ด๋น์ ์์ฐ์ฉ ยท ํ๋จ ์์ยท๊ฐ์ค์นยท๊ทผ๊ฑฐ ์ ์ฒด ๋ ธ์ถ ๋ชจ๋</p><div style="margin-top:8px;">{_api_badge}</div></div>""")
app.py+238-56