|
|
import gradio as gr |
|
|
import pickle, random |
|
|
from sentence_transformers import SentenceTransformer |
|
|
|
|
|
|
|
|
with open("intent_model.pkl", "rb") as f: |
|
|
data = pickle.load(f) |
|
|
|
|
|
clf = data["classifier"] |
|
|
id2label = data["id2label"] |
|
|
embedder = SentenceTransformer(data["embed_model"]) |
|
|
intents_meta = data["intents_meta"] |
|
|
|
|
|
def predict_intent(user_input): |
|
|
"""Predict intent and return formatted response.""" |
|
|
if not user_input.strip(): |
|
|
return "Please enter a command." |
|
|
emb = embedder.encode([user_input]) |
|
|
pred = clf.predict(emb)[0] |
|
|
intent = id2label[pred] |
|
|
meta = intents_meta[intent] |
|
|
response = random.choice(meta["responses"]) |
|
|
action = meta["action"] |
|
|
return f"🧠 Intent: {intent}\n💬 Response: {response}\n⚙️ Action: {action}" |
|
|
|
|
|
|
|
|
demo = gr.Interface( |
|
|
fn=predict_intent, |
|
|
inputs=gr.Textbox(label="Enter your command", placeholder="e.g. restart pc, open gmail"), |
|
|
outputs=gr.Textbox(label="Jarvis Response"), |
|
|
title="🧠 Jarvis Intent Classifier", |
|
|
description="Lightweight intent classification model that detects system commands and returns appropriate responses & actions." |
|
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
|
demo.launch() |
|
|
|