File size: 5,198 Bytes
b599e15
 
 
 
 
 
 
 
 
ba9c1ad
9b62d55
c732dd5
fb5e833
b599e15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fb5e833
5892f19
c732dd5
ba9c1ad
 
b599e15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
186276e
ba9c1ad
b599e15
 
 
 
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import gradio as gr
from datetime import datetime

from code import (initialize_session, 
                  image_to_df,
                  update_combined_df,
                  visualize_all       
                  )

def export_csv(df):
    file = df.to_csv("output.csv")
    return gr.File(value="output.csv", visible=True)
    
with gr.Blocks(gr.themes.Soft(primary_hue="emerald",
                              font=[gr.themes.GoogleFont("Quicksand"),"ui-sans-serif", "system-ui", "sans-serif"],
                              font_mono=[gr.themes.GoogleFont("IBM Plex Mono"), "ui-monospace", "Consolas","monospace"]
                              )) as demo:
    session_id = gr.State(initialize_session())

    gr.Markdown("# In this demo you can upload your receipt photo, make changes to data and visualize your expenses.")
    gr.Markdown("""
    ## Important Information
    - **Do not share your OpenAI API key**. Keep it secure and private.
    - Use environment-specific keys with limited permissions whenever possible.
    - The API key is used only during the session and is not stored permanently.
    """)

    with gr.Tabs():
        with gr.TabItem("Receipt info"):

            with gr.Row():
              api_key_input = gr.Textbox(label="Enter your OpenAI API key", type="password",max_lines=1,placeholder="sk-...")

            with gr.Row():
                with gr.Column(scale=1):
                  gr.Markdown("# Upload your receipts")
                with gr.Column(scale=2):
                  gr.Markdown("""
                  # Check and edit your data.
                  The model makes mistakes, for this reason, check extracted data, apply changes, then hit 'Confirm' button.""")

            with gr.Row():
                with gr.Column(scale=1):
                  image_input = gr.Files(type="filepath", file_count="multiple", scale=1)
                with gr.Column(scale=2):
                  gr.Markdown("""Possible categories:
                              'protein_foods', 'dairy', 'fruits', 'vegetables', 'grains', 'nuts_and_seeds','beverages', 'snacks',
                              'condiments', 'frozen_foods', 'bakery', 'canned_goods','household', 'personal_care', 'pet_supplies', 'other'""")




            with gr.Row():

                uploaded_images = gr.Gallery(label="Uploaded Receipt Images")
                image_output = gr.Dataframe(interactive=True,
                                            headers=['store', 'address', 'city', 'phone', 'receipt_no', 'date', 'time','total', 'number_items',
                                                     'payment_method', 'week', 'month', 'name','unit', 'price', 'amount', 'category'],
                                            col_count=(17, "fixed"),
                                            wrap=True,
                                            scale=2)

                combined_df = gr.Dataframe(visible=False)


            with gr.Row():
                with gr.Column(scale=1):
                    upload_button = gr.Button("Upload receipt")
                with gr.Column(scale=1):
                    combine_button = gr.Button("Confirm")
                with gr.Column(scale=1):
                    download_button = gr.DownloadButton("Export")
                    csv = gr.File(interactive=False, visible=False)
                #with gr.Column(scale=1):
                    #receipt_count_text = gr.Textbox(label="Uploaded files:", interactive=False)

            gr.Markdown("""
            # Visualize Expenses
            **Choose the base month and set your monthly budget, then hit 'Visualize'**""")

            with gr.Row():
                month = gr.Dropdown(value = datetime.now().strftime('%b'), 
                                    choices=["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"], 
                                    label="Month")
                
                budget = gr.Slider(0, 1000, 10, label="Budget")
            visualize_button = gr.Button("Visualize")

            with gr.Row():
                with gr.Column(scale=0.4):
                    fig1 = gr.Plot()
                with gr.Column(scale=0.6):
                    fig2 = gr.Plot()

            with gr.Row():
                with gr.Column(scale=0.4):
                    fig3 = gr.Plot()
                with gr.Column(scale=0.6):
                    fig4 = gr.Plot()

            with gr.Row():
                with gr.Column(scale=0.4):
                    fig5 = gr.Plot()
                with gr.Column(scale=0.6):
                    fig6 = gr.Plot()



    upload_button.click(image_to_df, inputs=[image_input, api_key_input], outputs=image_output)
    upload_button.click(fn=lambda files: [file.name for file in files], inputs=image_input, outputs=uploaded_images)
    combine_button.click(fn=update_combined_df, inputs=[image_output, session_id], outputs=combined_df)
    download_button.click(fn=export_csv, inputs=combined_df, outputs=csv)
    visualize_button.click(fn=visualize_all, inputs=[combined_df, month, budget], outputs=[fig1, fig2, fig3, fig4, fig5, fig6])

demo.launch(debug=True)