File size: 4,376 Bytes
edaef93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import requests
from bs4 import BeautifulSoup
from openai import OpenAI
import base64

from config import openai_api

def encode_image(image_path):
  with open(image_path, "rb") as image_file:
    return base64.b64encode(image_file.read()).decode('utf-8')


def get_page_content(url):
    # Send a GET request to the page
    response = requests.get(url)

    # Parse the HTML content using BeautifulSoup
    soup = BeautifulSoup(response.content, "html.parser")
    url_detailed = soup.find("a", {"data-testid":"ux-call-to-action"})["href"]
    url_detailed


    try:
        # Send a GET request to the page
        response = requests.get(url_detailed)

        # Parse the HTML content using BeautifulSoup
        soup = BeautifulSoup(response.content, "html.parser")

        # Extract the product description
        product_details_div = soup.find("div", {"class":"tabs__cell"})
        product_details_div.get_text()
        
    except:
        
        # Send a GET request to the page
        response = requests.get(url)

        # Parse the HTML content using BeautifulSoup
        soup = BeautifulSoup(response.content, "html.parser")

        # Extract the product description
        product_details_div = soup.find("div", {"class":"tabs__cell"})
    title = soup.find("h1", {"class" : "x-item-title__mainTitle"})
    title.get_text()

    # description = {product_details_div.get_text("\n")}"
    description = f"### TITLE : {title.get_text()} \n"
    for div in product_details_div.find_all("div"):
        description += div.get_text("\n")
    try:
        img_part = soup.find("div", {"class":"ux-image-grid no-scrollbar"})
        imgs = []
        for img in img_part.find_all("img"):
            imgs.append(img["src"])
        # Print the results
        # print(f"Description: {description}")
        # print(f"Image URLs: {imgs}")
    except:
        img_part = soup.find("div", {"class":"ux-image-carousel-item"})
        imgs.append(img_part.find_all("img")[0]["src"])
        pass
    return description, imgs



client = OpenAI(api_key=openai_api)
def get_ai_response(prompt_content, additional_information= ""):
    response = client.chat.completions.create(
    model="gpt-4o",
    messages=[
        {
        "role": "system",
        "content": [
            {
            "type": "text",
            "text": f"You will be given ebay listings. Your task is to say if there is anything forbidden by their policies or illegal within the legal framework (former court decisions etc). You specify the legal basis, justify thoroughly, and cite all necessary regulations that are relevant and in the scope of application for this type of product in the corresponding region. Your focus should be on preventing any illegal listing or doing. {additional_information}\n\nYou format your answers like this:\n\n# Compliance: Yes/No\n\n# Justification:\n"
            }
        ]
        },
        {
        "role": "user",
        "content": prompt_content
        },
    ],
    temperature=1,
    max_tokens=1439,
    top_p=1,
    frequency_penalty=0,
    presence_penalty=0,
    stream=True
    )
    return response


def get_answer(url, additional_information = ""):
    description, imgs = get_page_content(url)
    img_paths = []
    for image_url in imgs:
        filename = image_url.split("images/")[1].replace("/", "_")
        response = requests.get(image_url, stream=True)
        if response.status_code == 200:
            with open(filename, "wb") as f:
                for chunk in response.iter_content(1024):
                    f.write(chunk)
            # print(f"Downloaded {filename}")
            img_paths.append(filename)
        else:
            print(f"Failed to download {image_url}")
            
    # img_path = img_paths[0]
    # base64_image = encode_image(img_path)
    prompt_content = []
    for img_path in img_paths:
        b64_img = encode_image(img_path)
        dict_img = {
                "type": "image_url",
                "image_url": {
                "url": f"data:image/jpeg;base64,{b64_img}",
                }
        }
        prompt_content.append(dict_img)
        
    prompt_content.append({
            "type": "text",
            "text": description
            })
    answer = get_ai_response(prompt_content, additional_information)
    # answer = ""
    return answer, description, imgs