Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,59 +4,82 @@ from api.routes import router as api_router
|
|
| 4 |
import gradio as gr
|
| 5 |
import requests
|
| 6 |
|
| 7 |
-
# Your FastAPI app
|
| 8 |
app = FastAPI()
|
| 9 |
|
| 10 |
-
# CORS
|
| 11 |
app.add_middleware(
|
| 12 |
CORSMiddleware,
|
| 13 |
-
allow_origins=["*"], #
|
| 14 |
allow_credentials=True,
|
| 15 |
allow_methods=["*"],
|
| 16 |
allow_headers=["*"],
|
| 17 |
)
|
| 18 |
|
| 19 |
-
# Include API router
|
| 20 |
app.include_router(api_router)
|
| 21 |
|
| 22 |
-
# Root endpoint
|
| 23 |
@app.get("/")
|
| 24 |
def root():
|
| 25 |
-
return {"message": "π FastAPI
|
| 26 |
|
| 27 |
|
| 28 |
-
# --- Gradio
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
try:
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
"
|
| 37 |
-
"matricule": matricule,
|
| 38 |
-
"password": password,
|
| 39 |
-
}
|
| 40 |
-
response = requests.post(f"{BACKEND_URL}/admin/create-doctor", json=payload)
|
| 41 |
-
if response.status_code == 200:
|
| 42 |
-
return "β
Doctor created successfully!"
|
| 43 |
else:
|
| 44 |
-
return f"β
|
| 45 |
except Exception as e:
|
| 46 |
-
return f"β
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
-
# Mount Gradio
|
| 62 |
-
app = gr.mount_gradio_app(app,
|
|
|
|
| 4 |
import gradio as gr
|
| 5 |
import requests
|
| 6 |
|
|
|
|
| 7 |
app = FastAPI()
|
| 8 |
|
| 9 |
+
# --- CORS ---
|
| 10 |
app.add_middleware(
|
| 11 |
CORSMiddleware,
|
| 12 |
+
allow_origins=["*"], # Adjust as needed
|
| 13 |
allow_credentials=True,
|
| 14 |
allow_methods=["*"],
|
| 15 |
allow_headers=["*"],
|
| 16 |
)
|
| 17 |
|
|
|
|
| 18 |
app.include_router(api_router)
|
| 19 |
|
|
|
|
| 20 |
@app.get("/")
|
| 21 |
def root():
|
| 22 |
+
return {"message": "π FastAPI + MongoDB + JWT is running"}
|
| 23 |
|
| 24 |
|
| 25 |
+
# --- Gradio Logic ---
|
| 26 |
+
BACKEND_URL = "http://localhost:8000" # or your Space URL
|
| 27 |
|
| 28 |
+
def create_doctor(full_name, email, matricule, password):
|
| 29 |
+
payload = {
|
| 30 |
+
"full_name": full_name,
|
| 31 |
+
"email": email,
|
| 32 |
+
"matricule": matricule,
|
| 33 |
+
"password": password,
|
| 34 |
+
}
|
| 35 |
try:
|
| 36 |
+
res = requests.post(f"{BACKEND_URL}/admin/create-doctor", json=payload)
|
| 37 |
+
if res.status_code == 200:
|
| 38 |
+
return "β
Doctor account successfully created!"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
else:
|
| 40 |
+
return f"β {res.json().get('detail', 'Something went wrong')}"
|
| 41 |
except Exception as e:
|
| 42 |
+
return f"β Connection error: {str(e)}"
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
# --- Gradio Interface ---
|
| 46 |
+
with gr.Blocks(css="""
|
| 47 |
+
.gradio-container {
|
| 48 |
+
background-color: #f9fbfd;
|
| 49 |
+
font-family: 'Inter', sans-serif;
|
| 50 |
+
padding: 3rem;
|
| 51 |
+
}
|
| 52 |
+
.title-text {
|
| 53 |
+
text-align: center;
|
| 54 |
+
font-size: 2rem;
|
| 55 |
+
font-weight: 700;
|
| 56 |
+
color: #002538;
|
| 57 |
+
margin-bottom: 1rem;
|
| 58 |
+
}
|
| 59 |
+
.description-text {
|
| 60 |
+
text-align: center;
|
| 61 |
+
font-size: 1rem;
|
| 62 |
+
color: #666;
|
| 63 |
+
margin-bottom: 2.5rem;
|
| 64 |
+
}
|
| 65 |
+
.gr-input {
|
| 66 |
+
max-width: 500px;
|
| 67 |
+
margin: 0 auto;
|
| 68 |
+
}
|
| 69 |
+
""") as admin_ui:
|
| 70 |
+
gr.Markdown("<div class='title-text'>π¨ββοΈ Create Doctor Account</div>")
|
| 71 |
+
gr.Markdown("<div class='description-text'>Admin-only panel to register new doctor accounts.</div>")
|
| 72 |
+
|
| 73 |
+
with gr.Column(elem_classes=["gr-input"]):
|
| 74 |
+
full_name = gr.Textbox(label="Full Name", placeholder="e.g. Dr. Alice Johnson")
|
| 75 |
+
email = gr.Textbox(label="Email", placeholder="e.g. alice@example.com")
|
| 76 |
+
matricule = gr.Textbox(label="Matricule", placeholder="e.g. DOC-7891")
|
| 77 |
+
password = gr.Textbox(label="Password", type="password", placeholder="Enter a secure password")
|
| 78 |
+
submit_btn = gr.Button("β
Create Doctor Account", size="lg")
|
| 79 |
+
|
| 80 |
+
output = gr.Textbox(label="", show_label=False)
|
| 81 |
+
|
| 82 |
+
submit_btn.click(fn=create_doctor, inputs=[full_name, email, matricule, password], outputs=output)
|
| 83 |
|
| 84 |
+
# Mount Gradio at /admin
|
| 85 |
+
app = gr.mount_gradio_app(app, admin_ui, path="/admin")
|