amirkhanbloch commited on
Commit
af64878
·
verified ·
1 Parent(s): 690b517

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -3
app.py CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
2
  import numpy as np
3
  import pandas as pd
4
  from catboost import CatBoostRegressor
 
5
 
6
  # Load CatBoost models
7
  modelos_cargados = []
@@ -25,17 +26,27 @@ array_cols = ['array_area', *[f'mean_{i}' for i in range(1, 151)],
25
  # Total expected columns
26
  expected_columns_count = len(array_cols) # This should be 1501
27
 
 
 
 
 
 
 
 
 
 
28
  def process_soil_image(image):
29
- # Dummy properties extraction (simulate the real extraction)
30
  arr = np.array(image) # Convert the image to a numpy array
31
  if arr.ndim != 3:
32
  raise ValueError("Expected a 3-dimensional array (height, width, channels).")
33
 
34
- # Simulated property extraction
35
- properties = np.random.rand(1500) # Simulated for demonstration, should match expected_columns_count - 1
36
  return properties
37
 
38
  def predecir_desde_imagen(image, modelos, array_cols):
 
39
  # Process the soil image to extract properties
40
  properties = process_soil_image(image)
41
  print(f"Extracted properties shape: {properties.shape}") # Debug statement
@@ -60,6 +71,7 @@ def predecir_desde_imagen(image, modelos, array_cols):
60
  return mediana_predicciones
61
 
62
  def predecir_desde_imagen_interface(image):
 
63
  # Ensure the image is in the expected format
64
  predicciones = predecir_desde_imagen(image, modelos_cargados, array_cols)
65
 
@@ -70,10 +82,12 @@ def predecir_desde_imagen_interface(image):
70
  'pH': float(predicciones[3])
71
  }
72
 
 
73
  demo = gr.Interface(
74
  fn=predecir_desde_imagen_interface,
75
  inputs=gr.Image(label="Upload Soil Image"),
76
  outputs=gr.JSON(label="Predictions")
77
  )
78
 
 
79
  demo.launch() # No share=True since it's not supported in Hugging Face Spaces
 
2
  import numpy as np
3
  import pandas as pd
4
  from catboost import CatBoostRegressor
5
+ from sklearn.decomposition import PCA
6
 
7
  # Load CatBoost models
8
  modelos_cargados = []
 
26
  # Total expected columns
27
  expected_columns_count = len(array_cols) # This should be 1501
28
 
29
+ # Initialize PCA
30
+ pca = PCA(n_components=10) # Adjust n_components as needed
31
+
32
+ def get_pca_dataset(datos_df):
33
+ """Fit PCA on the given DataFrame and return transformed data."""
34
+ pca.fit(datos_df) # Fit PCA on the training data (do this once on your training dataset)
35
+ transformed_data = pca.transform(datos_df) # Transform the new data
36
+ return transformed_data
37
+
38
  def process_soil_image(image):
39
+ """Extract properties from the uploaded soil image."""
40
  arr = np.array(image) # Convert the image to a numpy array
41
  if arr.ndim != 3:
42
  raise ValueError("Expected a 3-dimensional array (height, width, channels).")
43
 
44
+ # Simulated property extraction (replace with actual logic)
45
+ properties = np.random.rand(1500) # Simulated for demonstration
46
  return properties
47
 
48
  def predecir_desde_imagen(image, modelos, array_cols):
49
+ """Predict soil properties from the uploaded image."""
50
  # Process the soil image to extract properties
51
  properties = process_soil_image(image)
52
  print(f"Extracted properties shape: {properties.shape}") # Debug statement
 
71
  return mediana_predicciones
72
 
73
  def predecir_desde_imagen_interface(image):
74
+ """Gradio interface function to handle the prediction request."""
75
  # Ensure the image is in the expected format
76
  predicciones = predecir_desde_imagen(image, modelos_cargados, array_cols)
77
 
 
82
  'pH': float(predicciones[3])
83
  }
84
 
85
+ # Create Gradio interface
86
  demo = gr.Interface(
87
  fn=predecir_desde_imagen_interface,
88
  inputs=gr.Image(label="Upload Soil Image"),
89
  outputs=gr.JSON(label="Predictions")
90
  )
91
 
92
+ # Launch the Gradio application
93
  demo.launch() # No share=True since it's not supported in Hugging Face Spaces