Spaces:
Sleeping
Sleeping
File size: 2,772 Bytes
c4dc0b3 |
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 62 63 64 65 66 67 68 69 70 71 72 73 |
from fastapi import APIRouter,BackgroundTasks,HTTPException
import sqlite3
from passlib.context import CryptContext
import subprocess
from pydantic import BaseModel
ModelIndoorTrainer=APIRouter(prefix="/Trainer")
TrainingProcess2={}
def TrainModel(UserId):
global TrainingProcess2
if TrainingProcess2.get(UserId,False) and TrainingProcess2.get(UserId).poll():
raise HTTPException(status_code=400,detail="Model is already training.")
TrainingProcess2[UserId]=subprocess.Popen(["python","./IndoorLocalization/IndoorlocalizationTrainer.py",f"{UserId}"])
TrainingProcess2[UserId]=None
class DataType(BaseModel):
Email:str
Password:str
@ModelIndoorTrainer.post("/TrainIndoorModel")
async def SpeachToTextEndPoint(Tasks:BackgroundTasks,Data:DataType):
global TrainingProcess2
try:
connect=sqlite3.connect("DataBase/DataBase.bd")
cursor=connect.execute(f'''
SELECT UserId,Password FROM Users where Email='{Data.Email}'
''')
if len(cursor.fetchall())==1:
HasedPassword=cursor.fetchall()[0][1]
UserId=cursor.fetchall()[0][0]
State=Data.Password==HasedPassword
if State:
if TrainingProcess2.get(UserId,False) and TrainingProcess2.get(UserId).poll() is None:
raise HTTPException(status_code=400,detail="Model is already training.")
Tasks.add_task(TrainModel,args=[UserId])
return{"message":"Training Started"}
else:
return {"Status":False,"Message":"Email or Password is not correct"}
except Exception as e:
return {"Status":False,"Message":f"{e}"}
@ModelIndoorTrainer.post("/TrainIndoorModelStatus")
async def SpeachToTextEndPoint(Data:DataType):
global TrainingProcess2
try:
connect=sqlite3.connect("DataBase/DataBase.bd")
cursor=connect.execute(f'''
SELECT UserId, Password FROM Users where Email='{Data.Email}'
''')
if len(cursor.fetchall())==1:
HasedPassword=cursor.fetchall()[0][1]
UserId=cursor.fetchall()[0][0]
State=Data.Password==HasedPassword
if State:
if TrainingProcess2.get(UserId,False) and TrainingProcess2.get(UserId).poll() is None:
return{"message":"Model still Training "}
else:
return{"message":"Model Training ended"}
else:
return {"Status":False,"Message":"Email or Password is not correct"}
except Exception as e:
return {"Status":False,"Message":f"{e}"}
|