Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,21 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
from sentence_transformers import SentenceTransformer, util
|
| 4 |
|
| 5 |
# -----------------------------
|
| 6 |
-
#
|
| 7 |
# -----------------------------
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# -----------------------------
|
| 11 |
# Intent anchors
|
|
@@ -14,7 +24,6 @@ INTENT_LABELS = {
|
|
| 14 |
"chat": [
|
| 15 |
"casual conversation",
|
| 16 |
"small talk",
|
| 17 |
-
"talk to me",
|
| 18 |
"just chatting"
|
| 19 |
],
|
| 20 |
"search": [
|
|
@@ -29,33 +38,29 @@ INTENT_LABELS = {
|
|
| 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
|
| 59 |
# -----------------------------
|
| 60 |
anchor_embeddings = {
|
| 61 |
intent: model.encode(phrases, convert_to_tensor=True)
|
|
@@ -65,16 +70,16 @@ anchor_embeddings = {
|
|
| 65 |
# -----------------------------
|
| 66 |
# Classifier
|
| 67 |
# -----------------------------
|
| 68 |
-
def classify(text
|
| 69 |
if not text.strip():
|
| 70 |
-
return {
|
| 71 |
|
| 72 |
query_emb = model.encode(text, convert_to_tensor=True)
|
| 73 |
|
| 74 |
-
scores = {
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
|
| 79 |
top_intent = max(scores, key=scores.get)
|
| 80 |
|
|
@@ -92,7 +97,16 @@ demo = gr.Interface(
|
|
| 92 |
inputs=gr.Textbox(label="User Input"),
|
| 93 |
outputs=gr.JSON(),
|
| 94 |
title="⚡ Ultra-Fast Intent Router",
|
| 95 |
-
description="
|
| 96 |
)
|
| 97 |
|
| 98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
| 4 |
from sentence_transformers import SentenceTransformer, util
|
| 5 |
|
| 6 |
# -----------------------------
|
| 7 |
+
# Hard CPU enforcement (HF-safe)
|
| 8 |
# -----------------------------
|
| 9 |
+
os.environ["CUDA_VISIBLE_DEVICES"] = ""
|
| 10 |
+
torch.set_num_threads(1)
|
| 11 |
+
|
| 12 |
+
# -----------------------------
|
| 13 |
+
# Load model (FAST & SAFE)
|
| 14 |
+
# -----------------------------
|
| 15 |
+
model = SentenceTransformer(
|
| 16 |
+
"sentence-transformers/all-MiniLM-L6-v2",
|
| 17 |
+
device="cpu"
|
| 18 |
+
)
|
| 19 |
|
| 20 |
# -----------------------------
|
| 21 |
# Intent anchors
|
|
|
|
| 24 |
"chat": [
|
| 25 |
"casual conversation",
|
| 26 |
"small talk",
|
|
|
|
| 27 |
"just chatting"
|
| 28 |
],
|
| 29 |
"search": [
|
|
|
|
| 38 |
],
|
| 39 |
"code": [
|
| 40 |
"write code",
|
| 41 |
+
"debug a program",
|
| 42 |
"programming help"
|
| 43 |
],
|
| 44 |
"research": [
|
| 45 |
"deep research",
|
| 46 |
+
"academic analysis"
|
|
|
|
| 47 |
],
|
| 48 |
"study": [
|
| 49 |
"help me learn",
|
|
|
|
| 50 |
"study help"
|
| 51 |
],
|
| 52 |
"project": [
|
| 53 |
"build a project",
|
| 54 |
+
"create an application"
|
|
|
|
| 55 |
],
|
| 56 |
"action": [
|
| 57 |
"set a reminder",
|
|
|
|
| 58 |
"perform an action"
|
| 59 |
]
|
| 60 |
}
|
| 61 |
|
| 62 |
# -----------------------------
|
| 63 |
+
# Precompute embeddings (ONCE)
|
| 64 |
# -----------------------------
|
| 65 |
anchor_embeddings = {
|
| 66 |
intent: model.encode(phrases, convert_to_tensor=True)
|
|
|
|
| 70 |
# -----------------------------
|
| 71 |
# Classifier
|
| 72 |
# -----------------------------
|
| 73 |
+
def classify(text):
|
| 74 |
if not text.strip():
|
| 75 |
+
return {}
|
| 76 |
|
| 77 |
query_emb = model.encode(text, convert_to_tensor=True)
|
| 78 |
|
| 79 |
+
scores = {
|
| 80 |
+
intent: util.cos_sim(query_emb, emb).max().item()
|
| 81 |
+
for intent, emb in anchor_embeddings.items()
|
| 82 |
+
}
|
| 83 |
|
| 84 |
top_intent = max(scores, key=scores.get)
|
| 85 |
|
|
|
|
| 97 |
inputs=gr.Textbox(label="User Input"),
|
| 98 |
outputs=gr.JSON(),
|
| 99 |
title="⚡ Ultra-Fast Intent Router",
|
| 100 |
+
description="MiniLM semantic router (no training, sub-second)"
|
| 101 |
)
|
| 102 |
|
| 103 |
+
# -----------------------------
|
| 104 |
+
# REQUIRED FOR HF SPACES
|
| 105 |
+
# -----------------------------
|
| 106 |
+
if __name__ == "__main__":
|
| 107 |
+
demo.launch(
|
| 108 |
+
server_name="0.0.0.0",
|
| 109 |
+
server_port=7860,
|
| 110 |
+
share=True,
|
| 111 |
+
show_error=True
|
| 112 |
+
)
|