File size: 852 Bytes
97208ad
 
830b723
97208ad
bba0bcf
97208ad
 
 
 
d587396
97208ad
d587396
 
97208ad
d587396
 
 
 
97208ad
 
 
 
 
 
 
 
 
 
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
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