|
|
import gradio as gr |
|
|
import pandas as pd |
|
|
import numpy as np |
|
|
import faiss |
|
|
from sentence_transformers import SentenceTransformer |
|
|
import joblib |
|
|
|
|
|
|
|
|
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." |
|
|
|
|
|
|
|
|
query_emb = model.encode([text]) |
|
|
print("Embedding shape:", query_emb.shape) |
|
|
|
|
|
|
|
|
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) |
|
|
|
|
|
|
|
|
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}" |
|
|
|
|
|
|
|
|
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() |
|
|
|