Skip to content

Commit

Permalink
feat: valid errors messages added
Browse files Browse the repository at this point in the history
  • Loading branch information
AshishBarvaliya committed Jan 10, 2024
1 parent c8c7f15 commit 4941139
Showing 1 changed file with 26 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,35 +57,53 @@ def ask_gpt(self, prompt):

def extract_text_from_img(self, img_url):
'''Extracts and returns first_name, middle_name, last_name, gender, salutation, designation contact_numbers, email_ids, company_name, website, address, mobile_number, phone_number, city, state and country from an image given the image URL'''
texts = None
project_id = self.get_vision_project_id()
private_key = self.get_vision_private_key().strip().replace('\\n', '\n')
client_email = self.get_vision_client_email()
token_uri = self.get_vision_token_uri()

if not project_id or not private_key or not client_email or not token_uri:
return frappe.throw("Missing required fields in AutoInscribe Settings")

response = requests.get(img_url)

if response.status_code != 200:
if response.status_code == 403:
return frappe.throw("You don't have permission to access this file. Make sure you upload file with public access.")
else:
return frappe.throw("Failed to fetch image from URL")

try:
credentials = service_account.Credentials.from_service_account_info({
"type": "service_account",
"project_id": self.get_vision_project_id(),
"private_key": self.get_vision_private_key().strip().replace('\\n', '\n'),
"client_email": self.get_vision_client_email(),
"token_uri": self.get_vision_token_uri(),
"project_id": project_id,
"private_key": private_key,
"client_email": client_email,
"token_uri": token_uri,
})
response = requests.get(img_url)

client = vision.ImageAnnotatorClient(credentials=credentials)

# Encode the image content to base64
base64_img = base64.b64encode(response.content).decode('utf-8')
client = vision.ImageAnnotatorClient(credentials=credentials)
img_data = base64.b64decode(base64_img)
# Create an image object
image = vision.Image(content=img_data)
# Perform OCR on the image
response = client.text_detection(image=image)
texts = response.text_annotations
except Exception as e:
frappe.throw("Please check your AutoInscribe Settings and try again")
return frappe.throw("Invalid Google Vision credentials. Please check your AutoInscribe Settings and try again")

# Extracting detected text
if texts:
detected_text = texts[0].description
prompt = f"From the following text, identify the first_name, middle_name, last_name, gender, salutation, designation contact_numbers, email_ids, company_name, website, address, mobile_number, phone_number, city, state, country: {detected_text}. Output must be a string containing one key-value pair per line and for absence of values use 'NULL' for value as placeholder. contact_numbers and email_ids must be comma-separated if there are multiple. Guess the salutation and gender. gender can be Male, Female, Transgender or Other. phone_number must be the telephone number whereas mobile_number must be the mobile number. country must have the value as full country name, e.g, US becomes United States, UK becomes United Kingdom."
reply = self.ask_gpt(prompt)
return reply
else:
return "No text detected"
return frappe.throw("No information extracted from image. Please try again with a different image")

def create_address(self, address):
'''Given an address string, extract city, state, postal_code, country and create an address if country exists & return the inserted doc. Return None otherwise.'''
Expand Down

0 comments on commit 4941139

Please sign in to comment.