Koen-E commited on
Commit
fb9daa8
·
verified ·
1 Parent(s): 5719c7d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -0
app.py ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn as nn
3
+ import torch.nn.functional as F
4
+ import gradio as gr
5
+
6
+ # --- Define Model ---
7
+ class FruitYieldModel(nn.Module):
8
+ def __init__(self):
9
+ super().__init__()
10
+ self.linear = nn.Linear(3, 2)
11
+
12
+ def forward(self, x):
13
+ return self.linear(x)
14
+
15
+ # --- Load & Train Data ---
16
+ inputs = torch.tensor([
17
+ [73, 67, 43],
18
+ [91, 88, 64],
19
+ [87, 134, 58],
20
+ [102, 43, 37],
21
+ [69, 96, 70],
22
+ [76, 112, 54],
23
+ [84, 70, 45],
24
+ [95, 122, 66],
25
+ [79, 89, 49],
26
+ [88, 111, 52],
27
+ [85, 95, 61],
28
+ [90, 84, 57],
29
+ [93, 128, 60],
30
+ [97, 77, 41],
31
+ [81, 102, 63]
32
+ ], dtype=torch.float32)
33
+
34
+ targets = torch.tensor([
35
+ [56, 70],
36
+ [81, 101],
37
+ [119, 133],
38
+ [22, 37],
39
+ [103, 119],
40
+ [68, 89],
41
+ [73, 84],
42
+ [110, 125],
43
+ [69, 91],
44
+ [95, 108],
45
+ [88, 99],
46
+ [86, 105],
47
+ [120, 130],
48
+ [39, 61],
49
+ [97, 112]
50
+ ], dtype=torch.float32)
51
+
52
+ # --- Model Training ---
53
+ model = FruitYieldModel()
54
+ model.load_state_dict(torch.load("fruit_model.pth", map_location=torch.device("cpu")))
55
+ model.eval()
56
+
57
+
58
+ # --- Prediction Function ---
59
+ def predict(temp, rain, humidity):
60
+ with torch.no_grad():
61
+ inp = torch.tensor([[temp, rain, humidity]], dtype=torch.float32)
62
+ output = model(inp).numpy().flatten()
63
+ return {
64
+ "Apple Yield": round(output[0], 2),
65
+ "Orange Yield": round(output[1], 2)
66
+ }
67
+
68
+ # --- Gradio Interface ---
69
+ interface = gr.Interface(
70
+ fn=predict,
71
+ inputs=[
72
+ gr.Number(label="Temperature (°F)"),
73
+ gr.Number(label="Rainfall (mm)"),
74
+ gr.Number(label="Humidity (%)")
75
+ ],
76
+ outputs=gr.Label(num_top_classes=2),
77
+ title="Fruit Yield Predictor",
78
+ description="Enter environmental data to predict apple and orange yields."
79
+ )
80
+
81
+ if __name__ == "__main__":
82
+ interface.launch()