Update app.py
Browse files
app.py
CHANGED
|
@@ -42,29 +42,37 @@ def classify_feedback(text, top_k=5):
|
|
| 42 |
print(f"\n🧠 New query: {text}")
|
| 43 |
|
| 44 |
if not text.strip():
|
| 45 |
-
return "⚠️ Please enter a feedback text."
|
| 46 |
|
| 47 |
-
# Embed
|
| 48 |
query_emb = model.encode([text])
|
| 49 |
print("Embedding shape:", query_emb.shape)
|
| 50 |
|
| 51 |
-
#
|
| 52 |
distances, indices = index.search(query_emb, top_k)
|
| 53 |
-
print("Retrieved indices:", indices)
|
| 54 |
-
|
| 55 |
retrieved = df.iloc[indices[0]]
|
| 56 |
|
| 57 |
-
# Predict sentiment
|
| 58 |
try:
|
| 59 |
sentiment = clf.predict(query_emb)[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
except Exception as e:
|
| 61 |
-
return f"❌ Model prediction error: {str(e)}"
|
| 62 |
|
| 63 |
-
examples = "\n".join(
|
| 64 |
-
[f"{i+1}. {s}" for i, s in enumerate(retrieved['Sentence'].tolist())]
|
| 65 |
-
)
|
| 66 |
print("✅ Prediction done")
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
# ------------------------
|
| 69 |
# Save user input to log
|
| 70 |
# ------------------------
|
|
|
|
| 42 |
print(f"\n🧠 New query: {text}")
|
| 43 |
|
| 44 |
if not text.strip():
|
| 45 |
+
return "⚠️ Please enter a feedback text."
|
| 46 |
|
| 47 |
+
# Embed the input
|
| 48 |
query_emb = model.encode([text])
|
| 49 |
print("Embedding shape:", query_emb.shape)
|
| 50 |
|
| 51 |
+
# Retrieve top similar examples
|
| 52 |
distances, indices = index.search(query_emb, top_k)
|
|
|
|
|
|
|
| 53 |
retrieved = df.iloc[indices[0]]
|
| 54 |
|
| 55 |
+
# Predict sentiment and probability
|
| 56 |
try:
|
| 57 |
sentiment = clf.predict(query_emb)[0]
|
| 58 |
+
if hasattr(clf, "predict_proba"):
|
| 59 |
+
confidence = clf.predict_proba(query_emb).max() * 100
|
| 60 |
+
confidence = round(confidence, 2)
|
| 61 |
+
else:
|
| 62 |
+
confidence = "N/A"
|
| 63 |
except Exception as e:
|
| 64 |
+
return f"❌ Model prediction error: {str(e)}"
|
| 65 |
|
| 66 |
+
examples = "\n".join([f"{i+1}. {s}" for i, s in enumerate(retrieved['Sentence'].tolist())])
|
|
|
|
|
|
|
| 67 |
print("✅ Prediction done")
|
| 68 |
|
| 69 |
+
return (
|
| 70 |
+
f"**Predicted Sentiment:** {sentiment}\n"
|
| 71 |
+
f"**Confidence:** {confidence}%\n\n"
|
| 72 |
+
f"**Similar Feedbacks:**\n{examples}"
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
|
| 76 |
# ------------------------
|
| 77 |
# Save user input to log
|
| 78 |
# ------------------------
|