Spaces:
Runtime error
Runtime error
Francisco Santos
commited on
Commit
·
b55b7f0
1
Parent(s):
aafc193
input handling
Browse files- app.py +249 -31
- app_files.py +173 -0
- output.html +31 -0
app.py
CHANGED
|
@@ -5,16 +5,137 @@ import time
|
|
| 5 |
import os
|
| 6 |
from transformers import AutoTokenizer, pipeline
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
models = {
|
| 9 |
"model_n1": "sileod/deberta-v3-base-tasksource-nli",
|
| 10 |
# "model_n2": "roberta-large-mnli",
|
| 11 |
# "model_n3": "facebook/bart-large-mnli",
|
| 12 |
# "model_n4": "cross-encoder/nli-deberta-v3-xsmall"
|
| 13 |
}
|
| 14 |
-
def
|
| 15 |
with open(file.name, "r") as f:
|
| 16 |
content = f.read()
|
| 17 |
-
return content
|
| 18 |
|
| 19 |
def find_form_fields(html_content):
|
| 20 |
|
|
@@ -66,7 +187,7 @@ def classify_lines(text, candidate_labels, model_name):
|
|
| 66 |
execution_time = end_time - start_time # Calculate execution time
|
| 67 |
return classified_lines, execution_time
|
| 68 |
|
| 69 |
-
def classify_lines_json(text, json_content, candidate_labels, model_name
|
| 70 |
start_time = time.time() # Start measuring time
|
| 71 |
classifier = pipeline('zero-shot-classification', model=model_name)
|
| 72 |
|
|
@@ -79,17 +200,114 @@ def classify_lines_json(text, json_content, candidate_labels, model_name, output
|
|
| 79 |
# Open the output.html file in write mode
|
| 80 |
output_content = []
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
|
| 95 |
end_time = time.time() # Stop measuring time
|
|
@@ -129,38 +347,38 @@ def retrieve_fields_from_file(file_path):
|
|
| 129 |
|
| 130 |
|
| 131 |
def process_files(html_file, json_file):
|
| 132 |
-
|
| 133 |
-
#
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
html_inputs = find_form_fields(html_content)
|
| 139 |
-
|
| 140 |
-
json_content = retrieve_fields_from_file(json_file)
|
| 141 |
#Classificar os inputs do json para ver em que tipo de input ["text", "radio", "checkbox", "button", "date"]
|
| 142 |
|
| 143 |
# Classify lines and measure execution time
|
| 144 |
for model_name in models.values():
|
| 145 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 146 |
|
| 147 |
-
html_classified_lines, html_execution_time = classify_lines(html_inputs, ["text", "radio", "checkbox", "button", "date"], model_name)
|
| 148 |
|
| 149 |
-
json_classified_lines, json_execution_time = classify_lines_json(
|
| 150 |
|
| 151 |
# print(str(html_execution_time) + " - " + str(html_classified_lines))
|
| 152 |
# print(str(json_execution_time) + " - " + str(json_classified_lines))
|
| 153 |
-
|
| 154 |
-
|
| 155 |
#print(type(json_classified_lines))
|
| 156 |
-
|
| 157 |
-
#json_classified_lines
|
| 158 |
#return '\n'.join(map(str, html_classified_lines))
|
| 159 |
return '\n'.join(map(str, json_classified_lines))
|
| 160 |
|
| 161 |
iface = gr.Interface(fn=process_files,
|
| 162 |
-
inputs=[gr.
|
| 163 |
-
outputs="
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
|
| 165 |
|
| 166 |
iface.launch()
|
|
|
|
| 5 |
import os
|
| 6 |
from transformers import AutoTokenizer, pipeline
|
| 7 |
|
| 8 |
+
example1 = '''<!DOCTYPE html>
|
| 9 |
+
<html>
|
| 10 |
+
<head>
|
| 11 |
+
<meta charset="UTF-8">
|
| 12 |
+
<title>Contact Form</title>
|
| 13 |
+
</head>
|
| 14 |
+
<body>
|
| 15 |
+
<h1>Contact Form</h1>
|
| 16 |
+
<form action="/submit-form" method="POST">
|
| 17 |
+
<label for="name">Name:</label>
|
| 18 |
+
<input type="text" id="name" name="name" required>
|
| 19 |
+
<br>
|
| 20 |
+
<label for="email">Email:</label>
|
| 21 |
+
<input type="email" id="email" name="email" required>
|
| 22 |
+
<br>
|
| 23 |
+
<label for="location">Location:</label>
|
| 24 |
+
<input type="text" id="location" name="location" required>
|
| 25 |
+
<br>
|
| 26 |
+
<label for="github">GitHub:</label>
|
| 27 |
+
<input type="url" id="github" name="github" required>
|
| 28 |
+
<br>
|
| 29 |
+
<label for="linkedin">LinkedIn:</label>
|
| 30 |
+
<input type="url" id="linkedin" name="linkedin" required>
|
| 31 |
+
<br>
|
| 32 |
+
<label for="phone">Phone:</label>
|
| 33 |
+
<input type="tel" id="phone" name="phone" required>
|
| 34 |
+
<br><br>
|
| 35 |
+
<input type="submit" value="Submit">
|
| 36 |
+
</form>
|
| 37 |
+
</body>
|
| 38 |
+
</html>
|
| 39 |
+
'''
|
| 40 |
+
|
| 41 |
+
solution1 = '''{
|
| 42 |
+
"name": "Ana Guida",
|
| 43 |
+
"email": "example@gmail.com",
|
| 44 |
+
"location": "Amsterdam, Netherlands",
|
| 45 |
+
"github": "https://github.com/34kmddfn",
|
| 46 |
+
"linkedin": "https://www.linkedin.com/in/ana-rguida/",
|
| 47 |
+
"phone": "+351 928 169 341"
|
| 48 |
+
}'''
|
| 49 |
+
|
| 50 |
+
example2 = '''<!DOCTYPE html>
|
| 51 |
+
<html>
|
| 52 |
+
<head>
|
| 53 |
+
<title>Resume Form</title>
|
| 54 |
+
</head>
|
| 55 |
+
<body>
|
| 56 |
+
<form action="/" method="POST">
|
| 57 |
+
<label>What kind of pet do you have?</label>
|
| 58 |
+
<br>
|
| 59 |
+
<input type="radio" id="dog" name="pet" value="dog">
|
| 60 |
+
<label for="dog">Dog</label>
|
| 61 |
+
<br>
|
| 62 |
+
<input type="radio" id="cat" name="pet" value="cat">
|
| 63 |
+
<label for="cat">Cat</label>
|
| 64 |
+
<br>
|
| 65 |
+
<input type="radio" id="other" name="pet" value="other">
|
| 66 |
+
<label for="other">Other</label>
|
| 67 |
+
<br><br>
|
| 68 |
+
<label>What color is your pet?</label>
|
| 69 |
+
<br>
|
| 70 |
+
<input type="checkbox" id="black" name="color" value="black">
|
| 71 |
+
<label for="black">Black</label>
|
| 72 |
+
<br>
|
| 73 |
+
<input type="checkbox" id="white" name="color" value="white">
|
| 74 |
+
<label for="white">White</label>
|
| 75 |
+
<br>
|
| 76 |
+
<input type="checkbox" id="brown" name="color" value="brown">
|
| 77 |
+
<label for="brown">Brown</label>
|
| 78 |
+
<br><br>
|
| 79 |
+
<input type="submit" value="Submit">
|
| 80 |
+
</form>
|
| 81 |
+
</body>
|
| 82 |
+
</html>
|
| 83 |
+
'''
|
| 84 |
+
|
| 85 |
+
solution2 = '''{
|
| 86 |
+
"pet": "dog",
|
| 87 |
+
"color": [
|
| 88 |
+
"black",
|
| 89 |
+
"brown"
|
| 90 |
+
]
|
| 91 |
+
}'''
|
| 92 |
+
|
| 93 |
+
example3 = '''<!DOCTYPE html>
|
| 94 |
+
<html>
|
| 95 |
+
<head>
|
| 96 |
+
<title>Create account Form</title>
|
| 97 |
+
</head>
|
| 98 |
+
<body>
|
| 99 |
+
<form action="/" method="POST">
|
| 100 |
+
<label for="name">Name:</label>
|
| 101 |
+
<input type="text" id="name" name="name" required>
|
| 102 |
+
<br>
|
| 103 |
+
<label for="country">Select your country:</label>
|
| 104 |
+
<br>
|
| 105 |
+
<select id="country" name="country">
|
| 106 |
+
<option value="usa">USA</option>
|
| 107 |
+
<option value="uk">UK</option>
|
| 108 |
+
<option value="germany">Germany</option>
|
| 109 |
+
<option value="japan">Japan</option>
|
| 110 |
+
</select>
|
| 111 |
+
<br><br>
|
| 112 |
+
<label for="birthday">Select your birthday:</label>
|
| 113 |
+
<br>
|
| 114 |
+
<input type="date" id="birthday" name="birthday">
|
| 115 |
+
<br><br>
|
| 116 |
+
<input type="submit" value="Submit">
|
| 117 |
+
</form>
|
| 118 |
+
</body>
|
| 119 |
+
</html>
|
| 120 |
+
'''
|
| 121 |
+
|
| 122 |
+
solution3 = '''{
|
| 123 |
+
"name": "Mike",
|
| 124 |
+
"country": "Germany",
|
| 125 |
+
"birthday": "1990-05-07"
|
| 126 |
+
}'''
|
| 127 |
+
|
| 128 |
+
|
| 129 |
models = {
|
| 130 |
"model_n1": "sileod/deberta-v3-base-tasksource-nli",
|
| 131 |
# "model_n2": "roberta-large-mnli",
|
| 132 |
# "model_n3": "facebook/bart-large-mnli",
|
| 133 |
# "model_n4": "cross-encoder/nli-deberta-v3-xsmall"
|
| 134 |
}
|
| 135 |
+
def find_form_fields_from_file(file):
|
| 136 |
with open(file.name, "r") as f:
|
| 137 |
content = f.read()
|
| 138 |
+
return find_form_fields(content)
|
| 139 |
|
| 140 |
def find_form_fields(html_content):
|
| 141 |
|
|
|
|
| 187 |
execution_time = end_time - start_time # Calculate execution time
|
| 188 |
return classified_lines, execution_time
|
| 189 |
|
| 190 |
+
def classify_lines_json(text, json_content, candidate_labels, model_name):
|
| 191 |
start_time = time.time() # Start measuring time
|
| 192 |
classifier = pipeline('zero-shot-classification', model=model_name)
|
| 193 |
|
|
|
|
| 200 |
# Open the output.html file in write mode
|
| 201 |
output_content = []
|
| 202 |
|
| 203 |
+
last_input = "None"
|
| 204 |
+
max_index = -1
|
| 205 |
+
|
| 206 |
+
for i, line in enumerate(lines):
|
| 207 |
+
if line.strip() and (line.strip().startswith("<input") or line.strip().startswith("<select") or line.strip().startswith("<option") ) and 'hidden' not in line.lower():
|
| 208 |
+
# Skip empty lines, classify lines starting with "<input", and exclude lines with 'hidden'
|
| 209 |
+
results = classifier(line, candidate_labels=["text", "radio", "checkbox", "button", "date", "select"])
|
| 210 |
+
if results['labels'][0] == "text" or results['labels'][0] == "date":
|
| 211 |
+
# print("text")
|
| 212 |
+
last_input = "text/date"
|
| 213 |
+
input_results = classifier(line, candidate_labels=candidate_labels)
|
| 214 |
+
top_classifications = input_results['labels'][:2] # Get the top two classifications
|
| 215 |
+
top_scores = input_results['scores'][:2] # Get the top two scores
|
| 216 |
+
line = line + f"<!-- Input: <{json_content[top_classifications[0]]}> - certainty: {format(top_scores[0], '.2f')} -->"
|
| 217 |
+
elif results['labels'][0] == "button":
|
| 218 |
+
# print("button")
|
| 219 |
+
last_input = "button"
|
| 220 |
+
line = line + f"<!-- Input: <{results['labels'][0]}> - certainty: {format(results['scores'][0], '.2f')} -->"
|
| 221 |
+
elif results['labels'][0] == "radio":
|
| 222 |
+
# print("radio")
|
| 223 |
+
if(last_input == "radio"):
|
| 224 |
+
radio_options.append(line)
|
| 225 |
+
radio_options_i.append(i)
|
| 226 |
+
else:
|
| 227 |
+
radio_options = [line]
|
| 228 |
+
radio_options_i = [i]
|
| 229 |
+
radio_results_list = []
|
| 230 |
+
last_input = "radio"
|
| 231 |
+
input_results = classifier(line, candidate_labels=candidate_labels)
|
| 232 |
+
top_classifications = input_results['labels'][:2] # Get the top two classifications
|
| 233 |
+
top_scores = input_results['scores'][:2] # Get the top two scores
|
| 234 |
+
|
| 235 |
+
radio_results = classifier(line, candidate_labels=[json_content[top_classifications[0]]])
|
| 236 |
+
radio_results_list.append(radio_results)
|
| 237 |
+
|
| 238 |
+
# Get the scores from the radio_results_list
|
| 239 |
+
scores = [result['scores'][0] for result in radio_results_list]
|
| 240 |
+
|
| 241 |
+
previous_max_index = max_index
|
| 242 |
+
# Find the index of the maximum score
|
| 243 |
+
max_index = scores.index(max(scores))
|
| 244 |
+
|
| 245 |
+
if previous_max_index != max_index:
|
| 246 |
+
|
| 247 |
+
line_selected = radio_options[previous_max_index]
|
| 248 |
+
real_index = radio_options_i[previous_max_index]
|
| 249 |
+
if real_index < len(output_content):
|
| 250 |
+
output_content[real_index] = line_selected
|
| 251 |
+
|
| 252 |
+
line_selected = radio_options[max_index]
|
| 253 |
+
line_selected = line_selected + f"<!-- Input: <{results['labels'][0]}> - certainty: {format(results['scores'][0], '.2f')}. LINE TO SELECT: <{radio_results['labels'][0]}> - certainty: {format(max(scores), '.2f')} -->"
|
| 254 |
+
real_index = radio_options_i[max_index]
|
| 255 |
+
|
| 256 |
+
if real_index < len(output_content):
|
| 257 |
+
output_content[real_index] = line_selected
|
| 258 |
+
else:
|
| 259 |
+
line = line_selected
|
| 260 |
+
elif results['labels'][0] == "checkbox":
|
| 261 |
+
# print("checkbox")
|
| 262 |
+
last_input = "checkbox"
|
| 263 |
+
input_results = classifier(line, candidate_labels=candidate_labels)
|
| 264 |
+
top_classifications = input_results['labels'][:2] # Get the top two classifications
|
| 265 |
+
top_scores = input_results['scores'][:2] # Get the top two scores
|
| 266 |
+
|
| 267 |
+
checkbox_results = classifier(line, candidate_labels=[json_content[top_classifications[0]]])
|
| 268 |
+
|
| 269 |
+
if checkbox_results['scores'][0] > 0.8:
|
| 270 |
+
line = line + f"<!-- Input: <{results['labels'][0]}> - certainty: {format(results['scores'][0], '.2f')}. LINE TO SELECT: <{checkbox_results['labels'][0]}> - certainty: {format(checkbox_results['scores'][0], '.2f')} -->"
|
| 271 |
+
else: #elif results['labels'][0] == "select" or results['labels'][0] == "option":
|
| 272 |
+
# print("select")
|
| 273 |
+
if(last_input == "select"):
|
| 274 |
+
select_options.append(line)
|
| 275 |
+
select_options_i.append(i)
|
| 276 |
+
else:
|
| 277 |
+
select_options = [line]
|
| 278 |
+
select_options_i = [i]
|
| 279 |
+
select_results_list = []
|
| 280 |
+
last_input = "select"
|
| 281 |
+
input_results = classifier(line, candidate_labels=candidate_labels)
|
| 282 |
+
top_classifications = input_results['labels'][:2] # Get the top two classifications
|
| 283 |
+
top_scores = input_results['scores'][:2] # Get the top two scores
|
| 284 |
+
|
| 285 |
+
select_results = classifier(line, candidate_labels=[json_content[top_classifications[0]]])
|
| 286 |
+
select_results_list.append(select_results)
|
| 287 |
+
|
| 288 |
+
# Get the scores from the select_results_list
|
| 289 |
+
scores = [result['scores'][0] for result in select_results_list]
|
| 290 |
+
|
| 291 |
+
previous_max_index = max_index
|
| 292 |
+
# Find the index of the maximum score
|
| 293 |
+
max_index = scores.index(max(scores))
|
| 294 |
+
|
| 295 |
+
if previous_max_index != max_index:
|
| 296 |
+
line_selected = select_options[previous_max_index]
|
| 297 |
+
real_index = select_options_i[previous_max_index]
|
| 298 |
+
if real_index < len(output_content):
|
| 299 |
+
output_content[real_index] = line_selected
|
| 300 |
+
|
| 301 |
+
line_selected = select_options[max_index]
|
| 302 |
+
line_selected = line_selected + f"<!-- Input: <{results['labels'][0]}> - certainty: {format(results['scores'][0], '.2f')}. LINE TO SELECT: <{select_results['labels'][0]}> - certainty: {format(max(scores), '.2f')} -->"
|
| 303 |
+
real_index = select_options_i[max_index]
|
| 304 |
+
|
| 305 |
+
if real_index < len(output_content):
|
| 306 |
+
output_content[real_index] = line_selected
|
| 307 |
+
else:
|
| 308 |
+
line = line_selected
|
| 309 |
+
|
| 310 |
+
output_content.append(line)
|
| 311 |
|
| 312 |
|
| 313 |
end_time = time.time() # Stop measuring time
|
|
|
|
| 347 |
|
| 348 |
|
| 349 |
def process_files(html_file, json_file):
|
| 350 |
+
|
| 351 |
+
#html_content = open_html(html_file)
|
| 352 |
+
#print(html_file)
|
| 353 |
+
html_inputs = find_form_fields(html_file)
|
| 354 |
+
#print(json_file)
|
| 355 |
+
json_content = retrieve_fields(json.loads(json_file))
|
|
|
|
|
|
|
|
|
|
| 356 |
#Classificar os inputs do json para ver em que tipo de input ["text", "radio", "checkbox", "button", "date"]
|
| 357 |
|
| 358 |
# Classify lines and measure execution time
|
| 359 |
for model_name in models.values():
|
| 360 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 361 |
|
| 362 |
+
#html_classified_lines, html_execution_time = classify_lines(html_inputs, ["text", "radio", "checkbox", "button", "date", "select"], model_name)
|
| 363 |
|
| 364 |
+
json_classified_lines, json_execution_time = classify_lines_json(html_file, json_content, list(json_content.keys()), model_name)
|
| 365 |
|
| 366 |
# print(str(html_execution_time) + " - " + str(html_classified_lines))
|
| 367 |
# print(str(json_execution_time) + " - " + str(json_classified_lines))
|
| 368 |
+
|
|
|
|
| 369 |
#print(type(json_classified_lines))
|
| 370 |
+
|
|
|
|
| 371 |
#return '\n'.join(map(str, html_classified_lines))
|
| 372 |
return '\n'.join(map(str, json_classified_lines))
|
| 373 |
|
| 374 |
iface = gr.Interface(fn=process_files,
|
| 375 |
+
inputs=[gr.Textbox(lines = 20, max_lines = 1000, label="Upload HTML File"), gr.Textbox(lines = 20, max_lines = 1000, label="Upload JSON File")],
|
| 376 |
+
outputs=gr.Textbox(lines = 20, max_lines = 1000, label="Output"),
|
| 377 |
+
examples=[
|
| 378 |
+
[example1, solution1],
|
| 379 |
+
[example2, solution2],
|
| 380 |
+
[example3, solution3],
|
| 381 |
+
])
|
| 382 |
|
| 383 |
|
| 384 |
iface.launch()
|
app_files.py
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from bs4 import BeautifulSoup
|
| 3 |
+
import json
|
| 4 |
+
import time
|
| 5 |
+
import os
|
| 6 |
+
from transformers import AutoTokenizer, pipeline
|
| 7 |
+
|
| 8 |
+
models = {
|
| 9 |
+
"model_n1": "sileod/deberta-v3-base-tasksource-nli",
|
| 10 |
+
# "model_n2": "roberta-large-mnli",
|
| 11 |
+
# "model_n3": "facebook/bart-large-mnli",
|
| 12 |
+
# "model_n4": "cross-encoder/nli-deberta-v3-xsmall"
|
| 13 |
+
}
|
| 14 |
+
def open_html(file):
|
| 15 |
+
with open(file.name, "r") as f:
|
| 16 |
+
content = f.read()
|
| 17 |
+
return content
|
| 18 |
+
|
| 19 |
+
def find_form_fields(html_content):
|
| 20 |
+
|
| 21 |
+
soup = BeautifulSoup(html_content, 'html.parser')
|
| 22 |
+
|
| 23 |
+
# find all form tags
|
| 24 |
+
forms = soup.find_all('form')
|
| 25 |
+
|
| 26 |
+
form_fields = []
|
| 27 |
+
|
| 28 |
+
for form in forms:
|
| 29 |
+
# find all input and select tags within each form
|
| 30 |
+
input_tags = form.find_all('input')
|
| 31 |
+
select_tags = form.find_all('select')
|
| 32 |
+
|
| 33 |
+
for tag in input_tags:
|
| 34 |
+
form_fields.append(str(tag))
|
| 35 |
+
|
| 36 |
+
for tag in select_tags:
|
| 37 |
+
form_fields.append(str(tag))
|
| 38 |
+
|
| 39 |
+
# Convert the list to a single string for display
|
| 40 |
+
return form_fields
|
| 41 |
+
|
| 42 |
+
def load_json(json_file):
|
| 43 |
+
with open(json_file, 'r') as f:
|
| 44 |
+
data = json.load(f)
|
| 45 |
+
return data
|
| 46 |
+
|
| 47 |
+
def classify_lines(text, candidate_labels, model_name):
|
| 48 |
+
start_time = time.time() # Start measuring time
|
| 49 |
+
classifier = pipeline('zero-shot-classification', model=model_name)
|
| 50 |
+
|
| 51 |
+
# Check if the text is already a list or if it needs splitting
|
| 52 |
+
if isinstance(text, list):
|
| 53 |
+
lines = text
|
| 54 |
+
else:
|
| 55 |
+
lines = text.split('\n')
|
| 56 |
+
|
| 57 |
+
classified_lines = []
|
| 58 |
+
for line in lines:
|
| 59 |
+
if line.strip() and (line.strip().startswith("<input") or line.strip().startswith("<select") )and 'hidden' not in line.lower():
|
| 60 |
+
# Skip empty lines, classify lines starting with "<input", and exclude lines with 'hidden'
|
| 61 |
+
results = classifier(line, candidate_labels=candidate_labels)
|
| 62 |
+
top_classifications = results['labels'][:2] # Get the top two classifications
|
| 63 |
+
top_scores = results['scores'][:2] # Get the top two scores
|
| 64 |
+
classified_lines.append((line, list(zip(top_classifications, top_scores))))
|
| 65 |
+
end_time = time.time() # Stop measuring time
|
| 66 |
+
execution_time = end_time - start_time # Calculate execution time
|
| 67 |
+
return classified_lines, execution_time
|
| 68 |
+
|
| 69 |
+
def classify_lines_json(text, json_content, candidate_labels, model_name, output_file_path):
|
| 70 |
+
start_time = time.time() # Start measuring time
|
| 71 |
+
classifier = pipeline('zero-shot-classification', model=model_name)
|
| 72 |
+
|
| 73 |
+
# Check if the text is already a list or if it needs splitting
|
| 74 |
+
if isinstance(text, list):
|
| 75 |
+
lines = text
|
| 76 |
+
else:
|
| 77 |
+
lines = text.split('\n')
|
| 78 |
+
|
| 79 |
+
# Open the output.html file in write mode
|
| 80 |
+
output_content = []
|
| 81 |
+
|
| 82 |
+
with open(output_file_path, 'w') as output_file:
|
| 83 |
+
for line in lines:
|
| 84 |
+
|
| 85 |
+
if line.strip() and (line.strip().startswith("<input") or line.strip().startswith("<select") )and 'hidden' not in line.lower():
|
| 86 |
+
# Skip empty lines, classify lines starting with "<input", and exclude lines with 'hidden'
|
| 87 |
+
results = classifier(line, candidate_labels=candidate_labels)
|
| 88 |
+
top_classifications = results['labels'][:2] # Get the top two classifications
|
| 89 |
+
top_scores = results['scores'][:2] # Get the top two scores
|
| 90 |
+
line = line + f"<!-- Input: {json_content[top_classifications[0]]} with this certainty: {top_scores[0]} -->"
|
| 91 |
+
output_file.write(line + '\n')
|
| 92 |
+
output_content.append(line + '\n')
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
end_time = time.time() # Stop measuring time
|
| 96 |
+
execution_time = end_time - start_time # Calculate execution time
|
| 97 |
+
return output_content, execution_time
|
| 98 |
+
|
| 99 |
+
def retrieve_fields(data, path=''):
|
| 100 |
+
"""Recursively retrieve all fields from a given JSON structure and prompt for filling."""
|
| 101 |
+
fields = {}
|
| 102 |
+
|
| 103 |
+
# If the data is a dictionary
|
| 104 |
+
if isinstance(data, dict):
|
| 105 |
+
for key, value in data.items():
|
| 106 |
+
# Construct the updated path for nested structures
|
| 107 |
+
new_path = f"{path}.{key}" if path else key
|
| 108 |
+
fields.update(retrieve_fields(value, new_path))
|
| 109 |
+
|
| 110 |
+
# If the data is a list, iterate over its items
|
| 111 |
+
elif isinstance(data, list):
|
| 112 |
+
for index, item in enumerate(data):
|
| 113 |
+
new_path = f"{path}[{index}]"
|
| 114 |
+
fields.update(retrieve_fields(item, new_path))
|
| 115 |
+
|
| 116 |
+
# If the data is a simple type (str, int, etc.)
|
| 117 |
+
else:
|
| 118 |
+
prompt = f"Please fill in the {path} field." if not data else data
|
| 119 |
+
fields[path] = prompt
|
| 120 |
+
|
| 121 |
+
return fields
|
| 122 |
+
|
| 123 |
+
def retrieve_fields_from_file(file_path):
|
| 124 |
+
"""Load JSON data from a file, then retrieve all fields and prompt for filling."""
|
| 125 |
+
with open(file_path.name, 'r') as f:
|
| 126 |
+
data = f.read()
|
| 127 |
+
|
| 128 |
+
return retrieve_fields(json.loads(data))
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
def process_files(html_file, json_file):
|
| 132 |
+
# This function will process the files.
|
| 133 |
+
# Replace this with your own logic.
|
| 134 |
+
output_file_path = "./output.html"
|
| 135 |
+
# Open and read the files
|
| 136 |
+
html_content = open_html(html_file)
|
| 137 |
+
#print(html_content)
|
| 138 |
+
html_inputs = find_form_fields(html_content)
|
| 139 |
+
|
| 140 |
+
json_content = retrieve_fields_from_file(json_file)
|
| 141 |
+
#Classificar os inputs do json para ver em que tipo de input ["text", "radio", "checkbox", "button", "date"]
|
| 142 |
+
|
| 143 |
+
# Classify lines and measure execution time
|
| 144 |
+
for model_name in models.values():
|
| 145 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 146 |
+
|
| 147 |
+
html_classified_lines, html_execution_time = classify_lines(html_inputs, ["text", "radio", "checkbox", "button", "date"], model_name)
|
| 148 |
+
|
| 149 |
+
json_classified_lines, json_execution_time = classify_lines_json(html_content, json_content, list(json_content.keys()), model_name, output_file_path)
|
| 150 |
+
|
| 151 |
+
# print(str(html_execution_time) + " - " + str(html_classified_lines))
|
| 152 |
+
# print(str(json_execution_time) + " - " + str(json_classified_lines))
|
| 153 |
+
#FILL HERE
|
| 154 |
+
|
| 155 |
+
#print(type(json_classified_lines))
|
| 156 |
+
# Assuming your function returns the processed HTML
|
| 157 |
+
#json_classified_lines
|
| 158 |
+
#return '\n'.join(map(str, html_classified_lines))
|
| 159 |
+
return '\n'.join(map(str, json_classified_lines))
|
| 160 |
+
|
| 161 |
+
iface = gr.Interface(fn=process_files,
|
| 162 |
+
inputs=[gr.inputs.File(label="Upload HTML File"), gr.inputs.File(label="Upload JSON File")],
|
| 163 |
+
outputs="text",
|
| 164 |
+
examples=[
|
| 165 |
+
# ["./examples/form0.html", "./examples/form0_answer.json"],
|
| 166 |
+
["./public/form1.html", "./public/form1_answer.json"],
|
| 167 |
+
["./public/form2.html", "./public/form2_answer.json"],
|
| 168 |
+
["./public/form3.html", "./public/form3_answer.json"],
|
| 169 |
+
["./public/form4.html", "./public/form4_answer.json"]
|
| 170 |
+
])
|
| 171 |
+
|
| 172 |
+
|
| 173 |
+
iface.launch()
|
output.html
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html>
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8">
|
| 5 |
+
<title>Contact Form</title>
|
| 6 |
+
</head>
|
| 7 |
+
<body>
|
| 8 |
+
<h1>Contact Form</h1>
|
| 9 |
+
<form action="/submit-form" method="POST">
|
| 10 |
+
<label for="name">Name:</label>
|
| 11 |
+
<input type="text" id="name" name="name" required><!-- Input: Please fill in the name field. with this certainty: 0.8276665210723877 -->
|
| 12 |
+
<br>
|
| 13 |
+
<label for="email">Email:</label>
|
| 14 |
+
<input type="email" id="email" name="email" required><!-- Input: frs98com@gmail.com with this certainty: 0.8814952373504639 -->
|
| 15 |
+
<br>
|
| 16 |
+
<label for="location">Location:</label>
|
| 17 |
+
<input type="text" id="location" name="location" required><!-- Input: Amsterdam, Netherlands with this certainty: 0.845346212387085 -->
|
| 18 |
+
<br>
|
| 19 |
+
<label for="github">GitHub:</label>
|
| 20 |
+
<input type="url" id="github" name="github" required><!-- Input: https://github.com/qtoino with this certainty: 0.6784256100654602 -->
|
| 21 |
+
<br>
|
| 22 |
+
<label for="linkedin">LinkedIn:</label>
|
| 23 |
+
<input type="url" id="linkedin" name="linkedin" required><!-- Input: https://www.linkedin.com/in/francisco-rsantos/ with this certainty: 0.735436737537384 -->
|
| 24 |
+
<br>
|
| 25 |
+
<label for="phone">Phone:</label>
|
| 26 |
+
<input type="tel" id="phone" name="phone" required><!-- Input: +351 927 050 265 with this certainty: 0.8759291768074036 -->
|
| 27 |
+
<br><br>
|
| 28 |
+
<input type="submit" value="Submit"><!-- Input: Please fill in the name field. with this certainty: 0.2793944180011749 -->
|
| 29 |
+
</form>
|
| 30 |
+
</body>
|
| 31 |
+
</html>
|