bricklerex commited on
Commit
adbaf40
·
verified ·
1 Parent(s): 3c9b44f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -0
app.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import openai
3
+ import base64
4
+ import os
5
+
6
+ # Configurable variables
7
+ API_KEY = os.getenv("API_KEY")
8
+ SYSTEM_PROMPT = (
9
+ 'You will be given an image of a page out of a bank statement. '
10
+ 'You must extract ONLY TWO numbers: Total Deposits, and Total Withdrawals.\n'
11
+ 'They may be called something else, so you must check. ONLY OUTPUT in the following format:\n'
12
+ '```\nTotal Deposits: {figure found}\nTotal Withdrawals: {figure found}\n```\n'
13
+ 'Do not output anything else. Say "N/A" if the figure is not found.'
14
+ )
15
+
16
+ # Function to process the image and get GPT-4o response
17
+ def process_image(image):
18
+ # Encode the image to base64
19
+ with open(image, "rb") as image_file:
20
+ base64_image = base64.b64encode(image_file.read()).decode('utf-8')
21
+
22
+ # Set up OpenAI API client
23
+ client = openai.OpenAI(api_key=API_KEY)
24
+
25
+ # Prepare the message payload
26
+ messages = [
27
+ {"role": "system", "content": [{
28
+ "type": "text",
29
+ "text": SYSTEM_PROMPT,
30
+ }]},
31
+ {"role": "user", "content": [{
32
+ "type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{base64_image}"}}]}
33
+ ]
34
+
35
+ # Call the OpenAI API
36
+ response = client.chat.completions.create(
37
+ model="gpt-4o",
38
+ messages=messages
39
+ )
40
+
41
+ # Extract and return the response content
42
+ return response.choices[0].message.content
43
+
44
+ # Create Gradio interface
45
+ iface = gr.Interface(
46
+ fn=process_image,
47
+ inputs=gr.Image(type="filepath", label="Upload Image"),
48
+ outputs=gr.Textbox(label="Response"),
49
+ title="Bank Statement Analysis",
50
+ description="Upload an image to receive the figures"
51
+ )
52
+
53
+ # # Launch the interface
54
+ # if __name__ == "__main__":
55
+ iface.launch(share=True)