Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from sentence_transformers import SentenceTransformer, util
|
| 4 |
+
|
| 5 |
+
# -----------------------------
|
| 6 |
+
# Load model (FAST)
|
| 7 |
+
# -----------------------------
|
| 8 |
+
model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 9 |
+
|
| 10 |
+
# -----------------------------
|
| 11 |
+
# Intent anchors
|
| 12 |
+
# -----------------------------
|
| 13 |
+
INTENT_LABELS = {
|
| 14 |
+
"chat": [
|
| 15 |
+
"casual conversation",
|
| 16 |
+
"small talk",
|
| 17 |
+
"talk to me",
|
| 18 |
+
"just chatting"
|
| 19 |
+
],
|
| 20 |
+
"search": [
|
| 21 |
+
"find information",
|
| 22 |
+
"look up facts",
|
| 23 |
+
"search the web"
|
| 24 |
+
],
|
| 25 |
+
"image_generation": [
|
| 26 |
+
"generate an image",
|
| 27 |
+
"create artwork",
|
| 28 |
+
"draw a picture"
|
| 29 |
+
],
|
| 30 |
+
"code": [
|
| 31 |
+
"write code",
|
| 32 |
+
"debug program",
|
| 33 |
+
"programming help"
|
| 34 |
+
],
|
| 35 |
+
"research": [
|
| 36 |
+
"deep research",
|
| 37 |
+
"academic analysis",
|
| 38 |
+
"investigate topic"
|
| 39 |
+
],
|
| 40 |
+
"study": [
|
| 41 |
+
"help me learn",
|
| 42 |
+
"teach me something",
|
| 43 |
+
"study help"
|
| 44 |
+
],
|
| 45 |
+
"project": [
|
| 46 |
+
"build a project",
|
| 47 |
+
"create an application",
|
| 48 |
+
"long term development"
|
| 49 |
+
],
|
| 50 |
+
"action": [
|
| 51 |
+
"set a reminder",
|
| 52 |
+
"schedule something",
|
| 53 |
+
"perform an action"
|
| 54 |
+
]
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
# -----------------------------
|
| 58 |
+
# Precompute anchor embeddings
|
| 59 |
+
# -----------------------------
|
| 60 |
+
anchor_embeddings = {
|
| 61 |
+
intent: model.encode(phrases, convert_to_tensor=True)
|
| 62 |
+
for intent, phrases in INTENT_LABELS.items()
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
# -----------------------------
|
| 66 |
+
# Classifier
|
| 67 |
+
# -----------------------------
|
| 68 |
+
def classify(text: str):
|
| 69 |
+
if not text.strip():
|
| 70 |
+
return {"error": "Empty input"}
|
| 71 |
+
|
| 72 |
+
query_emb = model.encode(text, convert_to_tensor=True)
|
| 73 |
+
|
| 74 |
+
scores = {}
|
| 75 |
+
for intent, emb in anchor_embeddings.items():
|
| 76 |
+
score = util.cos_sim(query_emb, emb).max().item()
|
| 77 |
+
scores[intent] = score
|
| 78 |
+
|
| 79 |
+
top_intent = max(scores, key=scores.get)
|
| 80 |
+
|
| 81 |
+
return {
|
| 82 |
+
"text": text,
|
| 83 |
+
"top_intent": top_intent,
|
| 84 |
+
"scores": scores
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
# -----------------------------
|
| 88 |
+
# Gradio UI
|
| 89 |
+
# -----------------------------
|
| 90 |
+
demo = gr.Interface(
|
| 91 |
+
fn=classify,
|
| 92 |
+
inputs=gr.Textbox(label="User Input"),
|
| 93 |
+
outputs=gr.JSON(),
|
| 94 |
+
title="⚡ Ultra-Fast Intent Router",
|
| 95 |
+
description="Sub-second semantic routing using MiniLM (no training)"
|
| 96 |
+
)
|
| 97 |
+
|
| 98 |
+
app = demo
|