mkaramb commited on
Commit
256319b
·
verified ·
1 Parent(s): 24cff5f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -8
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
- file_path = file.name
26
- file.save(file_path) # Save the file so we can open and read it later
27
- extracted_text, translated_text = batch_process_documents(file_path, "image/jpeg")
28
- return extracted_text, translated_text
29
-
30
- def batch_process_documents(file_path: str, file_mime_type: str) -> tuple:
 
 
31
  opts = documentai.ClientOptions(api_endpoint=f"{location}-documentai.googleapis.com")
32
  client = documentai.DocumentProcessorServiceClient(client_options=opts)
33
- with open(file_path, "rb") as file_stream:
34
- raw_document = RawDocument(content=file_stream.read(), mime_type=file_mime_type)
 
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)