# ================================================================== # 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()