xuan-superfine commited on
Commit
9692147
·
verified ·
1 Parent(s): b897400

Upload model.py

Browse files

add in API deployment file

Files changed (1) hide show
  1. model.py +50 -0
model.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import pandas as pd
3
+ from sklearn.model_selection import train_test_split
4
+ from sklearn.preprocessing import StandardScaler
5
+ from tensorflow.keras.models import Sequential
6
+ from tensorflow.keras.layers import Dense
7
+ from tensorflow.keras.optimizers import Adam
8
+
9
+ import torch
10
+ from fastapi import FastAPI
11
+ from pydantic import BaseModel
12
+ from flask import Flask, request, jsonify
13
+
14
+ # Load model and tokenizer from Hugging Face Hub
15
+ model_name = "model.joblib"
16
+ import os
17
+ import joblib
18
+ # Define the file paths to load the model and scaler
19
+ model_filename = 'model.joblib'
20
+ scaler_filename = 'scaler.joblib'
21
+
22
+ # Load the model and scaler using joblib
23
+ loaded_model = joblib.load(model_filename)
24
+ loaded_scaler = joblib.load(scaler_filename)
25
+
26
+ # Initialize Flask app
27
+ app = Flask(__name__)
28
+
29
+ # Define the prediction route
30
+ @app.route('/predict', methods=['POST'])
31
+ def predict():
32
+ # Get the input JSON data
33
+ data = request.get_json()
34
+ new_df = pd.DataFrame(data)
35
+ # Assuming you used pd.get_dummies during training
36
+ new_df_encoded = pd.get_dummies(new_df, columns=['Creative Set', 'Game Type', 'Partner', 'Country'])
37
+
38
+
39
+ new_data_scaled = loaded_scaler.transform(new_df_encoded)
40
+ #print(new_data_scaled)
41
+
42
+ # Step 4: Make predictions for the new data
43
+ predictions = loaded_model.predict(new_data_scaled)
44
+
45
+ # Return the prediction as a JSON response
46
+ return jsonify({"prediction": predictions.tolist()})
47
+
48
+ if __name__ == '__main__':
49
+ # Run the Flask app
50
+ app.run(debug=True, host='0.0.0.0', port=5000)