Aadhavan12344 commited on
Commit
f9c3a44
·
verified ·
1 Parent(s): 80d282f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -0
app.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import gradio as gr
3
+ from sentence_transformers import SentenceTransformer
4
+ from sklearn.metrics.pairwise import cosine_similarity
5
+
6
+ # -----------------------
7
+ # LOAD MODEL (CPU-safe)
8
+ # -----------------------
9
+ model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
10
+
11
+ # -----------------------
12
+ # INTENT DEFINITIONS
13
+ # -----------------------
14
+ INTENTS = {
15
+ "chat": "casual conversation, greetings, talking",
16
+ "search": "asking for information or facts",
17
+ "image_generation": "requesting image creation or visual generation",
18
+ "code": "programming, software development, debugging",
19
+ "research": "deep technical or academic research",
20
+ "study": "learning, studying, explanations, tutorials",
21
+ "project": "building or planning a project",
22
+ "action": "asking the system to do something"
23
+ }
24
+
25
+ intent_names = list(INTENTS.keys())
26
+ intent_embeddings = model.encode(
27
+ list(INTENTS.values()),
28
+ normalize_embeddings=True
29
+ )
30
+
31
+ # -----------------------
32
+ # ROUTER
33
+ # -----------------------
34
+ def classify(text: str):
35
+ text = text.strip()
36
+ if not text:
37
+ return {}
38
+
39
+ text_emb = model.encode([text], normalize_embeddings=True)
40
+ sims = cosine_similarity(text_emb, intent_embeddings)[0]
41
+
42
+ scores = {k: float(v) for k, v in zip(intent_names, sims)}
43
+ top_intent = max(scores, key=scores.get)
44
+
45
+ return {
46
+ "text": text,
47
+ "top_intent": top_intent,
48
+ "scores": scores
49
+ }
50
+
51
+ # -----------------------
52
+ # GRADIO APP (THIS IS IMPORTANT)
53
+ # -----------------------
54
+ app = gr.Interface(
55
+ fn=classify,
56
+ inputs=gr.Textbox(label="User Input"),
57
+ outputs=gr.JSON(label="Classification Result"),
58
+ title="🧠 Zero-Shot Intent Router",
59
+ description="Single-pass intent routing for prompt + MPC selection"
60
+ )