|
from flask import Flask, request, jsonify |
|
import gradio as gr |
|
import pytesseract |
|
from google.oauth2 import service_account |
|
from googleapiclient.discovery import build |
|
|
|
app = Flask(__name__) |
|
|
|
|
|
SCOPES = ['https://www.googleapis.com/auth/script.projects'] |
|
SERVICE_ACCOUNT_FILE = 'service_account_key.json' |
|
|
|
|
|
creds = service_account.Credentials.from_service_account_file( |
|
SERVICE_ACCOUNT_FILE, SCOPES=SCOPES) |
|
|
|
|
|
script_service = build('script', 'v1', credentials=creds) |
|
|
|
|
|
iface = gr.Interface( |
|
fn=lambda img: ocr(img), |
|
inputs="image", |
|
outputs="text", |
|
title="OCR App", |
|
description="Upload an image to extract text" |
|
) |
|
|
|
@app.route('/ocr', methods=['POST']) |
|
def ocr(img): |
|
|
|
text = pytesseract.image_to_string(img) |
|
return text |
|
|
|
@app.route('/google_chat_insert', methods=['POST']) |
|
def google_chat_insert(text): |
|
|
|
script_service.scripts().run(body={'function': 'insertText', 'parameters': [text]}).execute() |
|
return 'Text inserted into Google Chat' |
|
|
|
if __name__ == '__main__': |
|
iface.launch() |
|
app.run(debug=True) |