themanas021 commited on
Commit
ec9c6ed
·
verified ·
1 Parent(s): 462ee4b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +110 -0
app.py ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import openpyxl
2
+ from PIL import Image, ImageDraw, ImageFont
3
+ import base64
4
+ import requests
5
+ import gradio as gr
6
+
7
+ # OpenAI API Key
8
+ api_key = "sk-formanas-HAa5ntPi5XvOa0B2QpwTT3BlbkFJz4fYVfwl8inxMenGKluJ"
9
+
10
+ # Function to encode the image
11
+ def encode_image(image_path):
12
+ with open(image_path, "rb") as image_file:
13
+ return base64.b64encode(image_file.read()).decode('utf-8')
14
+
15
+ def take_screenshot_of_cells(file_path, sheet_name, start_cell, end_cell, query):
16
+ # Load the workbook and the specified sheet
17
+ workbook = openpyxl.load_workbook(file_path)
18
+ sheet = workbook[sheet_name]
19
+
20
+ # Get the coordinates of the start and end cells
21
+ start_col, start_row = openpyxl.utils.cell.coordinate_to_tuple(start_cell)
22
+ end_col, end_row = openpyxl.utils.cell.coordinate_to_tuple(end_cell)
23
+
24
+ # Calculate the number of rows and columns
25
+ num_cols = end_col - start_col + 1
26
+ num_rows = end_row - start_row + 1
27
+
28
+ # Set the width and height of each cell (you can adjust these values as needed)
29
+ cell_width = 100
30
+ cell_height = 30
31
+
32
+ # Create a blank image with the calculated size
33
+ image_width = cell_width * num_cols
34
+ image_height = cell_height * num_rows
35
+ image = Image.new('RGB', (image_width, image_height), 'white')
36
+ draw = ImageDraw.Draw(image)
37
+
38
+ # Load a font (you may need to specify the path to a font file on your system)
39
+ font = ImageFont.load_default()
40
+
41
+ # Draw the cells and their content
42
+ for row in range(num_rows):
43
+ for col in range(num_cols):
44
+ cell_value = sheet.cell(row=start_row + row, column=start_col + col).value
45
+ x0 = col * cell_width
46
+ y0 = row * cell_height
47
+ x1 = x0 + cell_width
48
+ y1 = y0 + cell_height
49
+
50
+ # Draw the cell border
51
+ draw.rectangle([x0, y0, x1, y1], outline='black')
52
+
53
+ # Draw the cell value
54
+ if cell_value is not None:
55
+ draw.text((x0 + 5, y0 + 5), str(cell_value), fill='black', font=font)
56
+
57
+ # Save the image
58
+ output_image_path = 'screenshot.png'
59
+ image.save(output_image_path)
60
+
61
+ # Encode the image to base64
62
+ base64_image = encode_image(output_image_path)
63
+
64
+ # Send the image to the OpenAI API
65
+ headers = {
66
+ "Content-Type": "application/json",
67
+ "Authorization": f"Bearer {api_key}"
68
+ }
69
+
70
+ payload = {
71
+ "model": "gpt-4o", # Assuming the model name is 'gpt-4-vision'
72
+ "messages": [
73
+ {
74
+ "role": "user",
75
+ "content": [
76
+ {
77
+ "type": "text",
78
+ "text": query
79
+ },
80
+ {
81
+ "type": "image_url",
82
+ "image_url": {
83
+ "url": f"data:image/jpeg;base64,{base64_image}"
84
+ }
85
+ }
86
+ ]
87
+ }
88
+ ],
89
+ "max_tokens": 300
90
+ }
91
+
92
+ response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
93
+ response=response.json()
94
+ return response['choices'][0]['message']['content']
95
+
96
+ def gradio_interface(excel_file, sheet_name, start_cell, end_cell, query):
97
+ return take_screenshot_of_cells(excel_file.name, sheet_name, start_cell, end_cell, query)
98
+
99
+ # Create Gradio Interface
100
+ inputs = [
101
+ gr.File(label="Upload Excel File"),
102
+ gr.Textbox(label="Sheet Name"),
103
+ gr.Textbox(label="Start Cell"),
104
+ gr.Textbox(label="End Cell"),
105
+ gr.Textbox(label="Query")
106
+ ]
107
+
108
+ outputs = gr.Textbox(label="Output")
109
+
110
+ gr.Interface(fn=gradio_interface, inputs=inputs, outputs=outputs, title="Excel Screenshot Analyzer").launch()