-
Notifications
You must be signed in to change notification settings - Fork 0
/
training_bot.py
61 lines (47 loc) · 1.9 KB
/
training_bot.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
55
56
57
58
59
60
61
from fastapi import FastAPI, Form, Request
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
from pydantic import BaseModel
from typing import List
import os
import json
HOSTNAME = os.getenv("HOSTNAME")
FOLDER_RESOURCES = os.path.dirname(os.path.abspath(__file__)) + "/resources/"
FOLDER_TEMPLATES = FOLDER_RESOURCES + "templates"
DATA_FILE = "training_data.json"
app = FastAPI()
templates = Jinja2Templates(directory=FOLDER_TEMPLATES)
# Load existing data from the JSON file (if it exists)
def load_data():
if os.path.exists(DATA_FILE):
with open(DATA_FILE, "r", encoding="utf-8") as file:
return json.load(file)
return []
# Save data to the JSON file
def save_data(data):
with open(DATA_FILE, "w", encoding="utf-8") as file:
json.dump(data, file, ensure_ascii=False, indent=4)
# Initialize the training data
training_data = load_data()
class TrainingDataItem(BaseModel):
input: str
output: str
@app.get("/", response_class=HTMLResponse)
async def root(request: Request):
data = {"request": request, "HOSTNAME": HOSTNAME, "mode": "support_bot"}
return templates.TemplateResponse("motul_bot.html", data)
@app.get("/training_bot", response_class=HTMLResponse)
async def root(request: Request):
data = {"request": request, "HOSTNAME": HOSTNAME, "mode": "training_bot"}
return templates.TemplateResponse("motul_bot.html", data)
@app.post("/add_data")
async def add_data(input: str = Form(...), output: str = Form(...)):
# Add the data to the list
training_data.append("input: "+ input.strip())
training_data.append("output: "+ output.strip())
# Save the updated data to the JSON file
save_data(training_data)
return {"message": "Data added successfully", "current_data": training_data}
@app.get("/get_data", response_model=List[TrainingDataItem])
async def get_data():
return training_data