Update app.py
Browse files
app.py
CHANGED
|
@@ -1,44 +1,44 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import pandas as pd
|
| 3 |
-
import numpy as np
|
| 4 |
-
import faiss
|
| 5 |
-
from sentence_transformers import SentenceTransformer
|
| 6 |
-
import joblib
|
| 7 |
-
|
| 8 |
-
# Load all assets
|
| 9 |
-
df = pd.read_csv("clean_feedback.csv")
|
| 10 |
-
embeddings = np.load("embeddings.npy")
|
| 11 |
-
index = faiss.read_index("feedback.index")
|
| 12 |
-
clf = joblib.load("
|
| 13 |
-
|
| 14 |
-
model = SentenceTransformer("paraphrase-multilingual-MiniLM-L12-v2")
|
| 15 |
-
|
| 16 |
-
def classify_feedback(text, top_k=5):
|
| 17 |
-
# Embed query
|
| 18 |
-
query_emb = model.encode([text])
|
| 19 |
-
|
| 20 |
-
# Retrieve top-k similar samples
|
| 21 |
-
distances, indices = index.search(query_emb, top_k)
|
| 22 |
-
|
| 23 |
-
# Gather context
|
| 24 |
-
retrieved = df.iloc[indices[0]]
|
| 25 |
-
context = "\n".join(retrieved['Sentence'].tolist())
|
| 26 |
-
|
| 27 |
-
# Predict sentiment
|
| 28 |
-
sentiment = clf.predict(query_emb)[0]
|
| 29 |
-
|
| 30 |
-
# Prepare explanation
|
| 31 |
-
examples = "\n".join([f"{i+1}. {s}" for i, s in enumerate(retrieved['Sentence'].tolist())])
|
| 32 |
-
|
| 33 |
-
return f"**Predicted Sentiment:** {sentiment}\n\n**Similar Feedbacks:**\n{examples}"
|
| 34 |
-
|
| 35 |
-
# Gradio UI
|
| 36 |
-
demo = gr.Interface(
|
| 37 |
-
fn=classify_feedback,
|
| 38 |
-
inputs=[gr.Textbox(label="Enter Student Feedback")],
|
| 39 |
-
outputs=[gr.Markdown(label="Prediction & Explanation")],
|
| 40 |
-
title="🎓 Student Feedback RAG System",
|
| 41 |
-
description="Classifies Roman Urdu/English student feedback with context and reasoning."
|
| 42 |
-
)
|
| 43 |
-
|
| 44 |
-
demo.launch()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
import faiss
|
| 5 |
+
from sentence_transformers import SentenceTransformer
|
| 6 |
+
import joblib
|
| 7 |
+
|
| 8 |
+
# Load all assets
|
| 9 |
+
df = pd.read_csv("clean_feedback.csv")
|
| 10 |
+
embeddings = np.load("embeddings.npy")
|
| 11 |
+
index = faiss.read_index("feedback.index")
|
| 12 |
+
clf = joblib.load("feedback_model.pkl")
|
| 13 |
+
|
| 14 |
+
model = SentenceTransformer("paraphrase-multilingual-MiniLM-L12-v2")
|
| 15 |
+
|
| 16 |
+
def classify_feedback(text, top_k=5):
|
| 17 |
+
# Embed query
|
| 18 |
+
query_emb = model.encode([text])
|
| 19 |
+
|
| 20 |
+
# Retrieve top-k similar samples
|
| 21 |
+
distances, indices = index.search(query_emb, top_k)
|
| 22 |
+
|
| 23 |
+
# Gather context
|
| 24 |
+
retrieved = df.iloc[indices[0]]
|
| 25 |
+
context = "\n".join(retrieved['Sentence'].tolist())
|
| 26 |
+
|
| 27 |
+
# Predict sentiment
|
| 28 |
+
sentiment = clf.predict(query_emb)[0]
|
| 29 |
+
|
| 30 |
+
# Prepare explanation
|
| 31 |
+
examples = "\n".join([f"{i+1}. {s}" for i, s in enumerate(retrieved['Sentence'].tolist())])
|
| 32 |
+
|
| 33 |
+
return f"**Predicted Sentiment:** {sentiment}\n\n**Similar Feedbacks:**\n{examples}"
|
| 34 |
+
|
| 35 |
+
# Gradio UI
|
| 36 |
+
demo = gr.Interface(
|
| 37 |
+
fn=classify_feedback,
|
| 38 |
+
inputs=[gr.Textbox(label="Enter Student Feedback")],
|
| 39 |
+
outputs=[gr.Markdown(label="Prediction & Explanation")],
|
| 40 |
+
title="🎓 Student Feedback RAG System",
|
| 41 |
+
description="Classifies Roman Urdu/English student feedback with context and reasoning."
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
demo.launch()
|