baseline
Browse files- app.py +76 -0
- requirements.txt +1 -0
app.py
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
|
4 |
+
|
5 |
+
EXAMPLE_MD = """
|
6 |
+
```python
|
7 |
+
import torch
|
8 |
+
|
9 |
+
t1 = torch.arange({n1}).view({dim1})
|
10 |
+
|
11 |
+
t2 = torch.arange({n2}).view({dim2})
|
12 |
+
|
13 |
+
(t1 @ t2).shape = {out_shape}
|
14 |
+
|
15 |
+
```
|
16 |
+
"""
|
17 |
+
|
18 |
+
|
19 |
+
def generate_example(dim1: list, dim2: list):
|
20 |
+
n1 = 1
|
21 |
+
n2 = 1
|
22 |
+
for i in dim1:
|
23 |
+
n1 *= i
|
24 |
+
for i in dim2:
|
25 |
+
n2 *= i
|
26 |
+
|
27 |
+
t1 = torch.arange(n1).view(dim1)
|
28 |
+
t2 = torch.arange(n2).view(dim2)
|
29 |
+
try:
|
30 |
+
out_shape = list((t1 @ t2).shape)
|
31 |
+
except RuntimeError:
|
32 |
+
out_shape = "error"
|
33 |
+
|
34 |
+
return n1,dim1,n2,dim2,out_shape
|
35 |
+
|
36 |
+
|
37 |
+
def sanitize_dimention(dim):
|
38 |
+
if dim is None:
|
39 |
+
gr.Error("one of the dimentions is empty, please fill it")
|
40 |
+
if "[" in dim:
|
41 |
+
dim = dim.replace("[", "")
|
42 |
+
if "]" in dim:
|
43 |
+
dim = dim.replace("]", "")
|
44 |
+
if "," in dim:
|
45 |
+
dim = dim.replace(",", " ").strip()
|
46 |
+
out = [int(i.strip()) for i in dim.split()]
|
47 |
+
else:
|
48 |
+
out = [int(dim.strip())]
|
49 |
+
if 0 in out:
|
50 |
+
gr.Error(
|
51 |
+
"Found the number 0 in one of the dimensions which is not allowed, consider using 1 instead"
|
52 |
+
)
|
53 |
+
return out
|
54 |
+
|
55 |
+
|
56 |
+
def predict(dim1, dim2):
|
57 |
+
dim1 = sanitize_dimention(dim1)
|
58 |
+
dim2 = sanitize_dimention(dim2)
|
59 |
+
n1,dim1,n2,dim2,out_shape = generate_example(dim1, dim2)
|
60 |
+
|
61 |
+
# TODO
|
62 |
+
# add code exmplanation here
|
63 |
+
|
64 |
+
return EXAMPLE_MD.format(
|
65 |
+
n1=str(n1), dim1=str(dim1), s2=str(n2), dim2=str(dim2), out_shape=str(out_shape)
|
66 |
+
)
|
67 |
+
|
68 |
+
|
69 |
+
demo = gr.Interface(
|
70 |
+
predict,
|
71 |
+
inputs=["text", "text"],
|
72 |
+
outputs=["markdown"],
|
73 |
+
examples=[["1,2,3", "5,3,7"], ["1,2,3", "5,2,7"]],
|
74 |
+
)
|
75 |
+
|
76 |
+
demo.launch(debug=True)
|
requirements.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
torch
|