File size: 5,518 Bytes
8a8ca7c
7057564
 
 
 
 
 
 
 
 
 
 
 
00a6a38
7057564
00a6a38
06dcd84
00a6a38
2309a7a
 
 
 
 
00a6a38
 
 
7057564
00a6a38
 
 
 
 
 
 
 
 
 
 
 
 
7057564
00a6a38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7057564
00a6a38
 
7057564
00a6a38
 
7057564
00a6a38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7057564
00a6a38
 
 
 
7057564
00a6a38
 
 
 
 
7057564
00a6a38
 
 
 
 
 
 
 
 
 
 
 
7057564
8615d33
0f112ae
 
8615d33
00a6a38
 
2309a7a
 
 
7057564
 
00a6a38
7057564
 
 
 
 
 
 
00a6a38
8a8ca7c
 
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import gradio as gr
import json
import io
import base64
import requests
from openai import OpenAI
import pandas as pd
import smtplib
import ssl
from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import os

def process_images(image_list, app_password_input, progress=gr.Progress()):
    app_password = os.getenv('app_password')
    if app_password_input == app_password:
        progress(0, desc="Starting")
        counter = 1
        with open('output_columns.json') as f:
            output_columns = json.load(f)
        out_df = pd.DataFrame(columns=output_columns)
        OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')
        email = os.getenv('email')
        email_password = os.getenv('email_password')

        for image in progress.tqdm(image_list):
            try:
                buffered = io.BytesIO()
                image.save(buffered, format="JPEG")
                image_base64 = base64.b64encode(buffered.getvalue()).decode("utf-8")

                headers = {
                "Content-Type": "application/json",
                "Authorization": f"Bearer {OPENAI_API_KEY}"
                }
                payload = {
                "model": "gpt-4o",
                "messages": [
                    {
                    "role": "user",
                    "content": [
                        {
                        "type": "text",
                        "text": """
                        Wie lautet der Unternehmensname, der Gesamtbetrag und das Datum in der folgenden Rechnung? 
                        Das Datum soll das Format TTMM haben.
                        Der Gesamtbetrag soll ein Komma zur Abtrennung von Euro und Cent haben.
                        Gebe mir die Antwort in folgender Struktur:
                        {
                            "Unternehmensname" : 'String',
                            "Gesamtbetrag" : 'String',
                            "Datum" : 'String'
                        }
                        """
                        },
                        {
                        "type": "image_url",
                        "image_url": {
                                "url": f"data:image/jpeg;base64,{image_base64}"
                                # "url": image_base64
                        }
                        }
                    ]
                    }
                ],
                "max_tokens": 300
                }
                response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
                resp = json.loads(response.json()['choices'][0]['message']['content'])

                booking = {
                    "Umsatz (ohne Soll/Haben-Kz)": resp["Gesamtbetrag"],
                    "Soll/Haben-Kennzeichen": "H",
                    "Konto": 1371,
                    "Gegenkonto (ohne BU-Schlüssel)": 3300,
                    "Belegdatum": resp["Datum"],
                    "Belegfeld 1": counter,
                    "Buchungstext": resp["Unternehmensname"],
                    "Festschreibung": 0
                }
            except:
                booking = {
                    "Umsatz (ohne Soll/Haben-Kz)": "1,00",
                    "Soll/Haben-Kennzeichen": "H",
                    "Konto": 1371,
                    "Gegenkonto (ohne BU-Schlüssel)": 3300,
                    "Belegdatum": 101,
                    "Belegfeld 1": counter,
                    "Buchungstext":"???",
                    "Festschreibung": 0
                }
            booking_df = pd.DataFrame(booking, index=[0])
            out_df = pd.concat([out_df, booking_df])
            counter = counter + 1

        subject = "Sending Datev ASCII file"
        body = "Sending Datev ASCII file"
        sender_email = email
        receiver_email = email

        message = MIMEMultipart()
        message["From"] = sender_email
        message["To"] = receiver_email
        message["Subject"] = subject
        message.attach(MIMEText(body, "plain"))

        buffer = io.StringIO()
        out_df.to_csv(buffer, index=False)
        buffer.seek(0)
        part = MIMEBase("application", "octet-stream")
        part.set_payload(buffer.getvalue())
        encoders.encode_base64(part)
        part.add_header(
            "Content-Disposition",
            "attachment; filename= Datev_ASCII_file.csv",
        )
        message.attach(part)
        text = message.as_string()

        # context = ssl.create_default_context()
        # with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server:
        with smtplib.SMTP("smtp.gmail.com", 587) as server:
            server.starttls()
            server.login(sender_email, email_password)
            server.sendmail(sender_email, receiver_email, text)
        return out_df
    
    return "wrong password"

with gr.Blocks() as demo:
    app_password = gr.Textbox(label="Enter app password")
    images = gr.State([])
    image_input = gr.Image(label="Upload Images", type="pil")
    save_image = gr.Button("Save Image")
    num_images = gr.Number(value=0, label="Number images", interactive=False)
    save_image.click(lambda i,l,n:(l+[i],n+1), inputs=[image_input, images, num_images], outputs=[images, num_images])
    submit = gr.Button("Send to OpenAI and email")
    output = gr.Textbox()
    submit.click(process_images, inputs=[images, app_password], outputs=output)

demo.launch()