vikaso001 commited on
Commit
0db5698
·
verified ·
1 Parent(s): f6556bd

Update rag_pipeline.py

Browse files
Files changed (1) hide show
  1. rag_pipeline.py +10 -15
rag_pipeline.py CHANGED
@@ -48,8 +48,10 @@ 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. If a value is missing in the prescription, use null.
52
 
 
 
 
53
  {{
54
  "Doctor Name": null,
55
  "Patient Name": null,
@@ -61,7 +63,9 @@ Return a valid JSON object with all the fields below. If a value is missing in t
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):
@@ -90,23 +94,14 @@ def extract_structured_data(pdf_file):
90
 
91
  print(f"\nGenerated Prompt:\n{prompt[:500]}...\n") # Debug
92
 
93
- result = qa_model(prompt, max_new_tokens=300)[0]['generated_text']
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
-
106
- # Extract valid JSON object
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:
 
48
  You are an AI that extracts structured data from prescriptions.
49
  Prescription text:
50
  \"\"\"{context[:1000]}\"\"\"
 
51
 
52
+ Return a valid JSON object between <json> and </json>. Use null for any missing values. Format:
53
+
54
+ <json>
55
  {{
56
  "Doctor Name": null,
57
  "Patient Name": null,
 
63
  }}
64
  ]
65
  }}
66
+ </json>
67
+
68
+ Only return the JSON block, nothing else.
69
  """
70
 
71
  def replace_empty_with_null(data):
 
94
 
95
  print(f"\nGenerated Prompt:\n{prompt[:500]}...\n") # Debug
96
 
97
+ result = qa_model(prompt, max_new_tokens=500)[0]['generated_text']
98
  print(f"Model Output:\n{result[:500]}...\n") # Debug
99
 
100
  try:
101
+ # Match JSON inside <json> ... </json>
102
+ json_match = re.search(r'<json>(.*?)</json>', result, re.DOTALL)
 
 
 
 
 
 
 
 
 
103
  if json_match:
104
+ cleaned_json = json_match.group(1).strip()
105
  parsed = json.loads(cleaned_json)
106
  return replace_empty_with_null(parsed)
107
  else: