Spaces:
				
			
			
	
			
			
		Runtime error
		
	
	
	
			
			
	
	
	
	
		
		
		Runtime error
		
	Updates Round 2
Browse files- user_history.py +0 -598
 
    	
        user_history.py
    DELETED
    
    | 
         @@ -1,598 +0,0 @@ 
     | 
|
| 1 | 
         
            -
            """
         
     | 
| 2 | 
         
            -
            User History is a plugin that you can add to your Spaces to cache generated images for your users.
         
     | 
| 3 | 
         
            -
             
     | 
| 4 | 
         
            -
            Key features:
         
     | 
| 5 | 
         
            -
            - 🤗 Sign in with Hugging Face
         
     | 
| 6 | 
         
            -
            - Save generated image, video, audio and document files with their metadata: prompts, timestamp, hyper-parameters, etc.
         
     | 
| 7 | 
         
            -
            - Export your history as zip.
         
     | 
| 8 | 
         
            -
            - Delete your history to respect privacy.
         
     | 
| 9 | 
         
            -
            - Compatible with Persistent Storage for long-term storage.
         
     | 
| 10 | 
         
            -
            - Admin panel to check configuration and disk usage .
         
     | 
| 11 | 
         
            -
             
     | 
| 12 | 
         
            -
            Useful links:
         
     | 
| 13 | 
         
            -
            - Demo: https://huggingface.co/spaces/Wauplin/gradio-user-history
         
     | 
| 14 | 
         
            -
            - README: https://huggingface.co/spaces/Wauplin/gradio-user-history/blob/main/README.md
         
     | 
| 15 | 
         
            -
            - Source file: https://huggingface.co/spaces/Wauplin/gradio-user-history/blob/main/user_history.py
         
     | 
| 16 | 
         
            -
            - Discussions: https://huggingface.co/spaces/Wauplin/gradio-user-history/discussions
         
     | 
| 17 | 
         
            -
            """
         
     | 
| 18 | 
         
            -
             
     | 
| 19 | 
         
            -
            __version__ = "0.2.0"
         
     | 
| 20 | 
         
            -
             
     | 
| 21 | 
         
            -
            import json
         
     | 
| 22 | 
         
            -
            import os
         
     | 
| 23 | 
         
            -
            import shutil
         
     | 
| 24 | 
         
            -
            import warnings
         
     | 
| 25 | 
         
            -
            from datetime import datetime
         
     | 
| 26 | 
         
            -
            from functools import cache
         
     | 
| 27 | 
         
            -
            from pathlib import Path
         
     | 
| 28 | 
         
            -
            from typing import Callable, Dict, List, Tuple
         
     | 
| 29 | 
         
            -
            from uuid import uuid4
         
     | 
| 30 | 
         
            -
             
     | 
| 31 | 
         
            -
            import gradio as gr
         
     | 
| 32 | 
         
            -
            import numpy as np
         
     | 
| 33 | 
         
            -
            import requests
         
     | 
| 34 | 
         
            -
            from filelock import FileLock
         
     | 
| 35 | 
         
            -
            from PIL.Image import Image
         
     | 
| 36 | 
         
            -
            import filetype
         
     | 
| 37 | 
         
            -
            import wave
         
     | 
| 38 | 
         
            -
            from mutagen.mp3 import MP3, EasyMP3
         
     | 
| 39 | 
         
            -
            import torchaudio
         
     | 
| 40 | 
         
            -
            import subprocess
         
     | 
| 41 | 
         
            -
             
     | 
| 42 | 
         
            -
             
     | 
| 43 | 
         
            -
            def setup(folder_path: str | Path | None = None) -> None:
         
     | 
| 44 | 
         
            -
                user_history = _UserHistory()
         
     | 
| 45 | 
         
            -
                user_history.folder_path = _resolve_folder_path(folder_path)
         
     | 
| 46 | 
         
            -
                user_history.initialized = True
         
     | 
| 47 | 
         
            -
             
     | 
| 48 | 
         
            -
             
     | 
| 49 | 
         
            -
            def render() -> None:
         
     | 
| 50 | 
         
            -
                user_history = _UserHistory()
         
     | 
| 51 | 
         
            -
             
     | 
| 52 | 
         
            -
                # initialize with default config
         
     | 
| 53 | 
         
            -
                if not user_history.initialized:
         
     | 
| 54 | 
         
            -
                    print("Initializing user history with default config. Use `user_history.setup(...)` to customize folder_path.")
         
     | 
| 55 | 
         
            -
                    setup()
         
     | 
| 56 | 
         
            -
             
     | 
| 57 | 
         
            -
                # Render user history tab
         
     | 
| 58 | 
         
            -
                gr.Markdown(
         
     | 
| 59 | 
         
            -
                    "## Your past generations\n\nLog in to keep a gallery of your previous generations. Your history will be saved"
         
     | 
| 60 | 
         
            -
                    " and available on your next visit. Make sure to export your images from time to time as this gallery may be"
         
     | 
| 61 | 
         
            -
                    " deleted in the future."
         
     | 
| 62 | 
         
            -
                )
         
     | 
| 63 | 
         
            -
             
     | 
| 64 | 
         
            -
                if os.getenv("SYSTEM") == "spaces" and not os.path.exists("/data"):
         
     | 
| 65 | 
         
            -
                    gr.Markdown(
         
     | 
| 66 | 
         
            -
                        "**⚠️ Persistent storage is disabled, meaning your history will be lost if the Space gets restarted."
         
     | 
| 67 | 
         
            -
                        " Only the Space owner can setup a Persistent Storage. If you are not the Space owner, consider"
         
     | 
| 68 | 
         
            -
                        " duplicating this Space to set your own storage.⚠️**"
         
     | 
| 69 | 
         
            -
                    )
         
     | 
| 70 | 
         
            -
             
     | 
| 71 | 
         
            -
                with gr.Row():
         
     | 
| 72 | 
         
            -
                    gr.LoginButton(min_width=250)
         
     | 
| 73 | 
         
            -
                    #gr.LogoutButton(min_width=250)
         
     | 
| 74 | 
         
            -
                    refresh_button = gr.Button(
         
     | 
| 75 | 
         
            -
                        "Refresh",
         
     | 
| 76 | 
         
            -
                        icon="./assets/icon_refresh.png",
         
     | 
| 77 | 
         
            -
                    )
         
     | 
| 78 | 
         
            -
                    export_button = gr.Button(
         
     | 
| 79 | 
         
            -
                        "Export",
         
     | 
| 80 | 
         
            -
                        icon="./assets/icon_download.png",
         
     | 
| 81 | 
         
            -
                    )
         
     | 
| 82 | 
         
            -
                    delete_button = gr.Button(
         
     | 
| 83 | 
         
            -
                        "Delete history",
         
     | 
| 84 | 
         
            -
                        icon="./assets/icon_delete.png",
         
     | 
| 85 | 
         
            -
                    )
         
     | 
| 86 | 
         
            -
             
     | 
| 87 | 
         
            -
                # "Export zip" row (hidden by default)
         
     | 
| 88 | 
         
            -
                with gr.Row():
         
     | 
| 89 | 
         
            -
                    export_file = gr.File(file_count="single", file_types=[".zip"], label="Exported history", visible=False)
         
     | 
| 90 | 
         
            -
             
     | 
| 91 | 
         
            -
                # "Config deletion" row (hidden by default)
         
     | 
| 92 | 
         
            -
                with gr.Row():
         
     | 
| 93 | 
         
            -
                    confirm_button = gr.Button("Confirm delete all history", variant="stop", visible=False)
         
     | 
| 94 | 
         
            -
                    cancel_button = gr.Button("Cancel", visible=False)
         
     | 
| 95 | 
         
            -
             
     | 
| 96 | 
         
            -
                # Gallery
         
     | 
| 97 | 
         
            -
                gallery = gr.Gallery(
         
     | 
| 98 | 
         
            -
                    label="Past images",
         
     | 
| 99 | 
         
            -
                    show_label=True,
         
     | 
| 100 | 
         
            -
                    elem_id="gradio_user_history_gallery",
         
     | 
| 101 | 
         
            -
                    object_fit="contain",
         
     | 
| 102 | 
         
            -
                    columns=5,
         
     | 
| 103 | 
         
            -
                    height=600,
         
     | 
| 104 | 
         
            -
                    preview=False,
         
     | 
| 105 | 
         
            -
                    show_share_button=False,
         
     | 
| 106 | 
         
            -
                    show_download_button=False,
         
     | 
| 107 | 
         
            -
                )
         
     | 
| 108 | 
         
            -
                gr.Markdown(
         
     | 
| 109 | 
         
            -
                    "User history is powered by"
         
     | 
| 110 | 
         
            -
                    " [Wauplin/gradio-user-history](https://huggingface.co/spaces/Wauplin/gradio-user-history). Integrate it to"
         
     | 
| 111 | 
         
            -
                    " your own Space in just a few lines of code!"
         
     | 
| 112 | 
         
            -
                )
         
     | 
| 113 | 
         
            -
                gallery.attach_load_event(_fetch_user_history, every=None)
         
     | 
| 114 | 
         
            -
             
     | 
| 115 | 
         
            -
                # Interactions
         
     | 
| 116 | 
         
            -
                refresh_button.click(fn=_fetch_user_history, inputs=[], outputs=[gallery], queue=False)
         
     | 
| 117 | 
         
            -
                export_button.click(fn=_export_user_history, inputs=[], outputs=[export_file], queue=False)
         
     | 
| 118 | 
         
            -
             
     | 
| 119 | 
         
            -
                # Taken from https://github.com/gradio-app/gradio/issues/3324#issuecomment-1446382045
         
     | 
| 120 | 
         
            -
                delete_button.click(
         
     | 
| 121 | 
         
            -
                    lambda: [gr.update(visible=True), gr.update(visible=True)],
         
     | 
| 122 | 
         
            -
                    outputs=[confirm_button, cancel_button],
         
     | 
| 123 | 
         
            -
                    queue=False,
         
     | 
| 124 | 
         
            -
                )
         
     | 
| 125 | 
         
            -
                cancel_button.click(
         
     | 
| 126 | 
         
            -
                    lambda: [gr.update(visible=False), gr.update(visible=False)],
         
     | 
| 127 | 
         
            -
                    outputs=[confirm_button, cancel_button],
         
     | 
| 128 | 
         
            -
                    queue=False,
         
     | 
| 129 | 
         
            -
                )
         
     | 
| 130 | 
         
            -
                confirm_button.click(_delete_user_history).then(
         
     | 
| 131 | 
         
            -
                    lambda: [gr.update(visible=False), gr.update(visible=False)],
         
     | 
| 132 | 
         
            -
                    outputs=[confirm_button, cancel_button],
         
     | 
| 133 | 
         
            -
                    queue=False,
         
     | 
| 134 | 
         
            -
                )
         
     | 
| 135 | 
         
            -
             
     | 
| 136 | 
         
            -
                # Admin section (only shown locally or when logged in as Space owner)
         
     | 
| 137 | 
         
            -
                _admin_section()
         
     | 
| 138 | 
         
            -
             
     | 
| 139 | 
         
            -
             
     | 
| 140 | 
         
            -
            def save_image(
         
     | 
| 141 | 
         
            -
                profile: gr.OAuthProfile | None,
         
     | 
| 142 | 
         
            -
                image: Image | np.ndarray | str | Path,
         
     | 
| 143 | 
         
            -
                label: str | None = None,
         
     | 
| 144 | 
         
            -
                metadata: Dict | None = None,
         
     | 
| 145 | 
         
            -
            ):
         
     | 
| 146 | 
         
            -
                # Ignore images from logged out users
         
     | 
| 147 | 
         
            -
                if profile is None:
         
     | 
| 148 | 
         
            -
                    return
         
     | 
| 149 | 
         
            -
                username = profile["preferred_username"]
         
     | 
| 150 | 
         
            -
             
     | 
| 151 | 
         
            -
                # Ignore images if user history not used
         
     | 
| 152 | 
         
            -
                user_history = _UserHistory()
         
     | 
| 153 | 
         
            -
                if not user_history.initialized:
         
     | 
| 154 | 
         
            -
                    warnings.warn(
         
     | 
| 155 | 
         
            -
                        "User history is not set in Gradio demo. Saving image is ignored. You must use `user_history.render(...)`"
         
     | 
| 156 | 
         
            -
                        " first."
         
     | 
| 157 | 
         
            -
                    )
         
     | 
| 158 | 
         
            -
                    return
         
     | 
| 159 | 
         
            -
             
     | 
| 160 | 
         
            -
                # Copy image to storage
         
     | 
| 161 | 
         
            -
                image_path = _copy_image(image, dst_folder=user_history._user_images_path(username))
         
     | 
| 162 | 
         
            -
             
     | 
| 163 | 
         
            -
                # Save new image + metadata
         
     | 
| 164 | 
         
            -
                if metadata is None:
         
     | 
| 165 | 
         
            -
                    metadata = {}
         
     | 
| 166 | 
         
            -
                if "datetime" not in metadata:
         
     | 
| 167 | 
         
            -
                    metadata["datetime"] = str(datetime.now())
         
     | 
| 168 | 
         
            -
                data = {"path": str(image_path), "label": label, "metadata": metadata}
         
     | 
| 169 | 
         
            -
                with user_history._user_lock(username):
         
     | 
| 170 | 
         
            -
                    with user_history._user_jsonl_path(username).open("a") as f:
         
     | 
| 171 | 
         
            -
                        f.write(json.dumps(data) + "\n")
         
     | 
| 172 | 
         
            -
                        
         
     | 
| 173 | 
         
            -
            def save_file(
         
     | 
| 174 | 
         
            -
                profile: gr.OAuthProfile | None,
         
     | 
| 175 | 
         
            -
                image: Image | np.ndarray | str | Path | None = None,
         
     | 
| 176 | 
         
            -
                video: str | Path | None = None,
         
     | 
| 177 | 
         
            -
                audio: str | Path | None = None,
         
     | 
| 178 | 
         
            -
                document: str | Path | None = None,
         
     | 
| 179 | 
         
            -
                label: str | None = None,
         
     | 
| 180 | 
         
            -
                metadata: Dict | None = None,
         
     | 
| 181 | 
         
            -
            ):
         
     | 
| 182 | 
         
            -
                # Ignore files from logged out users
         
     | 
| 183 | 
         
            -
                if profile is None:
         
     | 
| 184 | 
         
            -
                    return
         
     | 
| 185 | 
         
            -
                username = profile["preferred_username"]
         
     | 
| 186 | 
         
            -
             
     | 
| 187 | 
         
            -
                # Ignore files if user history not used
         
     | 
| 188 | 
         
            -
                user_history = _UserHistory()
         
     | 
| 189 | 
         
            -
                if not user_history.initialized:
         
     | 
| 190 | 
         
            -
                    warnings.warn(
         
     | 
| 191 | 
         
            -
                        "User history is not set in Gradio demo. Saving files is ignored. You must use `user_history.render(...)`"
         
     | 
| 192 | 
         
            -
                        " first."
         
     | 
| 193 | 
         
            -
                    )
         
     | 
| 194 | 
         
            -
                    return
         
     | 
| 195 | 
         
            -
                
         
     | 
| 196 | 
         
            -
                # Save new files + metadata
         
     | 
| 197 | 
         
            -
                if metadata is None:
         
     | 
| 198 | 
         
            -
                    metadata = {}
         
     | 
| 199 | 
         
            -
                if "datetime" not in metadata:
         
     | 
| 200 | 
         
            -
                    metadata["datetime"] = str(datetime.now())
         
     | 
| 201 | 
         
            -
                    
         
     | 
| 202 | 
         
            -
                # Copy image to storage
         
     | 
| 203 | 
         
            -
                image_path = None
         
     | 
| 204 | 
         
            -
                if image is not None:
         
     | 
| 205 | 
         
            -
                    image_path = _copy_image(image, dst_folder=user_history._user_images_path(username))
         
     | 
| 206 | 
         
            -
                    image_path = _add_metadata(image_path, metadata)
         
     | 
| 207 | 
         
            -
             
     | 
| 208 | 
         
            -
                # Copy video to storage
         
     | 
| 209 | 
         
            -
                if video is not None:    
         
     | 
| 210 | 
         
            -
                    video_path = _copy_file(video, dst_folder=user_history._user_file_path(username, "videos"))
         
     | 
| 211 | 
         
            -
                    video_path = _add_metadata(video_path, metadata)
         
     | 
| 212 | 
         
            -
             
     | 
| 213 | 
         
            -
                # Copy audio to storage
         
     | 
| 214 | 
         
            -
                if audio is not None:     
         
     | 
| 215 | 
         
            -
                    audio_path = _copy_file(audio, dst_folder=user_history._user_file_path(username, "audios"))
         
     | 
| 216 | 
         
            -
                    audio_path = _add_metadata(audio_path, metadata)
         
     | 
| 217 | 
         
            -
                
         
     | 
| 218 | 
         
            -
                # Copy document to storage
         
     | 
| 219 | 
         
            -
                if document is not None:     
         
     | 
| 220 | 
         
            -
                    document_path = _copy_file(document, dst_folder=user_history._user_file_path(username, "documents"))
         
     | 
| 221 | 
         
            -
                    document_path = _add_metadata(document_path, metadata)
         
     | 
| 222 | 
         
            -
             
     | 
| 223 | 
         
            -
                # Save Json file
         
     | 
| 224 | 
         
            -
                data = {"image_path": str(image_path), "video_path": str(video_path), "audio_path": str(audio_path), "document_path": str(document_path), "label": label, "metadata": metadata}
         
     | 
| 225 | 
         
            -
                with user_history._user_lock(username):
         
     | 
| 226 | 
         
            -
                    with user_history._user_jsonl_path(username).open("a") as f:
         
     | 
| 227 | 
         
            -
                        f.write(json.dumps(data) + "\n")
         
     | 
| 228 | 
         
            -
             
     | 
| 229 | 
         
            -
             
     | 
| 230 | 
         
            -
            #############
         
     | 
| 231 | 
         
            -
            # Internals #
         
     | 
| 232 | 
         
            -
            #############
         
     | 
| 233 | 
         
            -
             
     | 
| 234 | 
         
            -
             
     | 
| 235 | 
         
            -
            class _UserHistory(object):
         
     | 
| 236 | 
         
            -
                _instance = None
         
     | 
| 237 | 
         
            -
                initialized: bool = False
         
     | 
| 238 | 
         
            -
                folder_path: Path
         
     | 
| 239 | 
         
            -
             
     | 
| 240 | 
         
            -
                def __new__(cls):
         
     | 
| 241 | 
         
            -
                    # Using singleton pattern => we don't want to expose an object (more complex to use) but still want to keep
         
     | 
| 242 | 
         
            -
                    # state between `render` and `save_image` calls.
         
     | 
| 243 | 
         
            -
                    if cls._instance is None:
         
     | 
| 244 | 
         
            -
                        cls._instance = super(_UserHistory, cls).__new__(cls)
         
     | 
| 245 | 
         
            -
                    return cls._instance
         
     | 
| 246 | 
         
            -
             
     | 
| 247 | 
         
            -
                def _user_path(self, username: str) -> Path:
         
     | 
| 248 | 
         
            -
                    path = self.folder_path / username
         
     | 
| 249 | 
         
            -
                    path.mkdir(parents=True, exist_ok=True)
         
     | 
| 250 | 
         
            -
                    return path
         
     | 
| 251 | 
         
            -
             
     | 
| 252 | 
         
            -
                def _user_lock(self, username: str) -> FileLock:
         
     | 
| 253 | 
         
            -
                    """Ensure history is not corrupted if concurrent calls."""
         
     | 
| 254 | 
         
            -
                    return FileLock(self.folder_path / f"{username}.lock")  # lock outside of folder => better when exporting ZIP
         
     | 
| 255 | 
         
            -
             
     | 
| 256 | 
         
            -
                def _user_jsonl_path(self, username: str) -> Path:
         
     | 
| 257 | 
         
            -
                    return self._user_path(username) / "history.jsonl"
         
     | 
| 258 | 
         
            -
             
     | 
| 259 | 
         
            -
                def _user_images_path(self, username: str) -> Path:
         
     | 
| 260 | 
         
            -
                    path = self._user_path(username) / "images"
         
     | 
| 261 | 
         
            -
                    path.mkdir(parents=True, exist_ok=True)
         
     | 
| 262 | 
         
            -
                    return path
         
     | 
| 263 | 
         
            -
                
         
     | 
| 264 | 
         
            -
                def _user_file_path(self, username: str, filetype: str = "images") -> Path:        
         
     | 
| 265 | 
         
            -
                    path = self._user_path(username) / filetype
         
     | 
| 266 | 
         
            -
                    path.mkdir(parents=True, exist_ok=True)
         
     | 
| 267 | 
         
            -
                    return path
         
     | 
| 268 | 
         
            -
               
         
     | 
| 269 | 
         
            -
                
         
     | 
| 270 | 
         
            -
             
     | 
| 271 | 
         
            -
            def _fetch_user_history(profile: gr.OAuthProfile | None) -> List[Tuple[str, str]]:
         
     | 
| 272 | 
         
            -
                """Return saved history for that user, if it exists."""
         
     | 
| 273 | 
         
            -
                # Cannot load history for logged out users
         
     | 
| 274 | 
         
            -
                if profile is None:
         
     | 
| 275 | 
         
            -
                    return []
         
     | 
| 276 | 
         
            -
                username = profile["preferred_username"]
         
     | 
| 277 | 
         
            -
             
     | 
| 278 | 
         
            -
                user_history = _UserHistory()
         
     | 
| 279 | 
         
            -
                if not user_history.initialized:
         
     | 
| 280 | 
         
            -
                    warnings.warn("User history is not set in Gradio demo. You must use `user_history.render(...)` first.")
         
     | 
| 281 | 
         
            -
                    return []
         
     | 
| 282 | 
         
            -
             
     | 
| 283 | 
         
            -
                with user_history._user_lock(username):
         
     | 
| 284 | 
         
            -
                    # No file => no history saved yet
         
     | 
| 285 | 
         
            -
                    jsonl_path = user_history._user_jsonl_path(username)
         
     | 
| 286 | 
         
            -
                    if not jsonl_path.is_file():
         
     | 
| 287 | 
         
            -
                        return []
         
     | 
| 288 | 
         
            -
             
     | 
| 289 | 
         
            -
                    # Read history
         
     | 
| 290 | 
         
            -
                    images = []
         
     | 
| 291 | 
         
            -
                    for line in jsonl_path.read_text().splitlines():
         
     | 
| 292 | 
         
            -
                        data = json.loads(line)
         
     | 
| 293 | 
         
            -
                        images.append((data["path"], data["label"] or ""))
         
     | 
| 294 | 
         
            -
                    return list(reversed(images))
         
     | 
| 295 | 
         
            -
             
     | 
| 296 | 
         
            -
             
     | 
| 297 | 
         
            -
            def _export_user_history(profile: gr.OAuthProfile | None) -> Dict | None:
         
     | 
| 298 | 
         
            -
                """Zip all history for that user, if it exists and return it as a downloadable file."""
         
     | 
| 299 | 
         
            -
                # Cannot load history for logged out users
         
     | 
| 300 | 
         
            -
                if profile is None:
         
     | 
| 301 | 
         
            -
                    return None
         
     | 
| 302 | 
         
            -
                username = profile["preferred_username"]
         
     | 
| 303 | 
         
            -
             
     | 
| 304 | 
         
            -
                user_history = _UserHistory()
         
     | 
| 305 | 
         
            -
                if not user_history.initialized:
         
     | 
| 306 | 
         
            -
                    warnings.warn("User history is not set in Gradio demo. You must use `user_history.render(...)` first.")
         
     | 
| 307 | 
         
            -
                    return None
         
     | 
| 308 | 
         
            -
             
     | 
| 309 | 
         
            -
                # Zip history
         
     | 
| 310 | 
         
            -
                with user_history._user_lock(username):
         
     | 
| 311 | 
         
            -
                    path = shutil.make_archive(
         
     | 
| 312 | 
         
            -
                        str(_archives_path() / f"history_{username}"), "zip", user_history._user_path(username)
         
     | 
| 313 | 
         
            -
                    )
         
     | 
| 314 | 
         
            -
             
     | 
| 315 | 
         
            -
                return gr.update(visible=True, value=path)
         
     | 
| 316 | 
         
            -
             
     | 
| 317 | 
         
            -
             
     | 
| 318 | 
         
            -
            def _delete_user_history(profile: gr.OAuthProfile | None) -> None:
         
     | 
| 319 | 
         
            -
                """Delete all history for that user."""
         
     | 
| 320 | 
         
            -
                # Cannot load history for logged out users
         
     | 
| 321 | 
         
            -
                if profile is None:
         
     | 
| 322 | 
         
            -
                    return
         
     | 
| 323 | 
         
            -
                username = profile["preferred_username"]
         
     | 
| 324 | 
         
            -
             
     | 
| 325 | 
         
            -
                user_history = _UserHistory()
         
     | 
| 326 | 
         
            -
                if not user_history.initialized:
         
     | 
| 327 | 
         
            -
                    warnings.warn("User history is not set in Gradio demo. You must use `user_history.render(...)` first.")
         
     | 
| 328 | 
         
            -
                    return
         
     | 
| 329 | 
         
            -
             
     | 
| 330 | 
         
            -
                with user_history._user_lock(username):
         
     | 
| 331 | 
         
            -
                    shutil.rmtree(user_history._user_path(username))
         
     | 
| 332 | 
         
            -
             
     | 
| 333 | 
         
            -
             
     | 
| 334 | 
         
            -
            ####################
         
     | 
| 335 | 
         
            -
            # Internal helpers #
         
     | 
| 336 | 
         
            -
            ####################
         
     | 
| 337 | 
         
            -
             
     | 
| 338 | 
         
            -
             
     | 
| 339 | 
         
            -
            def _copy_image(image: Image | np.ndarray | str | Path, dst_folder: Path) -> Path:
         
     | 
| 340 | 
         
            -
                try:
         
     | 
| 341 | 
         
            -
                    """Copy image to the images folder."""
         
     | 
| 342 | 
         
            -
                    # Already a path => copy it
         
     | 
| 343 | 
         
            -
                    if isinstance(image, str):
         
     | 
| 344 | 
         
            -
                        image = Path(image)
         
     | 
| 345 | 
         
            -
                    if isinstance(image, Path):
         
     | 
| 346 | 
         
            -
                        dst = dst_folder / f"{uuid4().hex}_{Path(image).name}"  # keep file ext
         
     | 
| 347 | 
         
            -
                        shutil.copyfile(image, dst)
         
     | 
| 348 | 
         
            -
                        return dst
         
     | 
| 349 | 
         
            -
             
     | 
| 350 | 
         
            -
                    # Still a Python object => serialize it
         
     | 
| 351 | 
         
            -
                    if isinstance(image, np.ndarray):
         
     | 
| 352 | 
         
            -
                        image = Image.fromarray(image)
         
     | 
| 353 | 
         
            -
                    if isinstance(image, Image):
         
     | 
| 354 | 
         
            -
                        dst = dst_folder / f"Path(file).name}_{uuid4().hex}.png"
         
     | 
| 355 | 
         
            -
                        image.save(dst)
         
     | 
| 356 | 
         
            -
                        return dst
         
     | 
| 357 | 
         
            -
             
     | 
| 358 | 
         
            -
                    raise ValueError(f"Unsupported image type: {type(image)}")
         
     | 
| 359 | 
         
            -
                
         
     | 
| 360 | 
         
            -
                except Exception as e:
         
     | 
| 361 | 
         
            -
                    print(f"An error occurred: {e}")
         
     | 
| 362 | 
         
            -
                    if not isinstance(dst, Path):
         
     | 
| 363 | 
         
            -
                        dst = Path(image)
         
     | 
| 364 | 
         
            -
                    return dst  # Return the original file_location if an error occurs
         
     | 
| 365 | 
         
            -
             
     | 
| 366 | 
         
            -
            def _copy_file(file: any | np.ndarray | str | Path, dst_folder: Path) -> Path:
         
     | 
| 367 | 
         
            -
                try:
         
     | 
| 368 | 
         
            -
                    """Copy file to the appropriate folder."""
         
     | 
| 369 | 
         
            -
                    # Already a path => copy it
         
     | 
| 370 | 
         
            -
                    if isinstance(file, str):
         
     | 
| 371 | 
         
            -
                        file = Path(file)
         
     | 
| 372 | 
         
            -
                    if isinstance(file, Path):
         
     | 
| 373 | 
         
            -
                        dst = dst_folder / f"{file.stem}_{uuid4().hex}{file.suffix}"  # keep file ext
         
     | 
| 374 | 
         
            -
                        shutil.copyfile(file, dst)
         
     | 
| 375 | 
         
            -
                        return dst
         
     | 
| 376 | 
         
            -
             
     | 
| 377 | 
         
            -
                    # Still a Python object => serialize it
         
     | 
| 378 | 
         
            -
                    if isinstance(file, np.ndarray):
         
     | 
| 379 | 
         
            -
                        file = Image.fromarray(file)
         
     | 
| 380 | 
         
            -
                        dst = dst_folder / f"{file.filename}_{uuid4().hex}{file.suffix}"
         
     | 
| 381 | 
         
            -
                        file.save(dst)
         
     | 
| 382 | 
         
            -
                        return dst
         
     | 
| 383 | 
         
            -
             
     | 
| 384 | 
         
            -
                    # try other file types
         
     | 
| 385 | 
         
            -
                    kind = filetype.guess(file)
         
     | 
| 386 | 
         
            -
                    if kind is not None:
         
     | 
| 387 | 
         
            -
                        dst = dst_folder / f"{Path(file).stem}_{uuid4().hex}.{kind.extension}"
         
     | 
| 388 | 
         
            -
                        shutil.copyfile(file, dst)
         
     | 
| 389 | 
         
            -
                        return dst
         
     | 
| 390 | 
         
            -
                    raise ValueError(f"Unsupported file type: {type(file)}")
         
     | 
| 391 | 
         
            -
             
     | 
| 392 | 
         
            -
                except Exception as e:
         
     | 
| 393 | 
         
            -
                    print(f"An error occurred: {e}")
         
     | 
| 394 | 
         
            -
                    if not isinstance(dst, Path):
         
     | 
| 395 | 
         
            -
                        dst = Path(file)
         
     | 
| 396 | 
         
            -
                    return dst  # Return the original file_location if an error occurs
         
     | 
| 397 | 
         
            -
             
     | 
| 398 | 
         
            -
             
     | 
| 399 | 
         
            -
            def _add_metadata(file_location: Path, metadata: Dict[str, Any]) -> Path:
         
     | 
| 400 | 
         
            -
                try:
         
     | 
| 401 | 
         
            -
                    file_type = file_location.suffix
         
     | 
| 402 | 
         
            -
                    valid_file_types = [".wav", ".mp3", ".mp4", ".png"]
         
     | 
| 403 | 
         
            -
                    if file_type not in valid_file_types:
         
     | 
| 404 | 
         
            -
                        raise ValueError("Invalid file type. Valid file types are .wav, .mp3, .mp4, .png")
         
     | 
| 405 | 
         
            -
             
     | 
| 406 | 
         
            -
                    if file_type == ".wav":
         
     | 
| 407 | 
         
            -
                        # Open and process .wav file
         
     | 
| 408 | 
         
            -
                        with wave.open(file_location, 'rb') as wav_file:
         
     | 
| 409 | 
         
            -
                            # Get the current metadata
         
     | 
| 410 | 
         
            -
                            current_metadata = {key: value for key, value in wav_file.getparams()._asdict().items() if isinstance(value, (int, float))}
         
     | 
| 411 | 
         
            -
                            
         
     | 
| 412 | 
         
            -
                            # Update metadata
         
     | 
| 413 | 
         
            -
                            current_metadata.update(metadata)
         
     | 
| 414 | 
         
            -
             
     | 
| 415 | 
         
            -
                            # Reopen the WAV file in write mode
         
     | 
| 416 | 
         
            -
                            with wave.open(file_location, 'wb') as wav_output_file:
         
     | 
| 417 | 
         
            -
                                # Set the new metadata
         
     | 
| 418 | 
         
            -
                                wav_output_file.setparams(wav_file.getparams())
         
     | 
| 419 | 
         
            -
             
     | 
| 420 | 
         
            -
                        # Save the WAV file (overwriting the previous version)
         
     | 
| 421 | 
         
            -
                        wav_output_file.close()
         
     | 
| 422 | 
         
            -
                    elif file_type == ".mp3":
         
     | 
| 423 | 
         
            -
                        # Open and process .mp3 file
         
     | 
| 424 | 
         
            -
                        audio = EasyMP3(file_location)
         
     | 
| 425 | 
         
            -
             
     | 
| 426 | 
         
            -
                        # Add metadata to the file
         
     | 
| 427 | 
         
            -
                        for key, value in metadata.items():
         
     | 
| 428 | 
         
            -
                            audio[key] = value
         
     | 
| 429 | 
         
            -
             
     | 
| 430 | 
         
            -
                        # Save the MP3 file (overwriting the previous version)
         
     | 
| 431 | 
         
            -
                        audio.save()
         
     | 
| 432 | 
         
            -
                    elif file_type == ".mp4":
         
     | 
| 433 | 
         
            -
                        # Open and process .mp4 file
         
     | 
| 434 | 
         
            -
                        # Add metadata to the file
         
     | 
| 435 | 
         
            -
                        wav_file_location = file_location.with_suffix(".wav")
         
     | 
| 436 | 
         
            -
                        wave_exists = wav_file_location.exists()
         
     | 
| 437 | 
         
            -
                        if not wave_exists:
         
     | 
| 438 | 
         
            -
                            # Use torchaudio to create the WAV file if it doesn't exist
         
     | 
| 439 | 
         
            -
                            audio, sample_rate = torchaudio.load(file_location, normalize=True)
         
     | 
| 440 | 
         
            -
                            torchaudio.save(wav_file_location, audio, sample_rate, format='wav')
         
     | 
| 441 | 
         
            -
             
     | 
| 442 | 
         
            -
                        # Use ffmpeg to add metadata to the video file
         
     | 
| 443 | 
         
            -
                        metadata_args = [f"{key}={value}" for key, value in metadata.items()]
         
     | 
| 444 | 
         
            -
                        ffmpeg_metadata = ":".join(metadata_args)
         
     | 
| 445 | 
         
            -
                        ffmpeg_cmd = f'ffmpeg -i "{file_location}" -i "{wav_file_location}" -map 0:v:0 -map 1:a:0 -c:v copy -c:a aac -metadata "{ffmpeg_metadata}" "{file_location}"'
         
     | 
| 446 | 
         
            -
                        subprocess.run(ffmpeg_cmd, shell=True, check=True)
         
     | 
| 447 | 
         
            -
             
     | 
| 448 | 
         
            -
                        # Remove temporary WAV file
         
     | 
| 449 | 
         
            -
                        if not wave_exists:
         
     | 
| 450 | 
         
            -
                            wav_file_location.unlink()
         
     | 
| 451 | 
         
            -
                    elif file_type == ".png":
         
     | 
| 452 | 
         
            -
                        # Open and process .png file
         
     | 
| 453 | 
         
            -
                        image = Image.open(file_location)
         
     | 
| 454 | 
         
            -
                        exif_data = image.info.get("exif", {})
         
     | 
| 455 | 
         
            -
                        exif_data.update(metadata)
         
     | 
| 456 | 
         
            -
                        # Add metadata to the file
         
     | 
| 457 | 
         
            -
                        image.save(file_location, exif=exif_data)
         
     | 
| 458 | 
         
            -
             
     | 
| 459 | 
         
            -
                    return file_location  # Return the path to the modified file
         
     | 
| 460 | 
         
            -
             
     | 
| 461 | 
         
            -
                except Exception as e:
         
     | 
| 462 | 
         
            -
                    print(f"An error occurred: {e}")
         
     | 
| 463 | 
         
            -
                    return file_location  # Return the original file_location if an error occurs
         
     | 
| 464 | 
         
            -
               
         
     | 
| 465 | 
         
            -
            def _resolve_folder_path(folder_path: str | Path | None) -> Path:
         
     | 
| 466 | 
         
            -
                if folder_path is not None:
         
     | 
| 467 | 
         
            -
                    return Path(folder_path).expanduser().resolve()
         
     | 
| 468 | 
         
            -
             
     | 
| 469 | 
         
            -
                if os.getenv("SYSTEM") == "spaces" and os.path.exists("/data"):  # Persistent storage is enabled!
         
     | 
| 470 | 
         
            -
                    return Path("/data") / "_user_history"
         
     | 
| 471 | 
         
            -
             
     | 
| 472 | 
         
            -
                # Not in a Space or Persistent storage not enabled => local folder
         
     | 
| 473 | 
         
            -
                return Path("_user_history").resolve()
         
     | 
| 474 | 
         
            -
             
     | 
| 475 | 
         
            -
             
     | 
| 476 | 
         
            -
            def _archives_path() -> Path:
         
     | 
| 477 | 
         
            -
                # Doesn't have to be on persistent storage as it's only used for download
         
     | 
| 478 | 
         
            -
                path = Path(__file__).parent / "_user_history_exports"
         
     | 
| 479 | 
         
            -
                path.mkdir(parents=True, exist_ok=True)
         
     | 
| 480 | 
         
            -
                return path
         
     | 
| 481 | 
         
            -
             
     | 
| 482 | 
         
            -
             
     | 
| 483 | 
         
            -
            #################
         
     | 
| 484 | 
         
            -
            # Admin section #
         
     | 
| 485 | 
         
            -
            #################
         
     | 
| 486 | 
         
            -
             
     | 
| 487 | 
         
            -
             
     | 
| 488 | 
         
            -
            def _admin_section() -> None:
         
     | 
| 489 | 
         
            -
                title = gr.Markdown()
         
     | 
| 490 | 
         
            -
                title.attach_load_event(_display_if_admin(), every=None)
         
     | 
| 491 | 
         
            -
             
     | 
| 492 | 
         
            -
             
     | 
| 493 | 
         
            -
            def _display_if_admin() -> Callable:
         
     | 
| 494 | 
         
            -
                def _inner(profile: gr.OAuthProfile | None) -> str:
         
     | 
| 495 | 
         
            -
                    if profile is None:
         
     | 
| 496 | 
         
            -
                        return ""
         
     | 
| 497 | 
         
            -
                    if profile["preferred_username"] in _fetch_admins():
         
     | 
| 498 | 
         
            -
                        return _admin_content()
         
     | 
| 499 | 
         
            -
                    return ""
         
     | 
| 500 | 
         
            -
             
     | 
| 501 | 
         
            -
                return _inner
         
     | 
| 502 | 
         
            -
             
     | 
| 503 | 
         
            -
             
     | 
| 504 | 
         
            -
            def _admin_content() -> str:
         
     | 
| 505 | 
         
            -
                return f"""
         
     | 
| 506 | 
         
            -
            ## Admin section
         
     | 
| 507 | 
         
            -
             
     | 
| 508 | 
         
            -
            Running on **{os.getenv("SYSTEM", "local")}** (id: {os.getenv("SPACE_ID")}). {_get_msg_is_persistent_storage_enabled()}
         
     | 
| 509 | 
         
            -
             
     | 
| 510 | 
         
            -
            Admins: {', '.join(_fetch_admins())}
         
     | 
| 511 | 
         
            -
             
     | 
| 512 | 
         
            -
            {_get_nb_users()} user(s), {_get_nb_images()} image(s)
         
     | 
| 513 | 
         
            -
             
     | 
| 514 | 
         
            -
            ### Configuration
         
     | 
| 515 | 
         
            -
             
     | 
| 516 | 
         
            -
            History folder: *{_UserHistory().folder_path}*
         
     | 
| 517 | 
         
            -
             
     | 
| 518 | 
         
            -
            Exports folder: *{_archives_path()}*
         
     | 
| 519 | 
         
            -
             
     | 
| 520 | 
         
            -
            ### Disk usage
         
     | 
| 521 | 
         
            -
             
     | 
| 522 | 
         
            -
            {_disk_space_warning_message()}
         
     | 
| 523 | 
         
            -
            """
         
     | 
| 524 | 
         
            -
             
     | 
| 525 | 
         
            -
             
     | 
| 526 | 
         
            -
            def _get_nb_users() -> int:
         
     | 
| 527 | 
         
            -
                user_history = _UserHistory()
         
     | 
| 528 | 
         
            -
                if not user_history.initialized:
         
     | 
| 529 | 
         
            -
                    return 0
         
     | 
| 530 | 
         
            -
                if user_history.folder_path is not None and user_history.folder_path.exists():
         
     | 
| 531 | 
         
            -
                    return len([path for path in user_history.folder_path.iterdir() if path.is_dir()])
         
     | 
| 532 | 
         
            -
                return 0
         
     | 
| 533 | 
         
            -
             
     | 
| 534 | 
         
            -
             
     | 
| 535 | 
         
            -
            def _get_nb_images() -> int:
         
     | 
| 536 | 
         
            -
                user_history = _UserHistory()
         
     | 
| 537 | 
         
            -
                if not user_history.initialized:
         
     | 
| 538 | 
         
            -
                    return 0
         
     | 
| 539 | 
         
            -
                if user_history.folder_path is not None and user_history.folder_path.exists():
         
     | 
| 540 | 
         
            -
                    return len([path for path in user_history.folder_path.glob("*/images/*")])
         
     | 
| 541 | 
         
            -
                return 0
         
     | 
| 542 | 
         
            -
             
     | 
| 543 | 
         
            -
             
     | 
| 544 | 
         
            -
            def _get_msg_is_persistent_storage_enabled() -> str:
         
     | 
| 545 | 
         
            -
                if os.getenv("SYSTEM") == "spaces":
         
     | 
| 546 | 
         
            -
                    if os.path.exists("/data"):
         
     | 
| 547 | 
         
            -
                        return "Persistent storage is enabled."
         
     | 
| 548 | 
         
            -
                    else:
         
     | 
| 549 | 
         
            -
                        return (
         
     | 
| 550 | 
         
            -
                            "Persistent storage is not enabled. This means that user histories will be deleted when the Space is"
         
     | 
| 551 | 
         
            -
                            " restarted. Consider adding a Persistent Storage in your Space settings."
         
     | 
| 552 | 
         
            -
                        )
         
     | 
| 553 | 
         
            -
                return ""
         
     | 
| 554 | 
         
            -
             
     | 
| 555 | 
         
            -
             
     | 
| 556 | 
         
            -
            def _disk_space_warning_message() -> str:
         
     | 
| 557 | 
         
            -
                user_history = _UserHistory()
         
     | 
| 558 | 
         
            -
                if not user_history.initialized:
         
     | 
| 559 | 
         
            -
                    return ""
         
     | 
| 560 | 
         
            -
             
     | 
| 561 | 
         
            -
                message = ""
         
     | 
| 562 | 
         
            -
                if user_history.folder_path is not None:
         
     | 
| 563 | 
         
            -
                    total, used, _ = _get_disk_usage(user_history.folder_path)
         
     | 
| 564 | 
         
            -
                    message += f"History folder: **{used / 1e9 :.0f}/{total / 1e9 :.0f}GB** used ({100*used/total :.0f}%)."
         
     | 
| 565 | 
         
            -
             
     | 
| 566 | 
         
            -
                total, used, _ = _get_disk_usage(_archives_path())
         
     | 
| 567 | 
         
            -
                message += f"\n\nExports folder: **{used / 1e9 :.0f}/{total / 1e9 :.0f}GB** used ({100*used/total :.0f}%)."
         
     | 
| 568 | 
         
            -
             
     | 
| 569 | 
         
            -
                return f"{message.strip()}"
         
     | 
| 570 | 
         
            -
             
     | 
| 571 | 
         
            -
             
     | 
| 572 | 
         
            -
            def _get_disk_usage(path: Path) -> Tuple[int, int, int]:
         
     | 
| 573 | 
         
            -
                for path in [path] + list(path.parents):  # first check target_dir, then each parents one by one
         
     | 
| 574 | 
         
            -
                    try:
         
     | 
| 575 | 
         
            -
                        return shutil.disk_usage(path)
         
     | 
| 576 | 
         
            -
                    except OSError:  # if doesn't exist or can't read => fail silently and try parent one
         
     | 
| 577 | 
         
            -
                        pass
         
     | 
| 578 | 
         
            -
                return 0, 0, 0
         
     | 
| 579 | 
         
            -
             
     | 
| 580 | 
         
            -
             
     | 
| 581 | 
         
            -
            @cache
         
     | 
| 582 | 
         
            -
            def _fetch_admins() -> List[str]:
         
     | 
| 583 | 
         
            -
                # Running locally => fake user is admin
         
     | 
| 584 | 
         
            -
                if os.getenv("SYSTEM") != "spaces":
         
     | 
| 585 | 
         
            -
                    return ["FakeGradioUser"]
         
     | 
| 586 | 
         
            -
             
     | 
| 587 | 
         
            -
                # Running in Space but no space_id => ???
         
     | 
| 588 | 
         
            -
                space_id = os.getenv("SPACE_ID")
         
     | 
| 589 | 
         
            -
                if space_id is None:
         
     | 
| 590 | 
         
            -
                    return ["Unknown"]
         
     | 
| 591 | 
         
            -
             
     | 
| 592 | 
         
            -
                # Running in Space => try to fetch organization members
         
     | 
| 593 | 
         
            -
                # Otherwise, it's not an organization => namespace is the user
         
     | 
| 594 | 
         
            -
                namespace = space_id.split("/")[0]
         
     | 
| 595 | 
         
            -
                response = requests.get(f"https://huggingface.co/api/organizations/{namespace}/members")
         
     | 
| 596 | 
         
            -
                if response.status_code == 200:
         
     | 
| 597 | 
         
            -
                    return sorted((member["user"] for member in response.json()), key=lambda x: x.lower())
         
     | 
| 598 | 
         
            -
                return [namespace]
         
     | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         |