Update app.py
Browse files
app.py
CHANGED
|
@@ -36,27 +36,18 @@ def generate_advice(extracted_data):
|
|
| 36 |
query = f"{item['Component']} {item['Status']}"
|
| 37 |
print(f"Processing query: {query}") # Debug print
|
| 38 |
|
| 39 |
-
# Generate query embedding and
|
| 40 |
query_embedding = embedding_model.encode([query])
|
| 41 |
-
|
| 42 |
-
query_embedding = np.array(query_embedding, dtype="float32")
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
# Debugging the embedding
|
| 47 |
-
print(f"Query Embedding Shape: {query_embedding.shape}, FAISS Index Dim: {index.d}")
|
| 48 |
-
|
| 49 |
-
# Validate shape compatibility
|
| 50 |
-
if query_embedding.shape[1] != index.d:
|
| 51 |
-
raise ValueError(
|
| 52 |
-
f"Embedding dimension mismatch: Query ({query_embedding.shape[1]}), Index ({index.d})"
|
| 53 |
-
)
|
| 54 |
-
|
| 55 |
-
# Search for the closest match
|
| 56 |
_, idx = index.search(query_embedding, 1)
|
| 57 |
-
print(f"FAISS
|
| 58 |
|
|
|
|
| 59 |
best_match = kb[idx[0][0]]
|
|
|
|
|
|
|
| 60 |
|
| 61 |
# Prepare the LLM prompt
|
| 62 |
role = "Medical expert providing advice based on lab results."
|
|
@@ -82,7 +73,7 @@ def generate_advice(extracted_data):
|
|
| 82 |
return_tensors="pt",
|
| 83 |
)
|
| 84 |
|
| 85 |
-
# Generate response
|
| 86 |
output = llm.generate(
|
| 87 |
input_ids=input_text_with_your_role,
|
| 88 |
max_length=150,
|
|
|
|
| 36 |
query = f"{item['Component']} {item['Status']}"
|
| 37 |
print(f"Processing query: {query}") # Debug print
|
| 38 |
|
| 39 |
+
# Generate query embedding and ensure it’s a 2D numpy array
|
| 40 |
query_embedding = embedding_model.encode([query])
|
| 41 |
+
query_embedding = np.array(query_embedding, dtype="float32").reshape(1, -1)
|
|
|
|
| 42 |
|
| 43 |
+
# Search for the closest match in FAISS
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
_, idx = index.search(query_embedding, 1)
|
| 45 |
+
print(f"FAISS Index: {idx}, Best Match Raw: {kb[idx[0][0]]}") # Debug print
|
| 46 |
|
| 47 |
+
# Retrieve the closest match and validate
|
| 48 |
best_match = kb[idx[0][0]]
|
| 49 |
+
if not isinstance(best_match, dict):
|
| 50 |
+
raise ValueError(f"Best match retrieved is not a dictionary: {best_match}")
|
| 51 |
|
| 52 |
# Prepare the LLM prompt
|
| 53 |
role = "Medical expert providing advice based on lab results."
|
|
|
|
| 73 |
return_tensors="pt",
|
| 74 |
)
|
| 75 |
|
| 76 |
+
# Generate response using LLaMA
|
| 77 |
output = llm.generate(
|
| 78 |
input_ids=input_text_with_your_role,
|
| 79 |
max_length=150,
|