File size: 2,140 Bytes
8aca528
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import gradio as gr
import pandas as pd
from models import Reciept_Analyzer
from utils import find_product, get_info
import os
model = Reciept_Analyzer()

sample_images = []
for img_file in os.listdir("samples/"):
    sample_images.append(os.path.join("samples", img_file))

def predict(image):
    results = model.forward(image)
    return results



# Thiết kế giao diện với Gradio
def create_interface():
    with gr.Blocks() as app:
        gr.Markdown("# Ứng dụng phân tích hóa đơn siêu thị")

        with gr.Row():
            # Cột bên trái
            with gr.Column():
                gr.Markdown("### Tải lên hóa đơn hoặc chọn ảnh mẫu")
                image_input = gr.Image(label="Ảnh hóa đơn", type="filepath")

                

                res = None
                def on_image_selected(image_path):
                    global res
                    res = predict(image_path)
                    final = get_info(res)
                    print(res)
                    return final

                def handle_input(item_name):
                    global res
                    result = find_product(item_name, res)
                    return result
                

                gr.Markdown("### Ảnh mẫu")
                example = gr.Examples(
                    inputs=image_input,
                    examples=sample_images
                )

            # Cột bên phải
            with gr.Column():
                result_output = gr.Textbox(label="Kết quả phân tích")
                image_input.change(fn=on_image_selected, inputs=image_input, outputs=result_output)
                gr.Markdown("### Tìm kiếm thông tin item")
                item_input = gr.Textbox(label="Tên item cần tìm")
                output = gr.Textbox(label="Kết quả")

                search_button = gr.Button("Tìm kiếm")
                search_button.click(fn=handle_input, inputs=item_input, outputs=output)

    return app


# Chạy ứng dụng
app = create_interface()
app.launch()