Update app.py
Browse files
app.py
CHANGED
|
@@ -5,63 +5,59 @@ import faiss
|
|
| 5 |
from sentence_transformers import SentenceTransformer
|
| 6 |
import joblib
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
# ===============================
|
| 11 |
df = pd.read_csv("clean_feedback.csv")
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 "
|
| 25 |
|
| 26 |
-
#
|
| 27 |
query_emb = model.encode([text])
|
|
|
|
| 28 |
|
| 29 |
-
#
|
| 30 |
distances, indices = index.search(query_emb, top_k)
|
|
|
|
| 31 |
|
| 32 |
-
# Get similar sentences
|
| 33 |
retrieved = df.iloc[indices[0]]
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
#
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 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="
|
| 62 |
)
|
| 63 |
|
| 64 |
-
# ===============================
|
| 65 |
-
# πΉ Launch app
|
| 66 |
-
# ===============================
|
| 67 |
demo.launch()
|
|
|
|
| 5 |
from sentence_transformers import SentenceTransformer
|
| 6 |
import joblib
|
| 7 |
|
| 8 |
+
# Load assets
|
| 9 |
+
print("π Loading data and models...")
|
|
|
|
| 10 |
df = pd.read_csv("clean_feedback.csv")
|
| 11 |
+
print("β
CSV loaded with columns:", df.columns.tolist())
|
| 12 |
+
|
| 13 |
+
embeddings = np.load("embedings.npy")
|
| 14 |
+
print("β
Embeddings loaded with shape:", embeddings.shape)
|
| 15 |
+
|
| 16 |
+
index = faiss.read_index("feedback.index")
|
| 17 |
+
print("β
FAISS index loaded")
|
| 18 |
+
|
| 19 |
+
clf = joblib.load("feedback_model.pkl")
|
| 20 |
+
print("β
Sentiment model loaded")
|
| 21 |
|
|
|
|
| 22 |
model = SentenceTransformer("paraphrase-multilingual-MiniLM-L12-v2", device="cpu")
|
| 23 |
+
print("β
SentenceTransformer ready")
|
| 24 |
|
|
|
|
|
|
|
|
|
|
| 25 |
def classify_feedback(text, top_k=5):
|
| 26 |
+
print(f"\nπ§ New query: {text}")
|
| 27 |
+
|
| 28 |
if not text.strip():
|
| 29 |
+
return "β οΈ Please enter a feedback text."
|
| 30 |
|
| 31 |
+
# Embed query
|
| 32 |
query_emb = model.encode([text])
|
| 33 |
+
print("Embedding shape:", query_emb.shape)
|
| 34 |
|
| 35 |
+
# Search similar samples
|
| 36 |
distances, indices = index.search(query_emb, top_k)
|
| 37 |
+
print("Retrieved indices:", indices)
|
| 38 |
|
|
|
|
| 39 |
retrieved = df.iloc[indices[0]]
|
| 40 |
+
if "Sentence" not in df.columns:
|
| 41 |
+
return "β Column 'Sentence' not found in CSV. Columns are: " + ", ".join(df.columns)
|
| 42 |
|
| 43 |
+
# Predict sentiment
|
| 44 |
+
try:
|
| 45 |
+
sentiment = clf.predict(query_emb)[0]
|
| 46 |
+
except Exception as e:
|
| 47 |
+
return f"β Model prediction error: {str(e)}"
|
| 48 |
|
| 49 |
+
examples = "\n".join([f"{i+1}. {s}" for i, s in enumerate(retrieved['Sentence'].tolist())])
|
| 50 |
+
print("β
Prediction done")
|
|
|
|
|
|
|
| 51 |
|
| 52 |
return f"**Predicted Sentiment:** {sentiment}\n\n**Similar Feedbacks:**\n{examples}"
|
| 53 |
|
| 54 |
+
# Gradio UI
|
|
|
|
|
|
|
| 55 |
demo = gr.Interface(
|
| 56 |
fn=classify_feedback,
|
| 57 |
+
inputs=[gr.Textbox(label="Enter Student Feedback")],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
outputs=[gr.Markdown(label="Prediction & Explanation")],
|
| 59 |
title="π Student Feedback RAG System",
|
| 60 |
+
description="Classifies Roman Urdu/English student feedback with context and reasoning."
|
| 61 |
)
|
| 62 |
|
|
|
|
|
|
|
|
|
|
| 63 |
demo.launch()
|