TroglodyteDerivations commited on
Commit
2825d1e
1 Parent(s): 3c18979

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -0
app.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import matplotlib.pyplot as plt
4
+ from PIL import Image
5
+ from io import BytesIO
6
+
7
+ class Vector_Addition:
8
+ def __init__(self, v1, v2):
9
+ self.v1 = v1
10
+ self.v2 = v2
11
+
12
+ def problem_add(self):
13
+ problem_add_prompt = print(f'What does vector 1: {self.v1} + vector 2: {self.v2} equal?')
14
+ return problem_add_prompt
15
+
16
+ def vector_add(self):
17
+ v3 = print(f'The new vector 3 produced = vector 1: {self.v1} + vector 2: {self.v2} =', np.array([self.v1])+ np.array([self.v2]))
18
+ return v3
19
+
20
+ def plot_vector_add(self):
21
+ plt.figure(figsize=(10,5))
22
+ plt.plot([0, np.array(self.v1[0])], [0, np.array(self.v1[1])], 'b', label='v1')
23
+ plt.plot([0, np.array(self.v2[0])+np.array(self.v1[0])], [0, np.array(self.v2[1])+np.array(self.v1[1])], 'r', label='v2')
24
+ plt.plot([0, np.array(self.v1[0])+ np.array(self.v2[0])], [0, np.array(self.v1[1])+ np.array(self.v2[1])], 'k', label='v1+v2')
25
+ plt.xlabel('X')
26
+ plt.ylabel('Y')
27
+ plt.title('Vector Addition')
28
+ plt.grid(True)
29
+ plt.legend()
30
+
31
+ buf = BytesIO()
32
+ plt.savefig(buf, format='png')
33
+ buf.seek(0)
34
+ image = Image.open(buf)
35
+ plt.close()
36
+ return image
37
+
38
+ def vector_addition(v1, v2):
39
+ vector_addition = Vector_Addition(v1, v2)
40
+ v3 = vector_addition.vector_add()
41
+ plot = vector_addition.plot_vector_add()
42
+ return v3, plot
43
+
44
+ iface = gr.Interface(vector_addition,
45
+ [gr.inputs.Textbox(lines=2, label="Vector 1"),
46
+ gr.inputs.Textbox(lines=2, label="Vector 2")],
47
+ [gr.outputs.Textbox(label="Vector 3"),
48
+ gr.outputs.Image(label="Vector Addition Plot")])
49
+
50
+ iface.launch()