Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import torch | |
| from sentence_transformers import SentenceTransformer, util | |
| # ----------------------------- | |
| # Load model (FAST) | |
| # ----------------------------- | |
| model = SentenceTransformer("all-MiniLM-L6-v2") | |
| # ----------------------------- | |
| # Intent anchors | |
| # ----------------------------- | |
| INTENT_LABELS = { | |
| "chat": [ | |
| "casual conversation", | |
| "small talk", | |
| "talk to me", | |
| "just chatting" | |
| ], | |
| "search": [ | |
| "find information", | |
| "look up facts", | |
| "search the web" | |
| ], | |
| "image_generation": [ | |
| "generate an image", | |
| "create artwork", | |
| "draw a picture" | |
| ], | |
| "code": [ | |
| "write code", | |
| "debug program", | |
| "programming help" | |
| ], | |
| "research": [ | |
| "deep research", | |
| "academic analysis", | |
| "investigate topic" | |
| ], | |
| "study": [ | |
| "help me learn", | |
| "teach me something", | |
| "study help" | |
| ], | |
| "project": [ | |
| "build a project", | |
| "create an application", | |
| "long term development" | |
| ], | |
| "action": [ | |
| "set a reminder", | |
| "schedule something", | |
| "perform an action" | |
| ] | |
| } | |
| # ----------------------------- | |
| # Precompute anchor embeddings | |
| # ----------------------------- | |
| anchor_embeddings = { | |
| intent: model.encode(phrases, convert_to_tensor=True) | |
| for intent, phrases in INTENT_LABELS.items() | |
| } | |
| # ----------------------------- | |
| # Classifier | |
| # ----------------------------- | |
| def classify(text: str): | |
| if not text.strip(): | |
| return {"error": "Empty input"} | |
| query_emb = model.encode(text, convert_to_tensor=True) | |
| scores = {} | |
| for intent, emb in anchor_embeddings.items(): | |
| score = util.cos_sim(query_emb, emb).max().item() | |
| scores[intent] = score | |
| top_intent = max(scores, key=scores.get) | |
| return { | |
| "text": text, | |
| "top_intent": top_intent, | |
| "scores": scores | |
| } | |
| # ----------------------------- | |
| # Gradio UI | |
| # ----------------------------- | |
| demo = gr.Interface( | |
| fn=classify, | |
| inputs=gr.Textbox(label="User Input"), | |
| outputs=gr.JSON(), | |
| title="⚡ Ultra-Fast Intent Router", | |
| description="Sub-second semantic routing using MiniLM (no training)" | |
| ) | |
| app = demo |