File size: 7,343 Bytes
573a310 8c1fd84 573a310 339a825 573a310 8c1fd84 6d8c6b0 8c1fd84 573a310 6d8c6b0 339a825 573a310 6d8c6b0 8c1fd84 5495c4e 6d8c6b0 8c1fd84 95dfd20 573a310 6d8c6b0 95dfd20 ef28e79 339a825 ef28e79 95dfd20 8083d4b 95dfd20 8083d4b 95dfd20 8083d4b ef28e79 95dfd20 ef28e79 f4d294d 95dfd20 ef28e79 95dfd20 ef28e79 95dfd20 ef28e79 95dfd20 278e5bb d90a44b f4d294d c0922ce ef28e79 011d7c9 6d8c6b0 339a825 ef28e79 573a310 5495c4e 6d8c6b0 95dfd20 ef28e79 95dfd20 8c1fd84 95dfd20 011d7c9 8c1fd84 6d8c6b0 fe79fa6 8c1fd84 6d8c6b0 ea8ffe4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
import os
import io
from pdf2image import convert_from_path
from openai import OpenAI
import base64
import asyncio
from datetime import datetime
import gradio as gr
# We'll use an environment variable for the API key in Spaces
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
class PDFTextExtractor:
def __init__(self, api_key):
self.client = OpenAI(api_key=api_key)
async def extract_text_from_pdf(self, pdf_path):
try:
if not os.path.exists(pdf_path):
raise FileNotFoundError(f"PDF file not found: {pdf_path}")
print(f"Processing PDF: {pdf_path}")
images = convert_from_path(pdf_path)
extracted_texts = []
for i, image in enumerate(images):
print(f"Processing page {i+1}...")
img_buffer = io.BytesIO()
image.save(img_buffer, format='PNG')
img_base64 = base64.b64encode(img_buffer.getvalue()).decode('utf-8')
# Updated OpenAI API call using the correct format for newer SDK versions
response = self.client.chat.completions.create(
model="gpt-4o",
messages=[
{
"role": "system",
"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.
Follow this exact markdown structure:
# ER - DOCTORS INITIAL ASSESSMENT FORM
## DR.KAMAKSHI MEMORIAL HOSPITAL, PALLIKARANAI, CHENNAI.
### PATIENT PROFILE
*Please paste the sticker within the box*
* UHID: ______
* Date: [DD/MM/YYYY]
* Patient Name: ______
* Age/Gender: ______
* Doctor Name: ______
### CASE INFORMATION
* MLC: □ No □ Yes, AR No.: ________________
* Information Provided By: □ Self □ Care Taker
* Ambulation: □ Walking □ Wheelchair □ Stretcher
* Triage Code: □ Red □ Orange □ Yellow □ Green □ Blue
### ALLERGIES / INTOLERANCES
* □ Nil Known / □ Yes (See below)
* Drug & description of allergy / intolerance: ________________
### CHIEF COMPLAINTS:
________________
________________
________________
### ASSESSMENT
* Pain Score: ______ □ NRS □ NPRS-R
### PAST MEDICAL HISTORY:
* □ SHTN □ DM □ CAD □ CKD □ STROKE □ ASTHMA □ COPD □ SEIZURE □ HYPOTHYROIDISM
* □ OTHERS: ________________
### PAST SURGERIES:
________________
________________
### CURRENT MEDICATIONS:
* □ Regular □ Irregular □ Nil □ AYUSH: ________________
### GENERAL EXAMINATION
* Weight: ______ kgs
* CBG: ______ mg/dL
* □ Pallor □ Icterus □ Cyanosis □ Clubbing □ Lymphadenopathy □ Edema
### VITALS
* Temp: ______ °F
* BP: ______/______ mmHg
* HR: ______/min
* RR: ______/min
* SpO₂: ______%
* NEWS: ______
### SYSTEMIC EXAMINATION
* RS: ________________
* CVS: ________________
* P/A: ________________
* CNS: ________________
* GCS: E____ V____ M____
* Local O/E: ________________
*Form No: KMHPF190V1*
"""
},
{
"role": "user",
"content": [
{
"type": "text",
"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 □)."
},
{
"type": "image_url",
"image_url": {
"url": f"data:image/png;base64,{img_base64}"
}
}
]
}
],
max_tokens=4096
)
# Updated response parsing for newer SDK versions
extracted_texts.append({
'page': i + 1,
'text': response.choices[0].message.content
})
return extracted_texts
except Exception as e:
print(f"Error in text extraction: {str(e)}")
return [{'page': 0, 'text': f"Error: {str(e)}"}]
def extract_text(pdf_file):
if OPENAI_API_KEY is None:
return "Error: OpenAI API key not found. Please set the OPENAI_API_KEY environment variable."
extractor = PDFTextExtractor(OPENAI_API_KEY)
pdf_path = pdf_file.name
extracted_texts = asyncio.run(extractor.extract_text_from_pdf(pdf_path))
if extracted_texts:
if extracted_texts[0].get('text', '').startswith('Error:'):
return extracted_texts[0]['text']
output = ""
for page in extracted_texts:
output += f"\n\n=== Page {page['page']} ===\n\n"
output += page['text']
return output
else:
return "Failed to extract text from PDF"
iface = gr.Interface(
fn=extract_text,
inputs=gr.File(label="Upload PDF"),
outputs="text",
title="PDF Text Extractor",
description="Upload a PDF file to extract text using OpenAI's GPT 4o model."
)
iface.launch() |