Update app.py
Browse files
app.py
CHANGED
|
@@ -5,40 +5,63 @@ import faiss
|
|
| 5 |
from sentence_transformers import SentenceTransformer
|
| 6 |
import joblib
|
| 7 |
|
| 8 |
-
#
|
|
|
|
|
|
|
| 9 |
df = pd.read_csv("clean_feedback.csv")
|
| 10 |
-
embeddings = np.load("
|
| 11 |
-
index = faiss.read_index("feedback.index")
|
| 12 |
-
clf = joblib.load("feedback_model.pkl")
|
| 13 |
|
| 14 |
-
|
|
|
|
| 15 |
|
|
|
|
|
|
|
|
|
|
| 16 |
def classify_feedback(text, top_k=5):
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
| 18 |
query_emb = model.encode([text])
|
| 19 |
-
|
| 20 |
-
# Retrieve top-k similar
|
| 21 |
distances, indices = index.search(query_emb, top_k)
|
| 22 |
-
|
| 23 |
-
#
|
| 24 |
retrieved = df.iloc[indices[0]]
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
| 28 |
sentiment = clf.predict(query_emb)[0]
|
| 29 |
-
|
| 30 |
-
# Prepare explanation
|
| 31 |
-
examples = "\n".join(
|
| 32 |
-
|
|
|
|
|
|
|
| 33 |
return f"**Predicted Sentiment:** {sentiment}\n\n**Similar Feedbacks:**\n{examples}"
|
| 34 |
|
| 35 |
-
#
|
|
|
|
|
|
|
| 36 |
demo = gr.Interface(
|
| 37 |
fn=classify_feedback,
|
| 38 |
-
inputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
outputs=[gr.Markdown(label="Prediction & Explanation")],
|
| 40 |
title="π Student Feedback RAG System",
|
| 41 |
-
description="
|
| 42 |
)
|
| 43 |
|
|
|
|
|
|
|
|
|
|
| 44 |
demo.launch()
|
|
|
|
| 5 |
from sentence_transformers import SentenceTransformer
|
| 6 |
import joblib
|
| 7 |
|
| 8 |
+
# ===============================
|
| 9 |
+
# πΉ Load all assets
|
| 10 |
+
# ===============================
|
| 11 |
df = pd.read_csv("clean_feedback.csv")
|
| 12 |
+
embeddings = np.load("embedings.npy") # β
matches your file spelling
|
| 13 |
+
index = faiss.read_index("feedback.index") # β
matches the visible file
|
| 14 |
+
clf = joblib.load("feedback_model.pkl") # β
your classifier model
|
| 15 |
|
| 16 |
+
# β
Use CPU to avoid Hugging Face GPU issues
|
| 17 |
+
model = SentenceTransformer("paraphrase-multilingual-MiniLM-L12-v2", device="cpu")
|
| 18 |
|
| 19 |
+
# ===============================
|
| 20 |
+
# πΉ Define feedback classifier
|
| 21 |
+
# ===============================
|
| 22 |
def classify_feedback(text, top_k=5):
|
| 23 |
+
if not text.strip():
|
| 24 |
+
return "**Please enter some feedback text.**"
|
| 25 |
+
|
| 26 |
+
# Create embedding for user input
|
| 27 |
query_emb = model.encode([text])
|
| 28 |
+
|
| 29 |
+
# Retrieve top-k similar feedbacks from FAISS index
|
| 30 |
distances, indices = index.search(query_emb, top_k)
|
| 31 |
+
|
| 32 |
+
# Get similar sentences
|
| 33 |
retrieved = df.iloc[indices[0]]
|
| 34 |
+
|
| 35 |
+
# Combine context (optional, for RAG explainability)
|
| 36 |
+
context = "\n".join(retrieved['Sentence'].astype(str).tolist())
|
| 37 |
+
|
| 38 |
+
# Predict sentiment from classifier
|
| 39 |
sentiment = clf.predict(query_emb)[0]
|
| 40 |
+
|
| 41 |
+
# Prepare explanation list
|
| 42 |
+
examples = "\n".join(
|
| 43 |
+
[f"{i+1}. {s}" for i, s in enumerate(retrieved['Sentence'].astype(str).tolist())]
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
return f"**Predicted Sentiment:** {sentiment}\n\n**Similar Feedbacks:**\n{examples}"
|
| 47 |
|
| 48 |
+
# ===============================
|
| 49 |
+
# πΉ Gradio UI
|
| 50 |
+
# ===============================
|
| 51 |
demo = gr.Interface(
|
| 52 |
fn=classify_feedback,
|
| 53 |
+
inputs=[
|
| 54 |
+
gr.Textbox(
|
| 55 |
+
label="Enter Student Feedback",
|
| 56 |
+
placeholder="Type a Roman Urdu or English feedback sentence here..."
|
| 57 |
+
)
|
| 58 |
+
],
|
| 59 |
outputs=[gr.Markdown(label="Prediction & Explanation")],
|
| 60 |
title="π Student Feedback RAG System",
|
| 61 |
+
description="Analyzes student feedback (Roman Urdu or English) using Retrieval-Augmented Generation with contextual examples."
|
| 62 |
)
|
| 63 |
|
| 64 |
+
# ===============================
|
| 65 |
+
# πΉ Launch app
|
| 66 |
+
# ===============================
|
| 67 |
demo.launch()
|