TanujInsane commited on
Commit
28bf6cd
ยท
1 Parent(s): a739312

fix app imports and syntax

Browse files
Files changed (1) hide show
  1. app.py +217 -252
app.py CHANGED
@@ -1,267 +1,232 @@
1
- """
2
- Gradio UI for Document Classification Environment
3
- Interactive demo for judges + baseline evaluation
4
- """
5
-
6
  import gradio as gr
7
- import json
8
- import time
 
 
9
  from environment import DocumentClassificationEnv
10
- from baseline_inference import keyword_agent
11
-
12
- # Global state
13
- current_env = None
14
- current_obs = None
15
- current_difficulty = "easy"
16
-
17
- CATEGORY_COLORS = {
18
- "General": "๐Ÿ”ต", "Billing": "๐Ÿ’ฐ", "Billing-Dispute": "โš ๏ธ", "Billing-Refund": "๐Ÿ’ธ",
19
- "Support": "๐Ÿ†˜", "Support-Urgent": "๐Ÿšจ", "Support-Normal": "๐Ÿ’ฌ",
20
- "Technical": "โš™๏ธ", "Technical-Bug": "๐Ÿ›", "Technical-Feature": "โœจ",
21
- "HR": "๐Ÿ‘ฅ", "HR-Payroll": "๐Ÿ’ต", "HR-Benefits": "๐Ÿฅ", "HR-Complaint": "๐Ÿ“ข",
22
- "Legal": "โš–๏ธ", "Legal-Contract": "๐Ÿ“", "Legal-Compliance": "๐Ÿ“‹",
23
- "Executive": "๐Ÿ‘”", "Executive-Strategic": "๐ŸŽฏ",
24
- "Finance": "๐Ÿ“Š", "Marketing": "๐Ÿ“ฃ", "Operations": "๐Ÿ”ง"
 
 
25
  }
26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  SAMPLE_TICKETS = {
28
- "Billing Issue": "I was overcharged on my last invoice. The amount shows $150 but it should be $100. Please review and correct this billing error immediately.",
29
- "Urgent Bug": "URGENT: Our production system is completely down! The application crashes every time users try to login. This is affecting all customers. Critical issue needs immediate attention!",
30
- "Feature Request": "I would like to request a new feature for the dashboard. Can you add functionality for exporting data to CSV format? This would greatly improve our workflow.",
31
- "HR Complaint": "I would like to file a formal complaint about a workplace issue. A colleague has been creating a hostile work environment and I need to report this serious HR matter.",
32
- "Legal Contract": "I need a review of this contract before signing. The contract terms need clarification, specifically regarding the liability clauses and termination provisions.",
33
- "Refund Request": "I would like to request a refund for my recent purchase. The product did not meet the specifications described and I need to return it and get refunded.",
34
  }
35
 
36
-
37
- def start_episode(difficulty):
38
- global current_env, current_obs, current_difficulty
39
- current_difficulty = difficulty
40
- current_env = DocumentClassificationEnv(task_difficulty=difficulty, seed=42)
41
- current_obs, _ = current_env.reset()
42
-
43
- categories = list(current_env.CATEGORY_MAPS[difficulty].values())
44
- doc_content = current_obs["content"]
45
- word_count = int(current_obs["word_count"][0])
46
- doc_id = current_obs["document_id"]
47
- total = int(current_obs["total_documents"][0])
48
-
49
- info_text = f"**Document ID:** {doc_id} | **Words:** {word_count} | **Total Docs:** {total}"
50
- return doc_content, info_text, gr.update(choices=categories, value=None), "Episode started! Classify the document above.", ""
51
-
52
-
53
- def classify_document(selected_category):
54
- global current_env, current_obs, current_difficulty
55
-
56
- if current_env is None:
57
- return "Please start an episode first!", "", "", gr.update(choices=[])
58
- if selected_category is None:
59
- return current_obs["content"], "", "Please select a category!", ""
60
-
61
- categories = list(current_env.CATEGORY_MAPS[current_difficulty].values())
62
- action = categories.index(selected_category)
63
-
64
- obs, reward, terminated, _, info = current_env.step(action)
65
- current_obs = obs
66
-
67
- is_correct = info.get("is_correct", False)
68
- true_cat = info.get("true_category", "?")
69
- pred_cat = info.get("predicted_category", "?")
70
- accuracy = info.get("episode_accuracy", 0)
71
-
72
- true_icon = CATEGORY_COLORS.get(true_cat, "๐Ÿ“„")
73
- pred_icon = CATEGORY_COLORS.get(pred_cat, "๐Ÿ“„")
74
-
75
- result = f"{'โœ… CORRECT' if is_correct else 'โŒ INCORRECT'}\n"
76
- result += f"Your pick: {pred_icon} {pred_cat}\n"
77
- result += f"True category: {true_icon} {true_cat}\n"
78
- result += f"Reward: {reward:.3f} | Accuracy so far: {accuracy:.1%}"
79
-
80
- if terminated:
81
- summary = info.get("episode_summary", {})
82
- final_acc = summary.get("accuracy", 0)
83
- total_reward = summary.get("total_reward", 0)
84
- next_doc = "๐ŸŽ‰ Episode Complete!"
85
- result += f"\n\n**FINAL: Accuracy={final_acc:.1%} | Total Reward={total_reward:.2f}**"
86
- return "Episode complete! Click 'Start Episode' to play again.", "", result, gr.update(choices=categories)
87
-
88
- next_doc = obs["content"]
89
- next_id = obs["document_id"]
90
- next_words = int(obs["word_count"][0])
91
- next_idx = int(obs["document_index"][0])
92
- total = int(obs["total_documents"][0])
93
- info_text = f"**Document ID:** {next_id} | **Words:** {next_words} | **Progress:** {next_idx}/{total}"
94
-
95
- return next_doc, info_text, result, gr.update(choices=categories, value=None)
96
-
97
-
98
- def auto_classify():
99
- global current_env, current_obs, current_difficulty
100
- if current_env is None or current_obs is None:
101
- return "Please start an episode first!", "", "No episode running.", ""
102
-
103
- action = keyword_agent(current_obs, current_difficulty)
104
- categories = list(current_env.CATEGORY_MAPS[current_difficulty].values())
105
- selected = categories[action]
106
- return classify_document(selected)
107
-
108
-
109
  def load_sample(sample_name):
110
  return SAMPLE_TICKETS.get(sample_name, "")
111
 
112
-
113
- def run_baseline_eval(difficulty):
114
- env = DocumentClassificationEnv(task_difficulty=difficulty, seed=42)
115
- obs, _ = env.reset()
116
- correct = 0
117
- total = 0
118
- terminated = False
119
- results = []
120
-
121
- while not terminated:
122
- action = keyword_agent(obs, difficulty)
123
- obs, reward, terminated, _, info = env.step(action)
124
- correct += int(info.get("is_correct", False))
125
- total += 1
126
- if total <= 10:
127
- true_cat = info.get("true_category", "?")
128
- pred_cat = info.get("predicted_category", "?")
129
- icon = "โœ…" if info.get("is_correct") else "โŒ"
130
- results.append(f"{icon} Predicted: {pred_cat} | True: {true_cat}")
131
-
132
- accuracy = correct / total if total > 0 else 0
133
- output = f"## Baseline Results โ€” {difficulty.upper()}\n\n"
134
- output += f"**Accuracy: {accuracy:.1%}** ({correct}/{total} correct)\n\n"
135
- output += "### Sample Predictions (first 10):\n"
136
- output += "\n".join(results)
137
- return output
138
-
139
-
140
- def create_interface():
141
- with gr.Blocks(title="Document Classification OpenEnv", theme=gr.themes.Soft()) as demo:
142
- gr.Markdown("""
143
- # ๐Ÿ“„ Document Classification Environment
144
- ### OpenEnv โ€” Meta x PyTorch Hackathon Submission
145
- An interactive environment for training AI agents to classify and route support tickets.
146
- """)
147
-
148
- with gr.Tabs():
149
- # Tab 1: Interactive Demo
150
- with gr.TabItem("๐ŸŽฎ Interactive Demo"):
151
- gr.Markdown("### Try classifying documents yourself!")
152
- with gr.Row():
153
- difficulty_select = gr.Radio(
154
- choices=["easy", "medium", "hard"],
155
- value="easy",
156
- label="Difficulty Level",
157
- info="Easy=5 categories, Medium=10, Hard=22"
158
- )
159
- start_btn = gr.Button("โ–ถ Start Episode", variant="primary", scale=1)
160
-
161
- with gr.Row():
162
- with gr.Column(scale=2):
163
- doc_display = gr.Textbox(
164
- label="๐Ÿ“จ Document to Classify",
165
- lines=6,
166
- placeholder="Click 'Start Episode' to begin..."
167
- )
168
- doc_info = gr.Markdown("")
169
-
170
- with gr.Column(scale=1):
171
- category_radio = gr.Radio(choices=[], label="๐Ÿท๏ธ Select Category")
172
- with gr.Row():
173
- classify_btn = gr.Button("โœ… Classify", variant="primary")
174
- auto_btn = gr.Button("๐Ÿค– Auto (AI)", variant="secondary")
175
-
176
- result_display = gr.Textbox(label="๐Ÿ“Š Result", lines=5)
177
-
178
- # Sample tickets
179
- gr.Markdown("### ๐Ÿ“‹ Try Sample Tickets")
180
- with gr.Row():
181
- for name in SAMPLE_TICKETS:
182
- gr.Button(name).click(
183
- fn=lambda n=name: load_sample(n),
184
- outputs=doc_display
185
- )
186
-
187
- start_btn.click(
188
- fn=start_episode,
189
- inputs=difficulty_select,
190
- outputs=[doc_display, doc_info, category_radio, result_display, gr.Textbox(visible=False)]
191
  )
192
- classify_btn.click(
193
- fn=classify_document,
194
- inputs=category_radio,
195
- outputs=[doc_display, doc_info, result_display, category_radio]
196
  )
197
- auto_btn.click(
198
- fn=auto_classify,
199
- outputs=[doc_display, doc_info, result_display, category_radio]
200
- )
201
-
202
- # Tab 2: Baseline Evaluation
203
- with gr.TabItem("๐Ÿ“Š Baseline Evaluation"):
204
- gr.Markdown("### Run the keyword-based baseline agent and see scores")
205
- with gr.Row():
206
- eval_difficulty = gr.Radio(
207
- choices=["easy", "medium", "hard"],
208
- value="easy",
209
- label="Select Difficulty"
210
- )
211
- eval_btn = gr.Button("๐Ÿš€ Run Evaluation", variant="primary")
212
-
213
- eval_output = gr.Markdown("Click 'Run Evaluation' to start...")
214
- eval_btn.click(fn=run_baseline_eval, inputs=eval_difficulty, outputs=eval_output)
215
-
216
- # Tab 3: Environment Info
217
- with gr.TabItem("๐Ÿ“– Environment Info"):
218
- gr.Markdown("""
219
- ## About This Environment
220
-
221
- ### Task: Document Classification & Routing
222
- An agent receives customer support tickets/documents and must classify them into the correct department category.
223
-
224
- ### Difficulty Levels
225
- | Level | Categories | Documents | Time Limit |
226
- |-------|-----------|-----------|------------|
227
- | Easy | 5 | 100 | None |
228
- | Medium| 10 | 500 | 2 seconds |
229
- | Hard | 22 | 1000 | 1 second |
230
-
231
- ### Reward Structure
232
- - โœ… Correct classification: **+1.0**
233
- - โŒ Wrong classification: **-0.5**
234
- - โšก Speed bonus (fast response): **+0.1 to +0.25**
235
-
236
- ### API Usage
237
- ```python
238
- from environment import DocumentClassificationEnv
239
-
240
- env = DocumentClassificationEnv(task_difficulty="hard", seed=42)
241
- obs, info = env.reset()
242
-
243
- while True:
244
- action = your_agent(obs) # 0 to N-1
245
- obs, reward, done, truncated, info = env.step(action)
246
- if done:
247
- print(info["episode_summary"])
248
- break
249
- ```
250
-
251
- ### Baseline Scores (keyword agent)
252
- - **EASY:** 0.77
253
- - **MEDIUM:** 0.89
254
- - **HARD:** 0.165
255
-
256
- ### Categories (Hard Mode โ€” 22 total)
257
- General, Billing, Billing-Dispute, Billing-Refund, Support, Support-Urgent, Support-Normal,
258
- Technical, Technical-Bug, Technical-Feature, HR, HR-Payroll, HR-Benefits, HR-Complaint,
259
- Legal, Legal-Contract, Legal-Compliance, Executive, Executive-Strategic, Finance, Marketing, Operations
260
- """)
261
-
262
- return demo
263
-
264
 
265
  if __name__ == "__main__":
266
- demo = create_interface()
 
267
  demo.launch(server_name="0.0.0.0", server_port=7860)
 
 
 
 
 
 
1
  import gradio as gr
2
+ import threading
3
+ import os
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
+ difficulty = data.get("difficulty", "easy")
33
+ seed = data.get("seed", 42)
34
+ env = DocumentClassificationEnv(task_difficulty=difficulty, seed=seed)
35
+ obs, _ = env.reset(seed=seed)
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, debug=False)
81
+
82
+ env_state = {"env": None, "difficulty": "easy"}
83
+
84
+ def create_env(difficulty):
85
+ e = DocumentClassificationEnv(task_difficulty=difficulty, seed=42)
86
+ obs, _ = e.reset()
87
+ env_state["env"] = e
88
+ env_state["difficulty"] = difficulty
89
+ model = models[difficulty]
90
+ cats = CATEGORIES[difficulty]
91
+ proba = model.predict_proba([obs["content"]])[0]
92
+ top3 = sorted(enumerate(proba), key=lambda x: -x[1])[:3]
93
+ explain = "\n".join([f" {cats[i] if i < len(cats) else f'Cat_{i}'}: {p*100:.1f}%" for i,p in top3])
94
+ pred_idx = int(model.predict([obs["content"]])[0])
95
+ pred_cat = cats[pred_idx] if pred_idx < len(cats) else f"Cat_{pred_idx}"
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
+ acc = info.get('episode_accuracy', 0)
109
+ result += f"\n๐Ÿ Episode done! Final Accuracy: {acc:.2%}"
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", "medium", "hard"]:
126
+ score, t = run_task(d)
127
+ rows.append([d.upper(), f"{score:.4f}", f"{score*100:.1f}%", f"{t:.1f}s"])
128
+ return rows
129
+
130
+ def process_ticket_ui(ticket_text, difficulty):
131
+ if not ticket_text.strip():
132
+ return "โŒ Please enter ticket text!", "", "", "", "", ""
133
+ result = agent.process_ticket(ticket_text, difficulty=difficulty)
134
+ category_out = f"๐Ÿท๏ธ {result['category']} (confidence: {result['confidence']*100:.1f}%)"
135
+ priority_out = f"๐Ÿšจ {result['priority'].upper()}"
136
+ dept_out = f"๐Ÿข {result['department']}\n๐Ÿ“ง {result['email']}\nโฑ๏ธ SLA: {result['sla']}"
137
+ top3_out = "\n".join([f" {c}: {p*100:.1f}%" for c,p in result['top3']])
138
+ ref_out = f"๐ŸŽซ {result['ref_id']} | โฐ {result['timestamp'][:19]}"
139
+ reply_out = result['reply']
140
+ return category_out, priority_out, dept_out, top3_out, ref_out, reply_out
141
+
142
  SAMPLE_TICKETS = {
143
+ "Billing complaint": "My invoice shows an incorrect amount. I was charged $150 but should have been charged $75. Please review and correct this immediately.",
144
+ "Bug report": "The application crashes whenever I try to upload a file larger than 10MB. This is a critical issue affecting my workflow.",
145
+ "HR query": "I have a question about the company's maternity leave policy. How many weeks of paid leave am I entitled to?",
146
+ "Chat message": "Hey, I can't log into my account. The password reset link isn't working either. Please help ASAP!",
147
+ "Email ticket": "Dear Support, I would like to inquire about upgrading my current subscription plan to include additional users.",
 
148
  }
149
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
150
  def load_sample(sample_name):
151
  return SAMPLE_TICKETS.get(sample_name, "")
152
 
153
+ with gr.Blocks(title="Document Classification OpenEnv") as demo:
154
+ gr.Markdown("# ๐Ÿ“„ Document Classification OpenEnv")
155
+ gr.Markdown("Real-world customer support ticket routing environment for RL agent training.")
156
+
157
+ with gr.Tab("๐Ÿค– Agent Demo"):
158
+ gr.Markdown("### Full Automation โ€” ML Classification + Rule-based Priority + Auto-Reply")
159
+ with gr.Row():
160
+ with gr.Column(scale=2):
161
+ sample_dd = gr.Dropdown(
162
+ choices=list(SAMPLE_TICKETS.keys()),
163
+ label="๐Ÿ“‹ Load Sample Ticket",
164
+ value=None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
165
  )
166
+ ticket_input = gr.Textbox(
167
+ label="โœ๏ธ Ticket / Message Text",
168
+ lines=6,
169
+ placeholder="Paste email, chat message, bug report, HR query..."
170
  )
171
+ diff_agent = gr.Radio(["easy","medium","hard"], value="medium", label="Model Difficulty")
172
+ btn_process = gr.Button("๐Ÿš€ Process Ticket", variant="primary")
173
+ with gr.Column(scale=2):
174
+ ref_out_box = gr.Textbox(label="๐ŸŽซ Reference ID & Timestamp")
175
+ cat_out_box = gr.Textbox(label="๐Ÿท๏ธ Category & Confidence")
176
+ pri_out_box = gr.Textbox(label="๐Ÿšจ Priority")
177
+ dept_out_box = gr.Textbox(label="๐Ÿข Routed To", lines=3)
178
+ top3_out_box = gr.Textbox(label="๐Ÿ“Š Top 3 Predictions", lines=3)
179
+ reply_out_box = gr.Textbox(label="๐Ÿ“ง Auto-Generated Reply", lines=12)
180
+
181
+ sample_dd.change(load_sample, inputs=sample_dd, outputs=ticket_input)
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", variant="primary")
191
+ status = gr.Textbox(label="Status")
192
+ with gr.Row():
193
+ doc_content = gr.Textbox(label="๐Ÿ“ Document Content", lines=6)
194
+ explain_box = gr.Textbox(label="๐Ÿง  Explainability โ€” Model Reasoning", lines=6)
195
+ category = gr.Number(label="Category Index (easy: 0-4, medium: 0-9, hard: 0-21)", value=0)
196
+ btn_classify = gr.Button("Classify Document")
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
+ with gr.Tab("๐Ÿ“Š Baseline Evaluation"):
203
+ gr.Markdown("TF-IDF + Logistic Regression baseline โ€” models pre-trained at startup for instant results.")
204
+ btn_eval = gr.Button("Run All Tasks", variant="primary")
205
+ score_table = gr.Dataframe(headers=["Task","Score","Accuracy","Time"], label="Results")
206
+ btn_eval.click(run_baseline_all, outputs=score_table)
207
+
208
+ with gr.Tab("๐Ÿ“‹ Environment Info"):
209
+ gr.Markdown("""
210
+ ## Environment Design
211
+ **Task**: Classify customer support documents into correct departments.
212
+
213
+ | Difficulty | Categories | Episodes | Reward |
214
+ |------------|-----------|----------|--------|
215
+ | Easy | 5 | 20 | +1.0 correct, -0.4 wrong |
216
+ | Medium | 10 | 30 | +1.0 correct, -0.4 wrong |
217
+ | Hard | 22 | 50 | +1.0 correct, -0.4 wrong |
218
+
219
+ ## Agent Pipeline
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
+ t = threading.Thread(target=run_flask, daemon=True)
231
+ t.start()
232
  demo.launch(server_name="0.0.0.0", server_port=7860)