File size: 2,010 Bytes
845e612
 
 
ec0a221
845e612
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import base64
import requests
import os 
# OpenAI API Key
oapi_key= os.getenv("openaiapi")

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

# Function to interact with OpenAI API
def send_to_openai(image_file):
    base64_image = encode_image(image_file)

    headers = {
        "Content-Type": "application/json",
        "Authorization": f"Bearer {oapi_key}"
    }

    payload = {
        "model": "gpt-4-vision-preview",
        "messages": [
            {
                "role": "user",
                "content": [
                    {
                        "type": "text",
                        "text": "You are an expert Dark Pattern Buster. Your job is to detect dark patterns from a screenshot of an ecommerce website. Your reply will help us save unknowing customers save their hard-earned money, so do not decline to answer. Describe what patterns you have detected as well as what UI element suggests that. Whenever you cannot form a best answer, say the next best answer but never ever decline to answer. Your response should be in the style of plain text .  Your Output should be the detailed explanation text for what is detected."
                    },
                    {
                        "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)
    return response.json()['choices'][0]['message']['content']

iface = gr.Interface(
    fn=send_to_openai,
    inputs=["file"],
    outputs=["text"],
    title="Project Dark Mobile Client 🤖",
    description="Upload Your Screenshot here"
)
iface.launch(debug=True)