not-lain commited on
Commit
b1045a7
·
1 Parent(s): 92d4bc4

add examples

Browse files
Files changed (1) hide show
  1. app.py +68 -54
app.py CHANGED
@@ -23,6 +23,7 @@ for i, j in zip(t1, t2):
23
  ```
24
  """
25
 
 
26
  def generate_example(dim1: list, dim2: list):
27
  n1 = 1
28
  n2 = 1
@@ -64,35 +65,35 @@ def sanitize_dimention(dim):
64
  return out
65
 
66
 
67
- def create_row(dim,is_dim=None,checks=None,version=1):
68
  out = "| "
69
  n_dim = len(dim)
70
  for i in range(n_dim):
71
- if version == 1 :
72
  # infered last dims
73
- if (is_dim ==1 and i == n_dim-2) or (is_dim ==2 and i ==n_dim-1):
74
  color = "green"
75
  out += f"<strong style='color: {color}'> {dim[i]} </strong>| "
76
  # check every normal dimension
77
- elif (is_dim ==1 and i != n_dim-1) or (is_dim ==2 and i ==n_dim-1):
78
  color = "green" if checks[i] == "V" else "red"
79
  out += f"<strong style='color: {color}'> {dim[i]} </strong>| "
80
  # checks last 2 dims
81
- elif (is_dim ==1 and i == n_dim-1) or (is_dim ==2 and i ==n_dim-2):
82
  color = "blue" if checks[i] == "V" else "yellow"
83
  out += f"<strong style='color: {color}'> {dim[i]} </strong>| "
84
  # when using this function without checks
85
- else :
86
- out+= f"{dim[i]} | "
87
- if version == 2 :
88
- if (is_dim == 1 and i != n_dim-1) :
89
  out += f"<strong style='color: green'> {dim[i]} </strong>| "
90
- elif i == n_dim-1 :
91
  color = "blue" if checks[i] == "V" else "yellow"
92
  out += f"<strong style='color: {color}'> {dim[i]} </strong>| "
93
- else :
94
  out += f"{dim[i]} | "
95
-
96
  return out + "\n"
97
 
98
 
@@ -105,20 +106,20 @@ def create_header(n_dim, checks=None):
105
  return out
106
 
107
 
108
- def generate_table(dim1, dim2, checks=None,version=1):
109
  n_dim = len(dim1)
110
  table = create_header(n_dim, checks)
111
  # tensor 1
112
- if not checks :
113
  table += create_row(dim1)
114
- else :
115
- table += create_row(dim1,1,checks,version)
116
 
117
  # tensor 2
118
- if not checks :
119
  table += create_row(dim2)
120
- else :
121
- table += create_row(dim2,2,checks,version)
122
  return table
123
 
124
 
@@ -137,63 +138,71 @@ def alignment_and_fill_with_ones(dim1, dim2):
137
  dim2 = placeholder
138
  return dim1, dim2
139
 
140
- def check_validity(dim1,dim2):
 
141
  out = []
142
- for i in range(len(dim1)-2):
143
  if dim1[i] == dim2[i]:
144
  out.append("V")
145
- else :
146
  out.append("X")
147
  # final dims
148
  if dim1[-1] == dim2[-2]:
149
- out.extend(["V","V"])
150
- else :
151
- out.extend(["X","X"])
152
  return out
153
 
154
 
155
- def substitute_ones_with_concat(dim1,dim2,version = 1):
156
- n = len(dim1)-2 if version ==1 else len(dim1)-1
157
  for i in range(n):
158
  dim1[i] = dim2[i] if dim1[i] == 1 else dim1[i]
159
  dim2[i] = dim1[i] if dim2[i] == 1 else dim2[i]
160
  return dim1, dim2
161
 
 
162
  def predict(dim1, dim2):
163
  dim1 = sanitize_dimention(dim1)
164
  dim2 = sanitize_dimention(dim2)
165
- n1 , n2 = len(dim1) , len(dim2)
166
  dim1, dim2, out = generate_example(dim1, dim2)
167
- # TODO
168
- if n1 >1 and n2 > 1 :
169
- # Table 1
170
  dim1, dim2 = alignment_and_fill_with_ones(dim1, dim2)
171
  table1 = generate_table(dim1, dim2)
172
- # Table 2
173
- dim1, dim2 = substitute_ones_with_concat(dim1,dim2)
174
  table2 = generate_table(dim1, dim2)
175
- # Table 3
176
- checks = check_validity(dim1,dim2)
177
- table3 = generate_table(dim1,dim2,checks)
178
 
179
  out += "\n# Step1 (alignment and pre_append with ones)\n" + table1
180
- out += "\n# Step2 (susbtitute columns that have 1 with concat)\nexcept for last 2 dimensions\n" + table2
181
- out += "\n# Step3 (check if matrix multiplication is valid)\n"
 
 
 
182
  out += "* last dimension of dim1 should equal before last dimension of dim2 (blue or yellow colors)\n"
183
- out += "* all the other dimensions should be equal to one another (green or red colors)\n\n" + table3
184
- if "X" not in checks :
 
 
 
185
  dim1[-1] = dim2[-1]
186
  out += "\n# Final dimension\n"
187
- out+="as highlighted in <strong style='color:green'> green </strong> \n\n"
188
- out+= f"`output.shape = {dim1}`"
189
  # case single dims
190
- elif n1 == 1 and n2 == 1 :
191
  out += "# Single Dimensional Cases\n"
192
  out += "When both matricies have only single dims they should both have the same number of values in the first dimension\n"
193
  out += "meaning that `t1.shape == t2.shape`\n"
194
  out += "the output is a single value, think : \n"
195
  out += matrix_loop
196
- else :
197
  out += "# One of the dimensions is a single dimension\n"
198
  out += "In this case we need to assert that the last dimension of `t1` "
199
  out += "is equal to the last dimension of `t2`\n"
@@ -204,20 +213,18 @@ def predict(dim1, dim2):
204
  out += table
205
  out += "\n# Step2 (susbtitute columns that have 1 with concat)\n"
206
  out += ""
207
- dim1, dim2 = substitute_ones_with_concat(dim1,dim2,2)
208
- checks = ["V"] * (len(dim1)-1)
209
- if dim1[-1] == dim2[-1] :
210
  checks.append("V")
211
- else :
212
  checks.append("X")
213
- table = generate_table(dim1, dim2,checks,2)
214
- out+= table
215
- if "X" not in checks :
216
  out += "\n#Final dimension"
217
  out += "The final dimension is everything colored in <strong style='color:green'> green </strong> \n"
218
  out += f"\nfinal dimension = `{dim1[:-1]}` "
219
-
220
-
221
 
222
  return out
223
 
@@ -226,7 +233,14 @@ demo = gr.Interface(
226
  predict,
227
  inputs=["text", "text"],
228
  outputs=["markdown"],
229
- examples=[["9,2,1,3,3", "5,3,7"], ["7,4,2,3", "5,2,7"]],
 
 
 
 
 
 
 
230
  )
231
 
232
  demo.launch(debug=True)
 
23
  ```
24
  """
25
 
26
+
27
  def generate_example(dim1: list, dim2: list):
28
  n1 = 1
29
  n2 = 1
 
65
  return out
66
 
67
 
68
+ def create_row(dim, is_dim=None, checks=None, version=1):
69
  out = "| "
70
  n_dim = len(dim)
71
  for i in range(n_dim):
72
+ if version == 1:
73
  # infered last dims
74
+ if (is_dim == 1 and i == n_dim - 2) or (is_dim == 2 and i == n_dim - 1):
75
  color = "green"
76
  out += f"<strong style='color: {color}'> {dim[i]} </strong>| "
77
  # check every normal dimension
78
+ elif (is_dim == 1 and i != n_dim - 1) or (is_dim == 2 and i == n_dim - 1):
79
  color = "green" if checks[i] == "V" else "red"
80
  out += f"<strong style='color: {color}'> {dim[i]} </strong>| "
81
  # checks last 2 dims
82
+ elif (is_dim == 1 and i == n_dim - 1) or (is_dim == 2 and i == n_dim - 2):
83
  color = "blue" if checks[i] == "V" else "yellow"
84
  out += f"<strong style='color: {color}'> {dim[i]} </strong>| "
85
  # when using this function without checks
86
+ else:
87
+ out += f"{dim[i]} | "
88
+ if version == 2:
89
+ if is_dim == 1 and i != n_dim - 1:
90
  out += f"<strong style='color: green'> {dim[i]} </strong>| "
91
+ elif i == n_dim - 1:
92
  color = "blue" if checks[i] == "V" else "yellow"
93
  out += f"<strong style='color: {color}'> {dim[i]} </strong>| "
94
+ else:
95
  out += f"{dim[i]} | "
96
+
97
  return out + "\n"
98
 
99
 
 
106
  return out
107
 
108
 
109
+ def generate_table(dim1, dim2, checks=None, version=1):
110
  n_dim = len(dim1)
111
  table = create_header(n_dim, checks)
112
  # tensor 1
113
+ if not checks:
114
  table += create_row(dim1)
115
+ else:
116
+ table += create_row(dim1, 1, checks, version)
117
 
118
  # tensor 2
119
+ if not checks:
120
  table += create_row(dim2)
121
+ else:
122
+ table += create_row(dim2, 2, checks, version)
123
  return table
124
 
125
 
 
138
  dim2 = placeholder
139
  return dim1, dim2
140
 
141
+
142
+ def check_validity(dim1, dim2):
143
  out = []
144
+ for i in range(len(dim1) - 2):
145
  if dim1[i] == dim2[i]:
146
  out.append("V")
147
+ else:
148
  out.append("X")
149
  # final dims
150
  if dim1[-1] == dim2[-2]:
151
+ out.extend(["V", "V"])
152
+ else:
153
+ out.extend(["X", "X"])
154
  return out
155
 
156
 
157
+ def substitute_ones_with_concat(dim1, dim2, version=1):
158
+ n = len(dim1) - 2 if version == 1 else len(dim1) - 1
159
  for i in range(n):
160
  dim1[i] = dim2[i] if dim1[i] == 1 else dim1[i]
161
  dim2[i] = dim1[i] if dim2[i] == 1 else dim2[i]
162
  return dim1, dim2
163
 
164
+
165
  def predict(dim1, dim2):
166
  dim1 = sanitize_dimention(dim1)
167
  dim2 = sanitize_dimention(dim2)
168
+ n1, n2 = len(dim1), len(dim2)
169
  dim1, dim2, out = generate_example(dim1, dim2)
170
+ # TODO
171
+ if n1 > 1 and n2 > 1:
172
+ # Table 1
173
  dim1, dim2 = alignment_and_fill_with_ones(dim1, dim2)
174
  table1 = generate_table(dim1, dim2)
175
+ # Table 2
176
+ dim1, dim2 = substitute_ones_with_concat(dim1, dim2)
177
  table2 = generate_table(dim1, dim2)
178
+ # Table 3
179
+ checks = check_validity(dim1, dim2)
180
+ table3 = generate_table(dim1, dim2, checks)
181
 
182
  out += "\n# Step1 (alignment and pre_append with ones)\n" + table1
183
+ out += (
184
+ "\n# Step2 (susbtitute columns that have 1 with concat)\nexcept for last 2 dimensions\n"
185
+ + table2
186
+ )
187
+ out += "\n# Step3 (check if matrix multiplication is valid)\n"
188
  out += "* last dimension of dim1 should equal before last dimension of dim2 (blue or yellow colors)\n"
189
+ out += (
190
+ "* all the other dimensions should be equal to one another (green or red colors)\n\n"
191
+ + table3
192
+ )
193
+ if "X" not in checks:
194
  dim1[-1] = dim2[-1]
195
  out += "\n# Final dimension\n"
196
+ out += "as highlighted in <strong style='color:green'> green </strong> \n\n"
197
+ out += f"`output.shape = {dim1}`"
198
  # case single dims
199
+ elif n1 == 1 and n2 == 1:
200
  out += "# Single Dimensional Cases\n"
201
  out += "When both matricies have only single dims they should both have the same number of values in the first dimension\n"
202
  out += "meaning that `t1.shape == t2.shape`\n"
203
  out += "the output is a single value, think : \n"
204
  out += matrix_loop
205
+ else:
206
  out += "# One of the dimensions is a single dimension\n"
207
  out += "In this case we need to assert that the last dimension of `t1` "
208
  out += "is equal to the last dimension of `t2`\n"
 
213
  out += table
214
  out += "\n# Step2 (susbtitute columns that have 1 with concat)\n"
215
  out += ""
216
+ dim1, dim2 = substitute_ones_with_concat(dim1, dim2, 2)
217
+ checks = ["V"] * (len(dim1) - 1)
218
+ if dim1[-1] == dim2[-1]:
219
  checks.append("V")
220
+ else:
221
  checks.append("X")
222
+ table = generate_table(dim1, dim2, checks, 2)
223
+ out += table
224
+ if "X" not in checks:
225
  out += "\n#Final dimension"
226
  out += "The final dimension is everything colored in <strong style='color:green'> green </strong> \n"
227
  out += f"\nfinal dimension = `{dim1[:-1]}` "
 
 
228
 
229
  return out
230
 
 
233
  predict,
234
  inputs=["text", "text"],
235
  outputs=["markdown"],
236
+ examples=[
237
+ ["9,2,1,3,3", "5,3,7"],
238
+ ["7,4,2,3", "5,2,7"],
239
+ ["4,5,6,7", "7"],
240
+ ["7,5,3", "4"],
241
+ ["5", "5"],
242
+ ["8", "2"],
243
+ ],
244
  )
245
 
246
  demo.launch(debug=True)