Spaces:
Running
on
T4
Running
on
T4
user_history 0.3.4
Browse files- modules/user_history.py +92 -30
modules/user_history.py
CHANGED
|
@@ -18,7 +18,7 @@ Useful links:
|
|
| 18 |
Update by Surn (Charles Fettinger)
|
| 19 |
"""
|
| 20 |
|
| 21 |
-
__version__ = "0.3.
|
| 22 |
|
| 23 |
import json
|
| 24 |
import os
|
|
@@ -41,6 +41,7 @@ from mutagen.mp3 import MP3, EasyMP3
|
|
| 41 |
import torchaudio
|
| 42 |
import subprocess
|
| 43 |
from modules.file_utils import get_file_parts, rename_file_to_lowercase_extension
|
|
|
|
| 44 |
|
| 45 |
user_profile = gr.State(None)
|
| 46 |
|
|
@@ -50,9 +51,10 @@ def get_profile() -> gr.OAuthProfile | None:
|
|
| 50 |
|
| 51 |
return user_profile
|
| 52 |
|
| 53 |
-
def setup(folder_path: str | Path | None = None) -> None:
|
| 54 |
user_history = _UserHistory()
|
| 55 |
user_history.folder_path = _resolve_folder_path(folder_path)
|
|
|
|
| 56 |
user_history.initialized = True
|
| 57 |
|
| 58 |
|
|
@@ -188,6 +190,7 @@ def save_file(
|
|
| 188 |
document: str | Path | None = None,
|
| 189 |
label: str | None = None,
|
| 190 |
metadata: Dict | None = None,
|
|
|
|
| 191 |
):
|
| 192 |
# Ignore files from logged out users
|
| 193 |
if profile is None:
|
|
@@ -211,40 +214,99 @@ def save_file(
|
|
| 211 |
if "datetime" not in metadata:
|
| 212 |
metadata["datetime"] = str(datetime.now())
|
| 213 |
|
| 214 |
-
|
| 215 |
-
|
| 216 |
if audio is not None:
|
| 217 |
-
|
| 218 |
-
audio_path = _add_metadata(audio_path, metadata)
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
video_path = None
|
| 222 |
-
# Copy video to storage - need audio_path if available
|
| 223 |
if video is not None:
|
| 224 |
-
|
| 225 |
-
video_path = _add_metadata(video_path, metadata, str(audio_path))
|
| 226 |
-
|
| 227 |
-
# Copy image to storage - need video_path if available
|
| 228 |
-
image_path = None
|
| 229 |
if image is not None:
|
| 230 |
-
|
| 231 |
-
image_path = _add_metadata(image_path, metadata)
|
| 232 |
-
|
| 233 |
-
document_path = None
|
| 234 |
-
# Copy document to storage
|
| 235 |
if document is not None:
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 241 |
if image_path is None and video_path is None and audio_path is None and document_path is None:
|
| 242 |
return
|
| 243 |
-
#
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 248 |
|
| 249 |
def get_filepath():
|
| 250 |
"""Return the path to the user history folder."""
|
|
|
|
| 18 |
Update by Surn (Charles Fettinger)
|
| 19 |
"""
|
| 20 |
|
| 21 |
+
__version__ = "0.3.4"
|
| 22 |
|
| 23 |
import json
|
| 24 |
import os
|
|
|
|
| 41 |
import torchaudio
|
| 42 |
import subprocess
|
| 43 |
from modules.file_utils import get_file_parts, rename_file_to_lowercase_extension
|
| 44 |
+
from tqdm import tqdm
|
| 45 |
|
| 46 |
user_profile = gr.State(None)
|
| 47 |
|
|
|
|
| 51 |
|
| 52 |
return user_profile
|
| 53 |
|
| 54 |
+
def setup(folder_path: str | Path | None = None, display_type: str = "image_path") -> None:
|
| 55 |
user_history = _UserHistory()
|
| 56 |
user_history.folder_path = _resolve_folder_path(folder_path)
|
| 57 |
+
user_history.display_type = display_type
|
| 58 |
user_history.initialized = True
|
| 59 |
|
| 60 |
|
|
|
|
| 190 |
document: str | Path | None = None,
|
| 191 |
label: str | None = None,
|
| 192 |
metadata: Dict | None = None,
|
| 193 |
+
progress= gr.Progress(track_tqdm=True)
|
| 194 |
):
|
| 195 |
# Ignore files from logged out users
|
| 196 |
if profile is None:
|
|
|
|
| 214 |
if "datetime" not in metadata:
|
| 215 |
metadata["datetime"] = str(datetime.now())
|
| 216 |
|
| 217 |
+
# Count operations to later update progress
|
| 218 |
+
operations = []
|
| 219 |
if audio is not None:
|
| 220 |
+
operations.append("audio")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 221 |
if video is not None:
|
| 222 |
+
operations.append("video")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
if image is not None:
|
| 224 |
+
operations.append("image")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 225 |
if document is not None:
|
| 226 |
+
operations.append("document")
|
| 227 |
+
operations.append("jsonl")
|
| 228 |
+
operations.append("cleanup")
|
| 229 |
+
|
| 230 |
+
# Create a progress bar
|
| 231 |
+
with tqdm(total=len(operations), desc="Saving files to history..") as pb:
|
| 232 |
+
audio_path = None
|
| 233 |
+
# Copy audio to storage
|
| 234 |
+
if audio is not None:
|
| 235 |
+
audio_path1 = _copy_file(audio, dst_folder=user_history._user_file_path(username, "audios"), uniqueId=uniqueId)
|
| 236 |
+
audio_path = _add_metadata(audio_path1, metadata)
|
| 237 |
+
pb.update(1)
|
| 238 |
+
|
| 239 |
+
video_path = None
|
| 240 |
+
# Copy video to storage - need audio_path if available
|
| 241 |
+
if video is not None:
|
| 242 |
+
video_path1 = _copy_file(video, dst_folder=user_history._user_file_path(username, "videos"), uniqueId=uniqueId)
|
| 243 |
+
video_path = _add_metadata(video_path1, metadata, str(audio_path))
|
| 244 |
+
pb.update(1)
|
| 245 |
+
|
| 246 |
+
image_path = None
|
| 247 |
+
# Copy image to storage - need video_path if available
|
| 248 |
+
if image is not None:
|
| 249 |
+
image_path1 = _copy_image(image, dst_folder=user_history._user_images_path(username), uniqueId=uniqueId)
|
| 250 |
+
image_path = _add_metadata(image_path1, metadata)
|
| 251 |
+
pb.update(1)
|
| 252 |
+
|
| 253 |
+
document_path = None
|
| 254 |
+
# Copy document to storage
|
| 255 |
+
if document is not None:
|
| 256 |
+
document_path1 = _copy_file(document, dst_folder=user_history._user_file_path(username, "documents"), uniqueId=uniqueId)
|
| 257 |
+
document_path = _add_metadata(document_path1, metadata)
|
| 258 |
+
pb.update(1)
|
| 259 |
+
|
| 260 |
+
# Save Json file with combined data
|
| 261 |
+
data = {
|
| 262 |
+
"image_path": str(image_path),
|
| 263 |
+
"video_path": str(video_path),
|
| 264 |
+
"audio_path": str(audio_path),
|
| 265 |
+
"document_path": str(document_path),
|
| 266 |
+
"label": _UserHistory._sanitize_for_json(label),
|
| 267 |
+
"metadata": _UserHistory._sanitize_for_json(metadata)
|
| 268 |
+
}
|
| 269 |
+
with user_history._user_lock(username):
|
| 270 |
+
with user_history._user_jsonl_path(username).open("a") as f:
|
| 271 |
+
f.write(json.dumps(data) + "\n")
|
| 272 |
+
pb.update(1)
|
| 273 |
+
|
| 274 |
+
# Cleanup
|
| 275 |
+
if "audio" in operations and audio_path1 and audio_path1.exists():
|
| 276 |
+
try:
|
| 277 |
+
audio_path1.unlink()
|
| 278 |
+
except Exception as e:
|
| 279 |
+
print(f"An error occurred while deleting the audio history file: {e}")
|
| 280 |
+
if "video" in operations and video_path1 and video_path1.exists():
|
| 281 |
+
try:
|
| 282 |
+
video_path1.unlink()
|
| 283 |
+
except Exception as e:
|
| 284 |
+
print(f"An error occurred while deleting the video history file: {e}")
|
| 285 |
+
if "image" in operations and image_path1 and image_path1.exists():
|
| 286 |
+
try:
|
| 287 |
+
image_path1.unlink()
|
| 288 |
+
except Exception as e:
|
| 289 |
+
print(f"An error occurred while deleting the image history file: {e}")
|
| 290 |
+
if "document" in operations and document_path1 and document_path1.exists():
|
| 291 |
+
try:
|
| 292 |
+
document_path1.unlink()
|
| 293 |
+
except Exception as e:
|
| 294 |
+
print(f"An error occurred while deleting the document history file: {e}")
|
| 295 |
+
pb.update(1)
|
| 296 |
+
|
| 297 |
+
# If no files were saved, nothing to do
|
| 298 |
if image_path is None and video_path is None and audio_path is None and document_path is None:
|
| 299 |
return
|
| 300 |
+
# else:
|
| 301 |
+
# # Return the paths of the saved files
|
| 302 |
+
# return {
|
| 303 |
+
# "image_path": image_path,
|
| 304 |
+
# "video_path": video_path,
|
| 305 |
+
# "audio_path": audio_path,
|
| 306 |
+
# "document_path": document_path,
|
| 307 |
+
# "label": label,
|
| 308 |
+
# "metadata": metadata
|
| 309 |
+
# }, json.dumps(data)
|
| 310 |
|
| 311 |
def get_filepath():
|
| 312 |
"""Return the path to the user history folder."""
|