goldrode commited on
Commit
f30208e
·
verified ·
1 Parent(s): 58dc866

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -9
app.py CHANGED
@@ -30,18 +30,22 @@ llm = AutoModelForCausalLM.from_pretrained(llama_model_name, token=API_TOKEN)
30
  def generate_advice(extracted_data):
31
  try:
32
  recommendations = []
 
33
  for item in extracted_data:
34
  # Prepare the query string
35
  query = f"{item['Component']} {item['Status']}"
36
 
37
- # Generate query embedding (ensure it's a valid numpy array)
38
  query_embedding = embedding_model.encode([query])
39
- query_embedding = np.array(query_embedding).astype('float32') # Explicit cast
40
-
41
- if query_embedding.shape != (1, index.d): # FAISS index shape check
42
- raise ValueError("Query embedding shape mismatch. Check input to the embedding model.")
43
 
44
- # Search the FAISS index for the closest match
 
 
 
 
 
 
45
  _, idx = index.search(query_embedding, 1)
46
  best_match = kb[idx[0][0]]
47
 
@@ -69,7 +73,7 @@ def generate_advice(extracted_data):
69
  return_tensors="pt",
70
  )
71
 
72
- # Generate response
73
  output = llm.generate(
74
  input_ids=input_text_with_your_role,
75
  max_length=150,
@@ -77,15 +81,16 @@ def generate_advice(extracted_data):
77
  )
78
  advice = tokenizer.decode(output[0], skip_special_tokens=True).strip()
79
 
 
80
  recommendations.append({
81
  "Component": item["Component"],
82
  "Advice": advice
83
  })
 
84
  return recommendations
85
 
86
  except Exception as e:
87
- # Return structured error message
88
- return [{"error": str(e)}]
89
 
90
  # Gradio app with LLM integration
91
  import gradio as gr
 
30
  def generate_advice(extracted_data):
31
  try:
32
  recommendations = []
33
+
34
  for item in extracted_data:
35
  # Prepare the query string
36
  query = f"{item['Component']} {item['Status']}"
37
 
38
+ # Generate query embedding and ensure its a 2D numpy array
39
  query_embedding = embedding_model.encode([query])
40
+ query_embedding = np.array(query_embedding, dtype="float32").reshape(1, -1)
 
 
 
41
 
42
+ # Check query embedding shape against FAISS index
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 in the FAISS index
49
  _, idx = index.search(query_embedding, 1)
50
  best_match = kb[idx[0][0]]
51
 
 
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,
 
81
  )
82
  advice = tokenizer.decode(output[0], skip_special_tokens=True).strip()
83
 
84
+ # Append results
85
  recommendations.append({
86
  "Component": item["Component"],
87
  "Advice": advice
88
  })
89
+
90
  return recommendations
91
 
92
  except Exception as e:
93
+ return [{"error": f"Exception occurred: {str(e)}"}]
 
94
 
95
  # Gradio app with LLM integration
96
  import gradio as gr