File size: 1,944 Bytes
318931c
 
 
 
 
 
 
efe8f87
 
318931c
efe8f87
 
bcc1d48
efe8f87
 
 
 
 
 
 
318931c
7435869
efe8f87
318931c
 
efe8f87
 
7435869
efe8f87
7435869
efe8f87
318931c
efe8f87
7435869
efe8f87
318931c
efe8f87
7435869
318931c
efe8f87
 
7435869
efe8f87
 
 
 
 
7435869
efe8f87
 
7435869
318931c
 
efe8f87
318931c
 
efe8f87
318931c
 
efe8f87
318931c
 
 
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
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()