File size: 1,283 Bytes
ee3a6ac
9df0420
ee3a6ac
 
 
 
 
 
9df0420
ee3a6ac
9df0420
ee3a6ac
 
 
9df0420
ee3a6ac
9df0420
ee3a6ac
 
9df0420
 
 
 
 
 
 
 
ee3a6ac
9df0420
 
ee3a6ac
 
9df0420
ee3a6ac
9df0420
 
 
 
 
ee3a6ac
 
9df0420
ee3a6ac
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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__)

# Google Apps Script API credentials
SCOPES = ['https://www.googleapis.com/auth/script.projects']
SERVICE_ACCOUNT_FILE = 'service_account_key.json'

# Load credentials from service account file
creds = service_account.Credentials.from_service_account_file(
    SERVICE_ACCOUNT_FILE, SCOPES=SCOPES)

# Create Google Apps Script API client
script_service = build('script', 'v1', credentials=creds)

# Create Gradio interface
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):
    # Perform OCR using Tesseract
    text = pytesseract.image_to_string(img)
    return text

@app.route('/google_chat_insert', methods=['POST'])
def google_chat_insert(text):
    # Insert text into Google Chat using Google Apps Script API
    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)