doc-scan-openai / app.py
neuralleap's picture
Update app.py
3b2d928 verified
raw
history blame
1.4 kB
import gradio as gr
import openai
import base64
import io
from PIL import Image
import os
# Use Hugging Face Secrets to hide API Key
openai.api_key = os.getenv("OPENAI_API_KEY")
# Prompt definition
prompt = """
You are analyzing a medical document or an application form from a patient.
Extract the following fields as JSON:
- Position applied for
- Office/Ministry
- Duty station
- First name(s)
- Surname
- Date of birth
- Gender
- Citizenship
- Postal Address
- Residential Address
- Email
- Phone number (mobile)
"""
def process_image(image: Image.Image):
buffered = io.BytesIO()
image.save(buffered, format="JPEG")
base64_image = base64.b64encode(buffered.getvalue()).decode()
response = openai.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "user", "content": [
{"type": "text", "text": prompt},
{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{base64_image}"}}
]}
],
max_tokens=1000
)
return response.choices[0].message.content
# Gradio interface
demo = gr.Interface(
fn=process_image,
inputs=gr.Image(type="pil"),
outputs="textbox",
title="Healthelic Form Data Extractor (Doc Scanner) - OpenAI gpt 4o",
description="Upload a scanned medical form to extract key fields."
)
if __name__ == "__main__":
demo.launch()