Update app.py
Browse files
app.py
CHANGED
|
@@ -1,232 +1,164 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import threading
|
| 3 |
-
import
|
| 4 |
-
import pickle
|
| 5 |
from flask import Flask, request, jsonify
|
|
|
|
| 6 |
from environment import DocumentClassificationEnv
|
| 7 |
from baseline_inference import run_task, load_or_train
|
| 8 |
from agent import TicketAgent
|
| 9 |
|
|
|
|
|
|
|
|
|
|
| 10 |
print("Pre-training models...")
|
| 11 |
-
models = {}
|
| 12 |
-
for d in ["easy", "medium", "hard"]:
|
| 13 |
-
models[d] = load_or_train(d)
|
| 14 |
-
print(f" {d} model ready!")
|
| 15 |
print("All models ready!")
|
| 16 |
|
| 17 |
agent = TicketAgent(model_dir=".")
|
| 18 |
-
print("Agent ready!")
|
| 19 |
-
|
| 20 |
-
CATEGORIES = {
|
| 21 |
-
"easy": ["General","Billing","Support","Technical","HR"],
|
| 22 |
-
"medium": ["General","Billing","Support","Technical","HR","Legal","Sales","Marketing","Operations","Complaints"],
|
| 23 |
-
"hard": [f"Cat_{i}" for i in range(22)]
|
| 24 |
-
}
|
| 25 |
|
|
|
|
|
|
|
|
|
|
| 26 |
api = Flask(__name__)
|
| 27 |
api_envs = {}
|
| 28 |
|
| 29 |
@api.route("/api/reset", methods=["POST"])
|
| 30 |
def api_reset():
|
| 31 |
data = request.json or {}
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
env =
|
| 35 |
-
|
| 36 |
-
api_envs["current"] = env
|
| 37 |
-
return jsonify({"observation": {k: v.tolist() if hasattr(v, 'tolist') else v for k, v in obs.items()}})
|
| 38 |
-
|
| 39 |
-
@api.route("/api/step", methods=["POST"])
|
| 40 |
-
def api_step():
|
| 41 |
-
data = request.json or {}
|
| 42 |
-
action = data.get("action", 0)
|
| 43 |
-
env = api_envs.get("current")
|
| 44 |
-
if env is None:
|
| 45 |
-
return jsonify({"error": "Call /api/reset first"}), 400
|
| 46 |
-
obs, reward, done, _, info = env.step(int(action))
|
| 47 |
-
return jsonify({
|
| 48 |
-
"observation": {k: v.tolist() if hasattr(v, 'tolist') else v for k, v in obs.items()},
|
| 49 |
-
"reward": reward, "done": done, "info": info
|
| 50 |
-
})
|
| 51 |
-
|
| 52 |
-
@api.route("/api/run", methods=["POST"])
|
| 53 |
-
def api_run():
|
| 54 |
-
data = request.json or {}
|
| 55 |
-
difficulty = data.get("difficulty", "easy")
|
| 56 |
-
score, t = run_task(difficulty)
|
| 57 |
-
return jsonify({"difficulty": difficulty, "score": score, "time": t})
|
| 58 |
-
|
| 59 |
-
@api.route("/api/agent/process", methods=["POST"])
|
| 60 |
-
def api_agent_process():
|
| 61 |
-
data = request.json or {}
|
| 62 |
-
text = data.get("text", "")
|
| 63 |
-
difficulty = data.get("difficulty", "easy")
|
| 64 |
-
ticket_id = data.get("ticket_id", None)
|
| 65 |
-
if not text:
|
| 66 |
-
return jsonify({"error": "text field required"}), 400
|
| 67 |
-
result = agent.process_ticket(text, difficulty=difficulty, ticket_id=ticket_id)
|
| 68 |
-
return jsonify(result)
|
| 69 |
-
|
| 70 |
-
@api.route("/api/agent/classify", methods=["POST"])
|
| 71 |
-
def api_agent_classify():
|
| 72 |
-
data = request.json or {}
|
| 73 |
-
text = data.get("text", "")
|
| 74 |
-
difficulty = data.get("difficulty", "easy")
|
| 75 |
-
category, scores = agent.classify(text, difficulty)
|
| 76 |
-
priority = agent.get_priority(text)
|
| 77 |
-
return jsonify({"category": category, "priority": priority, "scores": scores})
|
| 78 |
|
| 79 |
def run_flask():
|
| 80 |
-
api.run(host="0.0.0.0", port=7861
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
env_state = {"env": None, "difficulty": "easy"}
|
| 83 |
|
| 84 |
def create_env(difficulty):
|
| 85 |
-
|
| 86 |
-
obs, _ =
|
| 87 |
-
env_state["env"] =
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
return (f"โ
Environment created! Difficulty: {difficulty}",
|
| 97 |
-
obs["content"],
|
| 98 |
-
f"๐ค Model predicts: **{pred_cat}**\n\nTop 3 predictions:\n{explain}",
|
| 99 |
-
"")
|
| 100 |
-
|
| 101 |
-
def classify_doc(category_idx):
|
| 102 |
-
e = env_state["env"]
|
| 103 |
-
if e is None:
|
| 104 |
-
return "Create environment first!", "", "", ""
|
| 105 |
-
obs, reward, done, _, info = e.step(int(category_idx))
|
| 106 |
-
result = f"Reward: {reward:.2f} | Correct: {info.get('is_correct', False)}"
|
| 107 |
if done:
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
return result, "Episode complete โ create new environment", "", str(info)
|
| 111 |
-
difficulty = env_state["difficulty"]
|
| 112 |
-
model = models[difficulty]
|
| 113 |
-
cats = CATEGORIES[difficulty]
|
| 114 |
-
proba = model.predict_proba([obs["content"]])[0]
|
| 115 |
-
top3 = sorted(enumerate(proba), key=lambda x: -x[1])[:3]
|
| 116 |
-
explain = "\n".join([f" {cats[i] if i < len(cats) else f'Cat_{i}'}: {p*100:.1f}%" for i,p in top3])
|
| 117 |
-
pred_idx = int(model.predict([obs["content"]])[0])
|
| 118 |
-
pred_cat = cats[pred_idx] if pred_idx < len(cats) else f"Cat_{pred_idx}"
|
| 119 |
-
return (result, obs["content"],
|
| 120 |
-
f"๐ค Model predicts: **{pred_cat}**\n\nTop 3 predictions:\n{explain}",
|
| 121 |
-
str(info))
|
| 122 |
|
| 123 |
def run_baseline_all():
|
| 124 |
rows = []
|
| 125 |
-
for d in ["easy",
|
| 126 |
score, t = run_task(d)
|
| 127 |
-
rows.append([d.upper(),
|
| 128 |
-
return rows
|
| 129 |
-
|
| 130 |
-
def process_ticket_ui(
|
| 131 |
-
if not
|
| 132 |
-
return "
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
|
|
|
| 159 |
with gr.Row():
|
| 160 |
-
with gr.Column(
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
)
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
btn_process.click(
|
| 183 |
-
process_ticket_ui,
|
| 184 |
-
inputs=[ticket_input, diff_agent],
|
| 185 |
-
outputs=[cat_out_box, pri_out_box, dept_out_box, top3_out_box, ref_out_box, reply_out_box]
|
| 186 |
-
)
|
| 187 |
-
|
| 188 |
-
with gr.Tab("๐ฎ Interactive Demo"):
|
| 189 |
diff = gr.Radio(["easy","medium","hard"], value="easy", label="Difficulty")
|
| 190 |
-
btn_create = gr.Button("Create Environment"
|
|
|
|
| 191 |
status = gr.Textbox(label="Status")
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
result = gr.Textbox(label="Result")
|
| 198 |
-
info_box = gr.Textbox(label="Info")
|
| 199 |
-
btn_create.click(create_env, inputs=diff, outputs=[status, doc_content, explain_box, result])
|
| 200 |
-
btn_classify.click(classify_doc, inputs=category, outputs=[result, doc_content, explain_box, info_box])
|
| 201 |
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
|
| 208 |
-
|
|
|
|
|
|
|
|
|
|
| 209 |
gr.Markdown("""
|
| 210 |
-
##
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
`Ticket Input` โ `ML Classify` โ `Rule Priority` โ `Department Route` โ `Auto Reply`
|
| 221 |
-
|
| 222 |
-
## API Endpoints (port 7861)
|
| 223 |
-
- `POST /api/reset` โ `{"difficulty": "easy", "seed": 42}`
|
| 224 |
-
- `POST /api/step` โ `{"action": 0}`
|
| 225 |
-
- `POST /api/agent/process` โ `{"text": "my invoice is wrong", "difficulty": "medium"}`
|
| 226 |
-
- `POST /api/agent/classify` โ `{"text": "urgent bug", "difficulty": "easy"}`
|
| 227 |
""")
|
| 228 |
|
|
|
|
|
|
|
|
|
|
| 229 |
if __name__ == "__main__":
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import threading
|
| 3 |
+
import pandas as pd
|
|
|
|
| 4 |
from flask import Flask, request, jsonify
|
| 5 |
+
|
| 6 |
from environment import DocumentClassificationEnv
|
| 7 |
from baseline_inference import run_task, load_or_train
|
| 8 |
from agent import TicketAgent
|
| 9 |
|
| 10 |
+
# =========================
|
| 11 |
+
# ๐ LOAD MODELS
|
| 12 |
+
# =========================
|
| 13 |
print("Pre-training models...")
|
| 14 |
+
models = {d: load_or_train(d) for d in ["easy", "medium", "hard"]}
|
|
|
|
|
|
|
|
|
|
| 15 |
print("All models ready!")
|
| 16 |
|
| 17 |
agent = TicketAgent(model_dir=".")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
# =========================
|
| 20 |
+
# ๐ FLASK API
|
| 21 |
+
# =========================
|
| 22 |
api = Flask(__name__)
|
| 23 |
api_envs = {}
|
| 24 |
|
| 25 |
@api.route("/api/reset", methods=["POST"])
|
| 26 |
def api_reset():
|
| 27 |
data = request.json or {}
|
| 28 |
+
env = DocumentClassificationEnv(task_difficulty=data.get("difficulty","easy"))
|
| 29 |
+
obs, _ = env.reset()
|
| 30 |
+
api_envs["env"] = env
|
| 31 |
+
return jsonify(obs)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
def run_flask():
|
| 34 |
+
api.run(host="0.0.0.0", port=7861)
|
| 35 |
+
|
| 36 |
+
# =========================
|
| 37 |
+
# ๐ฏ UI LOGIC
|
| 38 |
+
# =========================
|
| 39 |
|
| 40 |
env_state = {"env": None, "difficulty": "easy"}
|
| 41 |
|
| 42 |
def create_env(difficulty):
|
| 43 |
+
env = DocumentClassificationEnv(task_difficulty=difficulty)
|
| 44 |
+
obs, _ = env.reset()
|
| 45 |
+
env_state["env"] = env
|
| 46 |
+
return f"โ
Environment ready ({difficulty})", obs["content"]
|
| 47 |
+
|
| 48 |
+
def classify_doc(idx):
|
| 49 |
+
env = env_state["env"]
|
| 50 |
+
if not env:
|
| 51 |
+
return "โ ๏ธ Create environment first!", ""
|
| 52 |
+
obs, reward, done, _, info = env.step(int(idx))
|
| 53 |
+
msg = f"๐ฏ Reward: {reward} | Correct: {info['is_correct']}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
if done:
|
| 55 |
+
msg += "\n๐ Episode finished!"
|
| 56 |
+
return msg, obs["content"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
def run_baseline_all():
|
| 59 |
rows = []
|
| 60 |
+
for d in ["easy","medium","hard"]:
|
| 61 |
score, t = run_task(d)
|
| 62 |
+
rows.append([d.upper(), score, score*100, t])
|
| 63 |
+
return pd.DataFrame(rows, columns=["Task","Score","Accuracy (%)","Time (s)"])
|
| 64 |
+
|
| 65 |
+
def process_ticket_ui(text, difficulty):
|
| 66 |
+
if not text.strip():
|
| 67 |
+
return "Enter ticket!", "", "", "", "", ""
|
| 68 |
+
|
| 69 |
+
res = agent.process_ticket(text, difficulty=difficulty)
|
| 70 |
+
|
| 71 |
+
return (
|
| 72 |
+
f"{res['category']} ({res['confidence']*100:.1f}%)",
|
| 73 |
+
res["priority"],
|
| 74 |
+
res["department"],
|
| 75 |
+
"\n".join([f"{c}: {p*100:.1f}%" for c,p in res["top3"]]),
|
| 76 |
+
res["ref_id"],
|
| 77 |
+
res["reply"]
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
# =========================
|
| 81 |
+
# ๐จ UI DESIGN
|
| 82 |
+
# =========================
|
| 83 |
+
|
| 84 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="AI Ticket System") as demo:
|
| 85 |
+
|
| 86 |
+
gr.Markdown("""
|
| 87 |
+
# ๐ค AI Ticket Classification System
|
| 88 |
+
### Smart routing โข ML predictions โข Real-time feedback
|
| 89 |
+
""")
|
| 90 |
+
|
| 91 |
+
# -------------------------
|
| 92 |
+
# ๐ง Agent Tab
|
| 93 |
+
# -------------------------
|
| 94 |
+
with gr.Tab("๐ค Smart Agent"):
|
| 95 |
with gr.Row():
|
| 96 |
+
with gr.Column():
|
| 97 |
+
ticket_input = gr.Textbox(label="๐ฉ Ticket Text", lines=6)
|
| 98 |
+
difficulty = gr.Radio(["easy","medium","hard"], value="medium", label="Difficulty")
|
| 99 |
+
btn = gr.Button("๐ Process", variant="primary")
|
| 100 |
+
|
| 101 |
+
with gr.Column():
|
| 102 |
+
cat = gr.Textbox(label="๐ท Category")
|
| 103 |
+
pri = gr.Textbox(label="โก Priority")
|
| 104 |
+
dept = gr.Textbox(label="๐ข Department")
|
| 105 |
+
top3 = gr.Textbox(label="๐ Top Predictions")
|
| 106 |
+
ref = gr.Textbox(label="๐ซ Reference ID")
|
| 107 |
+
|
| 108 |
+
reply = gr.Textbox(label="๐ง Auto Reply", lines=6)
|
| 109 |
+
|
| 110 |
+
btn.click(process_ticket_ui,
|
| 111 |
+
inputs=[ticket_input, difficulty],
|
| 112 |
+
outputs=[cat, pri, dept, top3, ref, reply])
|
| 113 |
+
|
| 114 |
+
# -------------------------
|
| 115 |
+
# ๐ฎ Interactive Env
|
| 116 |
+
# -------------------------
|
| 117 |
+
with gr.Tab("๐ฎ Simulator"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
diff = gr.Radio(["easy","medium","hard"], value="easy", label="Difficulty")
|
| 119 |
+
btn_create = gr.Button("Create Environment")
|
| 120 |
+
|
| 121 |
status = gr.Textbox(label="Status")
|
| 122 |
+
doc = gr.Textbox(label="Document", lines=5)
|
| 123 |
+
|
| 124 |
+
idx = gr.Number(label="Category Index")
|
| 125 |
+
btn_step = gr.Button("Classify")
|
| 126 |
+
|
| 127 |
result = gr.Textbox(label="Result")
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
+
btn_create.click(create_env, inputs=diff, outputs=[status, doc])
|
| 130 |
+
btn_step.click(classify_doc, inputs=idx, outputs=[result, doc])
|
| 131 |
+
|
| 132 |
+
# -------------------------
|
| 133 |
+
# ๐ Evaluation Tab
|
| 134 |
+
# -------------------------
|
| 135 |
+
with gr.Tab("๐ Model Performance"):
|
| 136 |
+
gr.Markdown("Run baseline models across all difficulties.")
|
| 137 |
+
btn_eval = gr.Button("Run Evaluation", variant="primary")
|
| 138 |
+
table = gr.Dataframe()
|
| 139 |
+
|
| 140 |
+
btn_eval.click(run_baseline_all, outputs=table)
|
| 141 |
|
| 142 |
+
# -------------------------
|
| 143 |
+
# โน๏ธ Info
|
| 144 |
+
# -------------------------
|
| 145 |
+
with gr.Tab("โน๏ธ About"):
|
| 146 |
gr.Markdown("""
|
| 147 |
+
## ๐ System Overview
|
| 148 |
+
- ML-based classification
|
| 149 |
+
- Rule-based priority detection
|
| 150 |
+
- Auto ticket routing
|
| 151 |
+
- Real-time simulation environment
|
| 152 |
+
|
| 153 |
+
Built with:
|
| 154 |
+
- Gradio UI
|
| 155 |
+
- Flask API
|
| 156 |
+
- Custom RL Environment
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
""")
|
| 158 |
|
| 159 |
+
# =========================
|
| 160 |
+
# ๐ RUN
|
| 161 |
+
# =========================
|
| 162 |
if __name__ == "__main__":
|
| 163 |
+
threading.Thread(target=run_flask, daemon=True).start()
|
| 164 |
+
demo.launch(server_name="0.0.0.0", server_port=7860, share=True)
|
|
|