Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,13 @@ from transformers import LayoutLMForTokenClassification, LayoutLMTokenizer
|
|
| 2 |
import gradio as gr
|
| 3 |
import cv2
|
| 4 |
import easyocr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# Initialize EasyOCR reader for text extraction
|
| 7 |
reader = easyocr.Reader(['en'])
|
|
@@ -10,6 +17,9 @@ reader = easyocr.Reader(['en'])
|
|
| 10 |
model = LayoutLMForTokenClassification.from_pretrained("microsoft/layoutlm-large-uncased")
|
| 11 |
tokenizer = LayoutLMTokenizer.from_pretrained("microsoft/layoutlm-large-uncased")
|
| 12 |
|
|
|
|
|
|
|
|
|
|
| 13 |
# Function to extract text using EasyOCR and process with LayoutLM
|
| 14 |
def extract_patient_info(image):
|
| 15 |
# Convert the uploaded image to RGB (required by LayoutLM)
|
|
@@ -23,12 +33,42 @@ def extract_patient_info(image):
|
|
| 23 |
inputs = tokenizer(extracted_text, return_tensors="pt")
|
| 24 |
outputs = model(**inputs)
|
| 25 |
|
| 26 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
return extracted_text
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
# Gradio interface setup
|
| 30 |
with gr.Blocks() as demo:
|
| 31 |
-
gr.Markdown("### OCR Using LayoutLM Pretrained Model with EasyOCR")
|
| 32 |
|
| 33 |
# Image upload component
|
| 34 |
image_input = gr.Image(type="numpy", label="Upload Image")
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import cv2
|
| 4 |
import easyocr
|
| 5 |
+
from simple_salesforce import Salesforce
|
| 6 |
+
|
| 7 |
+
# Salesforce credentials
|
| 8 |
+
Salesforce_User_Name = 'sathkruthatech@hms.com' # Your Salesforce username
|
| 9 |
+
Salesforce_Password = 'Sathkrutha@06'
|
| 10 |
+
SALESFORCE_INSTANCE_URL = 'https://sathkruthatechsolutions63-dev-ed.develop.lightning.force.com'
|
| 11 |
+
SALESFORCE_ACCESS_TOKEN = 'UnByPih7PWmoWLzRuRyFrXzw'
|
| 12 |
|
| 13 |
# Initialize EasyOCR reader for text extraction
|
| 14 |
reader = easyocr.Reader(['en'])
|
|
|
|
| 17 |
model = LayoutLMForTokenClassification.from_pretrained("microsoft/layoutlm-large-uncased")
|
| 18 |
tokenizer = LayoutLMTokenizer.from_pretrained("microsoft/layoutlm-large-uncased")
|
| 19 |
|
| 20 |
+
# Salesforce Connection Setup
|
| 21 |
+
sf = Salesforce(username=Salesforce_User_Name, password=Salesforce_Password, security_token=SALESFORCE_ACCESS_TOKEN)
|
| 22 |
+
|
| 23 |
# Function to extract text using EasyOCR and process with LayoutLM
|
| 24 |
def extract_patient_info(image):
|
| 25 |
# Convert the uploaded image to RGB (required by LayoutLM)
|
|
|
|
| 33 |
inputs = tokenizer(extracted_text, return_tensors="pt")
|
| 34 |
outputs = model(**inputs)
|
| 35 |
|
| 36 |
+
# Here, extracted_text is already available from EasyOCR, we can extract relevant details
|
| 37 |
+
details = extract_details_from_text(extracted_text)
|
| 38 |
+
|
| 39 |
+
# Create a record in Salesforce using the extracted details
|
| 40 |
+
create_salesforce_record(details)
|
| 41 |
+
|
| 42 |
+
# Return the extracted text
|
| 43 |
return extracted_text
|
| 44 |
|
| 45 |
+
# Function to extract details from the extracted text (use regex or other methods to extract)
|
| 46 |
+
def extract_details_from_text(extracted_text):
|
| 47 |
+
# Simple example of extracting details, customize this according to the format of the text
|
| 48 |
+
details = {}
|
| 49 |
+
details['Name'] = "Shanthi" # Here, add the logic to extract the actual name
|
| 50 |
+
details['Age'] = "39" # Similarly, extract age, gender, and phone number
|
| 51 |
+
details['Gender'] = "Female"
|
| 52 |
+
details['Phone Number'] = "9955337097"
|
| 53 |
+
|
| 54 |
+
return details
|
| 55 |
+
|
| 56 |
+
# Function to create a record in Salesforce
|
| 57 |
+
def create_salesforce_record(details):
|
| 58 |
+
data = {
|
| 59 |
+
'Name__c': details['Name'],
|
| 60 |
+
'Age__c': int(details['Age']),
|
| 61 |
+
'Gender__c': details['Gender'],
|
| 62 |
+
'Phone_Number__c': details['Phone Number']
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
# Create a new record in Salesforce
|
| 66 |
+
sf.Patient_Registration__c.create(data)
|
| 67 |
+
print("Salesforce record created successfully!")
|
| 68 |
+
|
| 69 |
# Gradio interface setup
|
| 70 |
with gr.Blocks() as demo:
|
| 71 |
+
gr.Markdown("### OCR Using LayoutLM Pretrained Model with EasyOCR and Salesforce Integration")
|
| 72 |
|
| 73 |
# Image upload component
|
| 74 |
image_input = gr.Image(type="numpy", label="Upload Image")
|