File size: 4,751 Bytes
520bd8e | 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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | # ==================================================================
# app.py for Hugging Face Gradio Space
# ==================================================================
# STEP 1: Install necessary libraries (will be installed by Gradio Space)
# !pip install gradio huggingface_hub fasttext
import gradio as gr
import fasttext
import re
from huggingface_hub import hf_hub_download
import os
# ==================================================================
# STEP 2: Download the FastText model from Hugging Face Hub
# ==================================================================
# Replace 'Sheshank2609/Complaint_Classifier' with your actual repo_id
REPO_ID = "Sheshank2609/Complaint_Classifier"
MODEL_FILENAME = "complaint_classifier.ftz"
# Download the model file. It will be cached locally in the Space.
model_path = hf_hub_download(repo_id=REPO_ID, filename=MODEL_FILENAME)
# Load the FastText model once
model = fasttext.load_model(model_path)
# ==================================================================
# STEP 3: Preprocessing Functions (MUST be the same as training)
# ==================================================================
def clean_text(text):
text = str(text).lower()
text = re.sub(r'\s+', ' ', text).strip()
return text
def normalize_slang(text):
replacements = {
"bakwass": "bakwas",
"boht": "bahut",
"nhi": "nahi",
"nai": "nahi",
"yaarrr": "yaar",
"yawwrrr": "yaar",
"pls": "please",
"plz": "please"
}
for k, v in replacements.items():
text = text.replace(k, v)
return text
def boost_keywords(text):
if any(word in text for word in ["khana", "khaana", "Jevan", "food", "mess", "roti", "sabzi", "rice", "milk"]):
text += " mess food quality eating"
if any(word in text for word in ["ganda","flush","toilet", "washroom", "dirty", "dust", "garbage", "smell"]):
text += " cleanliness hygiene sanitation"
# TECH
if any(word in text for word in ["wifi", "internet", "net", "network", "server", "pcs", "system"]):
text += " technical network issue"
# INFRA
if any(word in text for word in ["ac", "fan", "light", "door", "bench", "lock", "window"]):
text += " infrastructure maintenance"
# ACADEMICS
if any(word in text for word in ["teacher", "lecture", "class", "test", "exam", "assignment"]):
text += " academics study"
# RAGGING
if any(word in text for word in ["ragging", "bully", "harass", "senior"]):
text += " ragging harassment"
return text
# ==================================================================
# STEP 4: Prediction Function for Gradio
# ==================================================================
def predict_complaint_gradio(complaint_text):
# Apply the same preprocessing as during training
processed_text = clean_text(complaint_text)
processed_text = normalize_slang(processed_text)
processed_text = boost_keywords(processed_text)
# Make prediction
# FastText predict returns a tuple: ([label], [probability])
predictions = model.predict(processed_text, k=1)
# Extract label and confidence
label = predictions[0][0].replace("__label__", "")
confidence = predictions[1][0] * 100
return f"Department: {label}", f"Confidence: {confidence:.2f}%"
# ==================================================================
# STEP 5: Create Gradio Interface
# ==================================================================
# Define example inputs
examples = [
["The projector in room 301 is not working. It's urgent for the class."],
["My room is full of dust and cobwebs. Please send someone to clean it."],
["The food in the mess is very spicy and unhealthy."],
["The wifi is constantly disconnecting in my hostel room."],
["The teacher didn't explain the concept properly in today's lecture."],
["Seniors are bothering first-year students in the common area."]
]
iface = gr.Interface(
fn=predict_complaint_gradio,
inputs=gr.Textbox(lines=5, placeholder="Enter your complaint here..."),
outputs=[gr.Textbox(label="Predicted Department"), gr.Textbox(label="Confidence")],
title="Student Complaint Classifier",
description="Enter a student complaint, and the model will classify it into the most relevant department along with a confidence score.",
examples=examples,
allow_flagging="manual" # If you want to allow users to flag incorrect predictions
)
# ==================================================================
# STEP 6: Launch the Gradio app
# ==================================================================
if __name__ == "__main__":
iface.launch() |