Update app.py
Browse files
app.py
CHANGED
@@ -22,16 +22,19 @@ def translate_text(text, target_language="en"):
|
|
22 |
return result["translatedText"]
|
23 |
|
24 |
def process_image(file):
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
|
|
|
|
31 |
opts = documentai.ClientOptions(api_endpoint=f"{location}-documentai.googleapis.com")
|
32 |
client = documentai.DocumentProcessorServiceClient(client_options=opts)
|
33 |
-
|
34 |
-
|
|
|
35 |
name = client.processor_path(project_id, location, processor_id)
|
36 |
request = documentai.ProcessRequest(name=name, raw_document=raw_document)
|
37 |
result = client.process_document(request=request)
|
|
|
22 |
return result["translatedText"]
|
23 |
|
24 |
def process_image(file):
|
25 |
+
try:
|
26 |
+
# Process the document directly from the file-like object
|
27 |
+
extracted_text, translated_text = batch_process_documents(file, "image/jpeg")
|
28 |
+
return extracted_text, translated_text
|
29 |
+
except Exception as e:
|
30 |
+
return f"An error occurred: {str(e)}", ""
|
31 |
+
|
32 |
+
def batch_process_documents(file, file_mime_type: str) -> tuple:
|
33 |
opts = documentai.ClientOptions(api_endpoint=f"{location}-documentai.googleapis.com")
|
34 |
client = documentai.DocumentProcessorServiceClient(client_options=opts)
|
35 |
+
|
36 |
+
# Read the file content directly from the file-like object
|
37 |
+
raw_document = RawDocument(content=file.read(), mime_type=file_mime_type)
|
38 |
name = client.processor_path(project_id, location, processor_id)
|
39 |
request = documentai.ProcessRequest(name=name, raw_document=raw_document)
|
40 |
result = client.process_document(request=request)
|