File size: 1,285 Bytes
d0dae0b
8cb5b3c
 
 
 
 
 
 
 
d0dae0b
8cb5b3c
 
 
 
d0dae0b
 
8cb5b3c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d0dae0b
8cb5b3c
 
 
 
d0dae0b
8cb5b3c
d0dae0b
 
 
8cb5b3c
d0dae0b
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
44
45
46
import gradio as gr
from dotenv import load_dotenv
import os 
import torch
import warnings
from PIL import Image
from util import file_helper
from inference.ocr import prepare_batch_for_inference
from inference.inference_handler import handle

os.system('sudo apt install -y -q tesseract-ocr')
os.system('sudo apt install -y -q libtesseract-dev')

load_dotenv()

def get_model():
    model_dir = "tmp"
    model_filename= 'receipt.pth'
    full_path = os.path.join(model_dir, model_filename)
    if os.path.isfile(full_path):
        return full_path
    
    return file_helper.download_gdrive(os.getenv('MODEL_ID'), model_dir, model_filename)

def run_inference(model_path, images_path):
    try:
        inference_batch = prepare_batch_for_inference(images_path)
        context = {"model_dir": model_path}
        print('handle....')
        handle(inference_batch,context)
    except Exception as err:        
        print('err...', err)


def run(img_path):    
    print('img path: ', img_path)
    model_path = get_model()
    run_inference(model_path, [img_path])

    return Image.open(img_path) 


gr.Markdown('Upload Foto Wajah Kamu (Pastikan hanya terdapat SATU wajah pada)')
iface = gr.Interface(fn=run, inputs=gr.Image(type="filepath"), outputs="image")
iface.launch()