tomofi commited on
Commit
669304b
·
1 Parent(s): c989abe

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -0
app.py ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import io
2
+ import gradio as gr
3
+ from google.oauth2 import service_account
4
+ from googleapiclient.discovery import build
5
+ from apiclient.http import MediaFileUpload, MediaIoBaseDownload
6
+
7
+ service = build('drive', 'v3', credentials=service_account.Credentials.from_service_account_file('hf-ocr-33c0cb785de5.json'))
8
+
9
+ def infer(im):
10
+ im.save('converted.png')
11
+ id = service.files().create(body={'mimeType': 'application/vnd.google-apps.document'}, media_body=MediaFileUpload('converted.png')).execute()['id']
12
+ with io.BytesIO() as f:
13
+ downloader = MediaIoBaseDownload(f, service.files().export_media(fileId=id, mimeType='text/plain'))
14
+ while downloader.next_chunk()[1] is False:
15
+ pass
16
+ service.files().delete(fileId=id).execute()
17
+ return f.getvalue().decode('UTF-8').replace('\ufeff________________\r\n\r\n', '')
18
+
19
+ iface = gr.Interface(
20
+ fn=infer,
21
+ title="Google Drive OCR",
22
+ description="When you convert an image to a Google doc, Drive uses Optical Character Recognition (OCR) to convert the image to text.",
23
+ inputs=[gr.inputs.Image(label='image', type='pil')],
24
+ outputs='text',
25
+ examples=[],
26
+ article="<a href=\"https://developers.google.com/drive/api/guides/manage-uploads#import_to_google_docs_types\">Docs</a>",
27
+ ).launch()