Files / app.py
lea97338's picture
Update app.py
1918287 verified
import gradio as gr
import hashlib
import os
UPLOAD_FOLDER = "uploads"
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
USERS = {
"admin": hashlib.sha256("admin123".encode()).hexdigest(),
"demo": hashlib.sha256("demo".encode()).hexdigest()
}
session = {"user": None}
# ======================
# BACKEND
# ======================
def login(username, password):
h = hashlib.sha256(password.encode()).hexdigest()
if username in USERS and USERS[username] == h:
session["user"] = username
return "✅ Connecté", gr.update(visible=True)
return "❌ Mauvais login", gr.update(visible=False)
def upload(file):
if session["user"] is None:
return "❌ Non connecté"
if file is None:
return "❌ Aucun fichier"
path = os.path.join(UPLOAD_FOLDER, file.name)
with open(path, "wb") as f:
f.write(file.read())
return f"✅ Upload : {file.name}"
def list_files():
if session["user"] is None:
return []
return os.listdir(UPLOAD_FOLDER)
# ======================
# UI (GRADIO)
# ======================
with gr.Blocks() as app:
# LOGIN
with gr.Column() as login_ui:
gr.Markdown("# 🔐 Login")
user = gr.Textbox(label="Username")
pw = gr.Textbox(label="Password", type="password")
btn = gr.Button("Login")
status = gr.Textbox()
# APP
with gr.Column(visible=False) as main_ui:
gr.Markdown("# 📁 Gestionnaire")
file = gr.File()
btn_upload = gr.Button("Upload")
upload_status = gr.Textbox()
btn_list = gr.Button("Lister fichiers")
files = gr.JSON()
# EVENTS
btn.click(login, [user, pw], [status, main_ui])
btn_upload.click(upload, file, upload_status)
btn_list.click(list_files, None, files)
app.launch()