Spaces:
Running
on
Zero
Running
on
Zero
Refactor: Replace session_hash with session_hash_code
Browse files- app.py +18 -18
- app/session_utils.py +62 -62
app.py
CHANGED
|
@@ -22,9 +22,9 @@ from app.utils import (
|
|
| 22 |
from app.session_utils import (
|
| 23 |
on_load,
|
| 24 |
on_unload,
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
get_active_task_flag_file,
|
| 29 |
|
| 30 |
)
|
|
@@ -43,23 +43,23 @@ from app.ui_utils import (
|
|
| 43 |
# --------------------------------------------------------
|
| 44 |
# Initialization
|
| 45 |
# --------------------------------------------------------
|
| 46 |
-
|
| 47 |
|
| 48 |
theme,css_style = get_custom_theme()
|
| 49 |
|
| 50 |
with gr.Blocks(theme=theme, css=css_style) as demo:
|
| 51 |
-
|
| 52 |
-
|
| 53 |
with gr.Accordion("📊 Active Sessions Hash", open=True ,visible=DEBUG):
|
| 54 |
sessions_table = gr.DataFrame(
|
| 55 |
-
headers=["
|
| 56 |
interactive=False,
|
| 57 |
wrap=True,
|
| 58 |
max_height=200,
|
| 59 |
)
|
| 60 |
-
gr.Timer(3.0).tick(fn=
|
| 61 |
|
| 62 |
-
demo.load(fn=on_load, inputs=None, outputs=[
|
| 63 |
demo.unload(on_unload)
|
| 64 |
stop_streaming_flags = gr.State(value={"stop": False})
|
| 65 |
active_filepath = gr.State(value=next(iter(EXAMPLE_CONFIGS)))
|
|
@@ -125,7 +125,7 @@ with gr.Blocks(theme=theme, css=css_style) as demo:
|
|
| 125 |
|
| 126 |
webrtc_stream.stream(
|
| 127 |
fn=read_and_stream_audio,
|
| 128 |
-
inputs=[active_filepath,
|
| 129 |
outputs=[webrtc_stream],
|
| 130 |
trigger=start_stream_button.click,
|
| 131 |
concurrency_id="audio_stream",
|
|
@@ -242,12 +242,12 @@ with gr.Blocks(theme=theme, css=css_style) as demo:
|
|
| 242 |
|
| 243 |
stop_stream_button.click(
|
| 244 |
fn=stop_streaming,
|
| 245 |
-
inputs=[
|
| 246 |
outputs=[stop_streaming_flags],
|
| 247 |
)
|
| 248 |
|
| 249 |
-
def stop_task_fn(
|
| 250 |
-
transcribe_active = get_active_task_flag_file(
|
| 251 |
if os.path.exists(transcribe_active):
|
| 252 |
os.remove(transcribe_active)
|
| 253 |
yield "Task stopped by user."
|
|
@@ -255,13 +255,13 @@ with gr.Blocks(theme=theme, css=css_style) as demo:
|
|
| 255 |
|
| 256 |
stop_task_button.click(
|
| 257 |
fn=stop_task_fn,
|
| 258 |
-
inputs=
|
| 259 |
outputs=transcription_output
|
| 260 |
)
|
| 261 |
-
# task(
|
| 262 |
|
| 263 |
def start_transcription(
|
| 264 |
-
|
| 265 |
task_type, lang_source, lang_target,
|
| 266 |
chunk_secs, left_context_secs, right_context_secs,
|
| 267 |
streaming_policy, alignatt_thr, waitk_lagging,
|
|
@@ -273,7 +273,7 @@ with gr.Blocks(theme=theme, css=css_style) as demo:
|
|
| 273 |
yield f"Starting {task_type.lower()}...\n\n",gr.update(visible=False),gr.update(visible=True)
|
| 274 |
|
| 275 |
# Boucle sur le générateur de `task()`
|
| 276 |
-
for msg in task(
|
| 277 |
accumulated += msg
|
| 278 |
yield accumulated,gr.update(visible=False),gr.update(visible=True)
|
| 279 |
|
|
@@ -282,7 +282,7 @@ with gr.Blocks(theme=theme, css=css_style) as demo:
|
|
| 282 |
start_task_button.click(
|
| 283 |
fn=start_transcription,
|
| 284 |
inputs=[
|
| 285 |
-
|
| 286 |
task_type, lang_source, lang_target,
|
| 287 |
chunk_secs, left_context_secs, right_context_secs,
|
| 288 |
streaming_policy, alignatt_thr, waitk_lagging,
|
|
|
|
| 22 |
from app.session_utils import (
|
| 23 |
on_load,
|
| 24 |
on_unload,
|
| 25 |
+
get_active_session_hash_code,
|
| 26 |
+
register_session_hash_code,
|
| 27 |
+
reset_all_active_session_hash_code,
|
| 28 |
get_active_task_flag_file,
|
| 29 |
|
| 30 |
)
|
|
|
|
| 43 |
# --------------------------------------------------------
|
| 44 |
# Initialization
|
| 45 |
# --------------------------------------------------------
|
| 46 |
+
reset_all_active_session_hash_code()
|
| 47 |
|
| 48 |
theme,css_style = get_custom_theme()
|
| 49 |
|
| 50 |
with gr.Blocks(theme=theme, css=css_style) as demo:
|
| 51 |
+
session_hash_code = gr.State()
|
| 52 |
+
session_hash_code_box = gr.Textbox(label="Session ID", interactive=False, visible=DEBUG)
|
| 53 |
with gr.Accordion("📊 Active Sessions Hash", open=True ,visible=DEBUG):
|
| 54 |
sessions_table = gr.DataFrame(
|
| 55 |
+
headers=["session_hash_code", "file", "start_time", "status"],
|
| 56 |
interactive=False,
|
| 57 |
wrap=True,
|
| 58 |
max_height=200,
|
| 59 |
)
|
| 60 |
+
gr.Timer(3.0).tick(fn=get_active_session_hash_code, outputs=sessions_table)
|
| 61 |
|
| 62 |
+
demo.load(fn=on_load, inputs=None, outputs=[session_hash_code, session_hash_code_box])
|
| 63 |
demo.unload(on_unload)
|
| 64 |
stop_streaming_flags = gr.State(value={"stop": False})
|
| 65 |
active_filepath = gr.State(value=next(iter(EXAMPLE_CONFIGS)))
|
|
|
|
| 125 |
|
| 126 |
webrtc_stream.stream(
|
| 127 |
fn=read_and_stream_audio,
|
| 128 |
+
inputs=[active_filepath, session_hash_code, stop_streaming_flags],
|
| 129 |
outputs=[webrtc_stream],
|
| 130 |
trigger=start_stream_button.click,
|
| 131 |
concurrency_id="audio_stream",
|
|
|
|
| 242 |
|
| 243 |
stop_stream_button.click(
|
| 244 |
fn=stop_streaming,
|
| 245 |
+
inputs=[session_hash_code, stop_streaming_flags],
|
| 246 |
outputs=[stop_streaming_flags],
|
| 247 |
)
|
| 248 |
|
| 249 |
+
def stop_task_fn(session_hash_code):
|
| 250 |
+
transcribe_active = get_active_task_flag_file(session_hash_code)
|
| 251 |
if os.path.exists(transcribe_active):
|
| 252 |
os.remove(transcribe_active)
|
| 253 |
yield "Task stopped by user."
|
|
|
|
| 255 |
|
| 256 |
stop_task_button.click(
|
| 257 |
fn=stop_task_fn,
|
| 258 |
+
inputs=session_hash_code,
|
| 259 |
outputs=transcription_output
|
| 260 |
)
|
| 261 |
+
# task(session_hash_code)
|
| 262 |
|
| 263 |
def start_transcription(
|
| 264 |
+
session_hash_code, stop_streaming_flags,
|
| 265 |
task_type, lang_source, lang_target,
|
| 266 |
chunk_secs, left_context_secs, right_context_secs,
|
| 267 |
streaming_policy, alignatt_thr, waitk_lagging,
|
|
|
|
| 273 |
yield f"Starting {task_type.lower()}...\n\n",gr.update(visible=False),gr.update(visible=True)
|
| 274 |
|
| 275 |
# Boucle sur le générateur de `task()`
|
| 276 |
+
for msg in task(session_hash_code):
|
| 277 |
accumulated += msg
|
| 278 |
yield accumulated,gr.update(visible=False),gr.update(visible=True)
|
| 279 |
|
|
|
|
| 282 |
start_task_button.click(
|
| 283 |
fn=start_transcription,
|
| 284 |
inputs=[
|
| 285 |
+
session_hash_code, stop_streaming_flags,
|
| 286 |
task_type, lang_source, lang_target,
|
| 287 |
chunk_secs, left_context_secs, right_context_secs,
|
| 288 |
streaming_policy, alignatt_thr, waitk_lagging,
|
app/session_utils.py
CHANGED
|
@@ -8,7 +8,7 @@ import gradio as gr
|
|
| 8 |
# TMP_DIR = "/tmp/canary_aed_streaming"
|
| 9 |
|
| 10 |
TMP_DIR = os.getenv("TMP_DIR", "/tmp/canary_aed_streaming")
|
| 11 |
-
ACTIVE_SESSIONS_HASH_FILE = os.path.join(TMP_DIR, "
|
| 12 |
ACTIVE_TASK_FLAG="task_active_"
|
| 13 |
NAME_FOLDER_CHUNKS="chunks_"
|
| 14 |
|
|
@@ -16,7 +16,7 @@ NAME_FOLDER_CHUNKS="chunks_"
|
|
| 16 |
# ---------------------------
|
| 17 |
# Helper to manage the JSON
|
| 18 |
# ---------------------------
|
| 19 |
-
def
|
| 20 |
if not os.path.exists(ACTIVE_SESSIONS_HASH_FILE):
|
| 21 |
return {}
|
| 22 |
try:
|
|
@@ -26,7 +26,7 @@ def _read_sessions_hash():
|
|
| 26 |
return {}
|
| 27 |
|
| 28 |
|
| 29 |
-
def
|
| 30 |
os.makedirs(os.path.dirname(ACTIVE_SESSIONS_HASH_FILE), exist_ok=True)
|
| 31 |
with open(ACTIVE_SESSIONS_HASH_FILE, "w") as f:
|
| 32 |
json.dump(data, f, indent=2)
|
|
@@ -37,20 +37,20 @@ def _write_sessions_hash(data):
|
|
| 37 |
# ---------------------------
|
| 38 |
def on_load(request: gr.Request):
|
| 39 |
"""Called when a new visitor opens the app."""
|
| 40 |
-
|
| 41 |
-
sessions =
|
| 42 |
|
| 43 |
-
sessions[
|
| 44 |
-
"
|
| 45 |
"file": "",
|
| 46 |
"start_time": datetime.utcnow().strftime("%H:%M:%S"),
|
| 47 |
"status": "active",
|
| 48 |
}
|
| 49 |
|
| 50 |
-
|
| 51 |
-
logging.info(f"[{
|
| 52 |
|
| 53 |
-
return
|
| 54 |
|
| 55 |
|
| 56 |
# ---------------------------
|
|
@@ -58,17 +58,17 @@ def on_load(request: gr.Request):
|
|
| 58 |
# ---------------------------
|
| 59 |
def on_unload(request: gr.Request):
|
| 60 |
"""Called when the visitor closes or refreshes the app."""
|
| 61 |
-
sid = request.
|
| 62 |
-
sessions =
|
| 63 |
|
| 64 |
if sid in sessions:
|
| 65 |
sessions.pop(sid)
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
logging.info(f"[{sid}]
|
| 70 |
else:
|
| 71 |
-
logging.info(f"[{sid}] No active
|
| 72 |
|
| 73 |
def ensure_tmp_dir():
|
| 74 |
"""Ensures the base temporary directory exists."""
|
|
@@ -78,17 +78,17 @@ def ensure_tmp_dir():
|
|
| 78 |
logging.error(f"Failed to create tmp directory {TMP_DIR}: {e}")
|
| 79 |
|
| 80 |
|
| 81 |
-
def
|
| 82 |
-
"""Removes all temporary
|
| 83 |
ensure_tmp_dir()
|
| 84 |
|
| 85 |
try:
|
| 86 |
-
# --- Remove active
|
| 87 |
if os.path.exists(ACTIVE_SESSIONS_HASH_FILE):
|
| 88 |
os.remove(ACTIVE_SESSIONS_HASH_FILE)
|
| 89 |
-
logging.info("Active
|
| 90 |
else:
|
| 91 |
-
logging.debug("No active
|
| 92 |
# --- Clean all flag files (stream + transcribe) ---
|
| 93 |
for f in os.listdir(TMP_DIR):
|
| 94 |
if (
|
|
@@ -113,31 +113,31 @@ def reset_all_active_sessions_hash():
|
|
| 113 |
|
| 114 |
logging.info("Temporary session cleanup completed successfully.")
|
| 115 |
except Exception as e:
|
| 116 |
-
logging.error(f"Error resetting active
|
| 117 |
|
| 118 |
-
def
|
| 119 |
-
"""Removes all temporary files and data related to a specific
|
| 120 |
-
if not
|
| 121 |
-
logging.warning("reset_session() called without a valid
|
| 122 |
return
|
| 123 |
|
| 124 |
try:
|
| 125 |
-
# --- Remove
|
| 126 |
if os.path.exists(ACTIVE_SESSIONS_HASH_FILE):
|
| 127 |
try:
|
| 128 |
with open(ACTIVE_SESSIONS_HASH_FILE, "r") as f:
|
| 129 |
data = json.load(f)
|
| 130 |
-
if
|
| 131 |
-
data.pop(
|
| 132 |
with open(ACTIVE_SESSIONS_HASH_FILE, "w") as f:
|
| 133 |
json.dump(data, f, indent=2)
|
| 134 |
-
logging.debug(f"[{
|
| 135 |
except Exception as e:
|
| 136 |
-
logging.warning(f"[{
|
| 137 |
|
| 138 |
-
# --- Define all possible
|
| 139 |
files_to_remove = [
|
| 140 |
-
get_active_task_flag_file(
|
| 141 |
]
|
| 142 |
|
| 143 |
# --- Remove all temporary files ---
|
|
@@ -146,33 +146,33 @@ def remove_session_hash_data(session_hash: str):
|
|
| 146 |
if os.path.exists(path):
|
| 147 |
try:
|
| 148 |
os.remove(path)
|
| 149 |
-
logging.debug(f"[{
|
| 150 |
except Exception as e:
|
| 151 |
-
logging.warning(f"[{
|
| 152 |
|
| 153 |
# --- Remove chunk folder if exists ---
|
| 154 |
-
chunk_dir = os.path.join(TMP_DIR, f"chunks_{
|
| 155 |
if os.path.isdir(chunk_dir):
|
| 156 |
try:
|
| 157 |
shutil.rmtree(chunk_dir)
|
| 158 |
-
logging.debug(f"[{
|
| 159 |
except Exception as e:
|
| 160 |
-
logging.warning(f"[{
|
| 161 |
|
| 162 |
-
logging.info(f"[{
|
| 163 |
|
| 164 |
except Exception as e:
|
| 165 |
-
logging.error(f"[{
|
| 166 |
|
| 167 |
def generate_session_id() -> str:
|
| 168 |
-
"""Generates a unique
|
| 169 |
sid = str(uuid.uuid4())
|
| 170 |
-
logging.debug(f"[{sid}] New
|
| 171 |
return sid
|
| 172 |
|
| 173 |
|
| 174 |
-
def
|
| 175 |
-
"""Registers a new
|
| 176 |
ensure_tmp_dir()
|
| 177 |
data = {}
|
| 178 |
if os.path.exists(ACTIVE_SESSIONS_HASH_FILE):
|
|
@@ -182,8 +182,8 @@ def register_session_hash(session_hash: str, filepath: str):
|
|
| 182 |
except Exception:
|
| 183 |
data = {}
|
| 184 |
|
| 185 |
-
data[
|
| 186 |
-
"
|
| 187 |
"file": filepath,
|
| 188 |
"start_time": datetime.utcnow().strftime("%H:%M:%S"),
|
| 189 |
"status": "active",
|
|
@@ -192,28 +192,28 @@ def register_session_hash(session_hash: str, filepath: str):
|
|
| 192 |
with open(ACTIVE_SESSIONS_HASH_FILE, "w") as f:
|
| 193 |
json.dump(data, f)
|
| 194 |
|
| 195 |
-
logging.debug(f"[{
|
| 196 |
|
| 197 |
|
| 198 |
-
def
|
| 199 |
-
"""Removes a
|
| 200 |
if not os.path.exists(ACTIVE_SESSIONS_HASH_FILE):
|
| 201 |
return
|
| 202 |
|
| 203 |
try:
|
| 204 |
with open(ACTIVE_SESSIONS_HASH_FILE, "r") as f:
|
| 205 |
data = json.load(f)
|
| 206 |
-
if
|
| 207 |
-
data.pop(
|
| 208 |
with open(ACTIVE_SESSIONS_HASH_FILE, "w") as f:
|
| 209 |
json.dump(data, f)
|
| 210 |
-
logging.debug(f"[{
|
| 211 |
except Exception as e:
|
| 212 |
-
logging.error(f"[{
|
| 213 |
|
| 214 |
|
| 215 |
-
def
|
| 216 |
-
"""Returns active
|
| 217 |
if not os.path.exists(ACTIVE_SESSIONS_HASH_FILE):
|
| 218 |
return []
|
| 219 |
|
|
@@ -223,7 +223,7 @@ def get_active_sessions_hash():
|
|
| 223 |
|
| 224 |
rows = [
|
| 225 |
[
|
| 226 |
-
s.get("
|
| 227 |
s.get("file", ""),
|
| 228 |
s.get("start_time", ""),
|
| 229 |
s.get("status", ""),
|
|
@@ -232,13 +232,13 @@ def get_active_sessions_hash():
|
|
| 232 |
]
|
| 233 |
return rows
|
| 234 |
except Exception as e:
|
| 235 |
-
logging.error(f"Error reading active
|
| 236 |
return []
|
| 237 |
|
| 238 |
|
| 239 |
-
def get_active_task_flag_file(
|
| 240 |
-
return os.path.join(TMP_DIR, f"{ACTIVE_TASK_FLAG}{
|
| 241 |
|
| 242 |
|
| 243 |
-
def get_folder_chunks(
|
| 244 |
-
return os.path.join(TMP_DIR, f"{NAME_FOLDER_CHUNKS}{
|
|
|
|
| 8 |
# TMP_DIR = "/tmp/canary_aed_streaming"
|
| 9 |
|
| 10 |
TMP_DIR = os.getenv("TMP_DIR", "/tmp/canary_aed_streaming")
|
| 11 |
+
ACTIVE_SESSIONS_HASH_FILE = os.path.join(TMP_DIR, "active_session_hash_code.json")
|
| 12 |
ACTIVE_TASK_FLAG="task_active_"
|
| 13 |
NAME_FOLDER_CHUNKS="chunks_"
|
| 14 |
|
|
|
|
| 16 |
# ---------------------------
|
| 17 |
# Helper to manage the JSON
|
| 18 |
# ---------------------------
|
| 19 |
+
def _read_session_hash_code():
|
| 20 |
if not os.path.exists(ACTIVE_SESSIONS_HASH_FILE):
|
| 21 |
return {}
|
| 22 |
try:
|
|
|
|
| 26 |
return {}
|
| 27 |
|
| 28 |
|
| 29 |
+
def _write_session_hash_code(data):
|
| 30 |
os.makedirs(os.path.dirname(ACTIVE_SESSIONS_HASH_FILE), exist_ok=True)
|
| 31 |
with open(ACTIVE_SESSIONS_HASH_FILE, "w") as f:
|
| 32 |
json.dump(data, f, indent=2)
|
|
|
|
| 37 |
# ---------------------------
|
| 38 |
def on_load(request: gr.Request):
|
| 39 |
"""Called when a new visitor opens the app."""
|
| 40 |
+
session_hash_code = request.session_hash
|
| 41 |
+
sessions = _read_session_hash_code()
|
| 42 |
|
| 43 |
+
sessions[session_hash_code] = {
|
| 44 |
+
"session_hash_code": session_hash_code,
|
| 45 |
"file": "",
|
| 46 |
"start_time": datetime.utcnow().strftime("%H:%M:%S"),
|
| 47 |
"status": "active",
|
| 48 |
}
|
| 49 |
|
| 50 |
+
_write_session_hash_code(sessions)
|
| 51 |
+
logging.info(f"[{session_hash_code}] session_hash_code registered (on_load).")
|
| 52 |
|
| 53 |
+
return session_hash_code, session_hash_code # can be used as gr.State + display
|
| 54 |
|
| 55 |
|
| 56 |
# ---------------------------
|
|
|
|
| 58 |
# ---------------------------
|
| 59 |
def on_unload(request: gr.Request):
|
| 60 |
"""Called when the visitor closes or refreshes the app."""
|
| 61 |
+
sid = request.session_hash_code
|
| 62 |
+
sessions = _read_session_hash_code()
|
| 63 |
|
| 64 |
if sid in sessions:
|
| 65 |
sessions.pop(sid)
|
| 66 |
+
_write_session_hash_code(sessions)
|
| 67 |
+
remove_session_hash_code_data(sid)
|
| 68 |
+
unregister_session_hash_code_hash(sid)
|
| 69 |
+
logging.info(f"[{sid}] session_hash_code removed (on_unload).")
|
| 70 |
else:
|
| 71 |
+
logging.info(f"[{sid}] No active session_hash_code found to remove.")
|
| 72 |
|
| 73 |
def ensure_tmp_dir():
|
| 74 |
"""Ensures the base temporary directory exists."""
|
|
|
|
| 78 |
logging.error(f"Failed to create tmp directory {TMP_DIR}: {e}")
|
| 79 |
|
| 80 |
|
| 81 |
+
def reset_all_active_session_hash_code():
|
| 82 |
+
"""Removes all temporary session_hash_code files and folders at startup."""
|
| 83 |
ensure_tmp_dir()
|
| 84 |
|
| 85 |
try:
|
| 86 |
+
# --- Remove active session_hash_codes file ---
|
| 87 |
if os.path.exists(ACTIVE_SESSIONS_HASH_FILE):
|
| 88 |
os.remove(ACTIVE_SESSIONS_HASH_FILE)
|
| 89 |
+
logging.info("Active session_hash_codes file reset at startup.")
|
| 90 |
else:
|
| 91 |
+
logging.debug("No active session_hash_codes file found to reset.")
|
| 92 |
# --- Clean all flag files (stream + transcribe) ---
|
| 93 |
for f in os.listdir(TMP_DIR):
|
| 94 |
if (
|
|
|
|
| 113 |
|
| 114 |
logging.info("Temporary session cleanup completed successfully.")
|
| 115 |
except Exception as e:
|
| 116 |
+
logging.error(f"Error resetting active session_hash_codes: {e}")
|
| 117 |
|
| 118 |
+
def remove_session_hash_code_data(session_hash_code: str):
|
| 119 |
+
"""Removes all temporary files and data related to a specific session_hash_code."""
|
| 120 |
+
if not session_hash_code:
|
| 121 |
+
logging.warning("reset_session() called without a valid session_hash_code.")
|
| 122 |
return
|
| 123 |
|
| 124 |
try:
|
| 125 |
+
# --- Remove session_hash_code from active_sessions.json ---
|
| 126 |
if os.path.exists(ACTIVE_SESSIONS_HASH_FILE):
|
| 127 |
try:
|
| 128 |
with open(ACTIVE_SESSIONS_HASH_FILE, "r") as f:
|
| 129 |
data = json.load(f)
|
| 130 |
+
if session_hash_code in data:
|
| 131 |
+
data.pop(session_hash_code)
|
| 132 |
with open(ACTIVE_SESSIONS_HASH_FILE, "w") as f:
|
| 133 |
json.dump(data, f, indent=2)
|
| 134 |
+
logging.debug(f"[{session_hash_code}] Removed from {ACTIVE_SESSIONS_HASH_FILE}.")
|
| 135 |
except Exception as e:
|
| 136 |
+
logging.warning(f"[{session_hash_code}] Failed to update {ACTIVE_SESSIONS_HASH_FILE}: {e}")
|
| 137 |
|
| 138 |
+
# --- Define all possible session_hash_code file patterns ---
|
| 139 |
files_to_remove = [
|
| 140 |
+
get_active_task_flag_file(session_hash_code),
|
| 141 |
]
|
| 142 |
|
| 143 |
# --- Remove all temporary files ---
|
|
|
|
| 146 |
if os.path.exists(path):
|
| 147 |
try:
|
| 148 |
os.remove(path)
|
| 149 |
+
logging.debug(f"[{session_hash_code}] Removed file: {fname}")
|
| 150 |
except Exception as e:
|
| 151 |
+
logging.warning(f"[{session_hash_code}] Failed to remove file {fname}: {e}")
|
| 152 |
|
| 153 |
# --- Remove chunk folder if exists ---
|
| 154 |
+
chunk_dir = os.path.join(TMP_DIR, f"chunks_{session_hash_code}")
|
| 155 |
if os.path.isdir(chunk_dir):
|
| 156 |
try:
|
| 157 |
shutil.rmtree(chunk_dir)
|
| 158 |
+
logging.debug(f"[{session_hash_code}] Removed chunk folder: chunks_{session_hash_code}")
|
| 159 |
except Exception as e:
|
| 160 |
+
logging.warning(f"[{session_hash_code}] Failed to remove chunk folder: {e}")
|
| 161 |
|
| 162 |
+
logging.info(f"[{session_hash_code}] session_hash_code fully reset.")
|
| 163 |
|
| 164 |
except Exception as e:
|
| 165 |
+
logging.error(f"[{session_hash_code}] Error during reset_session: {e}")
|
| 166 |
|
| 167 |
def generate_session_id() -> str:
|
| 168 |
+
"""Generates a unique session_hash_code ID."""
|
| 169 |
sid = str(uuid.uuid4())
|
| 170 |
+
logging.debug(f"[{sid}] New session_hash_code created.")
|
| 171 |
return sid
|
| 172 |
|
| 173 |
|
| 174 |
+
def register_session_hash_code(session_hash_code: str, filepath: str):
|
| 175 |
+
"""Registers a new session_hash_code."""
|
| 176 |
ensure_tmp_dir()
|
| 177 |
data = {}
|
| 178 |
if os.path.exists(ACTIVE_SESSIONS_HASH_FILE):
|
|
|
|
| 182 |
except Exception:
|
| 183 |
data = {}
|
| 184 |
|
| 185 |
+
data[session_hash_code] = {
|
| 186 |
+
"session_hash_code": session_hash_code,
|
| 187 |
"file": filepath,
|
| 188 |
"start_time": datetime.utcnow().strftime("%H:%M:%S"),
|
| 189 |
"status": "active",
|
|
|
|
| 192 |
with open(ACTIVE_SESSIONS_HASH_FILE, "w") as f:
|
| 193 |
json.dump(data, f)
|
| 194 |
|
| 195 |
+
logging.debug(f"[{session_hash_code}] session_hash_code registered in active_sessions.json.")
|
| 196 |
|
| 197 |
|
| 198 |
+
def unregister_session_hash_code_hash(session_hash_code: str):
|
| 199 |
+
"""Removes a session_hash_code from the registry."""
|
| 200 |
if not os.path.exists(ACTIVE_SESSIONS_HASH_FILE):
|
| 201 |
return
|
| 202 |
|
| 203 |
try:
|
| 204 |
with open(ACTIVE_SESSIONS_HASH_FILE, "r") as f:
|
| 205 |
data = json.load(f)
|
| 206 |
+
if session_hash_code in data:
|
| 207 |
+
data.pop(session_hash_code)
|
| 208 |
with open(ACTIVE_SESSIONS_HASH_FILE, "w") as f:
|
| 209 |
json.dump(data, f)
|
| 210 |
+
logging.debug(f"[{session_hash_code}] session_hash_code unregistered.")
|
| 211 |
except Exception as e:
|
| 212 |
+
logging.error(f"[{session_hash_code}] Error unregistering session_hash_code: {e}")
|
| 213 |
|
| 214 |
|
| 215 |
+
def get_active_session_hash_code():
|
| 216 |
+
"""Returns active session_hash_codes as a list of rows for the DataFrame."""
|
| 217 |
if not os.path.exists(ACTIVE_SESSIONS_HASH_FILE):
|
| 218 |
return []
|
| 219 |
|
|
|
|
| 223 |
|
| 224 |
rows = [
|
| 225 |
[
|
| 226 |
+
s.get("session_hash_code", ""),
|
| 227 |
s.get("file", ""),
|
| 228 |
s.get("start_time", ""),
|
| 229 |
s.get("status", ""),
|
|
|
|
| 232 |
]
|
| 233 |
return rows
|
| 234 |
except Exception as e:
|
| 235 |
+
logging.error(f"Error reading active session_hash_codes: {e}")
|
| 236 |
return []
|
| 237 |
|
| 238 |
|
| 239 |
+
def get_active_task_flag_file(session_hash_code: str):
|
| 240 |
+
return os.path.join(TMP_DIR, f"{ACTIVE_TASK_FLAG}{session_hash_code}.txt")
|
| 241 |
|
| 242 |
|
| 243 |
+
def get_folder_chunks(session_hash_code: str):
|
| 244 |
+
return os.path.join(TMP_DIR, f"{NAME_FOLDER_CHUNKS}{session_hash_code}")
|