Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,22 @@
|
|
| 1 |
-
import
|
| 2 |
-
from
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
def koda_assistant(user_input, history):
|
| 17 |
# Check if input matches FAQ keywords
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
from sentence_transformers import SentenceTransformer
|
| 3 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
faq_df = pd.read_csv("data/koda_faq.csv")
|
| 7 |
+
faq_questions = faq_df["question"].tolist()
|
| 8 |
+
faq_answers = faq_df["answer"].tolist()
|
| 9 |
+
|
| 10 |
+
embedder = SentenceTransformer("all-MiniLM-L6-v2")
|
| 11 |
+
faq_embs = embedder.encode(faq_questions, normalize_embeddings=True)
|
| 12 |
+
|
| 13 |
+
def retrieve_answer(user_q, top_k=1, thresh=0.35):
|
| 14 |
+
q_emb = embedder.encode([user_q], normalize_embeddings=True)
|
| 15 |
+
sims = cosine_similarity(q_emb, faq_embs)[0]
|
| 16 |
+
idx = int(np.argmax(sims))
|
| 17 |
+
if sims[idx] >= thresh:
|
| 18 |
+
return faq_answers[idx], float(sims[idx])
|
| 19 |
+
return None, float(sims[idx])
|
| 20 |
|
| 21 |
def koda_assistant(user_input, history):
|
| 22 |
# Check if input matches FAQ keywords
|