File size: 3,723 Bytes
ec9c6ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import openpyxl
from PIL import Image, ImageDraw, ImageFont
import base64
import requests
import gradio as gr

# OpenAI API Key
api_key = "sk-formanas-HAa5ntPi5XvOa0B2QpwTT3BlbkFJz4fYVfwl8inxMenGKluJ"

# Function to encode the image
def encode_image(image_path):
    with open(image_path, "rb") as image_file:
        return base64.b64encode(image_file.read()).decode('utf-8')

def take_screenshot_of_cells(file_path, sheet_name, start_cell, end_cell, query):
    # Load the workbook and the specified sheet
    workbook = openpyxl.load_workbook(file_path)
    sheet = workbook[sheet_name]
    
    # Get the coordinates of the start and end cells
    start_col, start_row = openpyxl.utils.cell.coordinate_to_tuple(start_cell)
    end_col, end_row = openpyxl.utils.cell.coordinate_to_tuple(end_cell)
    
    # Calculate the number of rows and columns
    num_cols = end_col - start_col + 1
    num_rows = end_row - start_row + 1
    
    # Set the width and height of each cell (you can adjust these values as needed)
    cell_width = 100
    cell_height = 30
    
    # Create a blank image with the calculated size
    image_width = cell_width * num_cols
    image_height = cell_height * num_rows
    image = Image.new('RGB', (image_width, image_height), 'white')
    draw = ImageDraw.Draw(image)
    
    # Load a font (you may need to specify the path to a font file on your system)
    font = ImageFont.load_default()
    
    # Draw the cells and their content
    for row in range(num_rows):
        for col in range(num_cols):
            cell_value = sheet.cell(row=start_row + row, column=start_col + col).value
            x0 = col * cell_width
            y0 = row * cell_height
            x1 = x0 + cell_width
            y1 = y0 + cell_height
            
            # Draw the cell border
            draw.rectangle([x0, y0, x1, y1], outline='black')
            
            # Draw the cell value
            if cell_value is not None:
                draw.text((x0 + 5, y0 + 5), str(cell_value), fill='black', font=font)
    
    # Save the image
    output_image_path = 'screenshot.png'
    image.save(output_image_path)

    # Encode the image to base64
    base64_image = encode_image(output_image_path)
    
    # Send the image to the OpenAI API
    headers = {
        "Content-Type": "application/json",
        "Authorization": f"Bearer {api_key}"
    }

    payload = {
        "model": "gpt-4o",  # Assuming the model name is 'gpt-4-vision'
        "messages": [
            {
                "role": "user",
                "content": [
                    {
                        "type": "text",
                        "text": query
                    },
                    {
                        "type": "image_url",
                        "image_url": {
                            "url": f"data:image/jpeg;base64,{base64_image}"
                        }
                    }
                ]
            }
        ],
        "max_tokens": 300
    }

    response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
    response=response.json()
    return response['choices'][0]['message']['content']

def gradio_interface(excel_file, sheet_name, start_cell, end_cell, query):
    return take_screenshot_of_cells(excel_file.name, sheet_name, start_cell, end_cell, query)

# Create Gradio Interface
inputs = [
    gr.File(label="Upload Excel File"),
    gr.Textbox(label="Sheet Name"),
    gr.Textbox(label="Start Cell"),
    gr.Textbox(label="End Cell"),
    gr.Textbox(label="Query")
]

outputs = gr.Textbox(label="Output")

gr.Interface(fn=gradio_interface, inputs=inputs, outputs=outputs, title="Excel Screenshot Analyzer").launch()