Ali2206 commited on
Commit
3456970
Β·
verified Β·
1 Parent(s): 71ff4cb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -95
app.py CHANGED
@@ -3,6 +3,11 @@ from fastapi.middleware.cors import CORSMiddleware
3
  from api import api_router
4
  import gradio as gr
5
  import requests
 
 
 
 
 
6
 
7
  app = FastAPI()
8
 
@@ -19,6 +24,7 @@ app.include_router(api_router)
19
 
20
  @app.get("/")
21
  def root():
 
22
  return {"message": "πŸš€ FastAPI with MongoDB + JWT is running."}
23
 
24
  # Gradio doctor creation logic
@@ -28,108 +34,64 @@ def create_doctor(full_name, email, matricule, password, specialty):
28
  payload = {
29
  "full_name": full_name,
30
  "email": email,
31
- "license_number": matricule, # Map 'matricule' to 'license_number' as expected by the backend
32
  "password": password,
33
  "specialty": specialty,
34
  }
35
  try:
36
  res = requests.post(f"{BACKEND_URL}/auth/admin/doctors", json=payload)
37
- if res.status_code == 201: # Status code for successful creation
38
  return "βœ… Doctor created successfully!"
39
  return f"❌ {res.json().get('detail', 'Error occurred')}"
40
  except Exception as e:
41
  return f"❌ Network Error: {str(e)}"
42
 
43
- # Modern admin UI
44
- with gr.Blocks(css="""
45
- .gradio-container {
46
- background-color: #1A1B1F;
47
- color: #E2E8F0;
48
- font-family: 'Segoe UI', sans-serif;
49
- padding: 3rem;
50
- }
51
-
52
- .title-text {
53
- text-align: center;
54
- font-size: 2rem;
55
- font-weight: 700;
56
- color: #37B6E9;
57
- margin-bottom: 0.5rem;
58
- }
59
-
60
- .description-text {
61
- text-align: center;
62
- font-size: 1rem;
63
- color: #A0AEC0;
64
- margin-bottom: 2rem;
65
- }
66
-
67
- .gr-box, .gr-form, .gr-column, .gr-panel {
68
- background-color: #2D2F36 !important;
69
- border-radius: 16px !important;
70
- padding: 2rem !important;
71
- max-width: 600px;
72
- margin: auto;
73
- box-shadow: 0 0 0 1px #3B3E47;
74
- }
75
-
76
- label {
77
- font-weight: 600;
78
- color: #F7FAFC;
79
- margin-bottom: 6px;
80
- }
81
-
82
- input, select, textarea {
83
- background-color: #1A1B1F !important;
84
- color: #F7FAFC !important;
85
- border: 1px solid #4A5568 !important;
86
- font-size: 14px;
87
- padding: 10px;
88
- border-radius: 10px;
89
- }
90
-
91
- button {
92
- background-color: #37B6E9 !important;
93
- color: #1A1B1F !important;
94
- border-radius: 10px !important;
95
- font-weight: 600;
96
- padding: 12px;
97
- width: 100%;
98
- margin-top: 1.5rem;
99
- }
100
-
101
- .output-box textarea {
102
- background-color: transparent !important;
103
- border: none;
104
- color: #90CDF4;
105
- font-size: 14px;
106
- margin-top: 1rem;
107
- }
108
- """) as admin_ui:
109
- gr.Markdown("<div class='title-text'>πŸ‘¨β€βš•οΈ Doctor Account Creator</div>")
110
- gr.Markdown("<div class='description-text'>Admins can register new doctors using this secure panel. Generated at 02:54 PM CET on Saturday, May 17, 2025.</div>")
111
-
112
- with gr.Column():
113
- full_name = gr.Textbox(label="Full Name", placeholder="e.g. Dr. Sarah Hopkins")
114
- email = gr.Textbox(label="Email", placeholder="e.g. doctor@clinic.org")
115
- matricule = gr.Textbox(label="Matricule", placeholder="e.g. DOC-1234")
116
- specialty = gr.Dropdown(
117
- label="Specialty",
118
- choices=[
119
- "Cardiology", "Neurology", "Pediatrics", "Oncology",
120
- "General Practice", "Psychiatry", "Dermatology", "Orthopedics"
121
- ],
122
- value="Cardiology"
123
- )
124
- password = gr.Textbox(label="Password", type="password", placeholder="Secure password")
125
- submit_btn = gr.Button("Create Doctor Account")
126
- output = gr.Textbox(label="", show_label=False, elem_classes=["output-box"])
127
-
128
- submit_btn.click(
129
- fn=create_doctor,
130
- inputs=[full_name, email, matricule, specialty, password],
131
- outputs=output
132
- )
133
-
134
- # Mount Gradio at /admin
135
- app = gr.mount_gradio_app(app, admin_ui, path="/admin")
 
3
  from api import api_router
4
  import gradio as gr
5
  import requests
6
+ import logging
7
+
8
+ logging.basicConfig(level=logging.DEBUG)
9
+ logger = logging.getLogger(__name__)
10
+ logger.debug("Initializing application")
11
 
12
  app = FastAPI()
13
 
 
24
 
25
  @app.get("/")
26
  def root():
27
+ logger.debug("Root endpoint accessed")
28
  return {"message": "πŸš€ FastAPI with MongoDB + JWT is running."}
29
 
30
  # Gradio doctor creation logic
 
34
  payload = {
35
  "full_name": full_name,
36
  "email": email,
37
+ "license_number": matricule,
38
  "password": password,
39
  "specialty": specialty,
40
  }
41
  try:
42
  res = requests.post(f"{BACKEND_URL}/auth/admin/doctors", json=payload)
43
+ if res.status_code == 201:
44
  return "βœ… Doctor created successfully!"
45
  return f"❌ {res.json().get('detail', 'Error occurred')}"
46
  except Exception as e:
47
  return f"❌ Network Error: {str(e)}"
48
 
49
+ # Define Gradio interface as a separate function
50
+ def setup_gradio():
51
+ logger.debug("Setting up Gradio interface")
52
+ with gr.Blocks(css="""
53
+ .gradio-container {
54
+ background-color: #1A1B1F;
55
+ color: #E2E8F0;
56
+ font-family: 'Segoe UI', sans-serif;
57
+ padding: 3rem;
58
+ }
59
+ .title-text { text-align: center; font-size: 2rem; font-weight: 700; color: #37B6E9; margin-bottom: 0.5rem; }
60
+ .description-text { text-align: center; font-size: 1rem; color: #A0AEC0; margin-bottom: 2rem; }
61
+ .gr-box, .gr-form, .gr-column, .gr-panel { background-color: #2D2F36 !important; border-radius: 16px !important; padding: 2rem !important; max-width: 600px; margin: auto; box-shadow: 0 0 0 1px #3B3E47; }
62
+ label { font-weight: 600; color: #F7FAFC; margin-bottom: 6px; }
63
+ input, select, textarea { background-color: #1A1B1F !important; color: #F7FAFC !important; border: 1px solid #4A5568 !important; font-size: 14px; padding: 10px; border-radius: 10px; }
64
+ button { background-color: #37B6E9 !important; color: #1A1B1F !important; border-radius: 10px !important; font-weight: 600; padding: 12px; width: 100%; margin-top: 1.5rem; }
65
+ .output-box textarea { background-color: transparent !important; border: none; color: #90CDF4; font-size: 14px; margin-top: 1rem; }
66
+ """) as admin_ui:
67
+ gr.Markdown("<div class='title-text'>πŸ‘¨β€βš•οΈ Doctor Account Creator</div>")
68
+ gr.Markdown("<div class='description-text'>Admins can register new doctors using this secure panel. Generated at 03:15 PM CET on Saturday, May 17, 2025.</div>")
69
+
70
+ with gr.Column():
71
+ full_name = gr.Textbox(label="Full Name", placeholder="e.g. Dr. Sarah Hopkins")
72
+ email = gr.Textbox(label="Email", placeholder="e.g. doctor@clinic.org")
73
+ matricule = gr.Textbox(label="Matricule", placeholder="e.g. DOC-1234")
74
+ specialty = gr.Dropdown(
75
+ label="Specialty",
76
+ choices=["Cardiology", "Neurology", "Pediatrics", "Oncology", "General Practice", "Psychiatry", "Dermatology", "Orthopedics"],
77
+ value="Cardiology"
78
+ )
79
+ password = gr.Textbox(label="Password", type="password", placeholder="Secure password")
80
+ submit_btn = gr.Button("Create Doctor Account")
81
+ output = gr.Textbox(label="", show_label=False, elem_classes=["output-box"])
82
+
83
+ submit_btn.click(
84
+ fn=create_doctor,
85
+ inputs=[full_name, email, matricule, specialty, password],
86
+ outputs=output
87
+ )
88
+ return admin_ui
89
+
90
+ # Mount Gradio at /admin (delayed execution)
91
+ if __name__ == "__main__":
92
+ logger.debug("Running main block")
93
+ admin_ui = setup_gradio()
94
+ app = gr.mount_gradio_app(app, admin_ui, path="/admin")
95
+ logger.debug("Gradio mounted, starting app")
96
+ import uvicorn
97
+ uvicorn.run(app, host="0.0.0.0", port=8000)