Pārlūkot izejas kodu

Changes in server.py file

oscarleiva 7 gadi atpakaļ
vecāks
revīzija
58288ede17
2 mainītis faili ar 65 papildinājumiem un 33 dzēšanām
  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 pandas as pd
 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
 
+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.json_encoder = ApiJsonEncoder
 
 @app.route('/')
 def index():
@@ -21,13 +31,18 @@ def apicall():
 	Pandas dataframe (sent as a payload) from API Call
 	"""
 	prepro = PreProcessing()
-	print(request)
+
+	
 	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 = 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
 		forecast_datetime = data.index
@@ -40,6 +55,7 @@ def apicall():
 
 	if data.empty:
 		return(bad_request())
+		
 	else:
 		#Load the saved model
 		print("Loading the model...")
@@ -60,12 +76,12 @@ def apicall():
 		"""
 		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.
 		   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
 
 		return (responses)
@@ -83,4 +99,4 @@ def bad_request(error=None):
 	return resp
 
 if __name__ == '__main__':
-	app.run(host='0.0.0.0')
+	app.run(host='0.0.0.0', debug=True)

Failā izmaiņas netiks attēlotas, jo tās ir par lielu
+ 40 - 24
notebooks/test-flask-api.ipynb


Daži faili netika attēloti, jo izmaiņu fails ir pārāk liels