tahamueed23's picture
Update app.py
bcc1d48 verified
import gradio as gr
import pandas as pd
import numpy as np
import faiss
from sentence_transformers import SentenceTransformer
import joblib
# Load assets
print("πŸ”„ Loading data and models...")
df = pd.read_csv("clean_feedback.csv")
print("βœ… CSV loaded with columns:", df.columns.tolist())
embeddings = np.load("embeddings.npy")
print("βœ… Embeddings loaded with shape:", embeddings.shape)
index = faiss.read_index("feedback.index")
print("βœ… FAISS index loaded")
clf = joblib.load("feedback_model.pkl")
print("βœ… Sentiment model loaded")
model = SentenceTransformer("paraphrase-multilingual-MiniLM-L12-v2", device="cpu")
print("βœ… SentenceTransformer ready")
def classify_feedback(text, top_k=5):
print(f"\n🧠 New query: {text}")
if not text.strip():
return "⚠️ Please enter a feedback text."
# Embed query
query_emb = model.encode([text])
print("Embedding shape:", query_emb.shape)
# Search similar samples
distances, indices = index.search(query_emb, top_k)
print("Retrieved indices:", indices)
retrieved = df.iloc[indices[0]]
if "Sentence" not in df.columns:
return "❌ Column 'Sentence' not found in CSV. Columns are: " + ", ".join(df.columns)
# Predict sentiment
try:
sentiment = clf.predict(query_emb)[0]
except Exception as e:
return f"❌ Model prediction error: {str(e)}"
examples = "\n".join([f"{i+1}. {s}" for i, s in enumerate(retrieved['Sentence'].tolist())])
print("βœ… Prediction done")
return f"**Predicted Sentiment:** {sentiment}\n\n**Similar Feedbacks:**\n{examples}"
# Gradio UI
demo = gr.Interface(
fn=classify_feedback,
inputs=[gr.Textbox(label="Enter Student Feedback")],
outputs=[gr.Markdown(label="Prediction & Explanation")],
title="πŸŽ“ Student Feedback RAG System",
description="Classifies Roman Urdu/English student feedback with context and reasoning."
)
demo.launch()