-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
54 lines (45 loc) · 4.73 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from stable_baselines3 import A2C
from stable_baselines3 import DDPG
from stable_baselines3 import PPO
from stable_baselines3 import SAC
from stable_baselines3 import TD3
from flask import Flask, request, jsonify
from env import TradingEnv
AGENTS = {
"A2C": A2C,
"DDPG": DDPG,
"PPO": PPO,
"SAC": SAC,
"TD3": TD3,
}
app = Flask(__name__)
env = TradingEnv()
class Model:
def __init__(self, env):
self.env = env
self.agent = None
@app.route('/create-agent', methods=['POST'])
def create_agent(self):
data = request.get_json()
agent = data['agent']
agent = AGENTS[agent.upper()]("MlpPolicy", env, verbose=1)
return agent
@app.route('/create-agent-from-pretrained', methods=['POST'])
def create_agent_from_pretrained(self):
data = request.get_json()
agent = data['agent']
agent_path = data['agent_path']
agent = AGENTS[agent.upper()].load(agent_path)
return agent
@app.route('/learn', methods=['POST'])
def learn(self):
data = request.get_json()
total_episodes = data['total_episodes']
self.agent.learn(total_timesteps=total_episodes)
return self.agent
@app.route('/predict', methods=['POST'])
def predict(self):
data = request.get_json()
action, _ = self.agent.predict(data)
return action
app.run(port=5001)