Spaces:
Running
on
T4
Running
on
T4
User History Update 0.2.3
Browse files- modules/user_history.py +16 -7
modules/user_history.py
CHANGED
|
@@ -18,7 +18,7 @@ Useful links:
|
|
| 18 |
Update by Surn (Charles Fettinger)
|
| 19 |
"""
|
| 20 |
|
| 21 |
-
__version__ = "0.2.
|
| 22 |
|
| 23 |
import json
|
| 24 |
import os
|
|
@@ -113,7 +113,7 @@ def render() -> None:
|
|
| 113 |
height=600,
|
| 114 |
preview=False,
|
| 115 |
show_share_button=False,
|
| 116 |
-
show_download_button=True,
|
| 117 |
)
|
| 118 |
gr.Markdown(
|
| 119 |
"User history is powered by"
|
|
@@ -243,6 +243,15 @@ def save_file(
|
|
| 243 |
with user_history._user_jsonl_path(username).open("a") as f:
|
| 244 |
f.write(json.dumps(data) + "\n")
|
| 245 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 246 |
|
| 247 |
#############
|
| 248 |
# Internals #
|
|
@@ -380,7 +389,7 @@ def _copy_image(image: Image | np.ndarray | str | Path, dst_folder: Path) -> Pat
|
|
| 380 |
if isinstance(image, str):
|
| 381 |
image = Path(image)
|
| 382 |
if isinstance(image, Path):
|
| 383 |
-
dst = dst_folder / f"{uuid4().hex}_{Path(image).name}" # keep file ext
|
| 384 |
shutil.copyfile(image, dst)
|
| 385 |
return dst
|
| 386 |
|
|
@@ -388,7 +397,7 @@ def _copy_image(image: Image | np.ndarray | str | Path, dst_folder: Path) -> Pat
|
|
| 388 |
if isinstance(image, np.ndarray):
|
| 389 |
image = Image.fromarray(image)
|
| 390 |
if isinstance(image, Image):
|
| 391 |
-
dst = dst_folder / f"{Path(
|
| 392 |
image.save(dst)
|
| 393 |
return dst
|
| 394 |
|
|
@@ -407,21 +416,21 @@ def _copy_file(file: Any | np.ndarray | str | Path, dst_folder: Path) -> Path:
|
|
| 407 |
if isinstance(file, str):
|
| 408 |
file = Path(file)
|
| 409 |
if isinstance(file, Path):
|
| 410 |
-
dst = dst_folder / f"{file.stem}_{uuid4().hex}{file.suffix}" # keep file ext
|
| 411 |
shutil.copyfile(file, dst)
|
| 412 |
return dst
|
| 413 |
|
| 414 |
# Still a Python object => serialize it
|
| 415 |
if isinstance(file, np.ndarray):
|
| 416 |
file = Image.fromarray(file)
|
| 417 |
-
dst = dst_folder / f"{file.filename}_{uuid4().hex}{file.suffix}"
|
| 418 |
file.save(dst)
|
| 419 |
return dst
|
| 420 |
|
| 421 |
# try other file types
|
| 422 |
kind = filetype.guess(file)
|
| 423 |
if kind is not None:
|
| 424 |
-
dst = dst_folder / f"{Path(file).stem}_{uuid4().hex}.{kind.extension}"
|
| 425 |
shutil.copyfile(file, dst)
|
| 426 |
return dst
|
| 427 |
raise ValueError(f"Unsupported file type: {type(file)}")
|
|
|
|
| 18 |
Update by Surn (Charles Fettinger)
|
| 19 |
"""
|
| 20 |
|
| 21 |
+
__version__ = "0.2.3"
|
| 22 |
|
| 23 |
import json
|
| 24 |
import os
|
|
|
|
| 113 |
height=600,
|
| 114 |
preview=False,
|
| 115 |
show_share_button=False,
|
| 116 |
+
show_download_button=True,
|
| 117 |
)
|
| 118 |
gr.Markdown(
|
| 119 |
"User history is powered by"
|
|
|
|
| 243 |
with user_history._user_jsonl_path(username).open("a") as f:
|
| 244 |
f.write(json.dumps(data) + "\n")
|
| 245 |
|
| 246 |
+
def get_filepath():
|
| 247 |
+
"""Return the path to the user history folder."""
|
| 248 |
+
user_history = _UserHistory()
|
| 249 |
+
if not user_history.initialized:
|
| 250 |
+
warnings.warn("User history is not set in Gradio demo. You must use `user_history.render(...)` first.")
|
| 251 |
+
return None
|
| 252 |
+
return user_history.folder_path
|
| 253 |
+
|
| 254 |
+
|
| 255 |
|
| 256 |
#############
|
| 257 |
# Internals #
|
|
|
|
| 389 |
if isinstance(image, str):
|
| 390 |
image = Path(image)
|
| 391 |
if isinstance(image, Path):
|
| 392 |
+
dst = dst_folder / f"{uuid4().hex[:4]}_{Path(image).name}_h.png" # keep file ext
|
| 393 |
shutil.copyfile(image, dst)
|
| 394 |
return dst
|
| 395 |
|
|
|
|
| 397 |
if isinstance(image, np.ndarray):
|
| 398 |
image = Image.fromarray(image)
|
| 399 |
if isinstance(image, Image):
|
| 400 |
+
dst = dst_folder / f"{Path(image).name}_h.png"
|
| 401 |
image.save(dst)
|
| 402 |
return dst
|
| 403 |
|
|
|
|
| 416 |
if isinstance(file, str):
|
| 417 |
file = Path(file)
|
| 418 |
if isinstance(file, Path):
|
| 419 |
+
dst = dst_folder / f"{file.stem}_{uuid4().hex[:4]}{file.suffix}" # keep file ext
|
| 420 |
shutil.copyfile(file, dst)
|
| 421 |
return dst
|
| 422 |
|
| 423 |
# Still a Python object => serialize it
|
| 424 |
if isinstance(file, np.ndarray):
|
| 425 |
file = Image.fromarray(file)
|
| 426 |
+
dst = dst_folder / f"{file.filename}_{uuid4().hex[:4]}{file.suffix}"
|
| 427 |
file.save(dst)
|
| 428 |
return dst
|
| 429 |
|
| 430 |
# try other file types
|
| 431 |
kind = filetype.guess(file)
|
| 432 |
if kind is not None:
|
| 433 |
+
dst = dst_folder / f"{Path(file).stem}_{uuid4().hex[:4]}.{kind.extension}"
|
| 434 |
shutil.copyfile(file, dst)
|
| 435 |
return dst
|
| 436 |
raise ValueError(f"Unsupported file type: {type(file)}")
|