Spaces:
Sleeping
Sleeping
| import os | |
| import io | |
| import json | |
| from dotenv import load_dotenv | |
| from google.oauth2 import service_account | |
| from google.cloud import vision | |
| load_dotenv() | |
| google_creds_json = os.getenv('GOOGLE_CLOUD_CREDENTIALS') | |
| if google_creds_json is None: | |
| raise EnvironmentError("GOOGLE_CLOUD_CREDENTIALS not set in Hugging Face Secrets") | |
| credentials_dict = json.loads(google_creds_json) | |
| credentials = service_account.Credentials.from_service_account_info(credentials_dict) | |
| client = vision.ImageAnnotatorClient(credentials=credentials) | |
| def google_cloud_ocr(tiff_file): | |
| content = tiff_file.read() | |
| image = vision.Image(content=content) | |
| response = client.document_text_detection(image=image) | |
| text = response.full_text_annotation.text | |
| if response.error.message: | |
| raise Exception(f'API Error: {response.error.message}') | |
| return text | |