Spaces:
Sleeping
Sleeping
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,112 +0,0 @@
|
|
| 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
|
| 22 |
-
# -----------------------------
|
| 23 |
-
INTENT_LABELS = {
|
| 24 |
-
"chat": [
|
| 25 |
-
"casual conversation",
|
| 26 |
-
"small talk",
|
| 27 |
-
"just chatting"
|
| 28 |
-
],
|
| 29 |
-
"search": [
|
| 30 |
-
"find information",
|
| 31 |
-
"look up facts",
|
| 32 |
-
"search the web"
|
| 33 |
-
],
|
| 34 |
-
"image_generation": [
|
| 35 |
-
"generate an image",
|
| 36 |
-
"create artwork",
|
| 37 |
-
"draw a picture"
|
| 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)
|
| 67 |
-
for intent, phrases in INTENT_LABELS.items()
|
| 68 |
-
}
|
| 69 |
-
|
| 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 |
-
|
| 86 |
-
return {
|
| 87 |
-
"text": text,
|
| 88 |
-
"top_intent": top_intent,
|
| 89 |
-
"scores": scores
|
| 90 |
-
}
|
| 91 |
-
|
| 92 |
-
# -----------------------------
|
| 93 |
-
# Gradio UI
|
| 94 |
-
# -----------------------------
|
| 95 |
-
demo = gr.Interface(
|
| 96 |
-
fn=classify,
|
| 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 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|