suplex-city's picture
Add Gradio app
290e262
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()