taneemishere commited on
Commit
9bfc5c9
1 Parent(s): dc9b4c6

adding compiler and improving main_program code

Browse files
.DS_Store CHANGED
Binary files a/.DS_Store and b/.DS_Store differ
 
__pycache__/main_program.cpython-38.pyc CHANGED
Binary files a/__pycache__/main_program.cpython-38.pyc and b/__pycache__/main_program.cpython-38.pyc differ
 
app.py CHANGED
@@ -9,7 +9,7 @@ def model_interface(image):
9
 
10
  # main method that call the main_program where code is generated and then compiled
11
  def main_model(input_image):
12
- result = main_program.main_program(input_image)
13
  return result
14
 
15
 
@@ -25,8 +25,8 @@ developing the structures of a web pages.</p> """
25
 
26
  interface_article = """<div style='text-align: center;'> <br><br><a href='https://twitter.com/taneemishere'
27
  target='_blank'>Developed by Taneem Jan</a> </div>
28
- <div style='text-align: center;'> <a href='https://taneemishere.github.io'
29
- target='_blank'>Paper</a> &ensp; &emsp; <a href='https://github.com/taneemishere'
30
  target='_blank'>Code</a> </div>
31
  """
32
 
 
9
 
10
  # main method that call the main_program where code is generated and then compiled
11
  def main_model(input_image):
12
+ result = main_program.main_method(input_image)
13
  return result
14
 
15
 
 
25
 
26
  interface_article = """<div style='text-align: center;'> <br><br><a href='https://twitter.com/taneemishere'
27
  target='_blank'>Developed by Taneem Jan</a> </div>
28
+ <div style='text-align: center;'> <a href='https://taneemishere.github.io/projects/project-one.html'
29
+ target='_blank'>Paper</a> &ensp; &emsp; <a href='https://github.com/taneemishere/html-code-generation-from-images-with-deep-neural-networks'
30
  target='_blank'>Code</a> </div>
31
  """
32
 
bin/meta_dataset.npy ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3327bd2481728a5d7306c51a057ae24a98aaf80a6f2a042b23c6af5e23d1a327
3
+ size 294
classes/Sampler.py CHANGED
@@ -3,7 +3,6 @@ from __future__ import absolute_import
3
  __author__ = 'Tony Beltramelli - www.tonybeltramelli.com'
4
 
5
  from .Vocabulary import *
6
- # from .BeamSearch import *
7
  from .Utils import *
8
 
9
 
@@ -58,59 +57,3 @@ class Sampler:
58
 
59
  return predictions, out_probas
60
 
61
- # def recursive_beam_search(self, model, input_img, current_context, beam, current_node, sequence_length):
62
- # probas = model.predict(input_img, np.array([current_context]))
63
-
64
- # predictions = []
65
- # for i in range(0, len(probas)):
66
- # predictions.append((i, probas[i], probas))
67
-
68
- # nodes = []
69
- # for i in range(0, len(predictions)):
70
- # prediction = predictions[i][0]
71
- # score = predictions[i][1]
72
- # output_probas = predictions[i][2]
73
- # nodes.append(Node(prediction, score, output_probas))
74
-
75
- # beam.add_nodes(current_node, nodes)
76
-
77
- # if beam.is_valid():
78
- # beam.prune_leaves()
79
- # if sequence_length == 1 or self.voc.token_lookup[beam.root.max_child().key] == END_TOKEN:
80
- # return
81
-
82
- # for node in beam.get_leaves():
83
- # prediction = node.key
84
-
85
- # new_context = []
86
- # for j in range(1, self.context_length):
87
- # new_context.append(current_context[j])
88
- # sparse_label = np.zeros(self.output_size)
89
- # sparse_label[prediction] = 1
90
- # new_context.append(sparse_label)
91
-
92
- # self.recursive_beam_search(model, input_img, new_context, beam, node, sequence_length - 1)
93
-
94
- # def predict_beam_search(self, model, input_img, beam_width=3, require_sparse_label=True, sequence_length=150):
95
- # predictions = START_TOKEN
96
- # out_probas = []
97
-
98
- # current_context = [self.voc.vocabulary[PLACEHOLDER]] * (self.context_length - 1)
99
- # current_context.append(self.voc.vocabulary[START_TOKEN])
100
- # if require_sparse_label:
101
- # current_context = Utils.sparsify(current_context, self.output_size)
102
-
103
- # beam = BeamSearch(beam_width=beam_width)
104
-
105
- # self.recursive_beam_search(model, input_img, current_context, beam, beam.root, sequence_length)
106
-
107
- # predicted_sequence, probas_sequence = beam.search()
108
-
109
- # for k in range(0, len(predicted_sequence)):
110
- # prediction = predicted_sequence[k]
111
- # probas = probas_sequence[k]
112
- # out_probas.append(probas)
113
-
114
- # predictions += self.voc.token_lookup[prediction]
115
-
116
- # return predictions, out_probas
 
3
  __author__ = 'Tony Beltramelli - www.tonybeltramelli.com'
4
 
5
  from .Vocabulary import *
 
6
  from .Utils import *
7
 
8
 
 
57
 
58
  return predictions, out_probas
59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
classes/__pycache__/Sampler.cpython-38.pyc CHANGED
Binary files a/classes/__pycache__/Sampler.cpython-38.pyc and b/classes/__pycache__/Sampler.cpython-38.pyc differ
 
classes/model/__pycache__/pix2code2.cpython-38.pyc CHANGED
Binary files a/classes/model/__pycache__/pix2code2.cpython-38.pyc and b/classes/model/__pycache__/pix2code2.cpython-38.pyc differ
 
classes/model/pix2code2.py CHANGED
@@ -2,7 +2,6 @@ __author__ = 'Ferdiand John Briones, attempt at pix2code2 through pretrained aut
2
 
3
  from keras.layers import Input, Dense, Dropout, RepeatVector, LSTM, concatenate, Flatten
4
  from keras.models import Sequential, Model
5
- # from keras.optimizers import RMSprop
6
  from tensorflow.keras.optimizers import RMSprop
7
  from keras import *
8
  from .Config import *
 
2
 
3
  from keras.layers import Input, Dense, Dropout, RepeatVector, LSTM, concatenate, Flatten
4
  from keras.models import Sequential, Model
 
5
  from tensorflow.keras.optimizers import RMSprop
6
  from keras import *
7
  from .Config import *
compiler/.DS_Store ADDED
Binary file (6.15 kB). View file
 
compiler/Compiler.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+ __author__ = 'Tony Beltramelli - www.tonybeltramelli.com'
3
+
4
+ import json
5
+ from compiler.Node import *
6
+
7
+
8
+ class Compiler:
9
+ def __init__(self, dsl_mapping_file_path):
10
+ with open(dsl_mapping_file_path) as data_file:
11
+ self.dsl_mapping = json.load(data_file)
12
+
13
+ self.opening_tag = self.dsl_mapping["opening-tag"]
14
+ self.closing_tag = self.dsl_mapping["closing-tag"]
15
+ self.content_holder = self.opening_tag + self.closing_tag
16
+
17
+ self.root = Node("body", None, self.content_holder)
18
+
19
+ def compile(self, input_file_path, output_file_path, rendering_function=None):
20
+ dsl_file = open(input_file_path)
21
+ current_parent = self.root
22
+
23
+ for token in dsl_file:
24
+ token = token.replace(" ", "").replace("\n", "")
25
+
26
+ if token.find(self.opening_tag) != -1:
27
+ token = token.replace(self.opening_tag, "")
28
+
29
+ element = Node(token, current_parent, self.content_holder)
30
+ current_parent.add_child(element)
31
+ current_parent = element
32
+ elif token.find(self.closing_tag) != -1:
33
+ current_parent = current_parent.parent
34
+ else:
35
+ tokens = token.split(",")
36
+ for t in tokens:
37
+ element = Node(t, current_parent, self.content_holder)
38
+ current_parent.add_child(element)
39
+
40
+ output_html = self.root.render(self.dsl_mapping, rendering_function=rendering_function)
41
+ with open(output_file_path, 'w') as output_file:
42
+ output_file.write(output_html)
43
+
44
+ return output_html
compiler/Node.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+ from __future__ import print_function
3
+ __author__ = 'Tony Beltramelli - www.tonybeltramelli.com'
4
+
5
+
6
+ class Node:
7
+ def __init__(self, key, parent_node, content_holder):
8
+ self.key = key
9
+ self.parent = parent_node
10
+ self.children = []
11
+ self.content_holder = content_holder
12
+
13
+ def add_child(self, child):
14
+ self.children.append(child)
15
+
16
+ def show(self):
17
+ print(self.key)
18
+ for child in self.children:
19
+ child.show()
20
+
21
+ def render(self, mapping, rendering_function=None):
22
+ content = ""
23
+ for child in self.children:
24
+ content += child.render(mapping, rendering_function)
25
+
26
+ value = mapping[self.key]
27
+ if rendering_function is not None:
28
+ value = rendering_function(self.key, value)
29
+
30
+ if len(self.children) != 0:
31
+ value = value.replace(self.content_holder, content)
32
+
33
+ return value
compiler/Utils.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ __author__ = 'Tony Beltramelli - www.tonybeltramelli.com'
2
+
3
+ import string
4
+ import random
5
+
6
+
7
+ class Utils:
8
+ @staticmethod
9
+ def get_random_text(length_text=10, space_number=1, with_upper_case=True):
10
+ results = []
11
+ while len(results) < length_text:
12
+ char = random.choice(string.ascii_letters[:26])
13
+ results.append(char)
14
+ if with_upper_case:
15
+ results[0] = results[0].upper()
16
+
17
+ current_spaces = []
18
+ while len(current_spaces) < space_number:
19
+ space_pos = random.randint(2, length_text - 3)
20
+ if space_pos in current_spaces:
21
+ break
22
+ results[space_pos] = " "
23
+ if with_upper_case:
24
+ results[space_pos + 1] = results[space_pos - 1].upper()
25
+
26
+ current_spaces.append(space_pos)
27
+
28
+ return ''.join(results)
29
+
30
+ @staticmethod
31
+ def get_ios_id(length=10):
32
+ results = []
33
+
34
+ while len(results) < length:
35
+ char = random.choice(string.digits + string.ascii_letters)
36
+ results.append(char)
37
+
38
+ results[3] = "-"
39
+ results[6] = "-"
40
+
41
+ return ''.join(results)
42
+
43
+ @staticmethod
44
+ def get_android_id(length=10):
45
+ results = []
46
+
47
+ while len(results) < length:
48
+ char = random.choice(string.ascii_letters)
49
+ results.append(char)
50
+
51
+ return ''.join(results)
compiler/__init__.py ADDED
File without changes
compiler/__pycache__/Compiler.cpython-38.pyc ADDED
Binary file (1.46 kB). View file
 
compiler/__pycache__/Node.cpython-38.pyc ADDED
Binary file (1.28 kB). View file
 
compiler/__pycache__/Utils.cpython-38.pyc ADDED
Binary file (1.51 kB). View file
 
compiler/__pycache__/__init__.cpython-38.pyc ADDED
Binary file (202 Bytes). View file
 
compiler/assets/.DS_Store ADDED
Binary file (6.15 kB). View file
 
compiler/assets/web-dsl-mapping.json ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "opening-tag": "{",
3
+ "closing-tag": "}",
4
+ "body": "<html>\n <header>\n <meta charset=\"utf-8\">\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n <link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css\" integrity=\"sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u\" crossorigin=\"anonymous\">\n<link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css\" integrity=\"sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp\" crossorigin=\"anonymous\">\n<style>\n.header{margin:20px 0}nav ul.nav-pills li{background-color:#333;border-radius:4px;margin-right:10px}.col-lg-3{width:24%;margin-right:1.333333%}.col-lg-6{width:49%;margin-right:2%}.col-lg-12,.col-lg-3,.col-lg-6{margin-bottom:20px;border-radius:6px;background-color:#f5f5f5;padding:20px}.row .col-lg-3:last-child,.row .col-lg-6:last-child{margin-right:0}footer{padding:20px 0;text-align:center;border-top:1px solid #bbb}\n</style>\n <title>Scaffold</title>\n </header>\n <body>\n <main class=\"container\">\n {}\n <footer class=\"footer\">\n <p>&copy; Tony Beltramelli 2017</p>\n </footer>\n </main>\n <script src=\"js/jquery.min.js\"></script>\n <script src=\"js/bootstrap.min.js\"></script>\n </body>\n</html>\n",
5
+ "header": "<div class=\"header clearfix\">\n <nav>\n <ul class=\"nav nav-pills pull-left\">\n {}\n </ul>\n </nav>\n</div>\n",
6
+ "btn-active": "<li class=\"active\"><a href=\"#\">[]</a></li>\n",
7
+ "btn-inactive": "<li><a href=\"#\">[]</a></li>\n",
8
+ "row": "<div class=\"row\">{}</div>\n",
9
+ "single": "<div class=\"col-lg-12\">\n{}\n</div>\n",
10
+ "double": "<div class=\"col-lg-6\">\n{}\n</div>\n",
11
+ "quadruple": "<div class=\"col-lg-3\">\n{}\n</div>\n",
12
+ "btn-green": "<a class=\"btn btn-success\" href=\"#\" role=\"button\">[]</a>\n",
13
+ "btn-orange": "<a class=\"btn btn-warning\" href=\"#\" role=\"button\">[]</a>\n",
14
+ "btn-red": "<a class=\"btn btn-danger\" href=\"#\" role=\"button\">[]</a>",
15
+ "big-title": "<h2>[]</h2>",
16
+ "small-title": "<h4>[]</h4>",
17
+ "text": "<p>[]</p>\n"
18
+ }
main_program.py CHANGED
@@ -8,18 +8,10 @@ from classes.Sampler import *
8
  from classes.model.pix2code2 import *
9
 
10
 
11
- def code_gen(path_to_input_image):
12
- # if len(sys.argv) < 1:
13
- # print("Error: not enough argument supplied:")
14
- # # print( "sample.py <trained weights path> <trained model name> <input image> <output path> <search method (
15
- # # default: " "greedy)>")
16
- # exit(0)
17
- # else:
18
  trained_weights_path = "../bin"
19
  trained_model_name = "pix2code2"
20
- # input_path = sys.argv[1]
21
- # input_path = "../data/test_gu.png"
22
- input_path = path_to_input_image
23
  output_path = "../data/output/"
24
  search_method = "greedy"
25
 
@@ -39,12 +31,7 @@ def code_gen(path_to_input_image):
39
  if search_method == "greedy":
40
  result, _ = sampler.predict_greedy(model, np.array([evaluation_img]))
41
  print("Result greedy: \n {}".format(result))
42
- # else:
43
- # beam_width = int(search_method)
44
- # print("Search with beam width: {}".format(beam_width))
45
- # result, _ = sampler.predict_beam_search(model, np.array([evaluation_img]), beam_width=beam_width)
46
- # print("Result beam: {}".format(result))
47
-
48
  with open("{}/{}.gui".format(output_path, file_name), 'w') as out_f:
49
  out_f.write(result.replace(START_TOKEN, "").replace(END_TOKEN, ""))
50
 
@@ -70,7 +57,7 @@ def compile_gui(outputpath, filename):
70
  FILL_WITH_RANDOM_TEXT = True
71
  TEXT_PLACE_HOLDER = "[]"
72
 
73
- dsl_path = "assets/web-dsl-mapping.json"
74
  compiler = Compiler(dsl_path)
75
 
76
  def render_content_with_text(key, value):
@@ -95,7 +82,7 @@ def compile_gui(outputpath, filename):
95
  return html_code
96
 
97
 
98
- def main_program(path_to_file):
99
- output_path, file_name = code_gen(path_to_file)
100
  result = compile_gui(output_path, file_name)
101
  return result
 
8
  from classes.model.pix2code2 import *
9
 
10
 
11
+ def dsl_code_geneneration(input_image):
 
 
 
 
 
 
12
  trained_weights_path = "../bin"
13
  trained_model_name = "pix2code2"
14
+ input_path = input_image
 
 
15
  output_path = "../data/output/"
16
  search_method = "greedy"
17
 
 
31
  if search_method == "greedy":
32
  result, _ = sampler.predict_greedy(model, np.array([evaluation_img]))
33
  print("Result greedy: \n {}".format(result))
34
+
 
 
 
 
 
35
  with open("{}/{}.gui".format(output_path, file_name), 'w') as out_f:
36
  out_f.write(result.replace(START_TOKEN, "").replace(END_TOKEN, ""))
37
 
 
57
  FILL_WITH_RANDOM_TEXT = True
58
  TEXT_PLACE_HOLDER = "[]"
59
 
60
+ dsl_path = "compiler/assets/web-dsl-mapping.json"
61
  compiler = Compiler(dsl_path)
62
 
63
  def render_content_with_text(key, value):
 
82
  return html_code
83
 
84
 
85
+ def main_method(input_image_from_interface):
86
+ output_path, file_name = dsl_code_geneneration(input_image_from_interface)
87
  result = compile_gui(output_path, file_name)
88
  return result