ghostai1 commited on
Commit
71b51dc
·
verified ·
1 Parent(s): 4071d42

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -0
app.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+ from sentence_transformers import SentenceTransformer, util
4
+
5
+ # ---------- Load data & model (all CPU-friendly) ----------
6
+ faq_df = pd.read_csv("faqs.csv")
7
+ questions = faq_df["question"].tolist()
8
+ answers = faq_df["answer"].tolist()
9
+
10
+ model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
11
+ question_embeddings = model.encode(questions, convert_to_tensor=True, normalize_embeddings=True)
12
+
13
+ # ---------- Search function ----------
14
+ def semantic_search(user_query, top_k=3):
15
+ query_embedding = model.encode(user_query, convert_to_tensor=True, normalize_embeddings=True)
16
+ scores = util.cos_sim(query_embedding, question_embeddings)[0]
17
+ top_k_idx = scores.topk(k=top_k).indices.cpu().numpy()
18
+
19
+ results = []
20
+ for idx in top_k_idx:
21
+ results.append(
22
+ {
23
+ "FAQ Question": questions[idx],
24
+ "FAQ Answer" : answers[idx],
25
+ "Similarity" : f"{scores[idx]:.3f}"
26
+ }
27
+ )
28
+ return results
29
+
30
+ # ---------- Gradio UI ----------
31
+ with gr.Blocks(title="MiniLM Semantic FAQ Search") as demo:
32
+ gr.Markdown(
33
+ """
34
+ # 🔍 Semantic FAQ Search
35
+ Enter a salon-related question. The model finds the closest FAQs and displays their answers.
36
+ """)
37
+
38
+ with gr.Row():
39
+ query_box = gr.Textbox(
40
+ label="Ask a question",
41
+ placeholder="e.g. Which spray protects hair from heat?"
42
+ )
43
+ topk_slider = gr.Slider(
44
+ 1, 5, value=3, step=1, label="Number of results"
45
+ )
46
+ search_btn = gr.Button("Search")
47
+ out = gr.Dataframe(headers=["FAQ Question", "FAQ Answer", "Similarity"], visible=True, wrap=True)
48
+
49
+ search_btn.click(semantic_search, [query_box, topk_slider], out)
50
+
51
+ if __name__ == "__main__":
52
+ demo.launch(server_name="0.0.0.0", show_error=True)