Update app.py
Browse files
app.py
CHANGED
|
@@ -1,149 +1,155 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import json
|
| 3 |
import os
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
json.dump(prompts_lib, f, indent=4)
|
| 36 |
-
|
| 37 |
-
prompts_lib = cargar_prompts_desde_archivo()
|
| 38 |
-
|
| 39 |
-
favorites = []
|
| 40 |
-
|
| 41 |
-
def guardar_contraseña(pw):
|
| 42 |
-
if len(pw) != 4 or not pw.isdigit():
|
| 43 |
-
return "La contraseña debe tener exactamente 4 dígitos"
|
| 44 |
-
with open(PASSWORD_FILE, "w") as f:
|
| 45 |
-
f.write(pw)
|
| 46 |
-
return "Contraseña guardada correctamente. Ahora úsala para entrar."
|
| 47 |
-
|
| 48 |
-
def cargar_contraseña():
|
| 49 |
-
if not os.path.exists(PASSWORD_FILE):
|
| 50 |
return None
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
def
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
def
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
entrar_btn.click(fn=lambda pw: gestionar_password(pw, crear=False), inputs=pass_input, outputs=estado)
|
| 110 |
-
|
| 111 |
-
with gr.Tab("Buscar y visualizar"):
|
| 112 |
-
categoria_input = gr.Textbox(label="Categoría de outfit", placeholder="Ejemplo: secretaria")
|
| 113 |
-
filtro_input = gr.Textbox(label="Palabra clave (opcional)", placeholder="Ejemplo: moda, secretaria...")
|
| 114 |
-
buscar_btn = gr.Button("🔍 Buscar")
|
| 115 |
-
resultado = gr.Dataframe(headers=["Nombre", "Prompt"])
|
| 116 |
-
export_btn = gr.Button("Exportar TXT")
|
| 117 |
-
output_txt = gr.Textbox(label="Prompts exportados", interactive=False)
|
| 118 |
-
|
| 119 |
-
buscar_btn.click(
|
| 120 |
-
fn=mostrar_prompts,
|
| 121 |
-
inputs=[categoria_input, filtro_input],
|
| 122 |
-
outputs=resultado
|
| 123 |
)
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
)
|
|
|
|
|
|
|
|
|
|
| 142 |
|
| 143 |
-
|
| 144 |
-
return gr.update(visible=(mensaje == "Acceso concedido"))
|
| 145 |
|
| 146 |
-
|
|
|
|
|
|
|
| 147 |
|
| 148 |
-
# Lanzar el demo
|
| 149 |
demo.launch()
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from hf_hub_cto import Repo
|
| 4 |
+
from hf_hub_cto.config import (
|
| 5 |
+
HF_TOKEN_FILE_PATH,
|
| 6 |
+
MAX_FILE_SIZE,
|
| 7 |
+
MAX_REPO_SIZE,
|
| 8 |
+
REPO_TYPES,
|
| 9 |
+
)
|
| 10 |
+
from hf_hub_cto.repo_info import RepoInfo
|
| 11 |
+
from hf_hub_cto.repo_utils import (
|
| 12 |
+
clone_repo,
|
| 13 |
+
delete_repo,
|
| 14 |
+
get_repo_info_df,
|
| 15 |
+
get_repo_files,
|
| 16 |
+
is_valid_file_size,
|
| 17 |
+
repo_file_upload,
|
| 18 |
+
repo_rename_file,
|
| 19 |
+
sync_repo,
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
# Initialize the huggingface hub repository
|
| 23 |
+
repo = Repo(
|
| 24 |
+
repo_id=os.getenv("REPO_ID"),
|
| 25 |
+
repo_type=os.getenv("REPO_TYPE"),
|
| 26 |
+
token=os.getenv("HF_TOKEN"),
|
| 27 |
+
local_dir=os.getenv("LOCAL_DIR"),
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
def get_session_info(session_id: str):
|
| 31 |
+
user_info = gr.LoginButton.get_user_info(session_id)
|
| 32 |
+
if not user_info:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
return None
|
| 34 |
+
return user_info
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def set_repo_info(repo_id: str, repo_type: str, token: str):
|
| 38 |
+
repo_id_info = RepoInfo(repo_id=repo_id, repo_type=repo_type)
|
| 39 |
+
return gr.update(value=repo_id), gr.update(value=repo_type), gr.update(value=token), repo_id_info
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def get_all_repos_df(session_id: str):
|
| 43 |
+
user_info = get_session_info(session_id)
|
| 44 |
+
if not user_info:
|
| 45 |
+
return gr.DataFrame(visible=False), gr.update(visible=True)
|
| 46 |
+
|
| 47 |
+
df = get_repo_info_df(user_info["name"], token=os.getenv("HF_TOKEN"))
|
| 48 |
+
df = df.sort_values(by="last_modified", ascending=False).reset_index(drop=True)
|
| 49 |
+
return gr.DataFrame(value=df, visible=True), gr.update(visible=False)
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def handle_repo_selection(
|
| 53 |
+
evt: gr.SelectData,
|
| 54 |
+
repo_df: gr.DataFrame,
|
| 55 |
+
session_id: str,
|
| 56 |
+
):
|
| 57 |
+
selected_row = repo_df.loc[evt.index[0]]
|
| 58 |
+
repo_id = selected_row["repoId"]
|
| 59 |
+
repo_type = selected_row["type"]
|
| 60 |
+
return (
|
| 61 |
+
gr.Textbox(value=repo_id),
|
| 62 |
+
gr.Textbox(value=repo_type),
|
| 63 |
+
gr.Button(visible=True),
|
| 64 |
+
gr.Button(visible=True),
|
| 65 |
+
gr.Button(visible=True),
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def handle_create_repo(repo_id: str, repo_type: str):
|
| 70 |
+
try:
|
| 71 |
+
if repo_type == "Model":
|
| 72 |
+
Repo(repo_id, repo_type="model").create_repo()
|
| 73 |
+
elif repo_type == "Dataset":
|
| 74 |
+
Repo(repo_id, repo_type="dataset").create_repo()
|
| 75 |
+
else:
|
| 76 |
+
Repo(repo_id, repo_type="space").create_repo()
|
| 77 |
+
return "Repo creado exitosamente.", None
|
| 78 |
+
except Exception as e:
|
| 79 |
+
return None, str(e)
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
def handle_delete_repo(repo_id: str, repo_type: str, session_id: str):
|
| 83 |
+
user_info = get_session_info(session_id)
|
| 84 |
+
if not user_info:
|
| 85 |
+
return "Error: usuario no autenticado."
|
| 86 |
+
try:
|
| 87 |
+
delete_repo(
|
| 88 |
+
repo_id,
|
| 89 |
+
repo_type,
|
| 90 |
+
user_info["name"],
|
| 91 |
+
os.getenv("HF_TOKEN"),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
)
|
| 93 |
+
return "Repo eliminado exitosamente."
|
| 94 |
+
except Exception as e:
|
| 95 |
+
return f"Error al eliminar el repositorio: {str(e)}"
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
def get_repo_contents(repo_id: str, repo_type: str):
|
| 99 |
+
files = get_repo_files(repo_id=repo_id, repo_type=repo_type, token=os.getenv("HF_TOKEN"))
|
| 100 |
+
return gr.DataFrame(value=files)
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
def handle_clone_repo(repo_id: str, repo_type: str):
|
| 104 |
+
try:
|
| 105 |
+
clone_repo(repo_id, repo_type)
|
| 106 |
+
return "Repositorio clonado exitosamente."
|
| 107 |
+
except Exception as e:
|
| 108 |
+
return f"Error al clonar el repositorio: {str(e)}"
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
def handle_sync_repo(repo_id: str, repo_type: str):
|
| 112 |
+
try:
|
| 113 |
+
sync_repo(repo_id, repo_type)
|
| 114 |
+
return "Repositorio sincronizado exitosamente."
|
| 115 |
+
except Exception as e:
|
| 116 |
+
return f"Error al sincronizar el repositorio: {str(e)}"
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
def handle_file_upload(
|
| 120 |
+
files: list, repo_id: str, repo_type: str, upload_folder: str
|
| 121 |
+
):
|
| 122 |
+
try:
|
| 123 |
+
for file in files:
|
| 124 |
+
if not is_valid_file_size(file.size, MAX_FILE_SIZE):
|
| 125 |
+
raise ValueError(
|
| 126 |
+
f"El archivo {file.name} excede el límite de tamaño de {MAX_FILE_SIZE / (1024 ** 2)} MB."
|
| 127 |
+
)
|
| 128 |
+
repo_file_upload(
|
| 129 |
+
repo_id, repo_type, file.name, upload_folder, os.getenv("HF_TOKEN")
|
| 130 |
+
)
|
| 131 |
+
return "Archivos cargados exitosamente."
|
| 132 |
+
except Exception as e:
|
| 133 |
+
return f"Error al cargar archivos: {str(e)}"
|
| 134 |
+
|
| 135 |
+
|
| 136 |
+
def handle_rename_file(repo_id: str, repo_type: str, old_path: str, new_path: str):
|
| 137 |
+
try:
|
| 138 |
+
repo_rename_file(
|
| 139 |
+
repo_id,
|
| 140 |
+
repo_type,
|
| 141 |
+
old_path,
|
| 142 |
+
new_path,
|
| 143 |
+
os.getenv("HF_TOKEN"),
|
| 144 |
)
|
| 145 |
+
return "Archivo renombrado exitosamente."
|
| 146 |
+
except Exception as e:
|
| 147 |
+
return f"Error al renombrar el archivo: {str(e)}"
|
| 148 |
|
| 149 |
+
# --- INTERFAZ DE USUARIO CON GRADIO ---
|
|
|
|
| 150 |
|
| 151 |
+
with gr.Blocks(theme=gr.themes.Base()) as demo:
|
| 152 |
+
# Resto del código de la interfaz de usuario...
|
| 153 |
+
pass
|
| 154 |
|
|
|
|
| 155 |
demo.launch()
|