Surn commited on
Commit
21c8b01
·
1 Parent(s): ce5d032

Update Readme

Browse files
Files changed (2) hide show
  1. README.md +7 -4
  2. modules/file_utils.py +0 -116
README.md CHANGED
@@ -56,7 +56,7 @@ hf_oauth: true
56
 
57
  ```bash
58
  # requirements.txt
59
- git+https://huggingface.co/spaces/Wauplin/gradio-user-history
60
  filetype @ git+https://github.com/h2non/filetype.py.git
61
  gradio[oauth]
62
  mutagen
@@ -81,18 +81,21 @@ def generate(prompt: str, profile: gr.OAuthProfile | None):
81
 
82
  # => Save generated image(s)
83
  gr_user_history.save_image(label=prompt, image=image, profile=profile)
84
- return image
 
 
85
 
86
 
87
  # => Render user history
88
  with gr.Blocks() as demo:
89
  (...)
90
 
91
- with gr.Accordion("Past generations", open=False):
 
 
92
  gr_user_history.render()
93
  ```
94
 
95
-
96
  **4. (optional) Add Persistent Storage in your Space settings.**
97
  Persistent Storage is suggested but not mandatory. If not enabled, the history is lost each time the Space restarts.
98
 
 
56
 
57
  ```bash
58
  # requirements.txt
59
+ git+https://huggingface.co/spaces/Surn/gradio-user-history
60
  filetype @ git+https://github.com/h2non/filetype.py.git
61
  gradio[oauth]
62
  mutagen
 
81
 
82
  # => Save generated image(s)
83
  gr_user_history.save_image(label=prompt, image=image, profile=profile)
84
+
85
+ # => Save generated image, video, audio, document, metadata
86
+ gr_user_history.save_file(profile=profile,image=image, video=video, audio=audio, document=document,label=string, metadata=metadata)
87
 
88
 
89
  # => Render user history
90
  with gr.Blocks() as demo:
91
  (...)
92
 
93
+ with gr.Accordion("Past generations", open=False):
94
+ # => OPTIONALLY display images or videos in the history gallery with display_type: "image_path" or "video_path"
95
+ gr_user_history.setup(display_type="image_path")
96
  gr_user_history.render()
97
  ```
98
 
 
99
  **4. (optional) Add Persistent Storage in your Space settings.**
100
  Persistent Storage is suggested but not mandatory. If not enabled, the history is lost each time the Space restarts.
101
 
modules/file_utils.py DELETED
@@ -1,116 +0,0 @@
1
- # file_utils
2
- import os
3
- from pathlib import Path
4
-
5
- def get_file_parts(file_path: str):
6
- # Split the path into directory and filename
7
- directory, filename = os.path.split(file_path)
8
-
9
- # Split the filename into name and extension
10
- name, ext = os.path.splitext(filename)
11
-
12
- # Convert the extension to lowercase
13
- new_ext = ext.lower()
14
- return directory, filename, name, ext, new_ext
15
-
16
- def rename_file_to_lowercase_extension(file_path: str) -> str:
17
- """
18
- Renames a file's extension to lowercase in place.
19
-
20
- Parameters:
21
- file_path (str): The original file path.
22
-
23
- Returns:
24
- str: The new file path with the lowercase extension.
25
-
26
- Raises:
27
- OSError: If there is an error renaming the file (e.g., file not found, permissions issue).
28
- """
29
- directory, filename, name, ext, new_ext = get_file_parts(file_path)
30
- # If the extension changes, rename the file
31
- if ext != new_ext:
32
- new_filename = name + new_ext
33
- new_file_path = os.path.join(directory, new_filename)
34
- try:
35
- os.rename(file_path, new_file_path)
36
- print(f"Rename {file_path} to {new_file_path}\n")
37
- except Exception as e:
38
- print(f"os.rename failed: {e}. Falling back to binary copy operation.")
39
- try:
40
- # Read the file in binary mode and write it to new_file_path
41
- with open(file_path, 'rb') as f:
42
- data = f.read()
43
- with open(new_file_path, 'wb') as f:
44
- f.write(data)
45
- print(f"Copied {file_path} to {new_file_path}\n")
46
- # Optionally, remove the original file after copying
47
- #os.remove(file_path)
48
- except Exception as inner_e:
49
- print(f"Failed to copy file from {file_path} to {new_file_path}: {inner_e}")
50
- raise inner_e
51
- return new_file_path
52
- else:
53
- return file_path
54
-
55
- def get_filename(file):
56
- # extract filename from file object
57
- filename = None
58
- if file is not None:
59
- filename = file.name
60
- return filename
61
-
62
- def convert_title_to_filename(title):
63
- # convert title to filename
64
- filename = title.lower().replace(" ", "_").replace("/", "_")
65
- return filename
66
-
67
- def get_filename_from_filepath(filepath):
68
- file_name = os.path.basename(filepath)
69
- file_base, file_extension = os.path.splitext(file_name)
70
- return file_base, file_extension
71
-
72
- def delete_file(file_path: str) -> None:
73
- """
74
- Deletes the specified file.
75
-
76
- Parameters:
77
- file_path (str): The path to thefile to delete.
78
-
79
- Raises:
80
- FileNotFoundError: If the file does not exist.
81
- Exception: If there is an error deleting the file.
82
- """
83
- try:
84
- path = Path(file_path)
85
- path.unlink()
86
- print(f"Deleted original file: {file_path}")
87
- except FileNotFoundError:
88
- print(f"File not found: {file_path}")
89
- except Exception as e:
90
- print(f"Error deleting file: {e}")
91
-
92
- def get_unique_file_path(directory, filename, file_ext, counter=0):
93
- """
94
- Recursively increments the filename until a unique path is found.
95
-
96
- Parameters:
97
- directory (str): The directory for the file.
98
- filename (str): The base filename.
99
- file_ext (str): The file extension including the leading dot.
100
- counter (int): The current counter value to append.
101
-
102
- Returns:
103
- str: A unique file path that does not exist.
104
- """
105
- if counter == 0:
106
- filepath = os.path.join(directory, f"{filename}{file_ext}")
107
- else:
108
- filepath = os.path.join(directory, f"{filename}{counter}{file_ext}")
109
-
110
- if not os.path.exists(filepath):
111
- return filepath
112
- else:
113
- return get_unique_file_path(directory, filename, file_ext, counter + 1)
114
-
115
- # Example usage:
116
- # new_file_path = get_unique_file_path(video_dir, title_file_name, video_new_ext)