tahamueed23 commited on
Commit
7435869
Β·
verified Β·
1 Parent(s): 318931c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -20
app.py CHANGED
@@ -5,40 +5,63 @@ 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()
 
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()