Update app.py
#1
by
mkaramb
- opened
app.py
CHANGED
@@ -1,8 +1,56 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
-
iface = gr.Interface(fn=greet, inputs="text", outputs="text")
|
7 |
iface.launch()
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import os
|
3 |
+
import pandas as pd
|
4 |
+
from google.cloud import documentai_v1 as documentai
|
5 |
+
from google.cloud.documentai_v1.types import RawDocument
|
6 |
+
from google.cloud import translate_v2 as translate
|
7 |
+
import zipfile
|
8 |
+
import io
|
9 |
|
10 |
+
# Assuming GOOGLE_APPLICATION_CREDENTIALS is set in your environment
|
11 |
+
# Set your Google Cloud Document AI processor details here
|
12 |
+
project_id = "herbaria-ai"
|
13 |
+
location = "us"
|
14 |
+
processor_id = "4307b078717a399a"
|
15 |
+
|
16 |
+
def translate_text(text, target_language="en"):
|
17 |
+
translate_client = translate.Client()
|
18 |
+
result = translate_client.translate(text, target_language=target_language)
|
19 |
+
return result["translatedText"]
|
20 |
+
|
21 |
+
def process_image(file):
|
22 |
+
file_path = file.name
|
23 |
+
file.save(file_path) # Save the file so we can open and read it later
|
24 |
+
extracted_text, translated_text = batch_process_documents(file_path, "image/jpeg")
|
25 |
+
return extracted_text, translated_text
|
26 |
+
|
27 |
+
def batch_process_documents(file_path: str, file_mime_type: str) -> tuple:
|
28 |
+
opts = documentai.ClientOptions(api_endpoint=f"{location}-documentai.googleapis.com")
|
29 |
+
client = documentai.DocumentProcessorServiceClient(client_options=opts)
|
30 |
+
with open(file_path, "rb") as file_stream:
|
31 |
+
raw_document = RawDocument(content=file_stream.read(), mime_type=file_mime_type)
|
32 |
+
name = client.processor_path(project_id, location, processor_id)
|
33 |
+
request = documentai.ProcessRequest(name=name, raw_document=raw_document)
|
34 |
+
result = client.process_document(request=request)
|
35 |
+
extracted_text = result.document.text
|
36 |
+
translated_text = translate_text(extracted_text)
|
37 |
+
return extracted_text, translated_text
|
38 |
+
|
39 |
+
iface = gr.Interface(
|
40 |
+
fn=process_image,
|
41 |
+
inputs=gr.inputs.File(label="Upload Image File"),
|
42 |
+
outputs=[
|
43 |
+
gr.outputs.Textbox(label="Extracted Text"),
|
44 |
+
gr.outputs.Textbox(label="Translated Text")
|
45 |
+
]
|
46 |
+
)
|
47 |
|
|
|
48 |
iface.launch()
|
49 |
|
50 |
+
|
51 |
+
# def greet(name):
|
52 |
+
# return "Hello " + name + "!!"
|
53 |
+
|
54 |
+
#iface = gr.Interface(fn=greet, inputs="text", outputs="text")
|
55 |
+
#iface.launch()
|
56 |
+
|