Ali2206 commited on
Commit
38b6139
Β·
verified Β·
1 Parent(s): 6c71d89

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -36
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 middleware
11
  app.add_middleware(
12
  CORSMiddleware,
13
- allow_origins=["*"], # Change to your frontend domain in prod
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 with MongoDB + JWT is running"}
26
 
27
 
28
- # --- Gradio Interface Logic ---
 
29
 
30
- BACKEND_URL = "https://rocketfarmstudios-cps-api.hf.space" # πŸ‘ˆ change to your Space URL if deployed
31
-
32
- def create_doctor_api(full_name, email, matricule, password):
 
 
 
 
33
  try:
34
- payload = {
35
- "full_name": full_name,
36
- "email": email,
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"❌ Error: {response.json().get('detail', 'Unknown error')}"
45
  except Exception as e:
46
- return f"❌ Failed to connect: {str(e)}"
47
-
48
- gradio_ui = gr.Interface(
49
- fn=create_doctor_api,
50
- inputs=[
51
- gr.Textbox(label="Full Name"),
52
- gr.Textbox(label="Email"),
53
- gr.Textbox(label="Matricule"),
54
- gr.Textbox(label="Password", type="password")
55
- ],
56
- outputs=gr.Textbox(label="Status"),
57
- title="πŸ‘¨β€βš•οΈ Admin - Create Doctor Account",
58
- description="Fill in the fields to register a doctor.",
59
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
 
61
- # Mount Gradio inside FastAPI app
62
- app = gr.mount_gradio_app(app, gradio_ui, path="/admin")
 
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")