AkashMnd commited on
Commit
845e612
1 Parent(s): f2d186e

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 base64
3
+ import requests
4
+
5
+ # OpenAI API Key
6
+ oapi_key= os.getenv("openaiapi")
7
+
8
+ # Function to encode the image
9
+ def encode_image(image_file):
10
+ with open(image_file.name, "rb") as img_file:
11
+ return base64.b64encode(img_file.read()).decode('utf-8')
12
+
13
+ # Function to interact with OpenAI API
14
+ def send_to_openai(image_file):
15
+ base64_image = encode_image(image_file)
16
+
17
+ headers = {
18
+ "Content-Type": "application/json",
19
+ "Authorization": f"Bearer {oapi_key}"
20
+ }
21
+
22
+ payload = {
23
+ "model": "gpt-4-vision-preview",
24
+ "messages": [
25
+ {
26
+ "role": "user",
27
+ "content": [
28
+ {
29
+ "type": "text",
30
+ "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."
31
+ },
32
+ {
33
+ "type": "image_url",
34
+ "image_url": {
35
+ "url": f"data:image/jpeg;base64,{base64_image}"
36
+ }
37
+ }
38
+ ]
39
+ }
40
+ ],
41
+ "max_tokens": 300
42
+ }
43
+
44
+
45
+ response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
46
+ return response.json()['choices'][0]['message']['content']
47
+
48
+ iface = gr.Interface(
49
+ fn=send_to_openai,
50
+ inputs=["file"],
51
+ outputs=["text"],
52
+ title="Project Dark Mobile Client 🤖",
53
+ description="Upload Your Screenshot here"
54
+ )
55
+ iface.launch(debug=True)