-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from kavicastelo/backend
code clean and fixed xml reading bug
- Loading branch information
Showing
6 changed files
with
71 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 0 additions & 42 deletions
42
src/main/java/com/api/air_quality/model/ai_models/AirHumidityModel.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 54 additions & 6 deletions
60
src/main/java/com/api/air_quality/python/AirHumidityModelPython.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,67 @@ | ||
from pyspark.sql import SparkSession | ||
from joblib import load | ||
from py4j.java_gateway import JavaGateway | ||
import numpy as np | ||
from pyspark.sql.functions import udf | ||
from pyspark.sql.types import BooleanType | ||
|
||
|
||
class AirHumidityModelPython: | ||
def __init__(self): | ||
def __init__(self, spark): | ||
# Connect to the Py4J gateway server | ||
self.gateway = JavaGateway() | ||
|
||
# Retrieve the Java instance of the model | ||
self.java_model = self.gateway.entry_point.getAirHumidityModel() | ||
self.java_model = self.gateway.entry_point | ||
|
||
# Load the PMML model or any other necessary initialization logic | ||
self.model = load("../../../../../../../AI_Models/airHumidity_model.joblib") | ||
|
||
# Define the UDF for point_inside_polygon | ||
self.point_inside_polygon_udf = udf(self.point_inside_polygon, BooleanType()) | ||
|
||
# Example Spark DataFrame | ||
self.df = spark.createDataFrame([(1.0, 2.0)], ["lat", "long"]) | ||
|
||
def point_inside_polygon(self, lat, long, polygon): | ||
# Implement your point_inside_polygon logic here | ||
# Return True if the point is inside the polygon, else False | ||
pass | ||
|
||
def predict_air_humidity(self, features): | ||
# Implement your prediction logic here | ||
return self.java_model.predictAirHumidity(features) | ||
try: | ||
# Explicitly convert NumPy array to Python list | ||
features_list = [float(val) for val in np.array(features)] | ||
|
||
# Perform prediction using the loaded model | ||
prediction = self.model.predict([features_list]) | ||
|
||
# Example usage of UDF for point_inside_polygon | ||
# Replace lat and long with actual features from the model | ||
inside_polygon_result = self.df.where(self.point_inside_polygon_udf('lat', 'long', features_list)) | ||
|
||
# Pass the prediction and result to the Java side | ||
return self.java_model.receivePrediction(prediction, inside_polygon_result) | ||
except Exception as e: | ||
# Handle any errors during prediction | ||
return str(e) | ||
|
||
|
||
def main(): | ||
# Initialize Spark session | ||
spark = SparkSession.builder.appName("AirHumidityModelApp").getOrCreate() | ||
|
||
if __name__ == "__main__": | ||
# Create an instance of the Python class | ||
air_humidity_model = AirHumidityModelPython() | ||
air_humidity_model = AirHumidityModelPython(spark) | ||
|
||
# Example prediction | ||
features = [1.0, 2.0, 4.0, 5.0, 7.0, 9.0, 10.0] | ||
result = air_humidity_model.predict_air_humidity(features) | ||
print(result) | ||
|
||
# Stop the Spark session | ||
spark.stop() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
25 changes: 0 additions & 25 deletions
25
src/main/java/com/api/air_quality/service/ai_services/AirHumidityModelService.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters