CPS-API / app.py
Ali2206's picture
Update app.py
2ba2f6e verified
raw
history blame
2.61 kB
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from api.routes import router as api_router
import gradio as gr
import requests
app = FastAPI()
# --- CORS ---
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Adjust as needed
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
app.include_router(api_router)
@app.get("/")
def root():
return {"message": "πŸš€ FastAPI + MongoDB + JWT is running"}
# --- Gradio Logic ---
BACKEND_URL = "https://rocketfarmstudios-cps-api.hf.space" # or your Space URL
def create_doctor(full_name, email, matricule, password):
payload = {
"full_name": full_name,
"email": email,
"matricule": matricule,
"password": password,
}
try:
res = requests.post(f"{BACKEND_URL}/admin/create-doctor", json=payload)
if res.status_code == 200:
return "βœ… Doctor account successfully created!"
else:
return f"❌ {res.json().get('detail', 'Something went wrong')}"
except Exception as e:
return f"❌ Connection error: {str(e)}"
# --- Gradio Interface ---
with gr.Blocks(css="""
.gradio-container {
background-color: #f9fbfd;
font-family: 'Inter', sans-serif;
padding: 3rem;
}
.title-text {
text-align: center;
font-size: 2rem;
font-weight: 700;
color: #002538;
margin-bottom: 1rem;
}
.description-text {
text-align: center;
font-size: 1rem;
color: #666;
margin-bottom: 2.5rem;
}
.gr-input {
max-width: 500px;
margin: 0 auto;
}
""") as admin_ui:
gr.Markdown("<div class='title-text'>πŸ‘¨β€βš•οΈ Create Doctor Account</div>")
gr.Markdown("<div class='description-text'>Admin-only panel to register new doctor accounts.</div>")
with gr.Column(elem_classes=["gr-input"]):
full_name = gr.Textbox(label="Full Name", placeholder="e.g. Dr. Alice Johnson")
email = gr.Textbox(label="Email", placeholder="e.g. alice@example.com")
matricule = gr.Textbox(label="Matricule", placeholder="e.g. DOC-7891")
password = gr.Textbox(label="Password", type="password", placeholder="Enter a secure password")
submit_btn = gr.Button("βœ… Create Doctor Account", size="lg")
output = gr.Textbox(label="", show_label=False)
submit_btn.click(fn=create_doctor, inputs=[full_name, email, matricule, password], outputs=output)
# Mount Gradio at /admin
app = gr.mount_gradio_app(app, admin_ui, path="/admin")