Ali2206 commited on
Commit
39d0c6d
·
verified ·
1 Parent(s): fe7ebe9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -28
app.py CHANGED
@@ -1,6 +1,6 @@
1
- from fastapi import FastAPI, Request, HTTPException, Response
2
  from fastapi.middleware.cors import CORSMiddleware
3
- from fastapi.responses import RedirectResponse, HTMLResponse
4
  from pydantic import BaseModel
5
  import gradio as gr
6
  import aiohttp
@@ -22,10 +22,10 @@ logger.debug("Initializing application")
22
  # FastAPI app
23
  app = FastAPI()
24
 
25
- # CORS Configuration (restrict in production)
26
  app.add_middleware(
27
  CORSMiddleware,
28
- allow_origins=["http://localhost:7860", "https://rocketfarmstudios-cps-api.hf.space"],
29
  allow_credentials=True,
30
  allow_methods=["*"],
31
  allow_headers=["*"],
@@ -65,12 +65,13 @@ class TokenManager:
65
  try:
66
  async with aiohttp.ClientSession() as session:
67
  payload = LoginPayload(username=ADMIN_EMAIL, password=ADMIN_PASSWORD)
68
- login_url = f"{BACKEND_URL}/auth/login"
69
  logger.debug(f"Sending login request to {login_url} with payload: {payload.dict()}")
70
  async with session.post(
71
  login_url,
72
  json=payload.dict(),
73
- timeout=10
 
74
  ) as response:
75
  logger.debug(f"Login response status: {response.status}, URL: {response.url}")
76
  if response.status == 200:
@@ -143,21 +144,15 @@ async def handle_local_auth_login(request: Request):
143
  async def test_backend():
144
  try:
145
  async with aiohttp.ClientSession() as session:
146
- async with session.get(BACKEND_URL, timeout=5) as response:
 
 
147
  logger.debug(f"Test backend response status: {response.status}, URL: {response.url}")
148
  return {"status": response.status, "url": str(response.url), "message": await response.text()}
149
  except aiohttp.ClientError as e:
150
  logger.error(f"Test backend error: {str(e)}")
151
  return {"status": "error", "message": str(e)}
152
 
153
- def authenticate_admin(email: str = None, password: str = None):
154
- if email != ADMIN_EMAIL or password != ADMIN_PASSWORD:
155
- logger.warning(f"Failed admin login attempt with email: {email}")
156
- raise HTTPException(status_code=401, detail="Unauthorized: Invalid email or password")
157
-
158
- logger.info(f"Admin authenticated successfully: {email}")
159
- return True
160
-
161
  async def async_create_doctor(full_name: str, email: str, license_number: str, specialty: str, password: str):
162
  try:
163
  # Validate inputs
@@ -179,14 +174,14 @@ async def async_create_doctor(full_name: str, email: str, license_number: str, s
179
  "Content-Type": "application/json"
180
  }
181
 
182
- doctor_url = f"{BACKEND_URL}/auth/admin/doctors"
183
  logger.debug(f"Sending doctor creation request to {doctor_url} with payload: {payload.dict()}")
184
  async with aiohttp.ClientSession() as session:
185
  async with session.post(
186
  doctor_url,
187
  json=payload.dict(),
188
  headers=headers,
189
- timeout=10
190
  ) as response:
191
  logger.debug(f"Doctor creation response status: {response.status}, URL: {response.url}")
192
  if response.status == 201:
@@ -199,7 +194,7 @@ async def async_create_doctor(full_name: str, email: str, license_number: str, s
199
  doctor_url,
200
  json=payload.dict(),
201
  headers=headers,
202
- timeout=10
203
  ) as retry_response:
204
  logger.debug(f"Retry doctor creation response status: {retry_response.status}, URL: {retry_response.url}")
205
  if retry_response.status == 201:
@@ -220,6 +215,18 @@ async def async_create_doctor(full_name: str, email: str, license_number: str, s
220
  def sync_create_doctor(full_name: str, email: str, license_number: str, specialty: str, password: str):
221
  return asyncio.run(async_create_doctor(full_name, email, license_number, specialty, password))
222
 
 
 
 
 
 
 
 
 
 
 
 
 
223
  # Gradio UI
224
  admin_ui = gr.Blocks(
225
  css="""
@@ -273,18 +280,11 @@ with admin_ui:
273
 
274
  app = gr.mount_gradio_app(app, admin_ui, path="/admin-auth")
275
 
 
276
  @app.get("/admin")
277
- async def admin_dashboard(email: str = None, password: str = None, response: Response = None):
278
  logger.debug("Admin dashboard accessed")
279
- try:
280
- authenticate_admin(email, password)
281
- return RedirectResponse(url="/admin-auth", status_code=307)
282
- except HTTPException as e:
283
- response.status_code = 401
284
- return HTMLResponse(content="""
285
- <h1>401 Unauthorized</h1>
286
- <p>Invalid admin credentials</p>
287
- """)
288
 
289
  @app.get("/admin-auth/gradio_api/queue/data")
290
  async def gradio_queue_data(session_hash: str):
 
1
+ from fastapi import FastAPI, Request, HTTPException
2
  from fastapi.middleware.cors import CORSMiddleware
3
+ from fastapi.responses import RedirectResponse
4
  from pydantic import BaseModel
5
  import gradio as gr
6
  import aiohttp
 
22
  # FastAPI app
23
  app = FastAPI()
24
 
25
+ # CORS Configuration (broadened for Hugging Face Spaces)
26
  app.add_middleware(
27
  CORSMiddleware,
28
+ allow_origins=["http://localhost:7860", "https://*.hf.space"],
29
  allow_credentials=True,
30
  allow_methods=["*"],
31
  allow_headers=["*"],
 
65
  try:
66
  async with aiohttp.ClientSession() as session:
67
  payload = LoginPayload(username=ADMIN_EMAIL, password=ADMIN_PASSWORD)
68
+ login_url = f"{BACKEND_URL.rstrip('/')}/auth/login"
69
  logger.debug(f"Sending login request to {login_url} with payload: {payload.dict()}")
70
  async with session.post(
71
  login_url,
72
  json=payload.dict(),
73
+ timeout=15,
74
+ headers={"Content-Type": "application/json"}
75
  ) as response:
76
  logger.debug(f"Login response status: {response.status}, URL: {response.url}")
77
  if response.status == 200:
 
144
  async def test_backend():
145
  try:
146
  async with aiohttp.ClientSession() as session:
147
+ test_url = f"{BACKEND_URL.rstrip('/')}/"
148
+ logger.debug(f"Testing backend connectivity to {test_url}")
149
+ async with session.get(test_url, timeout=5) as response:
150
  logger.debug(f"Test backend response status: {response.status}, URL: {response.url}")
151
  return {"status": response.status, "url": str(response.url), "message": await response.text()}
152
  except aiohttp.ClientError as e:
153
  logger.error(f"Test backend error: {str(e)}")
154
  return {"status": "error", "message": str(e)}
155
 
 
 
 
 
 
 
 
 
156
  async def async_create_doctor(full_name: str, email: str, license_number: str, specialty: str, password: str):
157
  try:
158
  # Validate inputs
 
174
  "Content-Type": "application/json"
175
  }
176
 
177
+ doctor_url = f"{BACKEND_URL.rstrip('/')}/auth/admin/doctors"
178
  logger.debug(f"Sending doctor creation request to {doctor_url} with payload: {payload.dict()}")
179
  async with aiohttp.ClientSession() as session:
180
  async with session.post(
181
  doctor_url,
182
  json=payload.dict(),
183
  headers=headers,
184
+ timeout=15
185
  ) as response:
186
  logger.debug(f"Doctor creation response status: {response.status}, URL: {response.url}")
187
  if response.status == 201:
 
194
  doctor_url,
195
  json=payload.dict(),
196
  headers=headers,
197
+ timeout=15
198
  ) as retry_response:
199
  logger.debug(f"Retry doctor creation response status: {retry_response.status}, URL: {retry_response.url}")
200
  if retry_response.status == 201:
 
215
  def sync_create_doctor(full_name: str, email: str, license_number: str, specialty: str, password: str):
216
  return asyncio.run(async_create_doctor(full_name, email, license_number, specialty, password))
217
 
218
+ # New endpoint for public doctor creation
219
+ @app.post("/create-doctor")
220
+ async def create_doctor(payload: DoctorPayload):
221
+ result = await async_create_doctor(
222
+ full_name=payload.full_name,
223
+ email=payload.email,
224
+ license_number=payload.license_number,
225
+ specialty=payload.specialty,
226
+ password=payload.password
227
+ )
228
+ return {"result": result}
229
+
230
  # Gradio UI
231
  admin_ui = gr.Blocks(
232
  css="""
 
280
 
281
  app = gr.mount_gradio_app(app, admin_ui, path="/admin-auth")
282
 
283
+ # Modified admin dashboard (no authentication)
284
  @app.get("/admin")
285
+ async def admin_dashboard():
286
  logger.debug("Admin dashboard accessed")
287
+ return RedirectResponse(url="/admin-auth", status_code=307)
 
 
 
 
 
 
 
 
288
 
289
  @app.get("/admin-auth/gradio_api/queue/data")
290
  async def gradio_queue_data(session_hash: str):