Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,45 +1,32 @@
|
|
| 1 |
from fastapi import FastAPI, HTTPException
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
from pydantic import BaseModel
|
| 4 |
-
import sqlite3
|
| 5 |
-
import hashlib
|
| 6 |
import os
|
| 7 |
-
import
|
| 8 |
-
|
| 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=["*"], #
|
| 18 |
allow_credentials=True,
|
| 19 |
allow_methods=["*"],
|
| 20 |
allow_headers=["*"],
|
| 21 |
)
|
| 22 |
|
| 23 |
-
# ===
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 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,
|
| 33 |
-
email TEXT UNIQUE NOT NULL,
|
| 34 |
-
password TEXT NOT NULL
|
| 35 |
-
)
|
| 36 |
-
''')
|
| 37 |
-
conn.commit()
|
| 38 |
|
|
|
|
| 39 |
def hash_password(password: str) -> str:
|
| 40 |
return hashlib.sha256(password.encode()).hexdigest()
|
| 41 |
|
| 42 |
-
# ===
|
| 43 |
class SignupForm(BaseModel):
|
| 44 |
email: str
|
| 45 |
password: str
|
|
@@ -48,24 +35,25 @@ class LoginForm(BaseModel):
|
|
| 48 |
email: str
|
| 49 |
password: str
|
| 50 |
|
| 51 |
-
# ===
|
| 52 |
@app.post("/signup")
|
| 53 |
-
def signup(data: SignupForm):
|
| 54 |
email = data.email.lower().strip()
|
| 55 |
password = hash_password(data.password)
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
return {"success": True, "message": "Account created successfully"}
|
| 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 |
-
|
| 68 |
-
user =
|
| 69 |
if user:
|
| 70 |
return {"success": True, "message": "Login successful"}
|
| 71 |
else:
|
|
@@ -73,22 +61,4 @@ def login(data: LoginForm):
|
|
| 73 |
|
| 74 |
@app.get("/")
|
| 75 |
def root():
|
| 76 |
-
return
|
| 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")
|
|
|
|
| 1 |
from fastapi import FastAPI, HTTPException
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
from pydantic import BaseModel
|
|
|
|
|
|
|
| 4 |
import os
|
| 5 |
+
import hashlib
|
| 6 |
+
import motor.motor_asyncio
|
|
|
|
|
|
|
| 7 |
|
|
|
|
| 8 |
app = FastAPI()
|
| 9 |
|
| 10 |
+
# === CORS ===
|
| 11 |
app.add_middleware(
|
| 12 |
CORSMiddleware,
|
| 13 |
+
allow_origins=["*"], # Open CORS (limit for prod)
|
| 14 |
allow_credentials=True,
|
| 15 |
allow_methods=["*"],
|
| 16 |
allow_headers=["*"],
|
| 17 |
)
|
| 18 |
|
| 19 |
+
# === MongoDB Setup ===
|
| 20 |
+
MONGO_URI = os.getenv("MONGO_URI")
|
| 21 |
+
client = motor.motor_asyncio.AsyncIOMotorClient(MONGO_URI)
|
| 22 |
+
db = client["cps_db"] # database name
|
| 23 |
+
users_collection = db["users"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
# === Utils ===
|
| 26 |
def hash_password(password: str) -> str:
|
| 27 |
return hashlib.sha256(password.encode()).hexdigest()
|
| 28 |
|
| 29 |
+
# === Models ===
|
| 30 |
class SignupForm(BaseModel):
|
| 31 |
email: str
|
| 32 |
password: str
|
|
|
|
| 35 |
email: str
|
| 36 |
password: str
|
| 37 |
|
| 38 |
+
# === Routes ===
|
| 39 |
@app.post("/signup")
|
| 40 |
+
async def signup(data: SignupForm):
|
| 41 |
email = data.email.lower().strip()
|
| 42 |
password = hash_password(data.password)
|
| 43 |
+
|
| 44 |
+
existing = await users_collection.find_one({"email": email})
|
| 45 |
+
if existing:
|
|
|
|
|
|
|
| 46 |
raise HTTPException(status_code=409, detail="Email already registered")
|
| 47 |
|
| 48 |
+
await users_collection.insert_one({"email": email, "password": password})
|
| 49 |
+
return {"success": True, "message": "Account created successfully"}
|
| 50 |
+
|
| 51 |
@app.post("/login")
|
| 52 |
+
async def login(data: LoginForm):
|
| 53 |
email = data.email.lower().strip()
|
| 54 |
password = hash_password(data.password)
|
| 55 |
+
|
| 56 |
+
user = await users_collection.find_one({"email": email, "password": password})
|
| 57 |
if user:
|
| 58 |
return {"success": True, "message": "Login successful"}
|
| 59 |
else:
|
|
|
|
| 61 |
|
| 62 |
@app.get("/")
|
| 63 |
def root():
|
| 64 |
+
return {"message": "MongoDB FastAPI is running ✅"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|