Archime commited on
Commit
025c641
·
1 Parent(s): 87f1f7d

delete obsoletes files

Browse files
app/new_session_utils.py DELETED
@@ -1,237 +0,0 @@
1
- import os
2
- import json
3
- import uuid
4
- import shutil
5
- from datetime import datetime
6
- from app.logger_config import logger as logging
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_FILE = os.path.join(TMP_DIR, "active_sessions.json")
12
-
13
-
14
- # ---------------------------
15
- # Helper to manage the JSON
16
- # ---------------------------
17
- def _read_sessions():
18
- if not os.path.exists(ACTIVE_SESSIONS_FILE):
19
- return {}
20
- try:
21
- with open(ACTIVE_SESSIONS_FILE, "r") as f:
22
- return json.load(f)
23
- except Exception:
24
- return {}
25
-
26
-
27
- def _write_sessions(data):
28
- os.makedirs(os.path.dirname(ACTIVE_SESSIONS_FILE), exist_ok=True)
29
- with open(ACTIVE_SESSIONS_FILE, "w") as f:
30
- json.dump(data, f, indent=2)
31
-
32
-
33
- # ---------------------------
34
- # LOAD
35
- # ---------------------------
36
- def on_load(request: gr.Request):
37
- """Called when a new visitor opens the app."""
38
- session_hash = request.session_hash # ✅ Directly use session_hash as unique ID
39
- sessions = _read_sessions()
40
-
41
- sessions[session_hash] = {
42
- "session_hash": session_hash,
43
- "file": "",
44
- "start_time": datetime.utcnow().strftime("%H:%M:%S"),
45
- "status": "active",
46
- }
47
-
48
- _write_sessions(sessions)
49
- logging.info(f"[{session_hash}] Session registered (on_load).")
50
-
51
- return session_hash, session_hash # can be used as gr.State + display
52
-
53
-
54
- # ---------------------------
55
- # UNLOAD
56
- # ---------------------------
57
- def on_unload(request: gr.Request):
58
- """Called when the visitor closes or refreshes the app."""
59
- sid = request.session_hash
60
- sessions = _read_sessions()
61
-
62
- if sid in sessions:
63
- create_stop_flag(sid)
64
- sessions.pop(sid)
65
- _write_sessions(sessions)
66
- remove_session_data(sid)
67
- unregister_session(sid)
68
- logging.info(f"[{sid}] Session removed (on_unload).")
69
- else:
70
- logging.info(f"[{sid}] No active session found to remove.")
71
-
72
- def ensure_tmp_dir():
73
- """Ensures the base temporary directory exists."""
74
- try:
75
- os.makedirs(TMP_DIR, exist_ok=True)
76
- except Exception as e:
77
- logging.error(f"Failed to create tmp directory {TMP_DIR}: {e}")
78
-
79
-
80
- def reset_all_active_sessions():
81
- """Removes all temporary session files and folders at startup."""
82
- ensure_tmp_dir()
83
-
84
- try:
85
- # --- Remove active sessions file ---
86
- if os.path.exists(ACTIVE_SESSIONS_FILE):
87
- os.remove(ACTIVE_SESSIONS_FILE)
88
- logging.info("Active sessions file reset at startup.")
89
- else:
90
- logging.debug("No active sessions file found to reset.")
91
-
92
- except Exception as e:
93
- logging.error(f"Error resetting active sessions: {e}")
94
-
95
- def remove_session_data(session_id: str):
96
- """Removes all temporary files and data related to a specific session."""
97
- if not session_id:
98
- logging.warning("reset_session() called without a valid session_id.")
99
- return
100
-
101
- try:
102
- # --- Remove session from active_sessions.json ---
103
- if os.path.exists(ACTIVE_SESSIONS_FILE):
104
- try:
105
- with open(ACTIVE_SESSIONS_FILE, "r") as f:
106
- data = json.load(f)
107
- if session_id in data:
108
- data.pop(session_id)
109
- with open(ACTIVE_SESSIONS_FILE, "w") as f:
110
- json.dump(data, f, indent=2)
111
- logging.debug(f"[{session_id}] Removed from active_sessions.json.")
112
- except Exception as e:
113
- logging.warning(f"[{session_id}] Failed to update active_sessions.json: {e}")
114
-
115
- # --- Define all possible session file patterns ---
116
- files_to_remove = [
117
- f"progress_{session_id}.json",
118
- # f"stream_stop_flag_{session_id}.txt",
119
- f"transcribe_stop_flag_{session_id}.txt",
120
- f"transcribe_active_{session_id}.txt",
121
- ]
122
-
123
- # --- Remove all temporary files ---
124
- for fname in files_to_remove:
125
- path = os.path.join(TMP_DIR, fname)
126
- if os.path.exists(path):
127
- try:
128
- os.remove(path)
129
- logging.debug(f"[{session_id}] Removed file: {fname}")
130
- except Exception as e:
131
- logging.warning(f"[{session_id}] Failed to remove file {fname}: {e}")
132
-
133
- # --- Remove chunk folder if exists ---
134
- chunk_dir = os.path.join(TMP_DIR, f"chunks_{session_id}")
135
- if os.path.isdir(chunk_dir):
136
- try:
137
- shutil.rmtree(chunk_dir)
138
- logging.debug(f"[{session_id}] Removed chunk folder: chunks_{session_id}")
139
- except Exception as e:
140
- logging.warning(f"[{session_id}] Failed to remove chunk folder: {e}")
141
-
142
- logging.info(f"[{session_id}] Session fully reset.")
143
-
144
- except Exception as e:
145
- logging.error(f"[{session_id}] Error during reset_session: {e}")
146
- def generate_session_id() -> str:
147
- """Generates a unique session ID."""
148
- sid = str(uuid.uuid4())
149
- logging.debug(f"[{sid}] New session created.")
150
- return sid
151
-
152
-
153
- def register_session(session_id: str, filepath: str):
154
- """Registers a new session."""
155
- ensure_tmp_dir()
156
- data = {}
157
- if os.path.exists(ACTIVE_SESSIONS_FILE):
158
- with open(ACTIVE_SESSIONS_FILE, "r") as f:
159
- try:
160
- data = json.load(f)
161
- except Exception:
162
- data = {}
163
-
164
- data[session_id] = {
165
- "session_id": session_id,
166
- "file": filepath,
167
- "start_time": datetime.utcnow().strftime("%H:%M:%S"),
168
- "status": "active",
169
- }
170
-
171
- with open(ACTIVE_SESSIONS_FILE, "w") as f:
172
- json.dump(data, f)
173
-
174
- logging.debug(f"[{session_id}] Session registered in active_sessions.json.")
175
-
176
-
177
- def unregister_session(session_id: str):
178
- """Removes a session from the registry."""
179
- if not os.path.exists(ACTIVE_SESSIONS_FILE):
180
- return
181
-
182
- try:
183
- with open(ACTIVE_SESSIONS_FILE, "r") as f:
184
- data = json.load(f)
185
- if session_id in data:
186
- data.pop(session_id)
187
- with open(ACTIVE_SESSIONS_FILE, "w") as f:
188
- json.dump(data, f)
189
- logging.debug(f"[{session_id}] Session unregistered.")
190
- except Exception as e:
191
- logging.error(f"[{session_id}] Error unregistering session: {e}")
192
-
193
-
194
- def get_active_sessions():
195
- """Returns active sessions as a list of rows for the DataFrame."""
196
- if not os.path.exists(ACTIVE_SESSIONS_FILE):
197
- return []
198
-
199
- try:
200
- with open(ACTIVE_SESSIONS_FILE, "r") as f:
201
- data = json.load(f)
202
-
203
- rows = [
204
- [
205
- s.get("session_id", ""),
206
- s.get("file", ""),
207
- s.get("start_time", ""),
208
- s.get("status", ""),
209
- ]
210
- for s in data.values()
211
- ]
212
- return rows
213
- except Exception as e:
214
- logging.error(f"Error reading active sessions: {e}")
215
- return []
216
-
217
-
218
- def stop_file_path(session_id: str) -> str:
219
- """Returns the stop-flag file path for a given session."""
220
- ensure_tmp_dir()
221
- return os.path.join(TMP_DIR, f"stream_stop_flag_{session_id}.txt")
222
-
223
-
224
- def create_stop_flag(session_id: str):
225
- """Creates a stop-flag file for this session."""
226
- path = stop_file_path(session_id)
227
- with open(path, "w") as f:
228
- f.write("1")
229
- logging.info(f"[{session_id}] Stop flag file created at {path}.")
230
-
231
-
232
- def clear_stop_flag(session_id: str):
233
- """Deletes the stop-flag file if it exists."""
234
- path = stop_file_path(session_id)
235
- if os.path.exists(path):
236
- os.remove(path)
237
- logging.debug(f"[{session_id}] Stop flag cleared.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app/old_session_utils.py DELETED
@@ -1,273 +0,0 @@
1
- import os
2
- import json
3
- import uuid
4
- import shutil
5
- from datetime import datetime
6
- from app.logger_config import logger as logging
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_FILE = os.path.join(TMP_DIR, "active_sessions.json")
12
-
13
-
14
- # ---------------------------
15
- # Helper to manage the JSON
16
- # ---------------------------
17
- def _read_sessions():
18
- if not os.path.exists(ACTIVE_SESSIONS_FILE):
19
- return {}
20
- try:
21
- with open(ACTIVE_SESSIONS_FILE, "r") as f:
22
- return json.load(f)
23
- except Exception:
24
- return {}
25
-
26
-
27
- def _write_sessions(data):
28
- os.makedirs(os.path.dirname(ACTIVE_SESSIONS_FILE), exist_ok=True)
29
- with open(ACTIVE_SESSIONS_FILE, "w") as f:
30
- json.dump(data, f, indent=2)
31
-
32
-
33
- # ---------------------------
34
- # LOAD
35
- # ---------------------------
36
- def on_load(request: gr.Request):
37
- """Called when a new visitor opens the app."""
38
- sid = request.session_hash # ✅ Directly use session_hash as unique ID
39
- sessions = _read_sessions()
40
-
41
- sessions[sid] = {
42
- "session_id": sid,
43
- "file": "",
44
- "start_time": datetime.utcnow().strftime("%H:%M:%S"),
45
- "status": "active",
46
- }
47
-
48
- _write_sessions(sessions)
49
- logging.info(f"[{sid}] Session registered (on_load).")
50
-
51
- return sid, sid # can be used as gr.State + display
52
-
53
-
54
- # ---------------------------
55
- # UNLOAD
56
- # ---------------------------
57
- def on_unload(request: gr.Request):
58
- """Called when the visitor closes or refreshes the app."""
59
- sid = request.session_hash
60
- sessions = _read_sessions()
61
-
62
- if sid in sessions:
63
- create_stop_flag(sid)
64
- sessions.pop(sid)
65
- _write_sessions(sessions)
66
- remove_session_data(sid)
67
- unregister_session(sid)
68
- logging.info(f"[{sid}] Session removed (on_unload).")
69
- else:
70
- logging.info(f"[{sid}] No active session found to remove.")
71
-
72
- def ensure_tmp_dir():
73
- """Ensures the base temporary directory exists."""
74
- try:
75
- os.makedirs(TMP_DIR, exist_ok=True)
76
- except Exception as e:
77
- logging.error(f"Failed to create tmp directory {TMP_DIR}: {e}")
78
-
79
-
80
- def reset_all_active_sessions():
81
- """Removes all temporary session files and folders at startup."""
82
- ensure_tmp_dir()
83
-
84
- try:
85
- # --- Remove active sessions file ---
86
- if os.path.exists(ACTIVE_SESSIONS_FILE):
87
- os.remove(ACTIVE_SESSIONS_FILE)
88
- logging.info("Active sessions file reset at startup.")
89
- else:
90
- logging.debug("No active sessions file found to reset.")
91
-
92
- # --- Clean progress files ---
93
- for f in os.listdir(TMP_DIR):
94
- if f.startswith("progress_") and f.endswith(".json"):
95
- path = os.path.join(TMP_DIR, f)
96
- try:
97
- os.remove(path)
98
- logging.debug(f"Removed leftover progress file: {f}")
99
- except Exception as e:
100
- logging.warning(f"Failed to remove progress file {f}: {e}")
101
-
102
- # --- Clean all flag files (stream + transcribe) ---
103
- for f in os.listdir(TMP_DIR):
104
- if (
105
- f.startswith("stream_stop_flag_")
106
- or f.startswith("transcribe_stop_flag_")
107
- or f.startswith("transcribe_active_")
108
- ) and f.endswith(".txt"):
109
- path = os.path.join(TMP_DIR, f)
110
- try:
111
- os.remove(path)
112
- logging.debug(f"Removed leftover flag file: {f}")
113
- except Exception as e:
114
- logging.warning(f"Failed to remove flag file {f}: {e}")
115
-
116
- # --- Clean chunk directories ---
117
- for name in os.listdir(TMP_DIR):
118
- path = os.path.join(TMP_DIR, name)
119
- if os.path.isdir(path) and name.startswith("chunks_"):
120
- try:
121
- shutil.rmtree(path)
122
- logging.debug(f"Removed leftover chunk folder: {name}")
123
- except Exception as e:
124
- logging.warning(f"Failed to remove chunk folder {name}: {e}")
125
-
126
- logging.info("Temporary session cleanup completed successfully.")
127
-
128
- except Exception as e:
129
- logging.error(f"Error resetting active sessions: {e}")
130
-
131
- def remove_session_data(session_id: str):
132
- """Removes all temporary files and data related to a specific session."""
133
- if not session_id:
134
- logging.warning("reset_session() called without a valid session_id.")
135
- return
136
-
137
- try:
138
- # --- Remove session from active_sessions.json ---
139
- if os.path.exists(ACTIVE_SESSIONS_FILE):
140
- try:
141
- with open(ACTIVE_SESSIONS_FILE, "r") as f:
142
- data = json.load(f)
143
- if session_id in data:
144
- data.pop(session_id)
145
- with open(ACTIVE_SESSIONS_FILE, "w") as f:
146
- json.dump(data, f, indent=2)
147
- logging.debug(f"[{session_id}] Removed from active_sessions.json.")
148
- except Exception as e:
149
- logging.warning(f"[{session_id}] Failed to update active_sessions.json: {e}")
150
-
151
- # --- Define all possible session file patterns ---
152
- files_to_remove = [
153
- f"progress_{session_id}.json",
154
- # f"stream_stop_flag_{session_id}.txt",
155
- f"transcribe_stop_flag_{session_id}.txt",
156
- f"transcribe_active_{session_id}.txt",
157
- ]
158
-
159
- # --- Remove all temporary files ---
160
- for fname in files_to_remove:
161
- path = os.path.join(TMP_DIR, fname)
162
- if os.path.exists(path):
163
- try:
164
- os.remove(path)
165
- logging.debug(f"[{session_id}] Removed file: {fname}")
166
- except Exception as e:
167
- logging.warning(f"[{session_id}] Failed to remove file {fname}: {e}")
168
-
169
- # --- Remove chunk folder if exists ---
170
- chunk_dir = os.path.join(TMP_DIR, f"chunks_{session_id}")
171
- if os.path.isdir(chunk_dir):
172
- try:
173
- shutil.rmtree(chunk_dir)
174
- logging.debug(f"[{session_id}] Removed chunk folder: chunks_{session_id}")
175
- except Exception as e:
176
- logging.warning(f"[{session_id}] Failed to remove chunk folder: {e}")
177
-
178
- logging.info(f"[{session_id}] Session fully reset.")
179
-
180
- except Exception as e:
181
- logging.error(f"[{session_id}] Error during reset_session: {e}")
182
- def generate_session_id() -> str:
183
- """Generates a unique session ID."""
184
- sid = str(uuid.uuid4())
185
- logging.debug(f"[{sid}] New session created.")
186
- return sid
187
-
188
-
189
- def register_session(session_id: str, filepath: str):
190
- """Registers a new session."""
191
- ensure_tmp_dir()
192
- data = {}
193
- if os.path.exists(ACTIVE_SESSIONS_FILE):
194
- with open(ACTIVE_SESSIONS_FILE, "r") as f:
195
- try:
196
- data = json.load(f)
197
- except Exception:
198
- data = {}
199
-
200
- data[session_id] = {
201
- "session_id": session_id,
202
- "file": filepath,
203
- "start_time": datetime.utcnow().strftime("%H:%M:%S"),
204
- "status": "active",
205
- }
206
-
207
- with open(ACTIVE_SESSIONS_FILE, "w") as f:
208
- json.dump(data, f)
209
-
210
- logging.debug(f"[{session_id}] Session registered in active_sessions.json.")
211
-
212
-
213
- def unregister_session(session_id: str):
214
- """Removes a session from the registry."""
215
- if not os.path.exists(ACTIVE_SESSIONS_FILE):
216
- return
217
-
218
- try:
219
- with open(ACTIVE_SESSIONS_FILE, "r") as f:
220
- data = json.load(f)
221
- if session_id in data:
222
- data.pop(session_id)
223
- with open(ACTIVE_SESSIONS_FILE, "w") as f:
224
- json.dump(data, f)
225
- logging.debug(f"[{session_id}] Session unregistered.")
226
- except Exception as e:
227
- logging.error(f"[{session_id}] Error unregistering session: {e}")
228
-
229
-
230
- def get_active_sessions():
231
- """Returns active sessions as a list of rows for the DataFrame."""
232
- if not os.path.exists(ACTIVE_SESSIONS_FILE):
233
- return []
234
-
235
- try:
236
- with open(ACTIVE_SESSIONS_FILE, "r") as f:
237
- data = json.load(f)
238
-
239
- rows = [
240
- [
241
- s.get("session_id", ""),
242
- s.get("file", ""),
243
- s.get("start_time", ""),
244
- s.get("status", ""),
245
- ]
246
- for s in data.values()
247
- ]
248
- return rows
249
- except Exception as e:
250
- logging.error(f"Error reading active sessions: {e}")
251
- return []
252
-
253
-
254
- def stop_file_path(session_id: str) -> str:
255
- """Returns the stop-flag file path for a given session."""
256
- ensure_tmp_dir()
257
- return os.path.join(TMP_DIR, f"stream_stop_flag_{session_id}.txt")
258
-
259
-
260
- def create_stop_flag(session_id: str):
261
- """Creates a stop-flag file for this session."""
262
- path = stop_file_path(session_id)
263
- with open(path, "w") as f:
264
- f.write("1")
265
- logging.info(f"[{session_id}] Stop flag file created at {path}.")
266
-
267
-
268
- def clear_stop_flag(session_id: str):
269
- """Deletes the stop-flag file if it exists."""
270
- path = stop_file_path(session_id)
271
- if os.path.exists(path):
272
- os.remove(path)
273
- logging.debug(f"[{session_id}] Stop flag cleared.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
data/bonjour2.wav ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ea84e513e9d6ef3cb69638fb658d581b9c3979c50559fa0d196d2e3896b7d9c8
3
+ size 21024206
tmp/canary_aed_streaming/active_sessions_hash.json ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "s6apkst3u9": {
3
+ "session_hash": "s6apkst3u9",
4
+ "file": "",
5
+ "start_time": "17:15:18",
6
+ "status": "active"
7
+ }
8
+ }