CPS-API / app.py
Ali2206's picture
Update app.py
49bff0f verified
raw
history blame
4.51 kB
from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import RedirectResponse
from api import api_router
import gradio as gr
import requests
import logging
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
logger.debug("Initializing application")
app = FastAPI()
# CORS
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
app.include_router(api_router)
@app.get("/")
def root():
logger.debug("Root endpoint accessed")
return {"message": "πŸš€ FastAPI with MongoDB + JWT is running."}
# Redirect /login to /auth/login
@app.post("/login")
async def redirect_login(request: Request):
logger.info("Redirecting /login to /auth/login")
return RedirectResponse(url="/auth/login", status_code=307)
# Gradio doctor creation logic
BACKEND_URL = "https://rocketfarmstudios-cps-api.hf.space"
def create_doctor(full_name, email, matricule, password, specialty):
payload = {
"full_name": full_name,
"email": email,
"license_number": matricule,
"password": password,
"specialty": specialty,
}
try:
res = requests.post(f"{BACKEND_URL}/auth/admin/doctors", json=payload)
if res.status_code == 201:
return "βœ… Doctor created successfully!"
return f"❌ {res.json().get('detail', 'Error occurred')}"
except Exception as e:
return f"❌ Network Error: {str(e)}"
# Define Gradio interface as a separate function
def setup_gradio():
logger.debug("Setting up Gradio interface")
with gr.Blocks(css="""
.gradio-container {
background-color: #1A1B1F;
color: #E2E8F0;
font-family: 'Segoe UI', sans-serif;
padding: 3rem;
}
.title-text { text-align: center; font-size: 2rem; font-weight: 700; color: #37B6E9; margin-bottom: 0.5rem; }
.description-text { text-align: center; font-size: 1rem; color: #A0AEC0; margin-bottom: 2rem; }
.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; }
label { font-weight: 600; color: #F7FAFC; margin-bottom: 6px; }
input, select, textarea { background-color: #1A1B1F !important; color: #F7FAFC !important; border: 1px solid #4A5568 !important; font-size: 14px; padding: 10px; border-radius: 10px; }
button { background-color: #37B6E9 !important; color: #1A1B1F !important; border-radius: 10px !important; font-weight: 600; padding: 12px; width: 100%; margin-top: 1.5rem; }
.output-box textarea { background-color: transparent !important; border: none; color: #90CDF4; font-size: 14px; margin-top: 1rem; }
""") as admin_ui:
gr.Markdown("<div class='title-text'>πŸ‘¨β€βš•οΈ Doctor Account Creator</div>")
gr.Markdown("<div class='description-text'>Admins can register new doctors using this secure panel. Generated at 03:43 PM CET on Saturday, May 17, 2025.</div>")
with gr.Column():
full_name = gr.Textbox(label="Full Name", placeholder="e.g. Dr. Sarah Hopkins")
email = gr.Textbox(label="Email", placeholder="e.g. doctor@clinic.org")
matricule = gr.Textbox(label="Matricule", placeholder="e.g. DOC-1234")
specialty = gr.Dropdown(
label="Specialty",
choices=["Cardiology", "Neurology", "Pediatrics", "Oncology", "General Practice", "Psychiatry", "Dermatology", "Orthopedics"],
value="Cardiology"
)
password = gr.Textbox(label="Password", type="password", placeholder="Secure password")
submit_btn = gr.Button("Create Doctor Account")
output = gr.Textbox(label="", show_label=False, elem_classes=["output-box"])
submit_btn.click(
fn=create_doctor,
inputs=[full_name, email, matricule, specialty, password],
outputs=output
)
return admin_ui
# Mount Gradio at /admin
if __name__ == "__main__":
logger.debug("Running main block")
admin_ui = setup_gradio()
app = gr.mount_gradio_app(app, admin_ui, path="/admin")
logger.debug("Gradio mounted, starting app")
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=7860) # Hugging Face Spaces default port