Update app.py
Browse files
app.py
CHANGED
|
@@ -34,19 +34,28 @@ def generate_advice(extracted_data):
|
|
| 34 |
for item in extracted_data:
|
| 35 |
# Prepare the query string
|
| 36 |
query = f"{item['Component']} {item['Status']}"
|
| 37 |
-
|
| 38 |
-
|
|
|
|
| 39 |
query_embedding = embedding_model.encode([query])
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
#
|
|
|
|
|
|
|
|
|
|
| 43 |
if query_embedding.shape[1] != index.d:
|
| 44 |
raise ValueError(
|
| 45 |
f"Embedding dimension mismatch: Query ({query_embedding.shape[1]}), Index ({index.d})"
|
| 46 |
)
|
| 47 |
|
| 48 |
-
# Search for the closest match
|
| 49 |
_, idx = index.search(query_embedding, 1)
|
|
|
|
|
|
|
| 50 |
best_match = kb[idx[0][0]]
|
| 51 |
|
| 52 |
# Prepare the LLM prompt
|
|
@@ -73,7 +82,7 @@ def generate_advice(extracted_data):
|
|
| 73 |
return_tensors="pt",
|
| 74 |
)
|
| 75 |
|
| 76 |
-
# Generate response
|
| 77 |
output = llm.generate(
|
| 78 |
input_ids=input_text_with_your_role,
|
| 79 |
max_length=150,
|
|
@@ -81,7 +90,7 @@ def generate_advice(extracted_data):
|
|
| 81 |
)
|
| 82 |
advice = tokenizer.decode(output[0], skip_special_tokens=True).strip()
|
| 83 |
|
| 84 |
-
# Append
|
| 85 |
recommendations.append({
|
| 86 |
"Component": item["Component"],
|
| 87 |
"Advice": advice
|
|
@@ -90,6 +99,7 @@ def generate_advice(extracted_data):
|
|
| 90 |
return recommendations
|
| 91 |
|
| 92 |
except Exception as e:
|
|
|
|
| 93 |
return [{"error": f"Exception occurred: {str(e)}"}]
|
| 94 |
|
| 95 |
# Gradio app with LLM integration
|
|
|
|
| 34 |
for item in extracted_data:
|
| 35 |
# Prepare the query string
|
| 36 |
query = f"{item['Component']} {item['Status']}"
|
| 37 |
+
print(f"Processing query: {query}") # Debug print
|
| 38 |
+
|
| 39 |
+
# Generate query embedding and force it into 2D float32 numpy array
|
| 40 |
query_embedding = embedding_model.encode([query])
|
| 41 |
+
if not isinstance(query_embedding, np.ndarray):
|
| 42 |
+
query_embedding = np.array(query_embedding, dtype="float32")
|
| 43 |
+
|
| 44 |
+
query_embedding = query_embedding.reshape(1, -1).astype("float32")
|
| 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 Search Result: {idx}") # Debug print
|
| 58 |
+
|
| 59 |
best_match = kb[idx[0][0]]
|
| 60 |
|
| 61 |
# Prepare the LLM prompt
|
|
|
|
| 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,
|
|
|
|
| 90 |
)
|
| 91 |
advice = tokenizer.decode(output[0], skip_special_tokens=True).strip()
|
| 92 |
|
| 93 |
+
# Append result
|
| 94 |
recommendations.append({
|
| 95 |
"Component": item["Component"],
|
| 96 |
"Advice": advice
|
|
|
|
| 99 |
return recommendations
|
| 100 |
|
| 101 |
except Exception as e:
|
| 102 |
+
print(f"Error occurred: {str(e)}") # Debugging error
|
| 103 |
return [{"error": f"Exception occurred: {str(e)}"}]
|
| 104 |
|
| 105 |
# Gradio app with LLM integration
|