File size: 2,461 Bytes
331412c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
039cd66
331412c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
039cd66
 
 
 
 
 
 
 
 
 
1e8af48
039cd66
 
 
 
 
 
 
 
 
331412c
 
 
 
 
039cd66
 
 
 
 
 
 
331412c
 
 
 
039cd66
 
 
 
331412c
 
 
 
 
 
039cd66
331412c
 
 
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
import gradio as gr
import torch


EXAMPLE_MD = """
```python
import torch 

t1 = torch.arange({n1}).view({dim1})

t2 = torch.arange({n2}).view({dim2})

(t1 @ t2).shape = {out_shape} 

```

"""


def generate_example(dim1: list, dim2: list):
    n1 = 1
    n2 = 1
    for i in dim1:
        n1 *= i
    for i in dim2:
        n2 *= i

    t1 = torch.arange(n1).view(dim1)
    t2 = torch.arange(n2).view(dim2)
    try:
        out_shape = list((t1 @ t2).shape)
    except RuntimeError:
        out_shape = "error"

    return n1,dim1,n2,dim2,out_shape


def sanitize_dimention(dim):
    if dim is None:
        gr.Error("one of the dimentions is empty, please fill it")
    if "[" in dim:
        dim = dim.replace("[", "")
    if "]" in dim:
        dim = dim.replace("]", "")
    if "," in dim:
        dim = dim.replace(",", " ").strip()
        out = [int(i.strip()) for i in dim.split()]
    else:
        out = [int(dim.strip())]
    if 0 in out:
        gr.Error(
            "Found the number 0 in one of the dimensions which is not allowed, consider using 1 instead"
        )
    return out

def generate_table(n1,dim1,n2,dim2,out_shape,n_dim):
    makdown_table = ""
    makdown_table += "| <!-- --> "*n_dim + "|\n" # header
    makdown_table += "|---"*n_dim + "|\n" # line

    # tensor 1
    for index in range(n_dim):
        if index < (n_dim - n1):
            makdown_table += "| "
        else:
            makdown_table += f"| {dim1[index - (n_dim- n1)]}"
    makdown_table +="|\n"
    # tensor 2
    for index in range(n_dim):
        if index < (n_dim - n2):
            makdown_table += "| "
        else:
            makdown_table += f"| {dim2[index - (n_dim- n2)]}"
    makdown_table +="|\n"
    return makdown_table

def predict(dim1, dim2):
    dim1 = sanitize_dimention(dim1)
    dim2 = sanitize_dimention(dim2)
    n1,dim1,n2,dim2,out_shape =  generate_example(dim1, dim2)
    code =  EXAMPLE_MD.format(
        n1=str(n1), dim1=str(dim1), n2=str(n2), dim2=str(dim2), out_shape=str(out_shape)
    )
    n1 = len(dim1)
    n2 = len(dim2)
    n_dim = max(n1,n2)
    table1 = generate_table(n1,dim1,n2,dim2,out_shape,n_dim)

    # TODO 
    # add code exmplanation here
    
    
    out = code
    out+= "\n# Step1 (alignment)" + "\n" + table1
    return out


demo = gr.Interface(
    predict,
    inputs=["text", "text"],
    outputs=["markdown"],
    examples=[["1,2,3,3,3", "5,3,7"], ["1,2,3", "5,2,7"]],
)

demo.launch(debug=True)