File size: 2,009 Bytes
290e262
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import gradio as gr
from transformers import pipeline

# Path to your local model folder inside this Space
MODEL_PATH = "./intent_model"

# Load the text classification pipeline
intent_model = pipeline("text-classification", model=MODEL_PATH)

# Map model labels to human-readable intent names
LABEL_TO_INTENT = {
    "LABEL_0": "admission_process",
    "LABEL_1": "eligibility",
    "LABEL_2": "fees_info",
    "LABEL_3": "admission_dates",
    "LABEL_4": "user_info",
    "LABEL_5": "document_requirements",
    "LABEL_6": "evaluation_process",
    "LABEL_7": "contact_info",
    "LABEL_8": "scholarship_info",
    "LABEL_9": "technical_support",
    "LABEL_10": "academic_details",
    "LABEL_11": "campus_life",
}

def predict_intent(query: str):
    """
    Takes a user query string and returns:
    - Predicted intent name
    - Confidence (percentage string)
    """
    if not query.strip():
        return "Please enter a message", ""

    # Run the model
    result = intent_model(query)[0]  # e.g. {"label": "LABEL_1", "score": 0.98}
    label = result["label"]
    confidence = float(result["score"])

    # Convert LABEL_X to human-readable intent
    intent_name = LABEL_TO_INTENT.get(label, label)

    return intent_name, f"{confidence:.2%}"

# Gradio interface (UI + REST API)
demo = gr.Interface(
    fn=predict_intent,
    inputs=gr.Textbox(lines=3, label="User message"),
    outputs=[
        gr.Textbox(label="Predicted intent"),
        gr.Textbox(label="Confidence"),
    ],
    title="University Intent Classifier",
    description="Classifies queries into intents like admission, eligibility, fees, campus life, etc.",
    examples=[
        ["How can I apply for admission?"],
        ["What is the eligibility for BTech?"],
        ["Is there any scholarship available?"],
        ["When do admissions start?"],
        ["Who should I contact for technical issues?"],
    ],
    api_name="predict",  # exposes /run/predict endpoint
)

if __name__ == "__main__":
    demo.launch()