Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
import torch.nn as nn
|
4 |
+
|
5 |
+
# Define the model architecture
|
6 |
+
class SumModel(nn.Module):
|
7 |
+
def __init__(self):
|
8 |
+
super(SumModel, self).__init__()
|
9 |
+
self.fc1 = nn.Linear(2, 128)
|
10 |
+
self.fc2 = nn.Linear(128, 128)
|
11 |
+
self.fc3 = nn.Linear(128, 1)
|
12 |
+
|
13 |
+
def forward(self, x):
|
14 |
+
x = torch.relu(self.fc1(x))
|
15 |
+
x = torch.relu(self.fc2(x))
|
16 |
+
x = self.fc3(x)
|
17 |
+
return x
|
18 |
+
|
19 |
+
# Load the pre-trained model
|
20 |
+
model = SumModel()
|
21 |
+
model.load_state_dict(torch.load('sum_model.pth'))
|
22 |
+
model.eval() # Set the model to evaluation mode
|
23 |
+
|
24 |
+
# Function to predict the sum of two numbers
|
25 |
+
def calculate_sum(num1, num2):
|
26 |
+
# Prepare the input tensor
|
27 |
+
inputs = torch.tensor([[num1, num2]]).float()
|
28 |
+
|
29 |
+
# Forward pass
|
30 |
+
with torch.no_grad(): # Disable gradient tracking during inference
|
31 |
+
outputs = model(inputs)
|
32 |
+
|
33 |
+
# Get the predicted sum
|
34 |
+
predicted_sum = outputs.item()
|
35 |
+
|
36 |
+
return predicted_sum
|
37 |
+
|
38 |
+
# Create a Gradio interface
|
39 |
+
iface = gr.Interface(
|
40 |
+
fn=calculate_sum,
|
41 |
+
inputs=["number", "number"],
|
42 |
+
outputs="number",
|
43 |
+
title="Sum Predictor",
|
44 |
+
description="Enter two numbers to predict their sum"
|
45 |
+
)
|
46 |
+
|
47 |
+
# Launch the Gradio app
|
48 |
+
iface.launch()
|