GodfreyOwino commited on
Commit
2b8c0f6
·
verified ·
1 Parent(s): 3547368

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +53 -0
app.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoConfig, AutoModel
3
+ import torch
4
+
5
+ # Load the model
6
+ config = AutoConfig.from_pretrained("GodfreyOwino/NPK_prediction_model2", trust_remote_code=True)
7
+ model = AutoModel.from_pretrained("GodfreyOwino/NPK_prediction_model2", config=config, trust_remote_code=True)
8
+
9
+ def predict(crop_name, target_yield, field_size, ph, organic_carbon, nitrogen, phosphorus, potassium, soil_moisture):
10
+ input_data = {
11
+ 'crop_name': [crop_name],
12
+ 'target_yield': [target_yield],
13
+ 'field_size': [field_size],
14
+ 'ph': [ph],
15
+ 'organic_carbon': [organic_carbon],
16
+ 'nitrogen': [nitrogen],
17
+ 'phosphorus': [phosphorus],
18
+ 'potassium': [potassium],
19
+ 'soil_moisture': [soil_moisture]
20
+ }
21
+
22
+ # Convert input data to tensors
23
+ input_tensors = {k: torch.tensor(v) for k, v in input_data.items()}
24
+
25
+ # Make prediction
26
+ with torch.no_grad():
27
+ prediction = model(input_tensors)
28
+
29
+ # Convert prediction to a list if it's a tensor
30
+ result = prediction.tolist() if isinstance(prediction, torch.Tensor) else prediction
31
+
32
+ return str(result) # Convert to string for Gradio output
33
+
34
+ # Define Gradio interface
35
+ iface = gr.Interface(
36
+ fn=predict,
37
+ inputs=[
38
+ gr.Textbox(label="Crop Name"),
39
+ gr.Number(label="Target Yield"),
40
+ gr.Number(label="Field Size"),
41
+ gr.Number(label="pH"),
42
+ gr.Number(label="Organic Carbon"),
43
+ gr.Number(label="Nitrogen"),
44
+ gr.Number(label="Phosphorus"),
45
+ gr.Number(label="Potassium"),
46
+ gr.Number(label="Soil Moisture")
47
+ ],
48
+ outputs="text",
49
+ title="NPK Prediction Model",
50
+ description="Enter the details to get NPK predictions."
51
+ )
52
+
53
+ iface.launch()