Spaces:
Sleeping
Sleeping
Stabilize: Downgrade to Gradio 4.44.1 and implement Lazy Loading to fix 500 error
Browse files- README.md +3 -3
- app.py +76 -68
- requirements.txt +1 -1
README.md
CHANGED
|
@@ -4,11 +4,11 @@ emoji: π‘οΈ
|
|
| 4 |
colorFrom: blue
|
| 5 |
colorTo: indigo
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version:
|
| 8 |
python_version: "3.10"
|
| 9 |
app_file: app.py
|
| 10 |
pinned: false
|
| 11 |
---
|
| 12 |
|
| 13 |
-
# π‘οΈ KCB AI μ μ© μ μ
|
| 14 |
-
|
|
|
|
| 4 |
colorFrom: blue
|
| 5 |
colorTo: indigo
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 4.44.1
|
| 8 |
python_version: "3.10"
|
| 9 |
app_file: app.py
|
| 10 |
pinned: false
|
| 11 |
---
|
| 12 |
|
| 13 |
+
# π‘οΈ KCB AI μ μ© μ μ μλ΄ μλΉμ€
|
| 14 |
+
κ°μ₯ μμ μ μΈ μ΅μ λ°°ν¬ λ²μ μ
λλ€.
|
app.py
CHANGED
|
@@ -13,21 +13,35 @@ import gradio as gr
|
|
| 13 |
import pandas as pd
|
| 14 |
import numpy as np
|
| 15 |
import os
|
| 16 |
-
import json
|
| 17 |
import time
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
from langchain_community.vectorstores import FAISS
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
#
|
| 28 |
-
|
| 29 |
|
| 30 |
-
# 14κ° νΌμ²μ λν μμΈ μ€λͺ
λ§€ν
|
| 31 |
FEATURES_MAP = {
|
| 32 |
'C1Z001386': '1λ
λ΄ μΉ΄λ μ΄ μ΄μ©κΈμ‘ (λ§μ)',
|
| 33 |
'C1M210000': 'μ μ©μΉ΄λ 건μ',
|
|
@@ -45,37 +59,35 @@ FEATURES_MAP = {
|
|
| 45 |
'PERF1': '90μΌ μ°μ²΄ μ¬λΆ'
|
| 46 |
}
|
| 47 |
|
| 48 |
-
# λͺ¨λΈμ΄ μꡬνλ μ νν μμ
|
| 49 |
ALL_FEATURES_KEYS = [
|
| 50 |
'C1Z001386', 'C1M210000', 'C18210000', 'C1L120001', 'C1L120004',
|
| 51 |
'L10210000', 'L90210100', 'L90210200', 'L10210B00', 'L10216000',
|
| 52 |
'L10217000', 'D10110000', 'D10133000', 'PERF1'
|
| 53 |
]
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
if history and history[-1].get("content") == "μκ° μ€...": history = history[:-1]
|
| 69 |
-
|
| 70 |
-
history = history + [{"role": "assistant", "content": ""}]
|
| 71 |
-
t0 = time.time()
|
| 72 |
|
| 73 |
# R
|
| 74 |
-
|
|
|
|
|
|
|
| 75 |
t_retrieve = time.time() - t0
|
| 76 |
context = "\n\n".join([doc.page_content for doc in docs])
|
| 77 |
|
| 78 |
# A
|
|
|
|
| 79 |
if analysis_report:
|
| 80 |
score_val = int(analysis_report["score"])
|
| 81 |
features_text = "\n".join([f"- {FEATURES_MAP.get(k, k)}: {v}" for k, v in analysis_report['features'].items()])
|
|
@@ -88,29 +100,40 @@ class CreditRAGConsultant:
|
|
| 88 |
|
| 89 |
# G
|
| 90 |
answer_buffer = ""
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
)
|
| 98 |
-
yield history, ""
|
| 99 |
-
except Exception as e:
|
| 100 |
-
history[-1]["content"] = f"β οΈ μλ¬: {str(e)}"
|
| 101 |
yield history, ""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
-
with gr.Blocks(title="KCB
|
| 106 |
analysis_report = gr.State(None)
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
gr.Markdown("# π‘οΈ KCB AI μ μ© μ μ λΆμ μμ€ν
")
|
| 110 |
|
| 111 |
with gr.Row():
|
| 112 |
with gr.Column(scale=1):
|
| 113 |
-
gr.Markdown("### π κΈμ΅ μ§ν μ
λ ₯")
|
| 114 |
input_list = []
|
| 115 |
for key in ALL_FEATURES_KEYS:
|
| 116 |
if key == 'PERF1':
|
|
@@ -121,37 +144,22 @@ with gr.Blocks(title="KCB Credit AI", theme=gr.themes.Soft()) as demo:
|
|
| 121 |
|
| 122 |
with gr.Column(scale=2):
|
| 123 |
result_display = gr.Label(label="μμΈ‘ μ μ© μ μ")
|
| 124 |
-
chatbot = gr.Chatbot(label="μ€μκ° μλ΄", height=
|
| 125 |
|
| 126 |
with gr.Row():
|
| 127 |
msg = gr.Textbox(placeholder="μ§λ¬Έμ μ
λ ₯νμΈμ...", scale=8, container=False)
|
| 128 |
submit_btn = gr.Button("μλ΄νκΈ°", variant="primary", scale=1)
|
| 129 |
|
| 130 |
-
def handle_predict(*args):
|
| 131 |
-
features_dict = {}
|
| 132 |
-
# μ
λ ₯ 리μ€νΈμ ν€ μμκ° λμΌν¨
|
| 133 |
-
for i, key in enumerate(ALL_FEATURES_KEYS):
|
| 134 |
-
if key == 'PERF1':
|
| 135 |
-
features_dict[key] = int(args[i])
|
| 136 |
-
else:
|
| 137 |
-
val = args[i] if args[i] else "0"
|
| 138 |
-
features_dict[key] = float(val)
|
| 139 |
-
|
| 140 |
-
score = predictor.predict(features_dict)
|
| 141 |
-
display_score = int(min(max(round(score), 0), 1000))
|
| 142 |
-
return {"features": features_dict, "score": display_score}, display_score
|
| 143 |
-
|
| 144 |
-
def user_msg(user_message, history):
|
| 145 |
-
if not user_message: return history, "", ""
|
| 146 |
-
return history + [{"role": "user", "content": user_message}, {"role": "assistant", "content": "μκ° μ€..."}], "", user_message
|
| 147 |
-
|
| 148 |
predict_btn.click(handle_predict, inputs=input_list, outputs=[analysis_report, result_display])
|
| 149 |
|
| 150 |
-
|
| 151 |
-
|
|
|
|
|
|
|
|
|
|
| 152 |
)
|
| 153 |
-
submit_btn.click(
|
| 154 |
-
|
| 155 |
)
|
| 156 |
|
| 157 |
if __name__ == "__main__":
|
|
|
|
| 13 |
import pandas as pd
|
| 14 |
import numpy as np
|
| 15 |
import os
|
|
|
|
| 16 |
import time
|
| 17 |
|
| 18 |
+
# μ§μ° λ‘λ©μ μν΄ μ μ λ³μλ‘ μ μΈ
|
| 19 |
+
_consultant = None
|
|
|
|
| 20 |
|
| 21 |
+
def get_consultant():
|
| 22 |
+
global _consultant
|
| 23 |
+
if _consultant is None:
|
| 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)
|
| 32 |
+
self.embedding_model = HuggingFaceEmbeddings(model_name=EMBEDDING_MODEL)
|
| 33 |
+
if os.path.exists(FAISS_PATH):
|
| 34 |
+
self.vectorstore = FAISS.load_local(FAISS_PATH, self.embedding_model, allow_dangerous_deserialization=True)
|
| 35 |
+
self.retriever = self.vectorstore.as_retriever(search_kwargs={"k": RETRIEVER_K})
|
| 36 |
+
else:
|
| 37 |
+
self.retriever = None
|
| 38 |
+
self.llm = ChatGoogleGenerativeAI(model="gemini-1.5-flash", temperature=0.7)
|
| 39 |
+
_consultant = Consultant()
|
| 40 |
+
return _consultant
|
| 41 |
|
| 42 |
+
# μμΈ‘κΈ° λ‘λ
|
| 43 |
+
from predictors.score_prediction import predictor
|
| 44 |
|
|
|
|
| 45 |
FEATURES_MAP = {
|
| 46 |
'C1Z001386': '1λ
λ΄ μΉ΄λ μ΄ μ΄μ©κΈμ‘ (λ§μ)',
|
| 47 |
'C1M210000': 'μ μ©μΉ΄λ 건μ',
|
|
|
|
| 59 |
'PERF1': '90μΌ μ°μ²΄ μ¬λΆ'
|
| 60 |
}
|
| 61 |
|
|
|
|
| 62 |
ALL_FEATURES_KEYS = [
|
| 63 |
'C1Z001386', 'C1M210000', 'C18210000', 'C1L120001', 'C1L120004',
|
| 64 |
'L10210000', 'L90210100', 'L90210200', 'L10210B00', 'L10216000',
|
| 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 |
+
if history and isinstance(history[-1], tuple) and history[-1][1] == "μκ° μ€...": history = history[:-1]
|
| 71 |
+
|
| 72 |
+
# μ± μν μ΄κΈ°ν
|
| 73 |
+
history = history + [[user_message, ""]]
|
| 74 |
+
t0 = time.time()
|
| 75 |
+
|
| 76 |
+
try:
|
| 77 |
+
# μ§μ° λ‘λ© μ€ν
|
| 78 |
+
history[-1][1] = "β‘ **μμ€ν
μ΄κΈ°ν μ€...**"
|
| 79 |
+
yield history, ""
|
| 80 |
+
cons = get_consultant()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
# R
|
| 83 |
+
history[-1][1] = "π **[R] λΆμ λ¬Έμ κ²μ μ€...**"
|
| 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()])
|
|
|
|
| 100 |
|
| 101 |
# G
|
| 102 |
answer_buffer = ""
|
| 103 |
+
for chunk in cons.llm.stream(prompt_text):
|
| 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"β οΈ μλ² μ€λ₯κ° λ°μνμ΅λλ€: {str(e)}"
|
| 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':
|
|
|
|
| 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 process_chat(message, history):
|
| 156 |
+
return "", history + [[message, "μκ° μ€..."]]
|
| 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__":
|
requirements.txt
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
gradio==
|
| 2 |
pandas
|
| 3 |
numpy<2.0.0
|
| 4 |
tensorflow-cpu==2.15.0
|
|
|
|
| 1 |
+
gradio==4.44.1
|
| 2 |
pandas
|
| 3 |
numpy<2.0.0
|
| 4 |
tensorflow-cpu==2.15.0
|