Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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 |
-
#
|
| 9 |
def load_data(file):
|
| 10 |
data = pd.read_csv(file)
|
| 11 |
return data
|
| 12 |
|
| 13 |
-
# Function to
|
| 14 |
def process_data(data):
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
return data
|
| 19 |
|
| 20 |
-
# Function to create an interactive map
|
| 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=
|
| 27 |
fill=True).add_to(m)
|
| 28 |
return m
|
| 29 |
|
| 30 |
-
#
|
| 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
|
| 38 |
-
iface = gr.Interface(fn=soil_moisture_mapping, inputs="file", outputs="html")
|
| 39 |
|
| 40 |
-
# Launch Gradio
|
| 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()
|