File size: 1,697 Bytes
2825d1e 55f6edc 2825d1e 5fb72ba 2825d1e 6803e5f 2825d1e 863b380 2825d1e 789f285 a679715 b7cc2fd 2825d1e 1b66d7e 2825d1e fdecb76 a78f833 2825d1e 55f6edc 2825d1e caf082f 4cdb8c0 2825d1e |
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 |
import gradio as gr
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
from io import BytesIO
class Vector_Addition:
def __init__(self, v1, v2):
self.v1 = np.array([v1, 1])
self.v2 = np.array([v2, 3])
def problem_add(self):
problem_add_prompt = print(f'What does vector 1: {self.v1} + vector 2: {self.v2} equal?')
return problem_add_prompt
def vector_add(self):
v3 = self.v1 + self.v2
v3_text = f'The new vector 3 produced = vector 1: {self.v1} + vector 2: {self.v2} = {v3}'
return v3_text
def plot_vector_add(self):
plt.figure(figsize=(10,5))
plt.plot([0, self.v1[0]], [0, self.v1[1]], 'b', label='v1')
plt.plot([0, self.v2[0]]+self.v1[0], [0, self.v2[1]]+self.v1[1], 'r', label='v2')
plt.plot([0, self.v1[0]+ self.v2[0]], [0, self.v1[1]+ self.v2[1]], 'k', label='v1+v2')
plt.xlabel('X')
plt.ylabel('Y')
plt.title('Vector Addition')
plt.grid(True)
plt.legend()
buf = BytesIO()
plt.savefig(buf, format='png')
buf.seek(0)
image = Image.open(buf)
plt.close()
return image
def vector_addition(v1, v2):
vector_addition = Vector_Addition(v1, v2)
v3 = vector_addition.vector_add()
plot = vector_addition.plot_vector_add()
return v3, plot
iface = gr.Interface(vector_addition,
[gr.components.Number(label="Vector 1"),
gr.components.Number(label="Vector 2")],
[gr.components.Textbox(label="Vector 3"),
gr.components.Image(label="Vector Addition Plot")])
iface.launch() |