File size: 7,834 Bytes
fef6972
 
8677f63
d173154
69e9c2b
 
d173154
 
 
8677f63
fef6972
 
 
 
 
295889a
fef6972
4f45e25
 
 
fef6972
69e9c2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b0da584
69e9c2b
4f45e25
d173154
69e9c2b
 
 
 
 
 
 
 
 
 
 
 
d173154
4f45e25
 
 
 
 
 
 
 
 
 
 
 
d173154
 
b0da584
080569b
 
 
 
 
 
 
 
69e9c2b
080569b
 
d173154
 
 
 
 
f7bb73f
 
 
 
 
 
 
69e9c2b
 
f7bb73f
d173154
 
 
 
4f45e25
fef6972
 
 
 
 
 
 
 
 
13aaad7
fef6972
 
4f45e25
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import gradio as gr
import torch
import spaces
import logging
from deep_translator import GoogleTranslator


# Configure logging to write messages to a file
logging.basicConfig(filename='app.log', level=logging.ERROR)

# Configuration
max_seq_length = 2048
dtype = None  # Auto detection of dtype
load_in_4bit = True  # Use 4-bit quantization to reduce memory usage

peft_model_name = "limitedonly41/website_mistral7b_v02_1200_finetuned_7"

# Initialize model and tokenizer variables
model = None
tokenizer = None




import pandas as pd
from tqdm import tqdm
import urllib
import aiohttp
import asyncio
from bs4 import BeautifulSoup

async def fetch_data(url):
    headers = {
        'Accept': '*/*',
        'Accept-Language': 'ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7',
        'Connection': 'keep-alive',
        # 'Origin': 'https://www.beckman.es',
        'Referer': f'{url}',
        'Sec-Fetch-Dest': 'empty',
        'Sec-Fetch-Mode': 'cors',
        'Sec-Fetch-Site': 'cross-site',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36',
        'sec-ch-ua': '"Google Chrome";v="125", "Chromium";v="125", "Not.A/Brand";v="24"',
        'sec-ch-ua-mobile': '?0',
        'sec-ch-ua-platform': '"macOS"',
    }


    # encoding = 'windows-1251'
    encoding = 'utf-8'

    timeout = 10  # Set your desired timeout value in seconds
    try:
        # Function to make the request using urllib
        def get_content():
            req = urllib.request.Request(url, headers=headers)
            with urllib.request.urlopen(req, timeout=timeout) as response:
                return response.read()

        response_content = await loop.run_in_executor(None, get_content)

        soup = BeautifulSoup(response_content, 'html.parser', from_encoding=encoding)

        title = soup.find('title').text
        description = soup.find('meta', attrs={'name': 'description'})
        if description and "content" in description.attrs:
            description = description.get("content")
        else:
            description = ""

        keywords = soup.find('meta', attrs={'name': 'keywords'})
        if keywords and "content" in keywords.attrs:
            keywords = keywords.get("content")
        else:
            keywords = ""

        # h1_all = " ".join(h.text for h in soup.find_all('h1'))
        # h2_all = " ".join(h.text for h in soup.find_all('h2'))
        # h3_all = " ".join(h.text for h in soup.find_all('h3'))
        # paragraphs_all = " ".join(p.text for p in soup.find_all('p'))



        h1 = soup.find_all('h1')
        h1_all = ""

        try:
            for x in range (len(h1)):
                if x ==  len(h1) -1:
                    h1_all = h1_all + h1[x].text
                else:
                    h1_all = h1_all + h1[x].text + ". "
        except:
            h1_all = ""

        paragraphs_all = ""
        paragraphs = soup.find_all('p')
        try:
            for x in range (len(paragraphs)):
                if x ==  len(paragraphs) -1:
                    paragraphs_all = paragraphs_all + paragraphs[x].text
                else:
                    paragraphs_all = paragraphs_all + paragraphs[x].text + ". "
        except:
            paragraphs_all = ""

        h2 = soup.find_all('h2')
        h2_all = ""
        try:
            for x in range (len(h2)):
                if x ==  len(h2) -1:
                    h2_all = h2_all + h2[x].text
                else:
                    h2_all = h2_all + h2[x].text + ". "
        except:
            h2_all = ""

        h3 = soup.find_all('h3')
        h3_all = ""

        try:
            for x in range (len(h3)):
                if x ==  len(h3) -1:
                    h3_all = h3_all + h3[x].text
                else:
                    h3_all = h3_all + h3[x].text + ". "
        except:
            h3_all = ""



        allthecontent = f"{title} {description} {h1_all} {h2_all} {h3_all} {paragraphs_all}"
        allthecontent = allthecontent[:4999]

        # Clean up the text
        h1_all = h1_all.replace(r'\xa0', ' ').replace('\n', ' ').replace('\t', ' ')
        h2_all = h2_all.replace(r'\xa0', ' ').replace('\n', ' ').replace('\t', ' ')
        h3_all = h3_all.replace(r'\xa0', ' ').replace('\n', ' ').replace('\t', ' ')

        title = title.replace(r'\xa0', ' ')
        description = description.replace(r'\xa0', ' ')
        keywords = keywords.replace(r'\xa0', ' ')

        return {
            'url': url,
            'title': title,
            'description': description,
            'keywords': keywords,
            'h1': h1_all,
            'h2': h2_all,
            'h3': h3_all,
            'paragraphs': paragraphs_all,
            'text': allthecontent
        }
    except Exception as e:
        print(url, e)
        return {
            'url': url,
            'title': None,
            'description': None,
            'keywords': None,
            'h1': None,
            'h2': None,
            'h3': None,
            'paragraphs': None,
            'text': None
        }

async def main(urls):
    tasks = [fetch_data(url) for url in urls]
    results = []
    for future in tqdm(asyncio.as_completed(tasks), total=len(tasks)):
        result = await future
        results.append(result)
    return results







@spaces.GPU()
def classify_website(url):
    global model, tokenizer  # Declare model and tokenizer as global variables

    urls = [url]
    
    # Run asyncio event loop
    loop = asyncio.get_event_loop()
    results_shop = await main(urls[:])  # Instead of loop.run_until_complete(main(urls))
    
    # Convert results to DataFrame
    df_result_train_more = pd.DataFrame(results_shop)
    
    text = df_result_train_more['text'][0]
    translated = GoogleTranslator(source='auto', target='en').translate(text[:4990])

    try:
        # Load the model and tokenizer if they are not already loaded
        if model is None or tokenizer is None:
            from unsloth import FastLanguageModel
            
            # Load the model and tokenizer
            model, tokenizer = FastLanguageModel.from_pretrained(
                model_name=peft_model_name,  # YOUR MODEL YOU USED FOR TRAINING
                max_seq_length=max_seq_length,
                dtype=dtype,
                load_in_4bit=load_in_4bit,
            )
            FastLanguageModel.for_inference(model)  # Enable native 2x faster inference
        
        prompt = f"""Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
    
### Instruction:
Categorize the website into one of the 3 categories:

1) OTHER
2) NEWS/BLOG
3) E-commerce

### Input:
{translated}

### Response:"""
        
        inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
        outputs = model.generate(**inputs, max_new_tokens=64, use_cache=True)
        ans = tokenizer.batch_decode(outputs)[0]
        ans_pred = ans.split('### Response:')[1].split('<')[0]
        
        if 'OTHER' in ans_pred:
            ans_pred = 'OTHER'
        elif 'NEWS/BLOG' in ans_pred:
            ans_pred = 'NEWS/BLOG'
        elif 'E-commerce' in ans_pred:
            ans_pred = 'E-commerce'
        # else:
        #     ans_pred = 'OTHER'
            
        return ans_pred

    except Exception as e:
        logging.exception(e)
        return str(e)

# Create a Gradio interface
iface = gr.Interface(
    fn=classify_website,
    inputs="text",
    outputs="text",
    title="Website Categorization",
    description="Categorize a website into one of the 3 categories: OTHER, NEWS/BLOG, or E-commerce."
)
iface.queue()  # <-- Sets up a queue with default parameters

# Launch the interface
iface.launch()