Update server/app.py
Browse files- server/app.py +61 -0
server/app.py
CHANGED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from environment import SupportEnv
|
| 4 |
+
from dataset import cases
|
| 5 |
+
|
| 6 |
+
app = FastAPI()
|
| 7 |
+
|
| 8 |
+
env = SupportEnv(cases)
|
| 9 |
+
|
| 10 |
+
# -------- Models --------
|
| 11 |
+
class Action(BaseModel):
|
| 12 |
+
message: str
|
| 13 |
+
|
| 14 |
+
# -------- Parser --------
|
| 15 |
+
def parse_action(message):
|
| 16 |
+
message = message.lower()
|
| 17 |
+
|
| 18 |
+
if "refund" in message:
|
| 19 |
+
return "refund"
|
| 20 |
+
elif "replace" in message:
|
| 21 |
+
return "replace"
|
| 22 |
+
elif "reject" in message:
|
| 23 |
+
return "reject"
|
| 24 |
+
elif "proof" in message:
|
| 25 |
+
return "ask_proof"
|
| 26 |
+
else:
|
| 27 |
+
return "unknown"
|
| 28 |
+
|
| 29 |
+
# -------- Routes --------
|
| 30 |
+
@app.post("/reset")
|
| 31 |
+
def reset():
|
| 32 |
+
obs = env.reset()
|
| 33 |
+
|
| 34 |
+
return {
|
| 35 |
+
"observation": {
|
| 36 |
+
"echoed_message": obs
|
| 37 |
+
},
|
| 38 |
+
"reward": 0.0,
|
| 39 |
+
"done": False
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
@app.post("/step")
|
| 43 |
+
def step(action: Action):
|
| 44 |
+
parsed_action = parse_action(action.message)
|
| 45 |
+
|
| 46 |
+
obs, reward, done = env.step(parsed_action)
|
| 47 |
+
|
| 48 |
+
return {
|
| 49 |
+
"observation": {
|
| 50 |
+
"echoed_message": obs
|
| 51 |
+
},
|
| 52 |
+
"reward": reward,
|
| 53 |
+
"done": done
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
def main():
|
| 57 |
+
import uvicorn
|
| 58 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
| 59 |
+
|
| 60 |
+
if __name__ == "__main__":
|
| 61 |
+
main()
|