Tanish28 commited on
Commit
ef28e79
·
verified ·
1 Parent(s): fe79fa6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -15
app.py CHANGED
@@ -31,9 +31,13 @@ class PDFTextExtractor:
31
  image.save(img_buffer, format='PNG')
32
  img_base64 = base64.b64encode(img_buffer.getvalue()).decode('utf-8')
33
 
34
- response = self.client.messages.create(
 
35
  model="gpt-4o",
36
- system="""You are a doctor at a hospital. You can understand sloppy handwriting and convert it to readable text. Extract all the data from the form according to the markdown structure given below.
 
 
 
37
  Follow this exact markdown structure:
38
  # PATIENT ADMISSION FORM
39
  ## DR.KAMAKSHI MEMORIAL HOSPITAL, PALLIKARANAI, CHENNAI.
@@ -119,9 +123,8 @@ class PDFTextExtractor:
119
 
120
  ---
121
  *Form No: KMHIPF002V3*
122
- """,
123
- max_tokens=4096,
124
- messages=[
125
  {
126
  "role": "user",
127
  "content": [
@@ -130,32 +133,32 @@ class PDFTextExtractor:
130
  "text": "Extract and format the Patient Admission Form from this image according to the specified markdown format. Even if the handwriting is sloppy, try to extract the text accurately. Preserve all form fields and checkboxes (as □)."
131
  },
132
  {
133
- "type": "image",
134
- "source": {
135
- "type": "base64",
136
- "media_type": "image/png",
137
- "data": img_base64
138
  }
139
  }
140
  ]
141
  }
142
- ]
 
143
  )
144
-
 
145
  extracted_texts.append({
146
  'page': i + 1,
147
- 'text': response.content[0].text
148
  })
149
 
150
  return extracted_texts
151
 
152
  except Exception as e:
153
  print(f"Error in text extraction: {str(e)}")
154
- return None
155
 
156
  def extract_text(pdf_file):
157
  if OPENAI_API_KEY is None:
158
- return "Error: OpenAI API key not found. Please set the ANTHROPIC_API_KEY environment variable."
159
 
160
  extractor = PDFTextExtractor(OPENAI_API_KEY)
161
 
@@ -163,6 +166,9 @@ def extract_text(pdf_file):
163
  extracted_texts = asyncio.run(extractor.extract_text_from_pdf(pdf_path))
164
 
165
  if extracted_texts:
 
 
 
166
  output = ""
167
  for page in extracted_texts:
168
  output += f"\n\n=== Page {page['page']} ===\n\n"
 
31
  image.save(img_buffer, format='PNG')
32
  img_base64 = base64.b64encode(img_buffer.getvalue()).decode('utf-8')
33
 
34
+ # Updated OpenAI API call using the correct format for newer SDK versions
35
+ response = self.client.chat.completions.create(
36
  model="gpt-4o",
37
+ messages=[
38
+ {
39
+ "role": "system",
40
+ "content": """You are a doctor at a hospital. You can understand sloppy handwriting and convert it to readable text. Extract all the data from the form according to the markdown structure given below.
41
  Follow this exact markdown structure:
42
  # PATIENT ADMISSION FORM
43
  ## DR.KAMAKSHI MEMORIAL HOSPITAL, PALLIKARANAI, CHENNAI.
 
123
 
124
  ---
125
  *Form No: KMHIPF002V3*
126
+ """
127
+ },
 
128
  {
129
  "role": "user",
130
  "content": [
 
133
  "text": "Extract and format the Patient Admission Form from this image according to the specified markdown format. Even if the handwriting is sloppy, try to extract the text accurately. Preserve all form fields and checkboxes (as □)."
134
  },
135
  {
136
+ "type": "image_url",
137
+ "image_url": {
138
+ "url": f"data:image/png;base64,{img_base64}"
 
 
139
  }
140
  }
141
  ]
142
  }
143
+ ],
144
+ max_tokens=4096
145
  )
146
+
147
+ # Updated response parsing for newer SDK versions
148
  extracted_texts.append({
149
  'page': i + 1,
150
+ 'text': response.choices[0].message.content
151
  })
152
 
153
  return extracted_texts
154
 
155
  except Exception as e:
156
  print(f"Error in text extraction: {str(e)}")
157
+ return [{'page': 0, 'text': f"Error: {str(e)}"}]
158
 
159
  def extract_text(pdf_file):
160
  if OPENAI_API_KEY is None:
161
+ return "Error: OpenAI API key not found. Please set the OPENAI_API_KEY environment variable."
162
 
163
  extractor = PDFTextExtractor(OPENAI_API_KEY)
164
 
 
166
  extracted_texts = asyncio.run(extractor.extract_text_from_pdf(pdf_path))
167
 
168
  if extracted_texts:
169
+ if extracted_texts[0].get('text', '').startswith('Error:'):
170
+ return extracted_texts[0]['text']
171
+
172
  output = ""
173
  for page in extracted_texts:
174
  output += f"\n\n=== Page {page['page']} ===\n\n"