Spaces:
Sleeping
Sleeping
File size: 4,088 Bytes
682caaf 1518185 682caaf 1518185 682caaf 1518185 682caaf 1518185 682caaf 1518185 682caaf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
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"
@app.get("/")
def root():
logger.debug("Root endpoint accessed")
return {"message": "🚀 CPS API with TxAgent Integration is running."}
@app.post("/login")
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")
@app.get("/admin")
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>
""")
@app.on_event("startup")
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) |