Spaces:
Running
Running
#BS_app.py_05_LIST_CREATE_FOLDER | |
##Trying List And Create SubFolder | |
import gradio as gr | |
import os | |
from transformers import AutoModelForCausalLM, AutoTokenizer, Trainer, TrainingArguments, DataCollatorForLanguageModeling | |
from datasets import load_dataset, Dataset | |
from huggingface_hub import HfApi, HfFolder, HfFileSystem | |
import requests | |
from io import BytesIO | |
# Récupérer token depuis les variables d'environnement | |
hf_token = os.getenv("MisterAI_bigscience_bloom_560m") | |
# Fenêtre d'authentification | |
#with gr.Blocks() as auth_demo: | |
# hf_token_input = gr.Textbox(label="Hugging Face Token") | |
# auth_button = gr.Button("Authentifier") | |
# | |
# def authenticate(hf_token): | |
# # Configurer le token pour l'utilisation avec Hugging Face | |
# if hf_token: | |
# HfFolder.save_token(hf_token) | |
# else: | |
# raise ValueError("Le token Hugging Face n'est pas configuré. Assurez-vous qu'il est défini dans les variables d'environnement.") | |
# | |
# auth_button.click( | |
# authenticate, | |
# inputs=hf_token_input, | |
# outputs=gr.Textbox(value="Authentification réussie", visible=False) | |
# ) | |
# | |
# Créer une instance de HfFileSystem | |
fs = HfFileSystem() | |
def list_repo_content(repo_path): | |
repo_id = repo_path.split("/")[0] | |
repo_info = fs.info(repo_id) | |
content = repo_info["subdirectories"] + repo_info["files"] | |
return content | |
def create_subfolder(repo_path, subfolder_name): | |
api = HfApi() | |
repo_id = repo_path.split("/")[0] | |
subfolder_path = f"{repo_path}/{subfolder_name}" | |
api.create_folder(repo_id, subfolder_path) | |
return f"Sous-répertoire {subfolder_path} créé avec succès." | |
# Interface Gradio | |
with gr.Blocks() as demo: | |
with gr.Row(): | |
repo_path = gr.Textbox(label="Chemin du dépôt") | |
list_button = gr.Button("Lister le contenu") | |
create_button = gr.Button("Créer un sous-répertoire") | |
subfolder_name = gr.Textbox(label="Nom du sous-répertoire") | |
with gr.Row(): | |
repo_content = gr.Textbox(label="Contenu du dépôt") | |
list_button.click( | |
list_repo_content, | |
inputs=repo_path, | |
outputs=repo_content | |
) | |
create_button.click( | |
create_subfolder, | |
inputs=[repo_path, subfolder_name], | |
outputs=repo_content | |
) | |
if __name__ == "__main__": | |
# auth_demo.launch() | |
demo.launch() | |