Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,6 @@
|
|
| 1 |
-
from fastapi import FastAPI, Request,
|
| 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,7 +34,7 @@ async def redirect_login(request: Request):
|
|
| 35 |
logger.info("Redirecting /login to /auth/login")
|
| 36 |
return RedirectResponse(url="/auth/login", status_code=307)
|
| 37 |
|
| 38 |
-
# Admin authentication
|
| 39 |
def authenticate_admin(email: str = None, password: str = None):
|
| 40 |
"""
|
| 41 |
Authenticate admin user with predefined email and password.
|
|
@@ -49,16 +48,58 @@ def authenticate_admin(email: str = None, password: str = None):
|
|
| 49 |
raise HTTPException(status_code=401, detail="Unauthorized: Invalid email or password")
|
| 50 |
|
| 51 |
logger.info(f"Admin authenticated successfully: {email}")
|
| 52 |
-
return
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
# Gradio doctor creation logic
|
| 64 |
BACKEND_URL = "https://rocketfarmstudios-cps-api.hf.space"
|
|
@@ -79,52 +120,6 @@ def create_doctor(full_name, email, matricule, password, specialty):
|
|
| 79 |
except Exception as e:
|
| 80 |
return f"❌ Network Error: {str(e)}"
|
| 81 |
|
| 82 |
-
# Define Gradio interface
|
| 83 |
-
with gr.Blocks(css="""
|
| 84 |
-
.gradio-container {
|
| 85 |
-
background-color: #1A1B1F;
|
| 86 |
-
color: #E2E8F0;
|
| 87 |
-
font-family: 'Segoe UI', sans-serif;
|
| 88 |
-
padding: 3rem;
|
| 89 |
-
}
|
| 90 |
-
.title-text { text-align: center; font-size: 2rem; font-weight: 700; color: #37B6E9; margin-bottom: 0.5rem; }
|
| 91 |
-
.description-text { text-align: center; font-size: 1rem; color: #A0AEC0; margin-bottom: 2rem; }
|
| 92 |
-
.gr-box, .gr-form, .gr-column, .gr-panel { background-color: #2D2F36 !important; border-radius: 16px !important; padding: 2rem !important; max-width: 600px; margin: auto; box-shadow: 0 0 0 1px #3B3E47; }
|
| 93 |
-
label { font-weight: 600; color: #F7FAFC; margin-bottom: 6px; }
|
| 94 |
-
input, select, textarea { background-color: #1A1B1F !important; color: #F7FAFC !important; border: 1px solid #4A5568 !important; font-size: 14px; padding: 10px; border-radius: 10px; }
|
| 95 |
-
button { background-color: #37B6E9 !important; color: #1A1B1F !important; border-radius: 10px !important; font-weight: 600; padding: 12px; width: 100%; margin-top: 1.5rem; }
|
| 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")
|
| 103 |
-
email = gr.Textbox(label="Email", placeholder="e.g. doctor@clinic.org")
|
| 104 |
-
matricule = gr.Textbox(label="Matricule", placeholder="e.g. DOC-1234")
|
| 105 |
-
specialty = gr.Dropdown(
|
| 106 |
-
label="Specialty",
|
| 107 |
-
choices=["Cardiology", "Neurology", "Pediatrics", "Oncology", "General Practice", "Psychiatry", "Dermatology", "Orthopedics"],
|
| 108 |
-
value="Cardiology"
|
| 109 |
-
)
|
| 110 |
-
password = gr.Textbox(label="Password", type="password", placeholder="Secure password")
|
| 111 |
-
submit_btn = gr.Button("Create Doctor Account")
|
| 112 |
-
output = gr.Textbox(label="", show_label=False, elem_classes=["output-box"])
|
| 113 |
-
|
| 114 |
-
submit_btn.click(
|
| 115 |
-
fn=create_doctor,
|
| 116 |
-
inputs=[full_name, email, matricule, specialty, password],
|
| 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")
|
| 130 |
import uvicorn
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request, HTTPException
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
from fastapi.responses import RedirectResponse, HTMLResponse
|
|
|
|
| 4 |
from api import api_router
|
| 5 |
import gradio as gr
|
| 6 |
import requests
|
|
|
|
| 34 |
logger.info("Redirecting /login to /auth/login")
|
| 35 |
return RedirectResponse(url="/auth/login", status_code=307)
|
| 36 |
|
| 37 |
+
# Admin authentication logic
|
| 38 |
def authenticate_admin(email: str = None, password: str = None):
|
| 39 |
"""
|
| 40 |
Authenticate admin user with predefined email and password.
|
|
|
|
| 48 |
raise HTTPException(status_code=401, detail="Unauthorized: Invalid email or password")
|
| 49 |
|
| 50 |
logger.info(f"Admin authenticated successfully: {email}")
|
| 51 |
+
return True
|
| 52 |
|
| 53 |
+
# Serve Gradio interface for /admin with authentication
|
| 54 |
+
@app.get("/admin")
|
| 55 |
+
async def admin_dashboard(email: str = None, password: str = None):
|
| 56 |
+
logger.debug("Admin dashboard accessed with email and password")
|
| 57 |
+
try:
|
| 58 |
+
authenticate_admin(email, password)
|
| 59 |
+
except HTTPException as e:
|
| 60 |
+
return HTMLResponse(content="<h1>401 Unauthorized</h1><p>Invalid email or password. Please use: email=yakdhanali97@gmail.com&password=123456</p>", status_code=401)
|
| 61 |
+
|
| 62 |
+
# Serve the Gradio interface
|
| 63 |
+
admin_ui = gr.Blocks(css="""
|
| 64 |
+
.gradio-container {
|
| 65 |
+
background-color: #1A1B1F;
|
| 66 |
+
color: #E2E8F0;
|
| 67 |
+
font-family: 'Segoe UI', sans-serif;
|
| 68 |
+
padding: 3rem;
|
| 69 |
+
}
|
| 70 |
+
.title-text { text-align: center; font-size: 2rem; font-weight: 700; color: #37B6E9; margin-bottom: 0.5rem; }
|
| 71 |
+
.description-text { text-align: center; font-size: 1rem; color: #A0AEC0; margin-bottom: 2rem; }
|
| 72 |
+
.gr-box, .gr-form, .gr-column, .gr-panel { background-color: #2D2F36 !important; border-radius: 16px !important; padding: 2rem !important; max-width: 600px; margin: auto; box-shadow: 0 0 0 1px #3B3E47; }
|
| 73 |
+
label { font-weight: 600; color: #F7FAFC; margin-bottom: 6px; }
|
| 74 |
+
input, select, textarea { background-color: #1A1B1F !important; color: #F7FAFC !important; border: 1px solid #4A5568 !important; font-size: 14px; padding: 10px; border-radius: 10px; }
|
| 75 |
+
button { background-color: #37B6E9 !important; color: #1A1B1F !important; border-radius: 10px !important; font-weight: 600; padding: 12px; width: 100%; margin-top: 1.5rem; }
|
| 76 |
+
.output-box textarea { background-color: transparent !important; border: none; color: #90CDF4; font-size: 14px; margin-top: 1rem; }
|
| 77 |
+
""")
|
| 78 |
+
|
| 79 |
+
with admin_ui:
|
| 80 |
+
gr.Markdown("<div class='title-text'>👨⚕️ Doctor Account Creator</div>")
|
| 81 |
+
gr.Markdown("<div class='description-text'>Admins can register new doctors using this secure panel. Generated at 09:49 PM CET on Sunday, May 25, 2025.</div>")
|
| 82 |
+
|
| 83 |
+
with gr.Column():
|
| 84 |
+
full_name = gr.Textbox(label="Full Name", placeholder="e.g. Dr. Sarah Hopkins")
|
| 85 |
+
email = gr.Textbox(label="Email", placeholder="e.g. doctor@clinic.org")
|
| 86 |
+
matricule = gr.Textbox(label="Matricule", placeholder="e.g. DOC-1234")
|
| 87 |
+
specialty = gr.Dropdown(
|
| 88 |
+
label="Specialty",
|
| 89 |
+
choices=["Cardiology", "Neurology", "Pediatrics", "Oncology", "General Practice", "Psychiatry", "Dermatology", "Orthopedics"],
|
| 90 |
+
value="Cardiology"
|
| 91 |
+
)
|
| 92 |
+
password = gr.Textbox(label="Password", type="password", placeholder="Secure password")
|
| 93 |
+
submit_btn = gr.Button("Create Doctor Account")
|
| 94 |
+
output = gr.Textbox(label="", show_label=False, elem_classes=["output-box"])
|
| 95 |
+
|
| 96 |
+
submit_btn.click(
|
| 97 |
+
fn=create_doctor,
|
| 98 |
+
inputs=[full_name, email, matricule, specialty, password],
|
| 99 |
+
outputs=output
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
return admin_ui.launch(share=False, inline=True)
|
| 103 |
|
| 104 |
# Gradio doctor creation logic
|
| 105 |
BACKEND_URL = "https://rocketfarmstudios-cps-api.hf.space"
|
|
|
|
| 120 |
except Exception as e:
|
| 121 |
return f"❌ Network Error: {str(e)}"
|
| 122 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
if __name__ == "__main__":
|
| 124 |
logger.debug("Running main block")
|
| 125 |
import uvicorn
|