Преглед на файлове

Changes in server.py file

oscarleiva преди 7 години
родител
ревизия
58288ede17
променени са 2 файла, в които са добавени 65 реда и са изтрити 33 реда
  1. 25 9
      flask_api/app/server.py
  2. 40 24
      notebooks/test-flask-api.ipynb

+ 25 - 9
flask_api/app/server.py

@@ -1,12 +1,22 @@
 import os
 import os
 import pandas as pd
 import pandas as pd
 import pickle
 import pickle
-from flask import Flask, jsonify, request
-from .utils import PreProcessing
+from flask import Flask, jsonify, request, json
+from utils import PreProcessing
+from datetime import date, datetime
+
 
 
 from sklearn.externals import joblib
 from sklearn.externals import joblib
 
 
+class ApiJsonEncoder(json.JSONEncoder):
+    def default(self, obj):
+        if isinstance(obj, (date, datetime)):
+            return obj.isoformat()
+        return super(ApiJsonEncoder, self).default(obj)
+
+
 app = Flask(__name__)
 app = Flask(__name__)
+app.json_encoder = ApiJsonEncoder
 
 
 @app.route('/')
 @app.route('/')
 def index():
 def index():
@@ -21,13 +31,18 @@ def apicall():
 	Pandas dataframe (sent as a payload) from API Call
 	Pandas dataframe (sent as a payload) from API Call
 	"""
 	"""
 	prepro = PreProcessing()
 	prepro = PreProcessing()
-	print(request)
+
+	
 	try:
 	try:
-		data_json = request.get_json()
-		data = pd.read_json(data_json, orient='index')
+		data_json = request.data
+		print(str(type(data_json)))
+		data = pd.read_json(data_json, orient='split')
+
+		data['fecha_hora'] = pd.to_datetime(data['fecha_hora'])
+		data.set_index('fecha_hora', inplace=True)
 
 
 		data = prepro.process(data)
 		data = prepro.process(data)
-		data = data[data.index.date == data.index.max().date()].drop('P', axis=1)
+		data = data[data.index.date == data.index.max().date()].drop('price', axis=1)
 
 
 		#Getting the datetime into separated out
 		#Getting the datetime into separated out
 		forecast_datetime = data.index
 		forecast_datetime = data.index
@@ -40,6 +55,7 @@ def apicall():
 
 
 	if data.empty:
 	if data.empty:
 		return(bad_request())
 		return(bad_request())
+		
 	else:
 	else:
 		#Load the saved model
 		#Load the saved model
 		print("Loading the model...")
 		print("Loading the model...")
@@ -60,12 +76,12 @@ def apicall():
 		"""
 		"""
 		prediction_series = list(pd.Series(predictions))
 		prediction_series = list(pd.Series(predictions))
 
 
-		final_predictions = pd.DataFrame({'pred_price': prediction_series}, index=forecast_datetime)
+		final_predictions = pd.DataFrame({'fecha_hora': forecast_datetime, 'pred_price': prediction_series})
 		
 		
 		"""We can be as creative in sending the responses.
 		"""We can be as creative in sending the responses.
 		   But we need to send the response codes as well.
 		   But we need to send the response codes as well.
 		"""
 		"""
-		responses = jsonify(predictions=final_predictions.to_json(orient="index", date_format='iso'))
+		responses = jsonify(predictions=final_predictions.to_dict(orient="records"))
 		responses.status_code = 200
 		responses.status_code = 200
 
 
 		return (responses)
 		return (responses)
@@ -83,4 +99,4 @@ def bad_request(error=None):
 	return resp
 	return resp
 
 
 if __name__ == '__main__':
 if __name__ == '__main__':
-	app.run(host='0.0.0.0')
+	app.run(host='0.0.0.0', debug=True)

Файловите разлики са ограничени, защото са твърде много
+ 40 - 24
notebooks/test-flask-api.ipynb


Някои файлове не бяха показани, защото твърде много файлове са промени