Spaces:
Sleeping
Sleeping
This file was not needed.
Browse files- training.py +0 -85
training.py
DELETED
@@ -1,85 +0,0 @@
|
|
1 |
-
from fastapi import APIRouter, Form, BackgroundTasks
|
2 |
-
from config import settings
|
3 |
-
import os
|
4 |
-
import json
|
5 |
-
from routers.donut_evaluate import run_evaluate_donut
|
6 |
-
from routers.donut_training import run_training_donut
|
7 |
-
import utils
|
8 |
-
|
9 |
-
|
10 |
-
router = APIRouter()
|
11 |
-
|
12 |
-
|
13 |
-
def invoke_training(max_epochs, val_check_interval, warmup_steps, model_in_use):
|
14 |
-
# if sparrow_key != settings.sparrow_key:
|
15 |
-
# return {"error": "Invalid Sparrow key."}
|
16 |
-
|
17 |
-
if model_in_use == 'donut':
|
18 |
-
processing_time = run_training_donut(max_epochs, val_check_interval, warmup_steps)
|
19 |
-
utils.log_stats(settings.training_stats_file, [processing_time, settings.model])
|
20 |
-
print(f"Processing time training: {processing_time:.2f} seconds")
|
21 |
-
|
22 |
-
|
23 |
-
@router.post("/training")
|
24 |
-
async def run_training(background_tasks: BackgroundTasks,
|
25 |
-
max_epochs: int = Form(30),
|
26 |
-
val_check_interval: float = Form(0.4),
|
27 |
-
warmup_steps: int = Form(81),
|
28 |
-
model_in_use: str = Form('donut')):
|
29 |
-
|
30 |
-
background_tasks.add_task(invoke_training, max_epochs, val_check_interval, warmup_steps, model_in_use)
|
31 |
-
|
32 |
-
return {"message": "Dnote Donut ML training started in the background"}
|
33 |
-
|
34 |
-
|
35 |
-
def invoke_evaluate(model_in_use):
|
36 |
-
# if sparrow_key != settings.sparrow_key:
|
37 |
-
# return {"error": "Invalid Sparrow key."}
|
38 |
-
|
39 |
-
if model_in_use == 'donut':
|
40 |
-
scores, accuracy, processing_time = run_evaluate_donut()
|
41 |
-
utils.log_stats(settings.evaluate_stats_file, [processing_time, scores, accuracy, settings.model])
|
42 |
-
print(f"Processing time evaluate: {processing_time:.2f} seconds")
|
43 |
-
|
44 |
-
|
45 |
-
@router.post("/evaluate")
|
46 |
-
async def run_evaluate(background_tasks: BackgroundTasks,
|
47 |
-
model_in_use: str = Form('donut')):
|
48 |
-
|
49 |
-
background_tasks.add_task(invoke_evaluate, model_in_use)
|
50 |
-
|
51 |
-
return {"message": "Dnote Donut ML model evaluation started in the background"}
|
52 |
-
|
53 |
-
|
54 |
-
@router.get("/statistics/training")
|
55 |
-
async def get_statistics_training():
|
56 |
-
file_path = settings.training_stats_file
|
57 |
-
|
58 |
-
# Check if the file exists, and read its content
|
59 |
-
if os.path.exists(file_path):
|
60 |
-
with open(file_path, 'r') as file:
|
61 |
-
try:
|
62 |
-
content = json.load(file)
|
63 |
-
except json.JSONDecodeError:
|
64 |
-
content = []
|
65 |
-
else:
|
66 |
-
content = []
|
67 |
-
|
68 |
-
return content
|
69 |
-
|
70 |
-
|
71 |
-
@router.get("/statistics/evaluate")
|
72 |
-
async def get_statistics_evaluate():
|
73 |
-
file_path = settings.evaluate_stats_file
|
74 |
-
|
75 |
-
# Check if the file exists, and read its content
|
76 |
-
if os.path.exists(file_path):
|
77 |
-
with open(file_path, 'r') as file:
|
78 |
-
try:
|
79 |
-
content = json.load(file)
|
80 |
-
except json.JSONDecodeError:
|
81 |
-
content = []
|
82 |
-
else:
|
83 |
-
content = []
|
84 |
-
|
85 |
-
return content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|