Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
-
from fastapi import FastAPI, Request
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
from fastapi.responses import RedirectResponse
|
|
|
|
| 4 |
from api import api_router
|
| 5 |
import gradio as gr
|
| 6 |
import requests
|
|
@@ -34,6 +35,31 @@ async def redirect_login(request: Request):
|
|
| 34 |
logger.info("Redirecting /login to /auth/login")
|
| 35 |
return RedirectResponse(url="/auth/login", status_code=307)
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
# Gradio doctor creation logic
|
| 38 |
BACKEND_URL = "https://rocketfarmstudios-cps-api.hf.space"
|
| 39 |
|
|
@@ -70,7 +96,7 @@ with gr.Blocks(css="""
|
|
| 70 |
.output-box textarea { background-color: transparent !important; border: none; color: #90CDF4; font-size: 14px; margin-top: 1rem; }
|
| 71 |
""") as admin_ui:
|
| 72 |
gr.Markdown("<div class='title-text'>👨⚕️ Doctor Account Creator</div>")
|
| 73 |
-
gr.Markdown("<div class='description-text'>Admins can register new doctors using this secure panel. Generated at
|
| 74 |
|
| 75 |
with gr.Column():
|
| 76 |
full_name = gr.Textbox(label="Full Name", placeholder="e.g. Dr. Sarah Hopkins")
|
|
@@ -91,8 +117,13 @@ with gr.Blocks(css="""
|
|
| 91 |
outputs=output
|
| 92 |
)
|
| 93 |
|
| 94 |
-
# Mount Gradio interface to FastAPI app
|
| 95 |
-
app = gr.mount_gradio_app(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
if __name__ == "__main__":
|
| 98 |
logger.debug("Running main block")
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request, Depends, HTTPException
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
from fastapi.responses import RedirectResponse
|
| 4 |
+
from fastapi.security import OAuth2PasswordBearer
|
| 5 |
from api import api_router
|
| 6 |
import gradio as gr
|
| 7 |
import requests
|
|
|
|
| 35 |
logger.info("Redirecting /login to /auth/login")
|
| 36 |
return RedirectResponse(url="/auth/login", status_code=307)
|
| 37 |
|
| 38 |
+
# Admin authentication dependency
|
| 39 |
+
def authenticate_admin(email: str = None, password: str = None):
|
| 40 |
+
"""
|
| 41 |
+
Authenticate admin user with predefined email and password.
|
| 42 |
+
In a production environment, this should use secure password hashing and a database.
|
| 43 |
+
"""
|
| 44 |
+
ADMIN_EMAIL = "yakdhanali97@gmail.com"
|
| 45 |
+
ADMIN_PASSWORD = "123456" # In production, hash this password and compare securely
|
| 46 |
+
|
| 47 |
+
if email != ADMIN_EMAIL or password != ADMIN_PASSWORD:
|
| 48 |
+
logger.warning(f"Failed admin login attempt with email: {email}")
|
| 49 |
+
raise HTTPException(status_code=401, detail="Unauthorized: Invalid email or password")
|
| 50 |
+
|
| 51 |
+
logger.info(f"Admin authenticated successfully: {email}")
|
| 52 |
+
return {"email": email, "is_admin": True}
|
| 53 |
+
|
| 54 |
+
async def get_admin_user(email: str = Depends(lambda: None), password: str = Depends(lambda: None)):
|
| 55 |
+
"""
|
| 56 |
+
Dependency to extract email and password from headers or query params.
|
| 57 |
+
For Gradio, we'll use query params since Gradio doesn't easily support custom headers.
|
| 58 |
+
"""
|
| 59 |
+
if not email or not password:
|
| 60 |
+
raise HTTPException(status_code=401, detail="Email and password are required")
|
| 61 |
+
return authenticate_admin(email, password)
|
| 62 |
+
|
| 63 |
# Gradio doctor creation logic
|
| 64 |
BACKEND_URL = "https://rocketfarmstudios-cps-api.hf.space"
|
| 65 |
|
|
|
|
| 96 |
.output-box textarea { background-color: transparent !important; border: none; color: #90CDF4; font-size: 14px; margin-top: 1rem; }
|
| 97 |
""") as admin_ui:
|
| 98 |
gr.Markdown("<div class='title-text'>👨⚕️ Doctor Account Creator</div>")
|
| 99 |
+
gr.Markdown("<div class='description-text'>Admins can register new doctors using this secure panel. Generated at 09:45 PM CET on Sunday, May 25, 2025.</div>")
|
| 100 |
|
| 101 |
with gr.Column():
|
| 102 |
full_name = gr.Textbox(label="Full Name", placeholder="e.g. Dr. Sarah Hopkins")
|
|
|
|
| 117 |
outputs=output
|
| 118 |
)
|
| 119 |
|
| 120 |
+
# Mount Gradio interface to FastAPI app with authentication
|
| 121 |
+
app = gr.mount_gradio_app(
|
| 122 |
+
app,
|
| 123 |
+
admin_ui,
|
| 124 |
+
path="/admin",
|
| 125 |
+
dependencies=[Depends(get_admin_user)]
|
| 126 |
+
)
|
| 127 |
|
| 128 |
if __name__ == "__main__":
|
| 129 |
logger.debug("Running main block")
|