TroglodyteDerivations's picture
Added a second element in lines 9 & 10
5fb72ba verified
raw
history blame
1.69 kB
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()