make gradio working with treads

This commit is contained in:
Jaikinator
2023-11-25 15:17:12 +01:00
parent bbb2c848e3
commit 93e5ce15f9
6 changed files with 101 additions and 21 deletions
+44
View File
@@ -0,0 +1,44 @@
"""
This file contains the functions which are related to monitoring the actual app usage.
Therefore, the app is to be more efficient in the usage of the resources.
By for example, unloading or reloading the model.
"""
import time
import gc
from typing import Union
import torch
import scraibe.app.global_var as gv
from scraibe.autotranscript import Scraibe
def load_model_thread(model : Union[Scraibe, dict] = None):
if model is None:
gv.MODEL = Scraibe()
elif type(model) is Scraibe:
gv.MODEL = model
elif type(model) is dict:
gv.MODEL = Scraibe(**model)
else:
raise TypeError("model must be of type Scraibe, or dict")
gv.LAST_USED = time.time()
# Create a thread to monitor user activity
def delete_unused_model():
while True:
_unload_porperty = (not gv.TRANSCRIBE_ACTIVE.is_set() and (time.time() - gv.LAST_USED > gv.TIMEOUT) and gv.MODEL is not None)
if _unload_porperty:
del gv.MODEL
gv.MODEL = None
gc.collect()
torch.cuda.empty_cache()
gv.MODEL_THREAD.join()
time.sleep(int(gv.TIMEOUT/5))