Spaces:
Running
Running
import gradio as gr | |
import google.generativeai as genai | |
import base64 | |
import io | |
from PIL import Image | |
import os | |
import json | |
# Configure Google Cloud credentials (replace with your actual API key or setup) | |
genai.configure(api_key=os.getenv("GOOGLE_API_KEY")) | |
# Select the Gemini Pro Vision model | |
model = genai.GenerativeModel('gemini-1.5-flash') | |
# Prompt definition | |
prompt = """ | |
You are analyzing a medical document or an application form from 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 | |
- Phone number (mobile) | |
""" | |
def process_image(image: Image.Image): | |
buffered = io.BytesIO() | |
image.save(buffered, format="JPEG") | |
base64_image = buffered.getvalue() | |
response = model.generate_content([ | |
prompt, | |
{ | |
"mime_type": "image/jpeg", | |
"data": base64_image | |
} | |
]) | |
return response.text | |
# Gradio interface | |
demo = gr.Interface( | |
fn=process_image, | |
inputs=gr.Image(type="pil"), | |
outputs="textbox", | |
title="Healthelic Form Data Extractor (Doc Scanner) - Gemini 1.5-flash", | |
description="Upload a scanned medical form to extract key fields." | |
) | |
if __name__ == "__main__": | |
demo.launch() |