Commit
Β·
adca896
1
Parent(s):
02830d8
Initial commit of Arch Router Simulator
Browse files- README.md +4 -4
- app.py +208 -0
- requirements.txt +6 -0
README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
| 1 |
---
|
| 2 |
title: ArchRouter Simulator
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 5.35.0
|
| 8 |
app_file: app.py
|
|
@@ -10,4 +10,4 @@ pinned: false
|
|
| 10 |
short_description: Demo working simulation of Arch Router
|
| 11 |
---
|
| 12 |
|
| 13 |
-
|
|
|
|
| 1 |
---
|
| 2 |
title: ArchRouter Simulator
|
| 3 |
+
emoji: π§
|
| 4 |
+
colorFrom: yellow
|
| 5 |
+
colorTo: blue
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 5.35.0
|
| 8 |
app_file: app.py
|
|
|
|
| 10 |
short_description: Demo working simulation of Arch Router
|
| 11 |
---
|
| 12 |
|
| 13 |
+
This is a demo for Arch Router
|
app.py
ADDED
|
@@ -0,0 +1,208 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import time
|
| 3 |
+
import ast
|
| 4 |
+
import torch
|
| 5 |
+
import gradio as gr
|
| 6 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 7 |
+
from gradio_consilium_roundtable import consilium_roundtable
|
| 8 |
+
|
| 9 |
+
# === Constants ===
|
| 10 |
+
MODEL_NAME = "katanemo/Arch-Router-1.5B"
|
| 11 |
+
ARCH_ROUTER = "Arch Router"
|
| 12 |
+
WAIT_DEPARTMENT = 5
|
| 13 |
+
WAIT_SYSTEM = 5
|
| 14 |
+
|
| 15 |
+
# === Load model/tokenizer ===
|
| 16 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 17 |
+
MODEL_NAME, device_map="auto", torch_dtype="auto", trust_remote_code=True
|
| 18 |
+
)
|
| 19 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 20 |
+
|
| 21 |
+
# === Route Definitions ===
|
| 22 |
+
route_config = [
|
| 23 |
+
{"name": "code_generation", "description": "Generating code based on prompts"},
|
| 24 |
+
{"name": "bug_fixing", "description": "Fixing errors or bugs in code"},
|
| 25 |
+
{"name": "performance_optimization", "description": "Improving code performance"},
|
| 26 |
+
{"name": "api_help", "description": "Assisting with APIs and libraries"},
|
| 27 |
+
{"name": "programming", "description": "General programming Q&A"},
|
| 28 |
+
{"name": "legal", "description": "Legal"},
|
| 29 |
+
{"name": "healthcare", "description": "Healthcare and medical related"},
|
| 30 |
+
]
|
| 31 |
+
|
| 32 |
+
departments = {
|
| 33 |
+
"code_generation": ("π»", "Code Generation"),
|
| 34 |
+
"bug_fixing": ("π", "Bug Fixing"),
|
| 35 |
+
"performance_optimization": ("β‘", "Performance Optimization"),
|
| 36 |
+
"api_help": ("π", "API Help"),
|
| 37 |
+
"programming": ("π", "Programming"),
|
| 38 |
+
"legal": ("βοΈ", "Legal"),
|
| 39 |
+
"healthcare": ("π©Ί", "Healthcare"),
|
| 40 |
+
"other": ("β", "Other / General Inquiry"),
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
# === Prompt Formatting ===
|
| 44 |
+
TASK_INSTRUCTION = """
|
| 45 |
+
You are a helpful assistant designed to find the best suited route. You are provided with route description within <routes></routes> XML tags:
|
| 46 |
+
<routes>
|
| 47 |
+
{routes}
|
| 48 |
+
</routes>
|
| 49 |
+
|
| 50 |
+
<conversation>
|
| 51 |
+
{conversation}
|
| 52 |
+
</conversation>
|
| 53 |
+
"""
|
| 54 |
+
|
| 55 |
+
FORMAT_PROMPT = """
|
| 56 |
+
Your task is to decide which route is best suit with user intent on the conversation in <conversation></conversation> XML tags. Follow the instruction:
|
| 57 |
+
1. If the latest intent from user is irrelevant or user intent is full filled, response with other route {"route": "other"}.
|
| 58 |
+
2. You must analyze the route descriptions and find the best match route for user latest intent.
|
| 59 |
+
3. You only response the name of the route that best matches the user's request, use the exact name in the <routes></routes>.
|
| 60 |
+
|
| 61 |
+
Based on your analysis, provide your response in the following JSON format:
|
| 62 |
+
{"route": "route_name"}
|
| 63 |
+
"""
|
| 64 |
+
|
| 65 |
+
def format_prompt(route_config, conversation):
|
| 66 |
+
return TASK_INSTRUCTION.format(
|
| 67 |
+
routes=json.dumps(route_config), conversation=json.dumps(conversation)
|
| 68 |
+
) + FORMAT_PROMPT
|
| 69 |
+
|
| 70 |
+
def parse_route(response_text):
|
| 71 |
+
try:
|
| 72 |
+
start = response_text.find("{")
|
| 73 |
+
end = response_text.rfind("}") + 1
|
| 74 |
+
return ast.literal_eval(response_text[start:end]).get("route", "other")
|
| 75 |
+
except Exception as e:
|
| 76 |
+
print("Parsing failed:", e)
|
| 77 |
+
return "other"
|
| 78 |
+
|
| 79 |
+
def init_state():
|
| 80 |
+
avatar_emojis = {
|
| 81 |
+
ARCH_ROUTER: "https://avatars.githubusercontent.com/u/112724757?s=200&v=4",
|
| 82 |
+
"code_generation": "https://raw.githubusercontent.com/twitter/twemoji/master/assets/72x72/1f4bb.png",
|
| 83 |
+
"bug_fixing": "https://raw.githubusercontent.com/twitter/twemoji/master/assets/72x72/1f41e.png",
|
| 84 |
+
"performance_optimization": "https://raw.githubusercontent.com/twitter/twemoji/master/assets/72x72/26a1.png",
|
| 85 |
+
"api_help": "https://raw.githubusercontent.com/twitter/twemoji/master/assets/72x72/1f50c.png",
|
| 86 |
+
"programming": "https://raw.githubusercontent.com/twitter/twemoji/master/assets/72x72/1f4da.png",
|
| 87 |
+
"legal": "https://raw.githubusercontent.com/twitter/twemoji/master/assets/72x72/2696.png",
|
| 88 |
+
"healthcare": "https://raw.githubusercontent.com/twitter/twemoji/master/assets/72x72/1fa7a.png",
|
| 89 |
+
"other": "https://raw.githubusercontent.com/twitter/twemoji/master/assets/72x72/2753.png",
|
| 90 |
+
}
|
| 91 |
+
return {
|
| 92 |
+
"messages": [],
|
| 93 |
+
"participants": [ARCH_ROUTER] + list(departments.keys()),
|
| 94 |
+
"currentSpeaker": None,
|
| 95 |
+
"thinking": [],
|
| 96 |
+
"showBubbles": [ARCH_ROUTER],
|
| 97 |
+
"avatarImages": avatar_emojis,
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
def route_and_visualize(user_input_text, rt_state, chat_history):
|
| 101 |
+
chat_history = chat_history or []
|
| 102 |
+
rt_state = rt_state or {"messages": []}
|
| 103 |
+
chat_history.append(("User", user_input_text))
|
| 104 |
+
|
| 105 |
+
# Step 1: Disable input and show route detection
|
| 106 |
+
rt_state["messages"] = [{"speaker": ARCH_ROUTER, "text": "π Identifying route, please wait..."}]
|
| 107 |
+
yield rt_state, chat_history, rt_state, gr.update(interactive=False)
|
| 108 |
+
|
| 109 |
+
# Step 2: Prepare prompt and get route
|
| 110 |
+
conversation = [{"role": "user", "content": user_input_text}]
|
| 111 |
+
route_prompt = format_prompt(route_config, conversation)
|
| 112 |
+
input_ids = tokenizer.apply_chat_template(
|
| 113 |
+
[{"role": "user", "content": route_prompt}],
|
| 114 |
+
add_generation_prompt=True,
|
| 115 |
+
return_tensors="pt"
|
| 116 |
+
).to(model.device)
|
| 117 |
+
|
| 118 |
+
with torch.no_grad():
|
| 119 |
+
output = model.generate(input_ids=input_ids, max_new_tokens=512)
|
| 120 |
+
|
| 121 |
+
prompt_len = input_ids.shape[1]
|
| 122 |
+
response = tokenizer.decode(output[0][prompt_len:], skip_special_tokens=True).strip()
|
| 123 |
+
print("MODEL RAW:", response)
|
| 124 |
+
route = parse_route(response)
|
| 125 |
+
|
| 126 |
+
emoji, dept_name = departments.get(route, departments["other"])
|
| 127 |
+
|
| 128 |
+
# Step 3: Show route identified
|
| 129 |
+
rt_state["messages"][0] = {
|
| 130 |
+
"speaker": ARCH_ROUTER,
|
| 131 |
+
"text": f"π Identified department: **{dept_name}**. Forwarding task..."
|
| 132 |
+
}
|
| 133 |
+
chat_history.append((ARCH_ROUTER, f"π Identified department: {dept_name}. Forwarding task..."))
|
| 134 |
+
yield rt_state, chat_history, rt_state, gr.update(interactive=False)
|
| 135 |
+
|
| 136 |
+
# Step 4: Show processing
|
| 137 |
+
time.sleep(3)
|
| 138 |
+
rt_state["messages"].extend([
|
| 139 |
+
{"speaker": route, "text": f"{emoji} {dept_name} simulation is processing your request in {WAIT_DEPARTMENT} secs..."},
|
| 140 |
+
{"speaker": ARCH_ROUTER, "text": "β³ Waiting for department to respond..."}
|
| 141 |
+
])
|
| 142 |
+
rt_state["showBubbles"] = [ARCH_ROUTER, route]
|
| 143 |
+
yield rt_state, chat_history, rt_state, gr.update(interactive=False)
|
| 144 |
+
|
| 145 |
+
# Step 5: Simulate delay and complete
|
| 146 |
+
time.sleep(WAIT_DEPARTMENT)
|
| 147 |
+
rt_state["messages"][-2]["text"] = f"β
{dept_name} completed the task."
|
| 148 |
+
rt_state["messages"][-1]["text"] = f"β
{dept_name} department has completed the task."
|
| 149 |
+
chat_history.append((ARCH_ROUTER, f"β
{dept_name} department completed the task."))
|
| 150 |
+
yield rt_state, chat_history, rt_state, gr.update(interactive=False)
|
| 151 |
+
|
| 152 |
+
# Step 6: Reset visible bubbles
|
| 153 |
+
rt_state["showBubbles"] = [ARCH_ROUTER]
|
| 154 |
+
yield rt_state, chat_history, rt_state, gr.update(interactive=False)
|
| 155 |
+
|
| 156 |
+
# Step 7: System ready
|
| 157 |
+
time.sleep(WAIT_SYSTEM)
|
| 158 |
+
rt_state["messages"].append({"speaker": ARCH_ROUTER, "text": "Arch Router is ready to discuss."})
|
| 159 |
+
yield rt_state, chat_history, rt_state, gr.update(interactive=True)
|
| 160 |
+
|
| 161 |
+
# === Gradio UI ===
|
| 162 |
+
with gr.Blocks(theme=gr.themes.Ocean()) as demo:
|
| 163 |
+
gr.Markdown("## π§ Arch Router Simulation: Smart Department Dispatcher")
|
| 164 |
+
|
| 165 |
+
gr.Markdown(
|
| 166 |
+
"""
|
| 167 |
+
<p>This is a demo simulation of <a href="https://huggingface.co/katanemo/Arch-Router-1.5B" target="_blank">katanemo/Arch-Router-1.5B</a></p>
|
| 168 |
+
"""
|
| 169 |
+
)
|
| 170 |
+
|
| 171 |
+
with gr.Row():
|
| 172 |
+
with gr.Column(scale=3):
|
| 173 |
+
rt_state = gr.State(init_state())
|
| 174 |
+
chat_state = gr.State([])
|
| 175 |
+
roundtable = consilium_roundtable(value=init_state())
|
| 176 |
+
|
| 177 |
+
with gr.Column(scale=2):
|
| 178 |
+
chatbot = gr.Chatbot(label="Chat History", max_height=300)
|
| 179 |
+
textbox = gr.Textbox(placeholder="Describe your issue...", label="Ask Arch Router")
|
| 180 |
+
submit_btn = gr.Button("Submit")
|
| 181 |
+
|
| 182 |
+
example_inputs = [
|
| 183 |
+
"How do I optimize this loop in Python?",
|
| 184 |
+
"Generate a function to sort an array in python",
|
| 185 |
+
"Help me anonymize patient health records before storing them",
|
| 186 |
+
"I'm getting a TypeError in following code",
|
| 187 |
+
"Do I need to include attribution for MIT-licensed software?",
|
| 188 |
+
"How do I connect to external API from this code?"
|
| 189 |
+
]
|
| 190 |
+
|
| 191 |
+
# Trigger submission via Enter or Button
|
| 192 |
+
for trigger in (textbox.submit, submit_btn.click):
|
| 193 |
+
trigger(
|
| 194 |
+
route_and_visualize,
|
| 195 |
+
inputs=[textbox, rt_state, chat_state],
|
| 196 |
+
outputs=[roundtable, chatbot, rt_state, textbox],
|
| 197 |
+
concurrency_limit=1
|
| 198 |
+
)
|
| 199 |
+
|
| 200 |
+
# Example block
|
| 201 |
+
gr.Examples(
|
| 202 |
+
examples=example_inputs,
|
| 203 |
+
inputs=textbox,
|
| 204 |
+
label="Try one of these examples"
|
| 205 |
+
)
|
| 206 |
+
|
| 207 |
+
if __name__ == "__main__":
|
| 208 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers>=4.30.0
|
| 2 |
+
torch>=1.13.0
|
| 3 |
+
gradio>=3.0
|
| 4 |
+
accelerate
|
| 5 |
+
sentencepiece
|
| 6 |
+
gradio_consilium_roundtable
|