Spaces:
Sleeping
Sleeping
| from fastapi import FastAPI, Request, HTTPException, Response | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from fastapi.responses import RedirectResponse, HTMLResponse | |
| from api import api_router | |
| import gradio as gr | |
| import logging | |
| # Configure logging | |
| logging.basicConfig(level=logging.DEBUG) | |
| logger = logging.getLogger(__name__) | |
| logger.debug("Initializing application") | |
| app = FastAPI() | |
| # CORS Configuration | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| app.include_router(api_router) | |
| # Constants | |
| ADMIN_EMAIL = "[email protected]" | |
| ADMIN_PASSWORD = "123456" | |
| def root(): | |
| logger.debug("Root endpoint accessed") | |
| return {"message": "🚀 CPS API with TxAgent Integration is running."} | |
| async def redirect_login(request: Request): | |
| logger.info("Redirecting /login to /auth/login") | |
| return RedirectResponse(url="/auth/login", status_code=307) | |
| def authenticate_admin(email: str = None, password: str = None): | |
| if email != ADMIN_EMAIL or password != ADMIN_PASSWORD: | |
| logger.warning(f"Failed admin login attempt with email: {email}") | |
| raise HTTPException(status_code=401, detail="Unauthorized: Invalid email or password") | |
| logger.info(f"Admin authenticated successfully: {email}") | |
| return True | |
| def create_doctor_account(full_name, email, matricule, password, specialty): | |
| """Simple doctor account creation without external API calls""" | |
| try: | |
| # This is a simplified version - in production you'd want to integrate with your user management system | |
| logger.info(f"Creating doctor account for: {email}") | |
| return "✅ Doctor account creation initiated. Please contact system administrator for account activation." | |
| except Exception as e: | |
| logger.error(f"Doctor creation failed: {str(e)}") | |
| return f"❌ System Error: {str(e)}" | |
| admin_ui = gr.Blocks( | |
| css=""" | |
| .gradio-container { | |
| font-family: Arial, sans-serif; | |
| max-width: 800px; | |
| margin: 0 auto; | |
| padding: 2rem; | |
| } | |
| .input-group { | |
| margin-bottom: 1.5rem; | |
| } | |
| input, select { | |
| width: 100%; | |
| padding: 0.5rem; | |
| margin-bottom: 1rem; | |
| } | |
| button { | |
| background-color: #4a6fa5; | |
| color: white; | |
| padding: 0.75rem; | |
| border: none; | |
| border-radius: 4px; | |
| cursor: pointer; | |
| width: 100%; | |
| } | |
| """ | |
| ) | |
| with admin_ui: | |
| gr.Markdown("# Doctor Account Creator") | |
| with gr.Column(): | |
| full_name = gr.Textbox(label="Full Name") | |
| email = gr.Textbox(label="Email") | |
| matricule = gr.Textbox(label="License Number") | |
| specialty = gr.Dropdown( | |
| label="Specialty", | |
| choices=["General Practice", "Cardiology", "Neurology", "Pediatrics"] | |
| ) | |
| password = gr.Textbox(label="Password", type="password") | |
| submit_btn = gr.Button("Create Account") | |
| output = gr.Textbox(label="Status", interactive=False) | |
| submit_btn.click( | |
| fn=create_doctor_account, | |
| inputs=[full_name, email, matricule, specialty, password], | |
| outputs=output | |
| ) | |
| app = gr.mount_gradio_app(app, admin_ui, path="/admin-auth") | |
| async def admin_dashboard(email: str = None, password: str = None, response: Response = None): | |
| logger.debug("Admin dashboard accessed") | |
| try: | |
| authenticate_admin(email, password) | |
| return RedirectResponse(url="/admin-auth", status_code=307) | |
| except HTTPException as e: | |
| response.status_code = 401 | |
| return HTMLResponse(content=""" | |
| <h1>401 Unauthorized</h1> | |
| <p>Invalid admin credentials</p> | |
| """) | |
| async def startup_event(): | |
| """Initialize application on startup""" | |
| logger.info("🚀 CPS API with TxAgent Integration starting up...") | |
| if __name__ == "__main__": | |
| logger.info("Starting application") | |
| import uvicorn | |
| uvicorn.run(app, host="0.0.0.0", port=7860) |