Spaces:
Sleeping
Sleeping
Ultra-Stable: Defer ALL heavy imports to function call to prevent boot crash (500 error)
Browse files
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import huggingface_hub
|
| 2 |
if not hasattr(huggingface_hub, "HfFolder"):
|
| 3 |
class MockHfFolder:
|
|
@@ -10,22 +11,31 @@ if not hasattr(huggingface_hub, "HfFolder"):
|
|
| 10 |
huggingface_hub.HfFolder = MockHfFolder
|
| 11 |
|
| 12 |
import gradio as gr
|
| 13 |
-
import pandas as pd
|
| 14 |
-
import numpy as np
|
| 15 |
import os
|
| 16 |
import time
|
| 17 |
|
| 18 |
-
# μ§μ° λ‘λ©
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
def
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 25 |
from langchain_huggingface import HuggingFaceEmbeddings
|
| 26 |
from langchain_community.vectorstores import FAISS
|
| 27 |
from config import EMBEDDING_MODEL, FAISS_PATH, RETRIEVER_K, GEMINI_API_KEY
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
class Consultant:
|
| 30 |
def __init__(self):
|
| 31 |
os.environ["GOOGLE_API_KEY"] = os.getenv("GOOGLE_API_KEY", GEMINI_API_KEY)
|
|
@@ -36,27 +46,16 @@ def get_consultant():
|
|
| 36 |
else:
|
| 37 |
self.retriever = None
|
| 38 |
self.llm = ChatGoogleGenerativeAI(model="gemini-1.5-flash", temperature=0.7)
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
# μμΈ‘κΈ° λ‘λ
|
| 43 |
-
from predictors.score_prediction import predictor
|
| 44 |
|
| 45 |
FEATURES_MAP = {
|
| 46 |
-
'C1Z001386': '1λ
λ΄ μΉ΄λ μ΄ μ΄μ©κΈμ‘ (λ§μ)',
|
| 47 |
-
'
|
| 48 |
-
'
|
| 49 |
-
'
|
| 50 |
-
'
|
| 51 |
-
'L10210000': 'μνμ
μ’
λμΆ κ±΄μ',
|
| 52 |
-
'L90210100': 'λλΆμ
μ’
λμΆ κ±΄μ',
|
| 53 |
-
'L90210200': 'μ μΆμν λμΆ κ±΄μ',
|
| 54 |
-
'L10210B00': 'μ£Όνλ΄λ³΄ λμΆ κ±΄μ',
|
| 55 |
-
'L10216000': 'μ μ©λμΆ μ΄ μμ‘ (λ§μ)',
|
| 56 |
-
'L10217000': 'λ΄λ³΄λμΆ μ΄ μμ‘ (λ§μ)',
|
| 57 |
-
'D10110000': 'μ°μ²΄ 건μ',
|
| 58 |
-
'D10133000': 'μ°μ²΄ μμ‘ (λ§μ)',
|
| 59 |
-
'PERF1': '90μΌ μ°μ²΄ μ¬λΆ'
|
| 60 |
}
|
| 61 |
|
| 62 |
ALL_FEATURES_KEYS = [
|
|
@@ -65,102 +64,81 @@ ALL_FEATURES_KEYS = [
|
|
| 65 |
'L10217000', 'D10110000', 'D10133000', 'PERF1'
|
| 66 |
]
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
def generate_response(history, user_message, analysis_report):
|
| 69 |
if not user_message: yield history, ""; return
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
# μ± μν μ΄κΈ°ν
|
| 73 |
-
history = history + [[user_message, ""]]
|
| 74 |
-
t0 = time.time()
|
| 75 |
|
| 76 |
try:
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
yield history, ""
|
| 80 |
-
cons = get_consultant()
|
| 81 |
|
| 82 |
-
# R
|
| 83 |
-
history[-1][1] = "π **
|
| 84 |
yield history, ""
|
| 85 |
docs = cons.retriever.invoke(user_message) if cons.retriever else []
|
| 86 |
-
t_retrieve = time.time() - t0
|
| 87 |
context = "\n\n".join([doc.page_content for doc in docs])
|
| 88 |
|
| 89 |
-
# A
|
| 90 |
from llm.prompt import QA_PROMPT
|
| 91 |
if analysis_report:
|
| 92 |
score_val = int(analysis_report["score"])
|
| 93 |
features_text = "\n".join([f"- {FEATURES_MAP.get(k, k)}: {v}" for k, v in analysis_report['features'].items()])
|
| 94 |
-
query_text = f"β κ³ κ°
|
| 95 |
else:
|
| 96 |
query_text = f"β μ§λ¬Έ: {user_message}"
|
| 97 |
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
answer_buffer = ""
|
| 103 |
-
for chunk in cons.llm.stream(
|
| 104 |
answer_buffer += chunk.content
|
| 105 |
-
history[-1][1] =
|
| 106 |
-
f"π **[R] μλ£** ({t_retrieve:.1f}s) | π **[A] μλ£** ({t_augment-t_retrieve:.2f}s)\n\n"
|
| 107 |
-
f"{answer_buffer}"
|
| 108 |
-
)
|
| 109 |
yield history, ""
|
| 110 |
|
| 111 |
except Exception as e:
|
| 112 |
-
history[-1][1] = f"β οΈ
|
| 113 |
yield history, ""
|
| 114 |
|
| 115 |
-
def handle_predict(*args):
|
| 116 |
-
try:
|
| 117 |
-
features_dict = {}
|
| 118 |
-
for i, key in enumerate(ALL_FEATURES_KEYS):
|
| 119 |
-
if key == 'PERF1': features_dict[key] = int(args[i])
|
| 120 |
-
else:
|
| 121 |
-
val = str(args[i]).strip() if args[i] else "0"
|
| 122 |
-
features_dict[key] = float(val) if val else 0.0
|
| 123 |
-
|
| 124 |
-
score = predictor.predict(features_dict)
|
| 125 |
-
display_score = int(min(max(round(score), 0), 1000)) if not np.isnan(score) else 0
|
| 126 |
-
return {"features": features_dict, "score": display_score}, display_score
|
| 127 |
-
except Exception as e:
|
| 128 |
-
return {"error": str(e)}, 0
|
| 129 |
-
|
| 130 |
with gr.Blocks(title="KCB AI Consultant") as demo:
|
| 131 |
analysis_report = gr.State(None)
|
| 132 |
-
|
| 133 |
-
gr.Markdown("# π‘οΈ KCB AI μ μ© μ μ λΆμ μμ€ν
(LTS)")
|
| 134 |
|
| 135 |
with gr.Row():
|
| 136 |
with gr.Column(scale=1):
|
| 137 |
-
input_list = []
|
| 138 |
-
for key in ALL_FEATURES_KEYS:
|
| 139 |
-
if key == 'PERF1':
|
| 140 |
-
input_list.append(gr.Checkbox(label=FEATURES_MAP[key], value=False))
|
| 141 |
-
else:
|
| 142 |
-
input_list.append(gr.Textbox(label=FEATURES_MAP[key], placeholder="0"))
|
| 143 |
predict_btn = gr.Button("π μ μ λΆμνκΈ°", variant="primary")
|
| 144 |
|
| 145 |
with gr.Column(scale=2):
|
| 146 |
result_display = gr.Label(label="μμΈ‘ μ μ© μ μ")
|
| 147 |
chatbot = gr.Chatbot(label="μ€μκ° μλ΄", height=500)
|
| 148 |
-
|
| 149 |
with gr.Row():
|
| 150 |
msg = gr.Textbox(placeholder="μ§λ¬Έμ μ
λ ₯νμΈμ...", scale=8, container=False)
|
| 151 |
submit_btn = gr.Button("μλ΄νκΈ°", variant="primary", scale=1)
|
| 152 |
|
| 153 |
predict_btn.click(handle_predict, inputs=input_list, outputs=[analysis_report, result_display])
|
| 154 |
|
| 155 |
-
def
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
msg.submit(process_chat, [msg, chatbot], [msg, chatbot]).then(
|
| 159 |
-
generate_response, [chatbot, msg, analysis_report], [chatbot, msg]
|
| 160 |
-
)
|
| 161 |
-
submit_btn.click(process_chat, [msg, chatbot], [msg, chatbot]).then(
|
| 162 |
-
generate_response, [chatbot, msg, analysis_report], [chatbot, msg]
|
| 163 |
-
)
|
| 164 |
|
| 165 |
if __name__ == "__main__":
|
| 166 |
demo.launch()
|
|
|
|
| 1 |
+
# 1. μ΅μλ¨μμλ μ€μ§ κ°λ²Όμ΄ λΌμ΄λΈλ¬λ¦¬λ§ λ‘λ
|
| 2 |
import huggingface_hub
|
| 3 |
if not hasattr(huggingface_hub, "HfFolder"):
|
| 4 |
class MockHfFolder:
|
|
|
|
| 11 |
huggingface_hub.HfFolder = MockHfFolder
|
| 12 |
|
| 13 |
import gradio as gr
|
|
|
|
|
|
|
| 14 |
import os
|
| 15 |
import time
|
| 16 |
|
| 17 |
+
# μ μ μν κ΄λ¦¬ (μ§μ° λ‘λ© μ©)
|
| 18 |
+
_models = {
|
| 19 |
+
"predictor": None,
|
| 20 |
+
"consultant": None
|
| 21 |
+
}
|
| 22 |
|
| 23 |
+
def load_all_models():
|
| 24 |
+
"""μ¬μ©μκ° λ²νΌμ μ²μ λλ₯Ό λλ§ λ¬΄κ±°μ΄ λͺ¨λΈλ€μ λ‘λν©λλ€."""
|
| 25 |
+
global _models
|
| 26 |
+
if _models["predictor"] is None:
|
| 27 |
+
print("Loading heavy models...")
|
| 28 |
+
# ν¨μ λ΄λΆμμ μν¬νΈνμ¬ λΆν
μ λΆν λ°©μ§
|
| 29 |
+
from predictors.score_prediction import CreditPredictor
|
| 30 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 31 |
from langchain_huggingface import HuggingFaceEmbeddings
|
| 32 |
from langchain_community.vectorstores import FAISS
|
| 33 |
from config import EMBEDDING_MODEL, FAISS_PATH, RETRIEVER_K, GEMINI_API_KEY
|
| 34 |
|
| 35 |
+
# 1. μμΈ‘κΈ° λ‘λ
|
| 36 |
+
_models["predictor"] = CreditPredictor()
|
| 37 |
+
|
| 38 |
+
# 2. RAG μλ΄μ λ‘λ
|
| 39 |
class Consultant:
|
| 40 |
def __init__(self):
|
| 41 |
os.environ["GOOGLE_API_KEY"] = os.getenv("GOOGLE_API_KEY", GEMINI_API_KEY)
|
|
|
|
| 46 |
else:
|
| 47 |
self.retriever = None
|
| 48 |
self.llm = ChatGoogleGenerativeAI(model="gemini-1.5-flash", temperature=0.7)
|
| 49 |
+
|
| 50 |
+
_models["consultant"] = Consultant()
|
| 51 |
+
print("Models loaded successfully!")
|
|
|
|
|
|
|
| 52 |
|
| 53 |
FEATURES_MAP = {
|
| 54 |
+
'C1Z001386': '1λ
λ΄ μΉ΄λ μ΄ μ΄μ©κΈμ‘ (λ§μ)', 'C1M210000': 'μ μ©μΉ΄λ 건μ', 'C18210000': '체ν¬μΉ΄λ 건μ',
|
| 55 |
+
'C1L120001': 'μΉ΄λ μ΄ νλκΈμ‘ (λ§μ)', 'C1L120004': 'μΉ΄λ κ°μ€μΌμ', 'L10210000': 'μνμ
μ’
λμΆ κ±΄μ',
|
| 56 |
+
'L90210100': 'λλΆμ
μ’
λμΆ κ±΄μ', 'L90210200': 'μ μΆμν λμΆ κ±΄μ', 'L10210B00': 'μ£Όνλ΄λ³΄ λμΆ κ±΄μ',
|
| 57 |
+
'L10216000': 'μ μ©λμΆ μ΄ μμ‘ (λ§μ)', 'L10217000': 'λ΄λ³΄λμΆ μ΄ μμ‘ (λ§μ)', 'D10110000': 'μ°μ²΄ 건μ',
|
| 58 |
+
'D10133000': 'μ°μ²΄ μμ‘ (λ§μ)', 'PERF1': '90μΌ μ°μ²΄ μ¬λΆ'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
}
|
| 60 |
|
| 61 |
ALL_FEATURES_KEYS = [
|
|
|
|
| 64 |
'L10217000', 'D10110000', 'D10133000', 'PERF1'
|
| 65 |
]
|
| 66 |
|
| 67 |
+
def handle_predict(*args):
|
| 68 |
+
try:
|
| 69 |
+
load_all_models()
|
| 70 |
+
features_dict = {}
|
| 71 |
+
for i, key in enumerate(ALL_FEATURES_KEYS):
|
| 72 |
+
if key == 'PERF1': features_dict[key] = int(args[i])
|
| 73 |
+
else:
|
| 74 |
+
val = str(args[i]).strip() if args[i] else "0"
|
| 75 |
+
features_dict[key] = float(val) if val else 0.0
|
| 76 |
+
|
| 77 |
+
score = _models["predictor"].predict(features_dict)
|
| 78 |
+
display_score = int(round(score)) if score is not None else 0
|
| 79 |
+
return {"features": features_dict, "score": display_score}, display_score
|
| 80 |
+
except Exception as e:
|
| 81 |
+
return {"error": str(e)}, 0
|
| 82 |
+
|
| 83 |
def generate_response(history, user_message, analysis_report):
|
| 84 |
if not user_message: yield history, ""; return
|
| 85 |
+
history = history + [[user_message, "β‘ **μμ€ν
μ€λΉ μ€... (μ΅λ 30μ΄ μμ)**"]]
|
| 86 |
+
yield history, ""
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
try:
|
| 89 |
+
load_all_models()
|
| 90 |
+
cons = _models["consultant"]
|
|
|
|
|
|
|
| 91 |
|
| 92 |
+
# R (Retrieval)
|
| 93 |
+
history[-1][1] = "π **λΆμ λ¬Έμ κ²μ μ€...**"
|
| 94 |
yield history, ""
|
| 95 |
docs = cons.retriever.invoke(user_message) if cons.retriever else []
|
|
|
|
| 96 |
context = "\n\n".join([doc.page_content for doc in docs])
|
| 97 |
|
| 98 |
+
# A (Augmentation)
|
| 99 |
from llm.prompt import QA_PROMPT
|
| 100 |
if analysis_report:
|
| 101 |
score_val = int(analysis_report["score"])
|
| 102 |
features_text = "\n".join([f"- {FEATURES_MAP.get(k, k)}: {v}" for k, v in analysis_report['features'].items()])
|
| 103 |
+
query_text = f"β κ³ κ° μ 보: {score_val}μ \n{features_text}\n\nβ μ§λ¬Έ: {user_message}"
|
| 104 |
else:
|
| 105 |
query_text = f"β μ§λ¬Έ: {user_message}"
|
| 106 |
|
| 107 |
+
# G (Generation)
|
| 108 |
+
history[-1][1] = "π¬ **λ΅λ³ μμ± μ€...**"
|
| 109 |
+
yield history, ""
|
| 110 |
+
|
| 111 |
answer_buffer = ""
|
| 112 |
+
for chunk in cons.llm.stream(QA_PROMPT.format(context=context, query=query_text)):
|
| 113 |
answer_buffer += chunk.content
|
| 114 |
+
history[-1][1] = answer_buffer
|
|
|
|
|
|
|
|
|
|
| 115 |
yield history, ""
|
| 116 |
|
| 117 |
except Exception as e:
|
| 118 |
+
history[-1][1] = f"β οΈ μ€λ₯ λ°μ: {str(e)}"
|
| 119 |
yield history, ""
|
| 120 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
with gr.Blocks(title="KCB AI Consultant") as demo:
|
| 122 |
analysis_report = gr.State(None)
|
| 123 |
+
gr.Markdown("# π‘οΈ KCB AI μ μ© μλ΄ μμ€ν
(Ultra-Stable)")
|
|
|
|
| 124 |
|
| 125 |
with gr.Row():
|
| 126 |
with gr.Column(scale=1):
|
| 127 |
+
input_list = [gr.Checkbox(label=FEATURES_MAP[k]) if k == 'PERF1' else gr.Textbox(label=FEATURES_MAP[k], placeholder="0") for k in ALL_FEATURES_KEYS]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
predict_btn = gr.Button("π μ μ λΆμνκΈ°", variant="primary")
|
| 129 |
|
| 130 |
with gr.Column(scale=2):
|
| 131 |
result_display = gr.Label(label="μμΈ‘ μ μ© μ μ")
|
| 132 |
chatbot = gr.Chatbot(label="μ€μκ° μλ΄", height=500)
|
|
|
|
| 133 |
with gr.Row():
|
| 134 |
msg = gr.Textbox(placeholder="μ§λ¬Έμ μ
λ ₯νμΈμ...", scale=8, container=False)
|
| 135 |
submit_btn = gr.Button("μλ΄νκΈ°", variant="primary", scale=1)
|
| 136 |
|
| 137 |
predict_btn.click(handle_predict, inputs=input_list, outputs=[analysis_report, result_display])
|
| 138 |
|
| 139 |
+
def clear_msg(m, h): return "", h + [[m, "μκ° μ€..."]]
|
| 140 |
+
msg.submit(clear_msg, [msg, chatbot], [msg, chatbot]).then(generate_response, [chatbot, msg, analysis_report], [chatbot, msg])
|
| 141 |
+
submit_btn.click(clear_msg, [msg, chatbot], [msg, chatbot]).then(generate_response, [chatbot, msg, analysis_report], [chatbot, msg])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
|
| 143 |
if __name__ == "__main__":
|
| 144 |
demo.launch()
|