Update app.py
Browse files
app.py
CHANGED
@@ -7,9 +7,19 @@ import zipfile
|
|
7 |
import os
|
8 |
import io
|
9 |
import gradio as gr
|
|
|
|
|
10 |
|
11 |
# Upload credential json file from default compute service account
|
12 |
-
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "herbaria-ai-3c860bcb0f44.json"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
# Global DataFrame declaration
|
15 |
results_df = pd.DataFrame(columns=["Filename", "Extracted Text", "Translated Text"])
|
@@ -20,13 +30,17 @@ location = "us"
|
|
20 |
processor_id = "4307b078717a399a"
|
21 |
|
22 |
def translate_text(text, target_language="en"):
|
23 |
-
translate_client = translate.Client()
|
24 |
result = translate_client.translate(text, target_language=target_language)
|
25 |
return result["translatedText"]
|
26 |
|
27 |
def batch_process_documents(file_path: str, file_mime_type: str) -> tuple:
|
28 |
-
opts = ClientOptions(api_endpoint=f"{location}-documentai.googleapis.com")
|
29 |
-
client
|
|
|
|
|
|
|
|
|
30 |
|
31 |
with open(file_path, "rb") as file_stream:
|
32 |
raw_document = RawDocument(content=file_stream.read(), mime_type=file_mime_type)
|
|
|
7 |
import os
|
8 |
import io
|
9 |
import gradio as gr
|
10 |
+
import json
|
11 |
+
from google.oauth2 import service_account
|
12 |
|
13 |
# Upload credential json file from default compute service account
|
14 |
+
#os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "herbaria-ai-3c860bcb0f44.json"
|
15 |
+
|
16 |
+
credentials_raw = os.environ.get("google_authentication")
|
17 |
+
|
18 |
+
# Parse the JSON credentials
|
19 |
+
credentials_json = json.loads(credentials_raw)
|
20 |
+
|
21 |
+
# Use the parsed credentials to create a service account
|
22 |
+
credentials = service_account.Credentials.from_service_account_info(credentials_json)
|
23 |
|
24 |
# Global DataFrame declaration
|
25 |
results_df = pd.DataFrame(columns=["Filename", "Extracted Text", "Translated Text"])
|
|
|
30 |
processor_id = "4307b078717a399a"
|
31 |
|
32 |
def translate_text(text, target_language="en"):
|
33 |
+
translate_client = translate.Client(credentials=credenentials)
|
34 |
result = translate_client.translate(text, target_language=target_language)
|
35 |
return result["translatedText"]
|
36 |
|
37 |
def batch_process_documents(file_path: str, file_mime_type: str) -> tuple:
|
38 |
+
#opts = ClientOptions(api_endpoint=f"{location}-documentai.googleapis.com")
|
39 |
+
# Initialize Document AI client with credentials
|
40 |
+
client_options = {'credentials': credentials}
|
41 |
+
|
42 |
+
client = documentai.DocumentProcessorServiceClient(client_options=client_options)
|
43 |
+
#client = documentai.DocumentProcessorServiceClient(client_options=opts)
|
44 |
|
45 |
with open(file_path, "rb") as file_stream:
|
46 |
raw_document = RawDocument(content=file_stream.read(), mime_type=file_mime_type)
|