Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -53,6 +53,36 @@ def authenticate_admin(email: str = None, password: str = None):
|
|
| 53 |
# Gradio doctor creation logic
|
| 54 |
BACKEND_URL = "https://rocketfarmstudios-cps-api.hf.space"
|
| 55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
def create_doctor(full_name, email, matricule, password, specialty):
|
| 57 |
payload = {
|
| 58 |
"full_name": full_name,
|
|
@@ -61,8 +91,11 @@ def create_doctor(full_name, email, matricule, password, specialty):
|
|
| 61 |
"password": password,
|
| 62 |
"specialty": specialty,
|
| 63 |
}
|
|
|
|
|
|
|
|
|
|
| 64 |
try:
|
| 65 |
-
res = requests.post(f"{BACKEND_URL}/auth/admin/doctors", json=payload)
|
| 66 |
if res.status_code == 201:
|
| 67 |
return "β
Doctor created successfully!"
|
| 68 |
return f"β {res.json().get('detail', 'Error occurred')}"
|
|
@@ -88,7 +121,7 @@ admin_ui = gr.Blocks(css="""
|
|
| 88 |
|
| 89 |
with admin_ui:
|
| 90 |
gr.Markdown("<div class='title-text'>π¨ββοΈ Doctor Account Creator</div>")
|
| 91 |
-
gr.Markdown("<div class='description-text'>Admins can register new doctors using this secure panel. Generated at 10:
|
| 92 |
|
| 93 |
with gr.Column():
|
| 94 |
full_name = gr.Textbox(label="Full Name", placeholder="e.g. Dr. Sarah Hopkins")
|
|
|
|
| 53 |
# Gradio doctor creation logic
|
| 54 |
BACKEND_URL = "https://rocketfarmstudios-cps-api.hf.space"
|
| 55 |
|
| 56 |
+
# Get admin token by logging in to the auth Space
|
| 57 |
+
def get_admin_token():
|
| 58 |
+
"""
|
| 59 |
+
Authenticate with the auth Space to get a JWT token for admin requests.
|
| 60 |
+
"""
|
| 61 |
+
login_payload = {
|
| 62 |
+
"username": "yakdhanali97@gmail.com",
|
| 63 |
+
"password": "123456",
|
| 64 |
+
"device_token": "admin-device-token" # Replace with a valid device token if required
|
| 65 |
+
}
|
| 66 |
+
try:
|
| 67 |
+
res = requests.post(f"{BACKEND_URL}/auth/login", json=login_payload)
|
| 68 |
+
if res.status_code == 200:
|
| 69 |
+
token = res.json().get("access_token")
|
| 70 |
+
logger.info("Successfully obtained admin token")
|
| 71 |
+
return token
|
| 72 |
+
else:
|
| 73 |
+
logger.error(f"Failed to obtain admin token: {res.status_code} - {res.text}")
|
| 74 |
+
raise Exception("Failed to obtain admin token")
|
| 75 |
+
except Exception as e:
|
| 76 |
+
logger.error(f"Error obtaining admin token: {str(e)}")
|
| 77 |
+
raise
|
| 78 |
+
|
| 79 |
+
# Store the admin token globally (in production, consider refreshing this token periodically)
|
| 80 |
+
try:
|
| 81 |
+
ADMIN_TOKEN = get_admin_token()
|
| 82 |
+
except Exception as e:
|
| 83 |
+
logger.critical("Could not initialize admin token, application startup failed")
|
| 84 |
+
raise
|
| 85 |
+
|
| 86 |
def create_doctor(full_name, email, matricule, password, specialty):
|
| 87 |
payload = {
|
| 88 |
"full_name": full_name,
|
|
|
|
| 91 |
"password": password,
|
| 92 |
"specialty": specialty,
|
| 93 |
}
|
| 94 |
+
headers = {
|
| 95 |
+
"Authorization": f"Bearer {ADMIN_TOKEN}"
|
| 96 |
+
}
|
| 97 |
try:
|
| 98 |
+
res = requests.post(f"{BACKEND_URL}/auth/admin/doctors", json=payload, headers=headers)
|
| 99 |
if res.status_code == 201:
|
| 100 |
return "β
Doctor created successfully!"
|
| 101 |
return f"β {res.json().get('detail', 'Error occurred')}"
|
|
|
|
| 121 |
|
| 122 |
with admin_ui:
|
| 123 |
gr.Markdown("<div class='title-text'>π¨ββοΈ Doctor Account Creator</div>")
|
| 124 |
+
gr.Markdown("<div class='description-text'>Admins can register new doctors using this secure panel. Generated at 10:09 PM CET on Sunday, May 25, 2025.</div>")
|
| 125 |
|
| 126 |
with gr.Column():
|
| 127 |
full_name = gr.Textbox(label="Full Name", placeholder="e.g. Dr. Sarah Hopkins")
|