File size: 3,189 Bytes
61a4623 646fc74 bfb6745 2d06ce3 bfb6745 646fc74 bfb6745 2d06ce3 bfb6745 2d06ce3 bfb6745 2d06ce3 bfb6745 2d06ce3 bfb6745 2d06ce3 bfb6745 2d06ce3 bfb6745 2d06ce3 bfb6745 2d06ce3 bfb6745 2d06ce3 bfb6745 2d06ce3 bfb6745 2d06ce3 bfb6745 2d06ce3 bfb6745 2d06ce3 bfb6745 646fc74 bfb6745 61a4623 bfb6745 |
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 |
import gradio as gr
import cv2
import numpy as np
from src.ocr_service import DocumentOCRService
from src.document_config import HealthcareProcess, DocumentType
import json
# Initialize OCR service
ocr_service = DocumentOCRService()
def process_document(image, document_type, process_type):
"""Process document and return results."""
try:
# Convert image to numpy array
image_np = np.array(image)
# Convert to BGR for OpenCV
if len(image_np.shape) == 3 and image_np.shape[2] == 3:
image_np = cv2.cvtColor(image_np, cv2.COLOR_RGB2BGR)
# Process document
result = ocr_service.process_document(image_np, document_type, process_type)
# Format the output
output = {
"Extracted Data": result["extracted_data"],
"Gemini Analysis": result["gemini_analysis"]["analysis"],
"Validation Result": result["validation_result"]
}
return json.dumps(output, indent=2)
except Exception as e:
return f"Error processing document: {str(e)}"
def get_requirements(process_type):
"""Get document requirements for a process."""
try:
process = HealthcareProcess(process_type)
requirements = ocr_service.get_process_requirements(process)
return json.dumps(requirements, indent=2)
except ValueError as e:
return f"Error: {str(e)}"
# Create Gradio interface
with gr.Blocks(title="TakeCare OCR Service") as demo:
gr.Markdown("# TakeCare OCR Service")
gr.Markdown("Upload your healthcare documents for processing and validation.")
with gr.Tab("Process Document"):
with gr.Row():
with gr.Column():
image_input = gr.Image(type="pil", label="Upload Document")
document_type = gr.Dropdown(
choices=[dt.value for dt in DocumentType],
label="Document Type"
)
process_type = gr.Dropdown(
choices=[pt.value for pt in HealthcareProcess],
label="Process Type (Optional)"
)
process_btn = gr.Button("Process Document")
with gr.Column():
output = gr.Textbox(label="Results", lines=20)
process_btn.click(
fn=process_document,
inputs=[image_input, document_type, process_type],
outputs=output
)
with gr.Tab("View Requirements"):
with gr.Row():
with gr.Column():
req_process_type = gr.Dropdown(
choices=[pt.value for pt in HealthcareProcess],
label="Select Process Type"
)
view_req_btn = gr.Button("View Requirements")
with gr.Column():
requirements_output = gr.Textbox(label="Document Requirements", lines=20)
view_req_btn.click(
fn=get_requirements,
inputs=[req_process_type],
outputs=requirements_output
)
# Launch the app
if __name__ == "__main__":
demo.launch() |