Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,66 +1,40 @@
|
|
| 1 |
-
import torch
|
| 2 |
import gradio as gr
|
|
|
|
|
|
|
| 3 |
import spaces
|
| 4 |
|
| 5 |
-
# Define
|
| 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 |
-
# Load the model
|
| 33 |
-
@spaces.GPU
|
| 34 |
-
def load_model():
|
| 35 |
-
model = SimpleAdditionModel()
|
| 36 |
-
model.load_state_dict(torch.load('simple_addition_model.pth'))
|
| 37 |
-
model.eval()
|
| 38 |
-
return model
|
| 39 |
-
|
| 40 |
-
# Train and save the model if it doesn't exist
|
| 41 |
-
try:
|
| 42 |
-
model = load_model()
|
| 43 |
-
except FileNotFoundError:
|
| 44 |
-
print("Training model...")
|
| 45 |
-
train_model()
|
| 46 |
-
model = load_model()
|
| 47 |
-
|
| 48 |
-
# Prediction function
|
| 49 |
-
@spaces.GPU
|
| 50 |
-
def add_numbers(num1, num2):
|
| 51 |
-
input_tensor = torch.tensor([num1, num2], dtype=torch.float32).unsqueeze(0)
|
| 52 |
-
with torch.no_grad():
|
| 53 |
-
result = model(input_tensor)
|
| 54 |
-
return round(result.item(), 2)
|
| 55 |
-
|
| 56 |
-
# Create Gradio interface
|
| 57 |
-
iface = gr.Interface(
|
| 58 |
-
fn=add_numbers,
|
| 59 |
-
inputs=[gr.Number(label="Number 1"), gr.Number(label="Number 2")],
|
| 60 |
-
outputs=gr.Number(label="Sum"),
|
| 61 |
-
title="Simple Addition Calculator",
|
| 62 |
-
description="Enter two numbers to see their sum (approximated by a neural network)."
|
| 63 |
-
)
|
| 64 |
|
| 65 |
# Launch the interface
|
| 66 |
-
iface.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
from sklearn.linear_model import LinearRegression
|
| 4 |
import spaces
|
| 5 |
|
| 6 |
+
# Define the linear regression function
|
| 7 |
+
def linear_regression(X, y, new_data):
|
| 8 |
+
X = np.array(X).reshape(-1, 1) # Reshape for sklearn
|
| 9 |
+
y = np.array(y)
|
| 10 |
+
|
| 11 |
+
# Perform the computation within the GPU context
|
| 12 |
+
with spaces.GPU():
|
| 13 |
+
model = LinearRegression()
|
| 14 |
+
model.fit(X, y)
|
| 15 |
+
prediction = model.predict(np.array(new_data).reshape(-1, 1))
|
| 16 |
+
|
| 17 |
+
return prediction.tolist()
|
| 18 |
+
|
| 19 |
+
# Create the Gradio interface
|
| 20 |
+
with gr.Blocks(title="Linear Regression with ZeroGPU") as iface:
|
| 21 |
+
gr.Markdown("# Linear Regression Example")
|
| 22 |
+
gr.Markdown("This example performs linear regression using ZeroGPU for computation.")
|
| 23 |
+
|
| 24 |
+
with gr.Row():
|
| 25 |
+
with gr.Column():
|
| 26 |
+
X = gr.Textbox(lines=5, label="Input Features (X)", placeholder="e.g., 1, 2, 3, 4")
|
| 27 |
+
y = gr.Textbox(lines=5, label="Target Values (y)", placeholder="e.g., 2, 4, 6, 8")
|
| 28 |
+
new_data = gr.Textbox(lines=1, label="New Data for Prediction", placeholder="e.g., 5")
|
| 29 |
+
submit_btn = gr.Button("Predict")
|
| 30 |
|
| 31 |
+
output = gr.Textbox(lines=5, label="Predicted Values")
|
| 32 |
+
|
| 33 |
+
submit_btn.click(
|
| 34 |
+
fn=linear_regression,
|
| 35 |
+
inputs=[X, y, new_data],
|
| 36 |
+
outputs=output
|
| 37 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
# Launch the interface
|
| 40 |
+
iface.launch(server_name="0.0.0.0", server_port=7860)
|