Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,41 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
# Load
|
| 4 |
-
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
# Function
|
| 7 |
-
def
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|