Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,30 +4,29 @@ from pydantic import BaseModel
|
|
| 4 |
import sqlite3
|
| 5 |
import hashlib
|
| 6 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
|
|
|
| 8 |
app = FastAPI()
|
| 9 |
|
| 10 |
-
# Allow cross-origin requests (adjust for production)
|
| 11 |
app.add_middleware(
|
| 12 |
CORSMiddleware,
|
| 13 |
-
allow_origins=["*"],
|
| 14 |
allow_credentials=True,
|
| 15 |
allow_methods=["*"],
|
| 16 |
allow_headers=["*"],
|
| 17 |
)
|
| 18 |
|
| 19 |
-
#
|
| 20 |
DB_DIR = "/tmp"
|
| 21 |
DB_PATH = os.path.join(DB_DIR, "users.db")
|
| 22 |
-
|
| 23 |
-
# Ensure the directory exists
|
| 24 |
os.makedirs(DB_DIR, exist_ok=True)
|
| 25 |
|
| 26 |
-
# Connect to the database
|
| 27 |
conn = sqlite3.connect(DB_PATH, check_same_thread=False)
|
| 28 |
cursor = conn.cursor()
|
| 29 |
-
|
| 30 |
-
# Create users table
|
| 31 |
cursor.execute('''
|
| 32 |
CREATE TABLE IF NOT EXISTS users (
|
| 33 |
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
@@ -37,11 +36,10 @@ cursor.execute('''
|
|
| 37 |
''')
|
| 38 |
conn.commit()
|
| 39 |
|
| 40 |
-
# Hash password using SHA256
|
| 41 |
def hash_password(password: str) -> str:
|
| 42 |
return hashlib.sha256(password.encode()).hexdigest()
|
| 43 |
|
| 44 |
-
# Pydantic
|
| 45 |
class SignupForm(BaseModel):
|
| 46 |
email: str
|
| 47 |
password: str
|
|
@@ -50,12 +48,11 @@ class LoginForm(BaseModel):
|
|
| 50 |
email: str
|
| 51 |
password: str
|
| 52 |
|
| 53 |
-
#
|
| 54 |
@app.post("/signup")
|
| 55 |
def signup(data: SignupForm):
|
| 56 |
email = data.email.lower().strip()
|
| 57 |
password = hash_password(data.password)
|
| 58 |
-
|
| 59 |
try:
|
| 60 |
cursor.execute("INSERT INTO users (email, password) VALUES (?, ?)", (email, password))
|
| 61 |
conn.commit()
|
|
@@ -63,21 +60,35 @@ def signup(data: SignupForm):
|
|
| 63 |
except sqlite3.IntegrityError:
|
| 64 |
raise HTTPException(status_code=409, detail="Email already registered")
|
| 65 |
|
| 66 |
-
# Login route
|
| 67 |
@app.post("/login")
|
| 68 |
def login(data: LoginForm):
|
| 69 |
email = data.email.lower().strip()
|
| 70 |
password = hash_password(data.password)
|
| 71 |
-
|
| 72 |
cursor.execute("SELECT * FROM users WHERE email = ? AND password = ?", (email, password))
|
| 73 |
user = cursor.fetchone()
|
| 74 |
-
|
| 75 |
if user:
|
| 76 |
return {"success": True, "message": "Login successful"}
|
| 77 |
else:
|
| 78 |
raise HTTPException(status_code=401, detail="Invalid credentials")
|
| 79 |
|
| 80 |
-
# Root route
|
| 81 |
@app.get("/")
|
| 82 |
def root():
|
| 83 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
import sqlite3
|
| 5 |
import hashlib
|
| 6 |
import os
|
| 7 |
+
import gradio as gr
|
| 8 |
+
from fastapi.staticfiles import StaticFiles
|
| 9 |
+
from fastapi.responses import RedirectResponse
|
| 10 |
+
from gradio.routes import mount_gradio_app
|
| 11 |
|
| 12 |
+
# === FastAPI setup ===
|
| 13 |
app = FastAPI()
|
| 14 |
|
|
|
|
| 15 |
app.add_middleware(
|
| 16 |
CORSMiddleware,
|
| 17 |
+
allow_origins=["*"], # For testing; restrict this in production
|
| 18 |
allow_credentials=True,
|
| 19 |
allow_methods=["*"],
|
| 20 |
allow_headers=["*"],
|
| 21 |
)
|
| 22 |
|
| 23 |
+
# === Database Setup ===
|
| 24 |
DB_DIR = "/tmp"
|
| 25 |
DB_PATH = os.path.join(DB_DIR, "users.db")
|
|
|
|
|
|
|
| 26 |
os.makedirs(DB_DIR, exist_ok=True)
|
| 27 |
|
|
|
|
| 28 |
conn = sqlite3.connect(DB_PATH, check_same_thread=False)
|
| 29 |
cursor = conn.cursor()
|
|
|
|
|
|
|
| 30 |
cursor.execute('''
|
| 31 |
CREATE TABLE IF NOT EXISTS users (
|
| 32 |
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
| 36 |
''')
|
| 37 |
conn.commit()
|
| 38 |
|
|
|
|
| 39 |
def hash_password(password: str) -> str:
|
| 40 |
return hashlib.sha256(password.encode()).hexdigest()
|
| 41 |
|
| 42 |
+
# === Pydantic Models ===
|
| 43 |
class SignupForm(BaseModel):
|
| 44 |
email: str
|
| 45 |
password: str
|
|
|
|
| 48 |
email: str
|
| 49 |
password: str
|
| 50 |
|
| 51 |
+
# === FastAPI Routes ===
|
| 52 |
@app.post("/signup")
|
| 53 |
def signup(data: SignupForm):
|
| 54 |
email = data.email.lower().strip()
|
| 55 |
password = hash_password(data.password)
|
|
|
|
| 56 |
try:
|
| 57 |
cursor.execute("INSERT INTO users (email, password) VALUES (?, ?)", (email, password))
|
| 58 |
conn.commit()
|
|
|
|
| 60 |
except sqlite3.IntegrityError:
|
| 61 |
raise HTTPException(status_code=409, detail="Email already registered")
|
| 62 |
|
|
|
|
| 63 |
@app.post("/login")
|
| 64 |
def login(data: LoginForm):
|
| 65 |
email = data.email.lower().strip()
|
| 66 |
password = hash_password(data.password)
|
|
|
|
| 67 |
cursor.execute("SELECT * FROM users WHERE email = ? AND password = ?", (email, password))
|
| 68 |
user = cursor.fetchone()
|
|
|
|
| 69 |
if user:
|
| 70 |
return {"success": True, "message": "Login successful"}
|
| 71 |
else:
|
| 72 |
raise HTTPException(status_code=401, detail="Invalid credentials")
|
| 73 |
|
|
|
|
| 74 |
@app.get("/")
|
| 75 |
def root():
|
| 76 |
+
return RedirectResponse(url="/gradio")
|
| 77 |
+
|
| 78 |
+
# === Gradio Interface ===
|
| 79 |
+
def view_users():
|
| 80 |
+
cursor.execute("SELECT id, email FROM users")
|
| 81 |
+
rows = cursor.fetchall()
|
| 82 |
+
return [["ID", "Email"]] + [[str(r[0]), r[1]] for r in rows]
|
| 83 |
+
|
| 84 |
+
with gr.Blocks() as demo:
|
| 85 |
+
gr.Markdown("## 👥 Registered Users")
|
| 86 |
+
user_table = gr.Dataframe(
|
| 87 |
+
value=view_users(),
|
| 88 |
+
headers=None,
|
| 89 |
+
interactive=False
|
| 90 |
+
)
|
| 91 |
+
gr.Button("🔄 Refresh").click(fn=view_users, outputs=user_table)
|
| 92 |
+
|
| 93 |
+
# === Mount Gradio App ===
|
| 94 |
+
app = mount_gradio_app(app, demo, path="/gradio")
|