Spaces:
Sleeping
Sleeping
Update agents/routing_agent.py
#24
by Muthuraja18 - opened
- agents/routing_agent.py +154 -15
agents/routing_agent.py
CHANGED
|
@@ -2,29 +2,168 @@ from prompts import ROUTER_PROMPT
|
|
| 2 |
|
| 3 |
|
| 4 |
class RouterAgent:
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
def __init__(self, llm):
|
| 7 |
self.llm = llm
|
| 8 |
|
| 9 |
def route(self, user_input: str) -> str:
|
| 10 |
-
"""
|
| 11 |
-
Returns one of:
|
| 12 |
-
- generate
|
| 13 |
-
- edit
|
| 14 |
-
- analyze
|
| 15 |
-
- ocr
|
| 16 |
-
- pdf
|
| 17 |
-
"""
|
| 18 |
|
| 19 |
-
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
return "generate"
|
| 29 |
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
|
| 4 |
class RouterAgent:
|
| 5 |
+
"""
|
| 6 |
+
Routes the user's request to the appropriate agent/tool.
|
| 7 |
+
"""
|
| 8 |
|
| 9 |
+
def __init__(self, llm=None):
|
| 10 |
self.llm = llm
|
| 11 |
|
| 12 |
def route(self, user_input: str) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
if not user_input:
|
| 15 |
+
return "chat"
|
| 16 |
|
| 17 |
+
prompt = user_input.lower().strip()
|
| 18 |
|
| 19 |
+
# -----------------------------
|
| 20 |
+
# ANALYZE IMAGE
|
| 21 |
+
# -----------------------------
|
| 22 |
+
analyze_keywords = [
|
| 23 |
+
"analyze",
|
| 24 |
+
"analyse",
|
| 25 |
+
"describe",
|
| 26 |
+
"explain",
|
| 27 |
+
"what is in",
|
| 28 |
+
"what's in",
|
| 29 |
+
"identify",
|
| 30 |
+
"detect",
|
| 31 |
+
"caption",
|
| 32 |
+
"inspect",
|
| 33 |
+
"look at",
|
| 34 |
+
"tell me about",
|
| 35 |
+
"analyze this image",
|
| 36 |
+
"describe this image"
|
| 37 |
+
]
|
| 38 |
|
| 39 |
+
# -----------------------------
|
| 40 |
+
# EDIT IMAGE
|
| 41 |
+
# -----------------------------
|
| 42 |
+
edit_keywords = [
|
| 43 |
+
"edit",
|
| 44 |
+
"change",
|
| 45 |
+
"modify",
|
| 46 |
+
"remove",
|
| 47 |
+
"replace",
|
| 48 |
+
"erase",
|
| 49 |
+
"add",
|
| 50 |
+
"swap",
|
| 51 |
+
"enhance",
|
| 52 |
+
"improve",
|
| 53 |
+
"background",
|
| 54 |
+
"blur",
|
| 55 |
+
"crop",
|
| 56 |
+
"resize",
|
| 57 |
+
"rotate",
|
| 58 |
+
"anime",
|
| 59 |
+
"cartoon",
|
| 60 |
+
"ghibli",
|
| 61 |
+
"pixar",
|
| 62 |
+
"watercolor",
|
| 63 |
+
"oil painting",
|
| 64 |
+
"black and white",
|
| 65 |
+
"make it",
|
| 66 |
+
"turn it into",
|
| 67 |
+
"convert"
|
| 68 |
+
]
|
| 69 |
|
| 70 |
+
# -----------------------------
|
| 71 |
+
# GENERATE IMAGE
|
| 72 |
+
# -----------------------------
|
| 73 |
+
generate_keywords = [
|
| 74 |
+
"generate",
|
| 75 |
+
"create",
|
| 76 |
+
"draw",
|
| 77 |
+
"design",
|
| 78 |
+
"imagine",
|
| 79 |
+
"make a",
|
| 80 |
+
"new image",
|
| 81 |
+
"text to image",
|
| 82 |
+
"logo",
|
| 83 |
+
"poster",
|
| 84 |
+
"banner"
|
| 85 |
+
]
|
| 86 |
+
|
| 87 |
+
# -----------------------------
|
| 88 |
+
# OCR
|
| 89 |
+
# -----------------------------
|
| 90 |
+
ocr_keywords = [
|
| 91 |
+
"ocr",
|
| 92 |
+
"extract text",
|
| 93 |
+
"read text",
|
| 94 |
+
"scan text",
|
| 95 |
+
"recognize text"
|
| 96 |
+
]
|
| 97 |
+
|
| 98 |
+
# -----------------------------
|
| 99 |
+
# PDF
|
| 100 |
+
# -----------------------------
|
| 101 |
+
pdf_keywords = [
|
| 102 |
+
"pdf",
|
| 103 |
+
"convert pdf",
|
| 104 |
+
"save pdf",
|
| 105 |
+
"export pdf"
|
| 106 |
+
]
|
| 107 |
+
|
| 108 |
+
# -----------------------------
|
| 109 |
+
# ROUTING
|
| 110 |
+
# -----------------------------
|
| 111 |
+
|
| 112 |
+
# Analyze
|
| 113 |
+
if any(word in prompt for word in analyze_keywords):
|
| 114 |
+
return "analyze"
|
| 115 |
+
|
| 116 |
+
# OCR
|
| 117 |
+
if any(word in prompt for word in ocr_keywords):
|
| 118 |
+
return "ocr"
|
| 119 |
+
|
| 120 |
+
# PDF
|
| 121 |
+
if any(word in prompt for word in pdf_keywords):
|
| 122 |
+
return "pdf"
|
| 123 |
+
|
| 124 |
+
# Edit
|
| 125 |
+
if any(word in prompt for word in edit_keywords):
|
| 126 |
+
return "edit"
|
| 127 |
+
|
| 128 |
+
# Generate
|
| 129 |
+
if any(word in prompt for word in generate_keywords):
|
| 130 |
return "generate"
|
| 131 |
|
| 132 |
+
# -----------------------------
|
| 133 |
+
# LLM Fallback (optional)
|
| 134 |
+
# -----------------------------
|
| 135 |
+
if self.llm:
|
| 136 |
+
try:
|
| 137 |
+
response = self.llm.invoke(f"""
|
| 138 |
+
You are an AI routing assistant.
|
| 139 |
+
|
| 140 |
+
Return ONLY ONE WORD from:
|
| 141 |
+
|
| 142 |
+
analyze
|
| 143 |
+
edit
|
| 144 |
+
generate
|
| 145 |
+
ocr
|
| 146 |
+
pdf
|
| 147 |
+
chat
|
| 148 |
+
|
| 149 |
+
User Request:
|
| 150 |
+
{user_input}
|
| 151 |
+
""")
|
| 152 |
+
|
| 153 |
+
task = response.content.strip().lower()
|
| 154 |
+
|
| 155 |
+
if task in [
|
| 156 |
+
"analyze",
|
| 157 |
+
"edit",
|
| 158 |
+
"generate",
|
| 159 |
+
"ocr",
|
| 160 |
+
"pdf",
|
| 161 |
+
"chat"
|
| 162 |
+
]:
|
| 163 |
+
return task
|
| 164 |
+
|
| 165 |
+
except Exception:
|
| 166 |
+
pass
|
| 167 |
+
|
| 168 |
+
# Default
|
| 169 |
+
return "chat"
|