|
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 = v1 |
|
self.v2 = v2 |
|
|
|
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 = print(f'The new vector 3 produced = vector 1: {self.v1} + vector 2: {self.v2} =', np.array([self.v1])+ np.array([self.v2])) |
|
return v3 |
|
|
|
def plot_vector_add(self): |
|
plt.figure(figsize=(10,5)) |
|
plt.plot([0, np.array(self.v1[0])], [0, np.array(self.v1[1])], 'b', label='v1') |
|
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') |
|
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') |
|
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.inputs.Textbox(lines=2, label="Vector 1"), |
|
gr.inputs.Textbox(lines=2, label="Vector 2")], |
|
[gr.outputs.Textbox(label="Vector 3"), |
|
gr.outputs.Image(label="Vector Addition Plot")]) |
|
|
|
iface.launch() |