Spaces:
Sleeping
Sleeping
File size: 1,387 Bytes
ada37b3 a8296ef 0d8ed91 ada37b3 a8296ef 75fb311 a8296ef b9632a5 ada37b3 a8296ef | 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 | import gradio as gr
import numpy as np
from sklearn.linear_model import LinearRegression
import spaces
# Define the linear regression function
def linear_regression(X, y, new_data):
X = np.array(X).reshape(-1, 1) # Reshape for sklearn
y = np.array(y)
# Perform the computation within the GPU context
with spaces.GPU():
model = LinearRegression()
model.fit(X, y)
prediction = model.predict(np.array(new_data).reshape(-1, 1))
return prediction.tolist()
# Create the Gradio interface
with gr.Blocks(title="Linear Regression with ZeroGPU") as iface:
gr.Markdown("# Linear Regression Example")
gr.Markdown("This example performs linear regression using ZeroGPU for computation.")
with gr.Row():
with gr.Column():
X = gr.Textbox(lines=5, label="Input Features (X)", placeholder="e.g., 1, 2, 3, 4")
y = gr.Textbox(lines=5, label="Target Values (y)", placeholder="e.g., 2, 4, 6, 8")
new_data = gr.Textbox(lines=1, label="New Data for Prediction", placeholder="e.g., 5")
submit_btn = gr.Button("Predict")
output = gr.Textbox(lines=5, label="Predicted Values")
submit_btn.click(
fn=linear_regression,
inputs=[X, y, new_data],
outputs=output
)
# Launch the interface
iface.launch(server_name="0.0.0.0", server_port=7860)
|