siddhantuniyal commited on
Commit
b42b45f
·
1 Parent(s): 159461c

Created hugging face space first time

Browse files
Files changed (6) hide show
  1. app.py +90 -0
  2. corn_model.h5 +3 -0
  3. crop_model.h5 +3 -0
  4. potato_model.h5 +3 -0
  5. rice_model.h5 +3 -0
  6. wheat_model.h5 +3 -0
app.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from keras.models import load_model
2
+ from fastapi import FastAPI
3
+ import gradio as gr
4
+ import requests
5
+ import numpy as np
6
+ from PIL import Image
7
+ from tensorflow import keras
8
+ from io import BytesIO
9
+ import uvicorn
10
+ from pyModel import Item
11
+
12
+
13
+
14
+ app = FastAPI()
15
+
16
+
17
+
18
+ @app.post('/post')
19
+ def newPost(str):
20
+ input_image = url_to_img(str)
21
+ processed_image = process(input_image)
22
+ crop = crop_predict(processed_image)
23
+ disease = disease_predict(processed_image, crop)
24
+ return [crop,disease]
25
+
26
+
27
+ def url_to_img(image_url):
28
+
29
+ # Load the image from the URL
30
+ response = requests.get(image_url)
31
+ input_image = Image.open(BytesIO(response.content))
32
+ return input_image
33
+
34
+
35
+ def process(input_image):
36
+
37
+ # Preprocess the image to match model's input dimensions
38
+ input_image = input_image.resize((224, 224)) # Adjust the dimensions
39
+ input_image = np.array(input_image) / 255.0 # Normalize pixel values to [0, 1]
40
+ processed_image = np.expand_dims(input_image, axis=0) # Add batch dimension
41
+ return processed_image
42
+
43
+ def crop_predict(processed_image):
44
+
45
+ # Load the trained model
46
+ model = keras.models.load_model('./crop_model.h5')
47
+ # Make predictions
48
+ predictions = model.predict(processed_image)
49
+ # Process the predictions (e.g., get class labels)
50
+ class_labels = ["Corn", "Potato", "Rice", "Wheat"]
51
+ predicted_class = np.argmax(predictions, axis=1)
52
+ predicted_label = class_labels[predicted_class[0]]
53
+ return predicted_label
54
+
55
+ def disease_predict(processed_image, crop):
56
+
57
+ if crop == "Potato":
58
+ # Load the trained model
59
+ model = keras.models.load_model('./potato_model.h5')
60
+ # Process the predictions (e.g., get class labels)
61
+ class_labels = ["Potato_Early_Blight", "Potato_Healthy", "Potato_Late_Blight"]
62
+
63
+ elif crop == "Corn":
64
+ # Load the trained model
65
+ model = keras.models.load_model('./corn_model.h5')
66
+ # Process the predictions (e.g., get class labels)
67
+ class_labels = ["Corn_Common_Rust", "Corn_Gray_Leaf_Spot", "Corn_Healthy", "Corn_Northern_Leaf_Blight"]
68
+
69
+ elif crop == "Rice":
70
+ # Load the trained model
71
+ model = keras.models.load_model('./rice_model.h5')
72
+ # Process the predictions (e.g., get class labels)
73
+ class_labels = ["Rice_Brown_Spot", "Rice_Healthy", "Rice_Leaf_Blast", "Rice_Neck_Blast"]
74
+
75
+ elif crop == "Wheat":
76
+ # Load the trained model
77
+ model = keras.models.load_model('./wheat_model.h5')
78
+ # Process the predictions (e.g., get class labels)
79
+ class_labels = ["Wheat_Brown_Rust", "Wheat_Healthy", "Wheat_Yellow_Rust"]
80
+
81
+ # Make predictions
82
+ predictions = model.predict(processed_image)
83
+ predicted_class = np.argmax(predictions, axis=1)
84
+ predicted_label = class_labels[predicted_class[0]]
85
+ return predicted_label
86
+
87
+
88
+ # iface = gr.Interface(fn = newPost , inputs=['text'] , outputs=['text'])
89
+
90
+ # iface.launch(share=True)
corn_model.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:11ae70a4bed9d5152d54c9321b3ea9a46e0222f89eed87c0f54482441a02cadb
3
+ size 134085664
crop_model.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:7b2a84a50154840d784718f5bf94af1618195d97acafc25df4029c69e6a1ec1d
3
+ size 134085664
potato_model.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5e2cb1f05f7837d4e76601e2d61b77ecc4d41db1367a411bded4ee0800d07b87
3
+ size 134083104
rice_model.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3f5ecd26b7b9cf977877bcfb966df594af446877aa5f6b1300b40236081bce74
3
+ size 134085664
wheat_model.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:78befd2189d6fe820442f896d652e9bf48db70f0073196a2c6aff2cdfad1d02a
3
+ size 134083104