Delete qamatcher_server.py
Browse files- qamatcher_server.py +0 -69
qamatcher_server.py
DELETED
@@ -1,69 +0,0 @@
|
|
1 |
-
|
2 |
-
import os
|
3 |
-
from flask import request, jsonify
|
4 |
-
from sentence_transformers import SentenceTransformer, util
|
5 |
-
|
6 |
-
# Define a writable directory for the model cache
|
7 |
-
cache_dir = os.path.join(os.getenv("XDG_CACHE_HOME", "/tmp/.cache"), "huggingface_models")
|
8 |
-
os.makedirs(cache_dir, exist_ok=True)
|
9 |
-
|
10 |
-
print("Loading SentenceTransformer model (paraphrase-MiniLM-L6-v2)...")
|
11 |
-
matcher_model = SentenceTransformer('paraphrase-MiniLM-L6-v2', cache_folder=cache_dir)
|
12 |
-
print("SentenceTransformer model loaded.")
|
13 |
-
|
14 |
-
# Define a threshold for a "good" match
|
15 |
-
SIMILARITY_THRESHOLD = 0.6
|
16 |
-
|
17 |
-
def handle_match_question():
|
18 |
-
data = request.get_json()
|
19 |
-
if not data or 'user_question' not in data or 'documents' not in data:
|
20 |
-
return jsonify({'error': 'Invalid request. "user_question" and "documents" are required.'}), 400
|
21 |
-
|
22 |
-
user_question = data['user_question']
|
23 |
-
documents = data['documents']
|
24 |
-
|
25 |
-
if not documents:
|
26 |
-
return jsonify({'answer': "There are no notes to search."})
|
27 |
-
|
28 |
-
# Flatten the list of questions from all documents
|
29 |
-
all_questions = []
|
30 |
-
# Map each question to the original note text
|
31 |
-
question_to_note_map = {}
|
32 |
-
|
33 |
-
for doc in documents:
|
34 |
-
note_text = doc.get('note_text', '')
|
35 |
-
for q in doc.get('questions', []):
|
36 |
-
all_questions.append(q)
|
37 |
-
question_to_note_map[q] = note_text
|
38 |
-
|
39 |
-
if not all_questions:
|
40 |
-
return jsonify({'answer': "No questions have been generated for your notes yet."})
|
41 |
-
|
42 |
-
try:
|
43 |
-
# Encode the user's question and all stored questions
|
44 |
-
user_embedding = matcher_model.encode(user_question, convert_to_tensor=True)
|
45 |
-
stored_embeddings = matcher_model.encode(all_questions, convert_to_tensor=True)
|
46 |
-
|
47 |
-
# Compute cosine similarity
|
48 |
-
cosine_scores = util.pytorch_cos_sim(user_embedding, stored_embeddings)
|
49 |
-
|
50 |
-
# Find the best match
|
51 |
-
best_match_idx = cosine_scores.argmax()
|
52 |
-
best_score = float(cosine_scores[0][best_match_idx])
|
53 |
-
best_question = all_questions[best_match_idx]
|
54 |
-
|
55 |
-
print(f"User Question: '{user_question}'")
|
56 |
-
print(f"Best matched stored question: '{best_question}' with score: {best_score:.4f}")
|
57 |
-
|
58 |
-
# Check if the match is good enough
|
59 |
-
if best_score > SIMILARITY_THRESHOLD:
|
60 |
-
# Return the note associated with the best-matched question
|
61 |
-
answer = question_to_note_map[best_question]
|
62 |
-
else:
|
63 |
-
answer = "Sorry, I couldn't find a relevant note to answer your question."
|
64 |
-
|
65 |
-
return jsonify({'answer': answer})
|
66 |
-
|
67 |
-
except Exception as e:
|
68 |
-
print(f"Error during question matching: {e}")
|
69 |
-
return jsonify({'error': str(e)}), 500
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|