Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -3,12 +3,13 @@ import openai
|
|
3 |
import base64
|
4 |
import io
|
5 |
from PIL import Image
|
|
|
6 |
import os
|
7 |
|
8 |
-
#
|
9 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
10 |
|
11 |
-
# Prompt
|
12 |
prompt = """
|
13 |
You are analyzing a medical document or an application form from a patient.
|
14 |
Extract the following fields as JSON:
|
@@ -26,31 +27,45 @@ Extract the following fields as JSON:
|
|
26 |
- Phone number (mobile)
|
27 |
"""
|
28 |
|
29 |
-
def
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
demo = gr.Interface(
|
49 |
-
fn=
|
50 |
-
inputs=gr.
|
51 |
outputs="textbox",
|
52 |
-
title="Healthelic Form Data Extractor (
|
53 |
-
description="Upload a scanned medical form to extract key fields."
|
54 |
)
|
55 |
|
56 |
if __name__ == "__main__":
|
|
|
3 |
import base64
|
4 |
import io
|
5 |
from PIL import Image
|
6 |
+
import fitz # PyMuPDF
|
7 |
import os
|
8 |
|
9 |
+
# Load API key
|
10 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
11 |
|
12 |
+
# Prompt for extraction
|
13 |
prompt = """
|
14 |
You are analyzing a medical document or an application form from a patient.
|
15 |
Extract the following fields as JSON:
|
|
|
27 |
- Phone number (mobile)
|
28 |
"""
|
29 |
|
30 |
+
def process_pdf(pdf_file):
|
31 |
+
doc = fitz.open(stream=pdf_file.read(), filetype="pdf")
|
32 |
+
results = []
|
33 |
+
|
34 |
+
for page_num in range(len(doc)):
|
35 |
+
page = doc.load_page(page_num)
|
36 |
+
pix = page.get_pixmap(dpi=200) # Adjust DPI if needed
|
37 |
+
|
38 |
+
# Convert pixmap to PIL Image
|
39 |
+
image = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
|
40 |
+
|
41 |
+
# Convert to base64 JPEG
|
42 |
+
buffered = io.BytesIO()
|
43 |
+
image.save(buffered, format="JPEG")
|
44 |
+
base64_image = base64.b64encode(buffered.getvalue()).decode()
|
45 |
+
|
46 |
+
# Send to GPT-4o
|
47 |
+
response = openai.chat.completions.create(
|
48 |
+
model="gpt-4o",
|
49 |
+
messages=[
|
50 |
+
{"role": "user", "content": [
|
51 |
+
{"type": "text", "text": prompt},
|
52 |
+
{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{base64_image}"}}
|
53 |
+
]}
|
54 |
+
],
|
55 |
+
max_tokens=1000
|
56 |
+
)
|
57 |
+
|
58 |
+
results.append(response.choices[0].message.content.strip())
|
59 |
+
|
60 |
+
return "\n\n---\n\n".join(results)
|
61 |
+
|
62 |
+
# Gradio UI
|
63 |
demo = gr.Interface(
|
64 |
+
fn=process_pdf,
|
65 |
+
inputs=gr.File(type="binary", label="Upload PDF Form"),
|
66 |
outputs="textbox",
|
67 |
+
title="Healthelic Form Data Extractor (PDF Scanner) - OpenAI GPT-4o",
|
68 |
+
description="Upload a scanned medical form in PDF format to extract key fields using GPT-4o vision model."
|
69 |
)
|
70 |
|
71 |
if __name__ == "__main__":
|