File size: 2,199 Bytes
5884368 18cb325 7815f1e 18cb325 5884368 a1491f4 18cb325 256319b 18cb325 256319b 18cb325 5884368 18cb325 |
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
import gradio as gr
import os
import pandas as pd
from google.cloud import documentai_v1 as documentai
from google.cloud.documentai_v1.types import RawDocument
from google.cloud import translate_v2 as translate
from google.api_core.client_options import ClientOptions
import zipfile
import io
import os
# Upload credential json file from default compute service account
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "herbaria-ai-3c860bcb0f44.json"
# Set your Google Cloud Document AI processor details here
project_id = "herbaria-ai"
location = "us"
processor_id = "4307b078717a399a"
def translate_text(text, target_language="en"):
translate_client = translate.Client()
result = translate_client.translate(text, target_language=target_language)
return result["translatedText"]
def process_image(file):
try:
# Process the document directly from the file-like object
extracted_text, translated_text = batch_process_documents(file, "image/jpeg")
return extracted_text, translated_text
except Exception as e:
return f"An error occurred: {str(e)}", ""
def batch_process_documents(file, file_mime_type: str) -> tuple:
opts = documentai.ClientOptions(api_endpoint=f"{location}-documentai.googleapis.com")
client = documentai.DocumentProcessorServiceClient(client_options=opts)
# Read the file content directly from the file-like object
raw_document = RawDocument(content=file.read(), mime_type=file_mime_type)
name = client.processor_path(project_id, location, processor_id)
request = documentai.ProcessRequest(name=name, raw_document=raw_document)
result = client.process_document(request=request)
extracted_text = result.document.text
translated_text = translate_text(extracted_text)
return extracted_text, translated_text
iface = gr.Interface(
fn=process_image,
inputs=gr.inputs.File(label="Upload Image File"),
outputs=[
gr.outputs.Textbox(label="Extracted Text"),
gr.outputs.Textbox(label="Translated Text")
]
)
iface.launch()
# def greet(name):
# return "Hello " + name + "!!"
#iface = gr.Interface(fn=greet, inputs="text", outputs="text")
#iface.launch()
|