File size: 9,841 Bytes
11402c8
 
d101797
11402c8
 
 
 
 
d101797
11402c8
 
d101797
11402c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d101797
11402c8
 
 
 
 
 
 
 
 
 
 
d101797
11402c8
d101797
 
 
 
 
 
 
 
 
 
11402c8
 
 
 
 
 
 
 
 
 
644a334
11402c8
 
 
 
 
644a334
 
11402c8
 
 
 
 
 
 
 
d101797
 
11402c8
 
 
 
 
644a334
11402c8
 
 
 
 
 
 
 
 
 
d101797
644a334
11402c8
 
 
d101797
11402c8
 
d101797
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11402c8
037009b
11402c8
 
 
 
 
 
d101797
11402c8
 
 
 
 
 
 
 
 
 
 
 
 
 
037009b
11402c8
 
037009b
11402c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
037009b
11402c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d101797
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11402c8
 
eb2d14a
 
 
 
 
 
d101797
eb2d14a
d101797
 
eb2d14a
 
d101797
eb2d14a
11402c8
 
d101797
 
 
 
 
 
 
 
 
 
 
 
 
 
11402c8
 
 
eb2d14a
 
 
 
 
 
d101797
eb2d14a
11402c8
d101797
11402c8
 
32520ce
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import torch
from torch.utils.data import Dataset, DataLoader
from transformers import BertTokenizer, BertForTokenClassification, AdamW
from sklearn.model_selection import train_test_split
import gradio as gr
import random
from faker import Faker
import html
import json
import numpy as np
from tqdm import tqdm
import os

# Constants
MAX_LENGTH = 512
BATCH_SIZE = 16
EPOCHS = 5
LEARNING_RATE = 2e-5

fake = Faker()


def generate_employee():
    name = fake.name()
    job = fake.job()
    ext = f"ext. {random.randint(1000, 9999)}"
    email = f"{name.lower().replace(' ', '.')}@example.com"
    return name, job, ext, email


def generate_html_content(num_employees=3):
    employees = [generate_employee() for _ in range(num_employees)]

    html_content = f"""
    <html>
    <head>
        <title>Employee Directory</title>
    </head>
    <body>
        <div class="row ts-three-column-row standard-row">
    """

    for name, job, ext, email in employees:
        html_content += f"""
            <div class="column ts-three-column">
                <div class="block">
                    <div class="text-block" style="text-align: center;">
                        <p>
                            <strong>{html.escape(name)}</strong><br>
                            <span style="font-size: 16px">{html.escape(job)}</span><br>
                            <span style="font-size: 16px">{html.escape(ext)}</span><br>
                            <a href="mailto:{html.escape(email)}">Send Email</a>
                        </p>
                    </div>
                </div>
            </div>
        """

    html_content += """
        </div>
    </body>
    </html>
    """

    return html_content, employees


def generate_dataset(num_samples=1000):
    dataset = []
    for _ in range(num_samples):
        html_content, employees = generate_html_content()
        dataset.append((html_content, employees))
    return dataset


class HTMLDataset(Dataset):
    def __init__(self, data, tokenizer, max_length):
        self.data = data
        self.tokenizer = tokenizer
        self.max_length = max_length
        self.label_map = {"O": 0, "B-NAME": 1, "I-NAME": 2, "B-JOB": 3, "I-JOB": 4, "B-EXT": 5, "I-EXT": 6,
                          "B-EMAIL": 7, "I-EMAIL": 8}

    def __len__(self):
        return len(self.data)

    def __getitem__(self, idx):
        html, employees = self.data[idx]
        encoding = self.tokenizer.encode_plus(
            html,
            add_special_tokens=True,
            max_length=self.max_length,
            padding='max_length',
            truncation=True,
            return_attention_mask=True,
            return_tensors='pt',
        )

        labels = self.create_labels(encoding['input_ids'][0], employees)

        return {
            'input_ids': encoding['input_ids'].flatten(),
            'attention_mask': encoding['attention_mask'].flatten(),
            'labels': torch.tensor(labels, dtype=torch.long)
        }

    def create_labels(self, tokens, employees):
        labels = [0] * len(tokens)  # Initialize all labels as "O"
        for name, job, ext, email in employees:
            self.label_sequence(tokens, name, "NAME", labels)
            self.label_sequence(tokens, job, "JOB", labels)
            self.label_sequence(tokens, ext, "EXT", labels)
            self.label_sequence(tokens, email, "EMAIL", labels)
        return labels

    def label_sequence(self, tokens, text, label_type, labels):
        text_tokens = self.tokenizer.encode(text, add_special_tokens=False)
        for i in range(len(tokens) - len(text_tokens) + 1):
            if tokens[i:i + len(text_tokens)] == text_tokens:
                labels[i] = self.label_map[f"B-{label_type}"]
                for j in range(1, len(text_tokens)):
                    labels[i + j] = self.label_map[f"I-{label_type}"]
                break


def train_model():
    # Generate synthetic dataset
    dataset = generate_dataset(num_samples=1000)
    train_data, val_data = train_test_split(dataset, test_size=0.2, random_state=42)

    # Initialize tokenizer and model
    tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
    model = BertForTokenClassification.from_pretrained('bert-base-uncased', num_labels=9)

    # Prepare datasets and dataloaders
    train_dataset = HTMLDataset(train_data, tokenizer, MAX_LENGTH)
    val_dataset = HTMLDataset(val_data, tokenizer, MAX_LENGTH)
    train_dataloader = DataLoader(train_dataset, batch_size=BATCH_SIZE, shuffle=True)
    val_dataloader = DataLoader(val_dataset, batch_size=BATCH_SIZE)

    # Initialize optimizer
    optimizer = AdamW(model.parameters(), lr=LEARNING_RATE)

    # Training loop
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    model.to(device)

    for epoch in range(EPOCHS):
        model.train()
        train_loss = 0
        for batch in tqdm(train_dataloader, desc=f"Epoch {epoch + 1}/{EPOCHS}"):
            input_ids = batch['input_ids'].to(device)
            attention_mask = batch['attention_mask'].to(device)
            labels = batch['labels'].to(device)

            outputs = model(input_ids, attention_mask=attention_mask, labels=labels)
            loss = outputs.loss
            train_loss += loss.item()

            loss.backward()
            optimizer.step()
            optimizer.zero_grad()

        # Validation
        model.eval()
        val_loss = 0
        with torch.no_grad():
            for batch in val_dataloader:
                input_ids = batch['input_ids'].to(device)
                attention_mask = batch['attention_mask'].to(device)
                labels = batch['labels'].to(device)

                outputs = model(input_ids, attention_mask=attention_mask, labels=labels)
                val_loss += outputs.loss.item()

        avg_train_loss = train_loss / len(train_dataloader)
        avg_val_loss = val_loss / len(val_dataloader)

        print(f"Epoch {epoch + 1}/{EPOCHS}, Train Loss: {avg_train_loss:.4f}, Val Loss: {avg_val_loss:.4f}")

    return model, tokenizer


def extract_content(html, model, tokenizer):
    model.eval()
    encoding = tokenizer.encode_plus(
        html,
        add_special_tokens=True,
        max_length=MAX_LENGTH,
        padding='max_length',
        truncation=True,
        return_attention_mask=True,
        return_tensors='pt',
    )

    input_ids = encoding['input_ids'].to(model.device)
    attention_mask = encoding['attention_mask'].to(model.device)

    with torch.no_grad():
        outputs = model(input_ids, attention_mask=attention_mask)
        predictions = outputs.logits.argmax(dim=2)

    tokens = tokenizer.convert_ids_to_tokens(input_ids[0])
    label_map = {0: "O", 1: "B-NAME", 2: "I-NAME", 3: "B-JOB", 4: "I-JOB", 5: "B-EXT", 6: "I-EXT", 7: "B-EMAIL",
                 8: "I-EMAIL"}

    extracted_info = []
    current_entity = {"type": None, "value": ""}

    for token, prediction in zip(tokens, predictions[0]):
        if token == "[PAD]":
            break

        label = label_map[prediction.item()]
        if label.startswith("B-"):
            if current_entity["type"]:
                extracted_info.append(current_entity)
            current_entity = {"type": label[2:], "value": token}
        elif label.startswith("I-"):
            if current_entity["type"] == label[2:]:
                current_entity["value"] += " " + token
        elif label == "O":
            if current_entity["type"]:
                extracted_info.append(current_entity)
                current_entity = {"type": None, "value": ""}

    if current_entity["type"]:
        extracted_info.append(current_entity)

    # Group entities by employee
    employees = []
    current_employee = {}
    for entity in extracted_info:
        if entity["type"] == "NAME":
            if current_employee:
                employees.append(current_employee)
            current_employee = {"name": entity["value"]}
        else:
            current_employee[entity["type"].lower()] = entity["value"]

    if current_employee:
        employees.append(current_employee)

    return json.dumps(employees, indent=2)


def test_model(html_input, model, tokenizer):
    result = extract_content(html_input, model, tokenizer)
    return result


def gradio_interface(html_input, test_type):
    global model, tokenizer
    if test_type == "Custom Input":
        result = test_model(html_input, model, tokenizer)
        return html_input, result
    elif test_type == "Generate Random HTML":
        random_html, _ = generate_html_content()
        result = test_model(random_html, model, tokenizer)
        return random_html, result


# Check if the model is already trained and saved
if os.path.exists('model.pth') and os.path.exists('tokenizer'):
    print("Loading pre-trained model...")
    model = BertForTokenClassification.from_pretrained('bert-base-uncased', num_labels=9)
    model.load_state_dict(torch.load('model.pth'))
    tokenizer = BertTokenizer.from_pretrained('tokenizer')
else:
    print("Training new model...")
    model, tokenizer = train_model()
    # Save the model and tokenizer
    torch.save(model.state_dict(), 'model.pth')
    tokenizer.save_pretrained('tokenizer')

print("Launching Gradio interface...")

iface = gr.Interface(
    fn=gradio_interface,
    inputs=[
        gr.Textbox(lines=10, label="Input HTML"),
        gr.Radio(["Custom Input", "Generate Random HTML"], label="Test Type", value="Custom Input")
    ],
    outputs=[
        gr.Textbox(lines=10, label="HTML Content"),
        gr.Textbox(label="Extracted Information (JSON)")
    ],
    title="HTML Content Extractor",
    description="Enter HTML content or generate random HTML to test the model. The model will extract employee information and return it in JSON format."
)

iface.launch()