Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,55 +1,119 @@
|
|
| 1 |
-
|
| 2 |
-
from flask import Flask,
|
| 3 |
-
from
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
register_user,
|
| 7 |
-
ACCESS_TOKEN_EXPIRE_MINUTES,
|
| 8 |
-
SECRET_KEY
|
| 9 |
)
|
| 10 |
-
import
|
| 11 |
-
|
|
|
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def register():
|
| 17 |
data = request.get_json()
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
@
|
| 25 |
def login():
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
)
|
| 35 |
-
return
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
if __name__ == "__main__":
|
| 55 |
-
app.run(
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from flask import Flask, jsonify, request
|
| 3 |
+
from flask_jwt_extended import (
|
| 4 |
+
JWTManager, create_access_token,
|
| 5 |
+
jwt_required, get_jwt_identity
|
|
|
|
|
|
|
|
|
|
| 6 |
)
|
| 7 |
+
from passlib.hash import pbkdf2_sha256
|
| 8 |
+
import json
|
| 9 |
+
import os
|
| 10 |
|
| 11 |
+
# Initialize Flask
|
| 12 |
+
flask_app = Flask(__name__)
|
| 13 |
+
flask_app.config["JWT_SECRET_KEY"] = os.getenv("JWT_SECRET_KEY", "super-secret-key")
|
| 14 |
+
jwt = JWTManager(flask_app)
|
| 15 |
|
| 16 |
+
# Simple JSON-based user storage
|
| 17 |
+
USERS_FILE = "users.json"
|
| 18 |
+
|
| 19 |
+
def load_users():
|
| 20 |
+
try:
|
| 21 |
+
with open(USERS_FILE, "r") as f:
|
| 22 |
+
return json.load(f)
|
| 23 |
+
except (FileNotFoundError, json.JSONDecodeError):
|
| 24 |
+
return {}
|
| 25 |
+
|
| 26 |
+
def save_users(users):
|
| 27 |
+
with open(USERS_FILE, "w") as f:
|
| 28 |
+
json.dump(users, f)
|
| 29 |
+
|
| 30 |
+
# Flask API Routes
|
| 31 |
+
@flask_app.route("/register", methods=["POST"])
|
| 32 |
def register():
|
| 33 |
data = request.get_json()
|
| 34 |
+
username = data.get("username")
|
| 35 |
+
password = data.get("password")
|
| 36 |
+
|
| 37 |
+
users = load_users()
|
| 38 |
+
|
| 39 |
+
if not username or not password:
|
| 40 |
+
return jsonify({"error": "Username and password required"}), 400
|
| 41 |
+
|
| 42 |
+
if username in users:
|
| 43 |
+
return jsonify({"error": "Username already exists"}), 400
|
| 44 |
+
|
| 45 |
+
users[username] = {"password": pbkdf2_sha256.hash(password)}
|
| 46 |
+
save_users(users)
|
| 47 |
+
|
| 48 |
+
return jsonify({"message": "User created successfully"}), 201
|
| 49 |
|
| 50 |
+
@flask_app.route("/login", methods=["POST"])
|
| 51 |
def login():
|
| 52 |
+
data = request.get_json()
|
| 53 |
+
username = data.get("username")
|
| 54 |
+
password = data.get("password")
|
| 55 |
+
|
| 56 |
+
users = load_users()
|
| 57 |
+
user = users.get(username)
|
| 58 |
+
|
| 59 |
+
if not user or not pbkdf2_sha256.verify(password, user["password"]):
|
| 60 |
+
return jsonify({"error": "Invalid credentials"}), 401
|
| 61 |
+
|
| 62 |
+
access_token = create_access_token(identity=username)
|
| 63 |
+
return jsonify({"access_token": access_token, "username": username})
|
| 64 |
+
|
| 65 |
+
@flask_app.route("/protected", methods=["GET"])
|
| 66 |
+
@jwt_required()
|
| 67 |
+
def protected():
|
| 68 |
+
current_user = get_jwt_identity()
|
| 69 |
+
return jsonify(logged_in_as=current_user), 200
|
| 70 |
+
|
| 71 |
+
# Gradio Interface
|
| 72 |
+
def register_interface(username, password):
|
| 73 |
+
response = requests.post(
|
| 74 |
+
"http://localhost:7860/register",
|
| 75 |
+
json={"username": username, "password": password}
|
| 76 |
)
|
| 77 |
+
return response.json().get("message", response.json().get("error", "Unknown error"))
|
| 78 |
+
|
| 79 |
+
def login_interface(username, password):
|
| 80 |
+
response = requests.post(
|
| 81 |
+
"http://localhost:7860/login",
|
| 82 |
+
json={"username": username, "password": password}
|
| 83 |
+
)
|
| 84 |
+
if response.status_code == 200:
|
| 85 |
+
token = response.json().get("access_token")
|
| 86 |
+
return f"Login successful! Token: {token[:15]}... (truncated)"
|
| 87 |
+
return response.json().get("error", "Login failed")
|
| 88 |
+
|
| 89 |
+
with gr.Blocks() as demo:
|
| 90 |
+
gr.Markdown("# Secure Authentication System")
|
| 91 |
+
|
| 92 |
+
with gr.Tab("Register"):
|
| 93 |
+
reg_username = gr.Textbox(label="Username")
|
| 94 |
+
reg_password = gr.Textbox(label="Password", type="password")
|
| 95 |
+
reg_output = gr.Textbox(label="Output")
|
| 96 |
+
reg_button = gr.Button("Register")
|
| 97 |
+
|
| 98 |
+
with gr.Tab("Login"):
|
| 99 |
+
login_username = gr.Textbox(label="Username")
|
| 100 |
+
login_password = gr.Textbox(label="Password", type="password")
|
| 101 |
+
login_output = gr.Textbox(label="Output")
|
| 102 |
+
login_button = gr.Button("Login")
|
| 103 |
+
|
| 104 |
+
reg_button.click(
|
| 105 |
+
register_interface,
|
| 106 |
+
inputs=[reg_username, reg_password],
|
| 107 |
+
outputs=reg_output
|
| 108 |
+
)
|
| 109 |
+
login_button.click(
|
| 110 |
+
login_interface,
|
| 111 |
+
inputs=[login_username, login_password],
|
| 112 |
+
outputs=login_output
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
+
# Combined app for HuggingFace Spaces
|
| 116 |
+
app = gr.mount_gradio_app(flask_app, demo, path="/")
|
| 117 |
|
| 118 |
if __name__ == "__main__":
|
| 119 |
+
app.run()
|