vishnu714 commited on
Commit
a8296ef
·
verified ·
1 Parent(s): ec07180

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -60
app.py CHANGED
@@ -1,66 +1,40 @@
1
- import torch
2
  import gradio as gr
 
 
3
  import spaces
4
 
5
- # Define a simple model
6
- class SimpleAdditionModel(torch.nn.Module):
7
- def __init__(self):
8
- super().__init__()
9
- self.layer = torch.nn.Linear(2, 1)
10
-
11
- def forward(self, x):
12
- return self.layer(x)
13
-
14
- # Train and save the model
15
- def train_model():
16
- model = SimpleAdditionModel()
17
- optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
18
- criterion = torch.nn.MSELoss()
19
-
20
- for _ in range(1000):
21
- x = torch.rand(100, 2) * 10 # Random numbers between 0 and 10
22
- y = x.sum(dim=1, keepdim=True)
 
 
 
 
 
 
23
 
24
- optimizer.zero_grad()
25
- output = model(x)
26
- loss = criterion(output, y)
27
- loss.backward()
28
- optimizer.step()
29
-
30
- torch.save(model.state_dict(), 'simple_addition_model.pth')
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)