Towards proper session handling.
Browse files
app.py
CHANGED
|
@@ -33,6 +33,7 @@
|
|
| 33 |
# * Vajira Thambawita <[email protected]>
|
| 34 |
# * Thomas Dreibholz <[email protected]>
|
| 35 |
|
|
|
|
| 36 |
import deepfakeecg
|
| 37 |
import ecg_plot
|
| 38 |
import gradio
|
|
@@ -49,10 +50,17 @@ import PIL
|
|
| 49 |
|
| 50 |
|
| 51 |
TempDirectory = None
|
|
|
|
| 52 |
LastResults = None
|
| 53 |
SelectedECGIndex = 0
|
| 54 |
|
| 55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
# ###### Make a unique session ID ###########################################
|
| 57 |
SessionCounterLock = threading.Lock()
|
| 58 |
SessionCounter = 0
|
|
@@ -69,6 +77,44 @@ def generateSessionID():
|
|
| 69 |
return sessionID
|
| 70 |
|
| 71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
# ###### Get last results ###################################################
|
| 73 |
def getLastResults() -> list:
|
| 74 |
return LastResults
|
|
@@ -184,20 +230,19 @@ def dataToCSV(data, outputFileName, ecgType = deepfakeecg.DATA_ECG12) -> sys.pat
|
|
| 184 |
|
| 185 |
|
| 186 |
# ###### Download CSV #######################################################
|
| 187 |
-
def downloadCSV(
|
| 188 |
-
|
| 189 |
-
|
| 190 |
|
| 191 |
# ###### Download PDF #######################################################
|
| 192 |
-
def downloadPDF(
|
| 193 |
-
|
| 194 |
-
print(f"sessionID={sessionID}")
|
| 195 |
|
| 196 |
|
| 197 |
# ###### Analyze the selected ECG ###########################################
|
| 198 |
-
def analyze() -> None:
|
| 199 |
|
| 200 |
-
|
| 201 |
|
| 202 |
data = getLastResult(SelectedECGIndex)
|
| 203 |
print(data)
|
|
@@ -275,6 +320,12 @@ img.logo-image {
|
|
| 275 |
# ====== Create GUI =========================================================
|
| 276 |
with gradio.Blocks(css = css, theme = gradio.themes.Glass(secondary_hue=gradio.themes.colors.blue)) as gui:
|
| 277 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 278 |
# ====== Unique session ID for this instance =============================
|
| 279 |
sessionID = gradio.State(0)
|
| 280 |
gui.load(generateSessionID, outputs = [ sessionID ])
|
|
@@ -324,8 +375,8 @@ with gradio.Blocks(css = css, theme = gradio.themes.Glass(secondary_hue=gradio.t
|
|
| 324 |
buttonAnalyze.click(analyze)
|
| 325 |
|
| 326 |
# ====== Add click event handling for download buttons ===================
|
| 327 |
-
buttonCSV.click(downloadCSV
|
| 328 |
-
buttonPDF.click(downloadPDF
|
| 329 |
|
| 330 |
# ====== Run on startup ==================================================
|
| 331 |
gui.load(predict,
|
|
@@ -336,8 +387,12 @@ with gradio.Blocks(css = css, theme = gradio.themes.Glass(secondary_hue=gradio.t
|
|
| 336 |
outputs = [ outputGallery ]
|
| 337 |
)
|
| 338 |
|
|
|
|
| 339 |
# ====== Run the GUI ========================================================
|
| 340 |
if __name__ == "__main__":
|
| 341 |
TempDirectory = tempfile.TemporaryDirectory('DeepFakeECGPlus')
|
| 342 |
-
|
|
|
|
|
|
|
| 343 |
TempDirectory.cleanup()
|
|
|
|
|
|
| 33 |
# * Vajira Thambawita <[email protected]>
|
| 34 |
# * Thomas Dreibholz <[email protected]>
|
| 35 |
|
| 36 |
+
import datetime
|
| 37 |
import deepfakeecg
|
| 38 |
import ecg_plot
|
| 39 |
import gradio
|
|
|
|
| 50 |
|
| 51 |
|
| 52 |
TempDirectory = None
|
| 53 |
+
Sessions = {}
|
| 54 |
LastResults = None
|
| 55 |
SelectedECGIndex = 0
|
| 56 |
|
| 57 |
|
| 58 |
+
# ###### Print log message ##################################################
|
| 59 |
+
def log(logstring):
|
| 60 |
+
print(('\x1b[34m' + datetime.datetime.now().strftime('%Y-%m-%dT%H:%M:%S') +
|
| 61 |
+
': ' + logstring + '\x1b[0m'));
|
| 62 |
+
|
| 63 |
+
|
| 64 |
# ###### Make a unique session ID ###########################################
|
| 65 |
SessionCounterLock = threading.Lock()
|
| 66 |
SessionCounter = 0
|
|
|
|
| 77 |
return sessionID
|
| 78 |
|
| 79 |
|
| 80 |
+
|
| 81 |
+
# ###### DeepFakeECG Plus Session (session with web browser) ################
|
| 82 |
+
class Session:
|
| 83 |
+
|
| 84 |
+
# ###### Constructor #####################################################
|
| 85 |
+
def __init__(self):
|
| 86 |
+
self.Lock = threading.Lock()
|
| 87 |
+
self.Counter = 0
|
| 88 |
+
self.Results = None
|
| 89 |
+
|
| 90 |
+
# ###### Increment counter ###############################################
|
| 91 |
+
def increment(self):
|
| 92 |
+
with self.lock:
|
| 93 |
+
self.counter += 1
|
| 94 |
+
return self.counter
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
# ###### Initialize a new session ###########################################
|
| 98 |
+
def initializeSession(request: gradio.Request):
|
| 99 |
+
Sessions[request.session_hash] = Session()
|
| 100 |
+
log(f'Session "{request.session_hash}" initialized')
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
# ###### Clean up a session #################################################
|
| 104 |
+
def cleanUpSession(request: gradio.Request):
|
| 105 |
+
if request.session_hash in Sessions:
|
| 106 |
+
del instances[request.session_hash]
|
| 107 |
+
log(f'Session "{request.session_hash}" cleaned up')
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
# ###### Increment counter in session #######################################
|
| 111 |
+
def incrementCounter(request: gradio.Request):
|
| 112 |
+
if request.session_hash in Sessions:
|
| 113 |
+
instance = Sessions[request.session_hash]
|
| 114 |
+
return instance.increment()
|
| 115 |
+
log(f'ERROR: Session "{request.session_hash}" is not initialized!')
|
| 116 |
+
|
| 117 |
+
|
| 118 |
# ###### Get last results ###################################################
|
| 119 |
def getLastResults() -> list:
|
| 120 |
return LastResults
|
|
|
|
| 230 |
|
| 231 |
|
| 232 |
# ###### Download CSV #######################################################
|
| 233 |
+
def downloadCSV(request: gradio.Request) -> None:
|
| 234 |
+
log(f'Session "{request.session_hash}": Download CSV file')
|
| 235 |
+
|
| 236 |
|
| 237 |
# ###### Download PDF #######################################################
|
| 238 |
+
def downloadPDF(request: gradio.Request) -> None:
|
| 239 |
+
log(f'Session "{request.session_hash}": Download PDF file')
|
|
|
|
| 240 |
|
| 241 |
|
| 242 |
# ###### Analyze the selected ECG ###########################################
|
| 243 |
+
def analyze(request: gradio.Request) -> None:
|
| 244 |
|
| 245 |
+
log(f'Session "{request.session_hash}": Analyze #{SelectedECGIndex}!')
|
| 246 |
|
| 247 |
data = getLastResult(SelectedECGIndex)
|
| 248 |
print(data)
|
|
|
|
| 320 |
# ====== Create GUI =========================================================
|
| 321 |
with gradio.Blocks(css = css, theme = gradio.themes.Glass(secondary_hue=gradio.themes.colors.blue)) as gui:
|
| 322 |
|
| 323 |
+
# ====== Session handling ================================================
|
| 324 |
+
# Session initialization, to be called when page is loaded
|
| 325 |
+
gui.load(initializeSession)
|
| 326 |
+
# Session clean-up, to be called when page is closed/refreshed
|
| 327 |
+
gui.unload(cleanUpSession)
|
| 328 |
+
|
| 329 |
# ====== Unique session ID for this instance =============================
|
| 330 |
sessionID = gradio.State(0)
|
| 331 |
gui.load(generateSessionID, outputs = [ sessionID ])
|
|
|
|
| 375 |
buttonAnalyze.click(analyze)
|
| 376 |
|
| 377 |
# ====== Add click event handling for download buttons ===================
|
| 378 |
+
buttonCSV.click(downloadCSV)
|
| 379 |
+
buttonPDF.click(downloadPDF)
|
| 380 |
|
| 381 |
# ====== Run on startup ==================================================
|
| 382 |
gui.load(predict,
|
|
|
|
| 387 |
outputs = [ outputGallery ]
|
| 388 |
)
|
| 389 |
|
| 390 |
+
|
| 391 |
# ====== Run the GUI ========================================================
|
| 392 |
if __name__ == "__main__":
|
| 393 |
TempDirectory = tempfile.TemporaryDirectory('DeepFakeECGPlus')
|
| 394 |
+
log(f'Prepared temporary directory {TempDirectory.name}')
|
| 395 |
+
gui.launch(allowed_paths = [ TempDirectory.name ])
|
| 396 |
+
log(f'Cleaning up temporary directory {TempDirectory.name}')
|
| 397 |
TempDirectory.cleanup()
|
| 398 |
+
log('Done!')
|