Spaces:
Runtime error
Runtime error
feat: add Gradio interfaces for vector operations; remove old vector interface file
Browse files
maths/vectors/{vectors_interface.py → utils.py}
RENAMED
@@ -5,10 +5,6 @@ import gradio as gr
|
|
5 |
import numpy as np
|
6 |
import json
|
7 |
from typing import Union
|
8 |
-
from maths.vectors.vector_add import vector_add
|
9 |
-
from maths.vectors.vector_subtract import vector_subtract
|
10 |
-
from maths.vectors.vector_dot_product import vector_dot_product
|
11 |
-
from maths.vectors.vector_cross_product import vector_cross_product
|
12 |
|
13 |
def parse_vector(vector_str: str) -> np.ndarray:
|
14 |
try:
|
@@ -37,48 +33,4 @@ def format_output(data: Union[np.ndarray, float, str]) -> str:
|
|
37 |
return str(data.tolist())
|
38 |
elif isinstance(data, (float, int, str)):
|
39 |
return str(data)
|
40 |
-
return "Output type not recognized."
|
41 |
-
|
42 |
-
vector_add_interface = gr.Interface(
|
43 |
-
fn=lambda v1_str, v2_str: format_output(vector_add(parse_vector(v1_str), parse_vector(v2_str))),
|
44 |
-
inputs=[
|
45 |
-
gr.Textbox(label="Vector 1 (JSON or CSV format)", placeholder="e.g., [1,2,3] or 1,2,3"),
|
46 |
-
gr.Textbox(label="Vector 2 (JSON or CSV format)", placeholder="e.g., [4,5,6] or 4,5,6")
|
47 |
-
],
|
48 |
-
outputs=gr.Textbox(label="Resulting Vector"),
|
49 |
-
title="Vector Addition",
|
50 |
-
description="Adds two vectors. Ensure they have the same dimensions."
|
51 |
-
)
|
52 |
-
|
53 |
-
vector_subtract_interface = gr.Interface(
|
54 |
-
fn=lambda v1_str, v2_str: format_output(vector_subtract(parse_vector(v1_str), parse_vector(v2_str))),
|
55 |
-
inputs=[
|
56 |
-
gr.Textbox(label="Vector 1 (Minuend)", placeholder="e.g., [4,5,6]"),
|
57 |
-
gr.Textbox(label="Vector 2 (Subtrahend)", placeholder="e.g., [1,2,3]")
|
58 |
-
],
|
59 |
-
outputs=gr.Textbox(label="Resulting Vector"),
|
60 |
-
title="Vector Subtraction",
|
61 |
-
description="Subtracts the second vector from the first. Ensure they have the same dimensions."
|
62 |
-
)
|
63 |
-
|
64 |
-
vector_dot_product_interface = gr.Interface(
|
65 |
-
fn=lambda v1_str, v2_str: format_output(vector_dot_product(parse_vector(v1_str), parse_vector(v2_str))),
|
66 |
-
inputs=[
|
67 |
-
gr.Textbox(label="Vector 1", placeholder="e.g., [1,2,3]"),
|
68 |
-
gr.Textbox(label="Vector 2", placeholder="e.g., [4,5,6]")
|
69 |
-
],
|
70 |
-
outputs=gr.Textbox(label="Dot Product (Scalar)"),
|
71 |
-
title="Vector Dot Product",
|
72 |
-
description="Calculates the dot product of two vectors. Ensure they have the same dimensions."
|
73 |
-
)
|
74 |
-
|
75 |
-
vector_cross_product_interface = gr.Interface(
|
76 |
-
fn=lambda v1_str, v2_str: format_output(vector_cross_product(parse_vector(v1_str), parse_vector(v2_str))),
|
77 |
-
inputs=[
|
78 |
-
gr.Textbox(label="Vector 1 (3D)", placeholder="e.g., [1,2,3]"),
|
79 |
-
gr.Textbox(label="Vector 2 (3D)", placeholder="e.g., [4,5,6]")
|
80 |
-
],
|
81 |
-
outputs=gr.Textbox(label="Resulting Vector (3D)"),
|
82 |
-
title="Vector Cross Product",
|
83 |
-
description="Calculates the cross product of two 3D vectors."
|
84 |
-
)
|
|
|
5 |
import numpy as np
|
6 |
import json
|
7 |
from typing import Union
|
|
|
|
|
|
|
|
|
8 |
|
9 |
def parse_vector(vector_str: str) -> np.ndarray:
|
10 |
try:
|
|
|
33 |
return str(data.tolist())
|
34 |
elif isinstance(data, (float, int, str)):
|
35 |
return str(data)
|
36 |
+
return "Output type not recognized."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
maths/vectors/vector_add.py
CHANGED
@@ -1,8 +1,10 @@
|
|
1 |
"""
|
2 |
Add two vectors of the same dimension using NumPy.
|
3 |
"""
|
|
|
4 |
import numpy as np
|
5 |
from typing import List, Union
|
|
|
6 |
|
7 |
Vector = Union[List[float], np.ndarray]
|
8 |
|
@@ -12,3 +14,14 @@ def vector_add(vector1: Vector, vector2: Vector) -> np.ndarray:
|
|
12 |
if v1.shape != v2.shape:
|
13 |
raise ValueError("Vectors must have the same dimensions for addition.")
|
14 |
return np.add(v1, v2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
"""
|
2 |
Add two vectors of the same dimension using NumPy.
|
3 |
"""
|
4 |
+
import gradio as gr
|
5 |
import numpy as np
|
6 |
from typing import List, Union
|
7 |
+
from maths.vectors.utils import parse_vector, format_output
|
8 |
|
9 |
Vector = Union[List[float], np.ndarray]
|
10 |
|
|
|
14 |
if v1.shape != v2.shape:
|
15 |
raise ValueError("Vectors must have the same dimensions for addition.")
|
16 |
return np.add(v1, v2)
|
17 |
+
|
18 |
+
vector_add_interface = gr.Interface(
|
19 |
+
fn=lambda v1_str, v2_str: format_output(vector_add(parse_vector(v1_str), parse_vector(v2_str))),
|
20 |
+
inputs=[
|
21 |
+
gr.Textbox(label="Vector 1 (JSON or CSV format)", placeholder="e.g., [1,2,3] or 1,2,3"),
|
22 |
+
gr.Textbox(label="Vector 2 (JSON or CSV format)", placeholder="e.g., [4,5,6] or 4,5,6")
|
23 |
+
],
|
24 |
+
outputs=gr.Textbox(label="Resulting Vector"),
|
25 |
+
title="Vector Addition",
|
26 |
+
description="Adds two vectors. Ensure they have the same dimensions."
|
27 |
+
)
|
maths/vectors/vector_cross_product.py
CHANGED
@@ -3,6 +3,9 @@ Compute the cross product of two 3-dimensional vectors using NumPy.
|
|
3 |
"""
|
4 |
import numpy as np
|
5 |
from typing import List, Union
|
|
|
|
|
|
|
6 |
|
7 |
Vector = Union[List[float], np.ndarray]
|
8 |
|
@@ -12,3 +15,14 @@ def vector_cross_product(vector1: Vector, vector2: Vector) -> np.ndarray:
|
|
12 |
if v1.shape != (3,) or v2.shape != (3,):
|
13 |
raise ValueError("Both vectors must be 3-dimensional for cross product.")
|
14 |
return np.cross(v1, v2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
"""
|
4 |
import numpy as np
|
5 |
from typing import List, Union
|
6 |
+
import gradio as gr
|
7 |
+
|
8 |
+
from maths.vectors.utils import parse_vector, format_output
|
9 |
|
10 |
Vector = Union[List[float], np.ndarray]
|
11 |
|
|
|
15 |
if v1.shape != (3,) or v2.shape != (3,):
|
16 |
raise ValueError("Both vectors must be 3-dimensional for cross product.")
|
17 |
return np.cross(v1, v2)
|
18 |
+
|
19 |
+
vector_cross_product_interface = gr.Interface(
|
20 |
+
fn=lambda v1_str, v2_str: format_output(vector_cross_product(parse_vector(v1_str), parse_vector(v2_str))),
|
21 |
+
inputs=[
|
22 |
+
gr.Textbox(label="Vector 1 (3D)", placeholder="e.g., [1,2,3]"),
|
23 |
+
gr.Textbox(label="Vector 2 (3D)", placeholder="e.g., [4,5,6]")
|
24 |
+
],
|
25 |
+
outputs=gr.Textbox(label="Resulting Vector (3D)"),
|
26 |
+
title="Vector Cross Product",
|
27 |
+
description="Calculates the cross product of two 3D vectors."
|
28 |
+
)
|
maths/vectors/vector_dot_product.py
CHANGED
@@ -3,6 +3,9 @@ Compute the dot product of two vectors of the same dimension using NumPy.
|
|
3 |
"""
|
4 |
import numpy as np
|
5 |
from typing import List, Union
|
|
|
|
|
|
|
6 |
|
7 |
Vector = Union[List[float], np.ndarray]
|
8 |
|
@@ -12,3 +15,14 @@ def vector_dot_product(vector1: Vector, vector2: Vector) -> float:
|
|
12 |
if v1.shape != v2.shape:
|
13 |
raise ValueError("Vectors must have the same dimensions for dot product.")
|
14 |
return np.dot(v1, v2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
"""
|
4 |
import numpy as np
|
5 |
from typing import List, Union
|
6 |
+
import gradio as gr
|
7 |
+
|
8 |
+
from maths.vectors.utils import parse_vector, format_output
|
9 |
|
10 |
Vector = Union[List[float], np.ndarray]
|
11 |
|
|
|
15 |
if v1.shape != v2.shape:
|
16 |
raise ValueError("Vectors must have the same dimensions for dot product.")
|
17 |
return np.dot(v1, v2)
|
18 |
+
|
19 |
+
vector_dot_product_interface = gr.Interface(
|
20 |
+
fn=lambda v1_str, v2_str: format_output(vector_dot_product(parse_vector(v1_str), parse_vector(v2_str))),
|
21 |
+
inputs=[
|
22 |
+
gr.Textbox(label="Vector 1", placeholder="e.g., [1,2,3]"),
|
23 |
+
gr.Textbox(label="Vector 2", placeholder="e.g., [4,5,6]")
|
24 |
+
],
|
25 |
+
outputs=gr.Textbox(label="Dot Product (Scalar)"),
|
26 |
+
title="Vector Dot Product",
|
27 |
+
description="Calculates the dot product of two vectors. Ensure they have the same dimensions."
|
28 |
+
)
|
maths/vectors/vector_subtract.py
CHANGED
@@ -3,6 +3,9 @@ Subtract one vector from another of the same dimension using NumPy.
|
|
3 |
"""
|
4 |
import numpy as np
|
5 |
from typing import List, Union
|
|
|
|
|
|
|
6 |
|
7 |
Vector = Union[List[float], np.ndarray]
|
8 |
|
@@ -12,3 +15,14 @@ def vector_subtract(vector1: Vector, vector2: Vector) -> np.ndarray:
|
|
12 |
if v1.shape != v2.shape:
|
13 |
raise ValueError("Vectors must have the same dimensions for subtraction.")
|
14 |
return np.subtract(v1, v2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
"""
|
4 |
import numpy as np
|
5 |
from typing import List, Union
|
6 |
+
import gradio as gr
|
7 |
+
|
8 |
+
from maths.vectors.utils import parse_vector, format_output
|
9 |
|
10 |
Vector = Union[List[float], np.ndarray]
|
11 |
|
|
|
15 |
if v1.shape != v2.shape:
|
16 |
raise ValueError("Vectors must have the same dimensions for subtraction.")
|
17 |
return np.subtract(v1, v2)
|
18 |
+
|
19 |
+
vector_subtract_interface = gr.Interface(
|
20 |
+
fn=lambda v1_str, v2_str: format_output(vector_subtract(parse_vector(v1_str), parse_vector(v2_str))),
|
21 |
+
inputs=[
|
22 |
+
gr.Textbox(label="Vector 1 (Minuend)", placeholder="e.g., [4,5,6]"),
|
23 |
+
gr.Textbox(label="Vector 2 (Subtrahend)", placeholder="e.g., [1,2,3]")
|
24 |
+
],
|
25 |
+
outputs=gr.Textbox(label="Resulting Vector"),
|
26 |
+
title="Vector Subtraction",
|
27 |
+
description="Subtracts the second vector from the first. Ensure they have the same dimensions."
|
28 |
+
)
|
maths/vectors/vectors_tab.py
CHANGED
@@ -1,8 +1,8 @@
|
|
1 |
import gradio as gr
|
2 |
-
from maths.vectors.
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
|
7 |
vectors_interfaces_list = [
|
8 |
vector_add_interface, vector_subtract_interface,
|
|
|
1 |
import gradio as gr
|
2 |
+
from maths.vectors.vector_add import vector_add_interface
|
3 |
+
from maths.vectors.vector_subtract import vector_subtract_interface
|
4 |
+
from maths.vectors.vector_dot_product import vector_dot_product_interface
|
5 |
+
from maths.vectors.vector_cross_product import vector_cross_product_interface
|
6 |
|
7 |
vectors_interfaces_list = [
|
8 |
vector_add_interface, vector_subtract_interface,
|