KiranRand commited on
Commit
8f3289d
·
verified ·
1 Parent(s): 99415c9

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -0
app.py ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import json
4
+ from sentence_transformers import SentenceTransformer, util
5
+
6
+ # Load precomputed QA pairs and embeddings
7
+ with open("qa.json", encoding="utf-8") as f:
8
+ QA_PAIRS = json.load(f)
9
+
10
+ QA_EMBEDDINGS = np.load("qa.npy")
11
+ hf_model = SentenceTransformer('all-MiniLM-L6-v2')
12
+
13
+ def find_similar(question):
14
+ user_embedding = hf_model.encode(question, convert_to_tensor=True)
15
+ similarities = util.cos_sim(user_embedding, QA_EMBEDDINGS)[0]
16
+ best_score = float(similarities.max())
17
+ best_index = int(similarities.argmax())
18
+
19
+ if best_score >= 0.6:
20
+ return QA_PAIRS[best_index]["answer"]
21
+ else:
22
+ return "Sorry, I could not find a relevant answer."
23
+
24
+ iface = gr.Interface(
25
+ fn=find_similar,
26
+ inputs=gr.Textbox(label="Your Question"),
27
+ outputs=gr.Textbox(label="Answer"),
28
+ title="Similarity Service"
29
+ )
30
+
31
+ iface.launch()