vikaso001 commited on
Commit
f6556bd
·
verified ·
1 Parent(s): 4e1e8bf

Update rag_pipeline.py

Browse files
Files changed (1) hide show
  1. rag_pipeline.py +22 -11
rag_pipeline.py CHANGED
@@ -48,22 +48,32 @@ def generate_prompt(context):
48
  You are an AI that extracts structured data from prescriptions.
49
  Prescription text:
50
  \"\"\"{context[:1000]}\"\"\"
51
- Return a valid JSON object with all the fields below, even if some values are empty. If a value is missing in the prescription, use an empty string ("") or empty list ([]) as appropriate.
52
 
53
  {{
54
- "Doctor Name": "",
55
- "Patient Name": "",
56
- "Date": "",
57
  "Medications": [
58
  {{
59
- "name": "",
60
- "dosage": ""
61
  }}
62
  ]
63
  }}
64
  Strictly output only valid JSON. Do not include any commentary, markdown, or extra characters.
65
  """
66
 
 
 
 
 
 
 
 
 
 
 
67
  def extract_structured_data(pdf_file):
68
  text = extract_text_from_pdf(pdf_file)
69
 
@@ -84,12 +94,12 @@ def extract_structured_data(pdf_file):
84
  print(f"Model Output:\n{result[:500]}...\n") # Debug
85
 
86
  try:
87
- # Fail-safe: model returned just []
88
  if result.strip() == "[]":
89
  return {
90
- "Doctor Name": "",
91
- "Patient Name": "",
92
- "Date": "",
93
  "Medications": []
94
  }
95
 
@@ -97,7 +107,8 @@ def extract_structured_data(pdf_file):
97
  json_match = re.search(r'{.*}', result, re.DOTALL)
98
  if json_match:
99
  cleaned_json = json_match.group(0)
100
- return json.loads(cleaned_json)
 
101
  else:
102
  raise ValueError("No valid JSON found.")
103
  except Exception as e:
 
48
  You are an AI that extracts structured data from prescriptions.
49
  Prescription text:
50
  \"\"\"{context[:1000]}\"\"\"
51
+ Return a valid JSON object with all the fields below. If a value is missing in the prescription, use null.
52
 
53
  {{
54
+ "Doctor Name": null,
55
+ "Patient Name": null,
56
+ "Date": null,
57
  "Medications": [
58
  {{
59
+ "name": null,
60
+ "dosage": null
61
  }}
62
  ]
63
  }}
64
  Strictly output only valid JSON. Do not include any commentary, markdown, or extra characters.
65
  """
66
 
67
+ def replace_empty_with_null(data):
68
+ if isinstance(data, dict):
69
+ return {k: replace_empty_with_null(v) for k, v in data.items()}
70
+ elif isinstance(data, list):
71
+ return [replace_empty_with_null(v) for v in data]
72
+ elif data in ("", [], {}):
73
+ return None
74
+ else:
75
+ return data
76
+
77
  def extract_structured_data(pdf_file):
78
  text = extract_text_from_pdf(pdf_file)
79
 
 
94
  print(f"Model Output:\n{result[:500]}...\n") # Debug
95
 
96
  try:
97
+ # Handle special case where model outputs just []
98
  if result.strip() == "[]":
99
  return {
100
+ "Doctor Name": None,
101
+ "Patient Name": None,
102
+ "Date": None,
103
  "Medications": []
104
  }
105
 
 
107
  json_match = re.search(r'{.*}', result, re.DOTALL)
108
  if json_match:
109
  cleaned_json = json_match.group(0)
110
+ parsed = json.loads(cleaned_json)
111
+ return replace_empty_with_null(parsed)
112
  else:
113
  raise ValueError("No valid JSON found.")
114
  except Exception as e: