goldrode commited on
Commit
f1125fe
·
verified ·
1 Parent(s): 3c1950a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -12
app.py CHANGED
@@ -38,44 +38,40 @@ def generate_advice(extracted_data):
38
 
39
  for item in extracted_data:
40
  query = f"{item['Component']} {item['Status']}"
41
- print(f"Query: {query}") # Debugging step
42
 
43
- # Generate query embedding
44
  query_embedding = embedding_model.encode([query])
45
  query_embedding = np.array(query_embedding, dtype="float32").reshape(1, -1)
46
 
47
- # Validate embedding shape
48
- if query_embedding.shape[1] != index.d:
49
- raise ValueError(f"Embedding dimension mismatch: FAISS expects {index.d}, but got {query_embedding.shape[1]}")
50
-
51
  # Search FAISS for the closest match
52
  _, idx = index.search(query_embedding, 1)
53
  best_match = kb[idx[0][0]]
54
 
55
- # LLM prompt
56
  role = "Medical expert providing advice based on lab results."
57
  prompt = f"""
58
  Lab Test: {item['Component']}
59
  Value: {item['Value']} {item['Units']}
60
  Status: {item['Status']}
61
-
62
  Medical Guidelines: {best_match['Advice']}
63
-
64
  Provide additional insights or recommendations.
65
  """
66
 
67
- # Generate advice
68
  message = [
69
  {"role": "system", "content": role},
70
  {"role": "user", "content": prompt},
71
  ]
72
 
73
  input_text = tokenizer.apply_chat_template(
74
- message, tokenize=False, add_generation_prompt=True, return_tensors="pt"
75
  )
76
 
77
  output = llm.generate(
78
- input_ids=input_text,
79
  max_length=150,
80
  num_return_sequences=1
81
  )
@@ -86,8 +82,10 @@ def generate_advice(extracted_data):
86
  return recommendations
87
 
88
  except Exception as e:
 
89
  return [{"error": f"Exception occurred: {str(e)}"}]
90
 
 
91
  # Extract structured data from the PDF
92
  def pdf_to_text(pdf_file):
93
  try:
 
38
 
39
  for item in extracted_data:
40
  query = f"{item['Component']} {item['Status']}"
41
+ print(f"Processing Query: {query}") # Debugging step
42
 
43
+ # Generate query embedding as float32
44
  query_embedding = embedding_model.encode([query])
45
  query_embedding = np.array(query_embedding, dtype="float32").reshape(1, -1)
46
 
 
 
 
 
47
  # Search FAISS for the closest match
48
  _, idx = index.search(query_embedding, 1)
49
  best_match = kb[idx[0][0]]
50
 
51
+ # Prepare LLM prompt
52
  role = "Medical expert providing advice based on lab results."
53
  prompt = f"""
54
  Lab Test: {item['Component']}
55
  Value: {item['Value']} {item['Units']}
56
  Status: {item['Status']}
57
+
58
  Medical Guidelines: {best_match['Advice']}
59
+
60
  Provide additional insights or recommendations.
61
  """
62
 
63
+ # Generate advice with LLaMA
64
  message = [
65
  {"role": "system", "content": role},
66
  {"role": "user", "content": prompt},
67
  ]
68
 
69
  input_text = tokenizer.apply_chat_template(
70
+ message, tokenize=True, add_generation_prompt=True, return_tensors="pt"
71
  )
72
 
73
  output = llm.generate(
74
+ input_ids=input_text["input_ids"],
75
  max_length=150,
76
  num_return_sequences=1
77
  )
 
82
  return recommendations
83
 
84
  except Exception as e:
85
+ print(f"Error: {e}") # Debugging any unexpected issues
86
  return [{"error": f"Exception occurred: {str(e)}"}]
87
 
88
+
89
  # Extract structured data from the PDF
90
  def pdf_to_text(pdf_file):
91
  try: