AzizWazir commited on
Commit
0dd468a
·
verified ·
1 Parent(s): 888c97a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -12
app.py CHANGED
@@ -1,41 +1,57 @@
1
- # Import necessary libraries
2
  import pandas as pd
3
  import numpy as np
4
  import folium
5
  from sklearn.cluster import KMeans
6
  import gradio as gr
 
7
 
8
- # Load dataset
9
  def load_data(file):
10
  data = pd.read_csv(file)
11
  return data
12
 
13
- # Function to process the data and apply clustering or regression model
14
  def process_data(data):
15
- X = data[['temperature', 'humidity', 'soil_type']] # Example features
16
- kmeans = KMeans(n_clusters=3) # Example clustering model
17
- data['moisture_category'] = kmeans.fit_predict(X)
 
 
 
 
 
 
18
  return data
19
 
20
- # Function to create an interactive map using folium
21
  def create_map(data):
 
22
  m = folium.Map(location=[data['latitude'].mean(), data['longitude'].mean()], zoom_start=12)
 
 
23
  for _, row in data.iterrows():
 
24
  folium.CircleMarker(location=[row['latitude'], row['longitude']],
25
  radius=10,
26
- color='blue' if row['moisture_category'] == 0 else 'green' if row['moisture_category'] == 1 else 'red',
27
  fill=True).add_to(m)
28
  return m
29
 
30
- # Gradio Interface
31
  def soil_moisture_mapping(file):
 
32
  data = load_data(file)
 
 
33
  processed_data = process_data(data)
 
 
34
  map_result = create_map(processed_data)
 
35
  return map_result
36
 
37
- # Gradio Interface setup
38
- iface = gr.Interface(fn=soil_moisture_mapping, inputs="file", outputs="html")
39
 
40
- # Launch Gradio app
41
  iface.launch()
 
 
1
  import pandas as pd
2
  import numpy as np
3
  import folium
4
  from sklearn.cluster import KMeans
5
  import gradio as gr
6
+ from sklearn.preprocessing import StandardScaler
7
 
8
+ # Function to load data from CSV file
9
  def load_data(file):
10
  data = pd.read_csv(file)
11
  return data
12
 
13
+ # Function to preprocess and apply clustering model to classify soil moisture
14
  def process_data(data):
15
+ # Preprocessing data (standardizing)
16
+ X = data[['temperature', 'humidity', 'soil_type']] # Example feature columns
17
+ scaler = StandardScaler()
18
+ X_scaled = scaler.fit_transform(X) # Scaling the data
19
+
20
+ # Clustering model: KMeans (you can replace with regression models as well)
21
+ kmeans = KMeans(n_clusters=3, random_state=42)
22
+ data['moisture_category'] = kmeans.fit_predict(X_scaled) # Classify into moisture categories (Low, Medium, High)
23
+
24
  return data
25
 
26
+ # Function to create an interactive folium map
27
  def create_map(data):
28
+ # Initializing the map at the center of the provided data
29
  m = folium.Map(location=[data['latitude'].mean(), data['longitude'].mean()], zoom_start=12)
30
+
31
+ # Plotting data points on the map
32
  for _, row in data.iterrows():
33
+ color = 'blue' if row['moisture_category'] == 0 else 'green' if row['moisture_category'] == 1 else 'red'
34
  folium.CircleMarker(location=[row['latitude'], row['longitude']],
35
  radius=10,
36
+ color=color,
37
  fill=True).add_to(m)
38
  return m
39
 
40
+ # Main function to process the uploaded file, analyze data, and create the map
41
  def soil_moisture_mapping(file):
42
+ # Load data
43
  data = load_data(file)
44
+
45
+ # Process data and apply the model
46
  processed_data = process_data(data)
47
+
48
+ # Generate map with the results
49
  map_result = create_map(processed_data)
50
+
51
  return map_result
52
 
53
+ # Setting up the Gradio interface for file upload
54
+ iface = gr.Interface(fn=soil_moisture_mapping, inputs="file", outputs="html", live=True)
55
 
56
+ # Launch the Gradio application
57
  iface.launch()