Spaces:
Sleeping

RillSvai commited on
Commit
67aa9ab
·
1 Parent(s): 59e584b

add global auth

Browse files
Files changed (1) hide show
  1. app.py +13 -2
app.py CHANGED
@@ -11,7 +11,7 @@ from pyannote.audio import Pipeline
11
  import openai
12
  import logging
13
  from datetime import datetime
14
- from fastapi import FastAPI, HTTPException, BackgroundTasks
15
 
16
  logging.basicConfig(
17
  level=logging.INFO,
@@ -31,6 +31,12 @@ openai_client = openai.Client(api_key=OPENAI_API_KEY) if OPENAI_API_KEY else Non
31
 
32
  progress_store = {}
33
 
 
 
 
 
 
 
34
  class DiarizeRequest(BaseModel):
35
  audio_source: str
36
  speakers_quantity: Optional[int] = 2
@@ -97,6 +103,11 @@ def download_from_url(url: str) -> bytes:
97
  logger.error(f"Error downloading file from URL: {e}")
98
  raise ValueError(f"Failed to download file from URL: {e}")
99
 
 
 
 
 
 
100
  def process_diarization(
101
  job_id: str,
102
  speakers_quantity: int,
@@ -207,7 +218,7 @@ def process_diarization(
207
  raise
208
 
209
  @app.post("/diarize")
210
- async def diarize_endpoint(diarize_request: DiarizeRequest, background_tasks: BackgroundTasks):
211
  """FastAPI endpoint for diarization"""
212
  try:
213
  job_id = f"job_{int(time.time())}"
 
11
  import openai
12
  import logging
13
  from datetime import datetime
14
+ from fastapi import FastAPI, HTTPException, BackgroundTasks, HTTPBearer, HTTPAuthorizationCredentials, Depends
15
 
16
  logging.basicConfig(
17
  level=logging.INFO,
 
31
 
32
  progress_store = {}
33
 
34
+ AUTH_TOKEN = os.getenv("API_AUTH_TOKEN")
35
+ if not AUTH_TOKEN:
36
+ raise EnvironmentError("API_AUTH_TOKEN is not set")
37
+
38
+ security = HTTPBearer()
39
+
40
  class DiarizeRequest(BaseModel):
41
  audio_source: str
42
  speakers_quantity: Optional[int] = 2
 
103
  logger.error(f"Error downloading file from URL: {e}")
104
  raise ValueError(f"Failed to download file from URL: {e}")
105
 
106
+
107
+ def authenticate_bearer_token(credentials: HTTPAuthorizationCredentials = Depends(security)):
108
+ if credentials.credentials != AUTH_TOKEN:
109
+ raise HTTPException(status_code=401, detail="Not authenticated")
110
+
111
  def process_diarization(
112
  job_id: str,
113
  speakers_quantity: int,
 
218
  raise
219
 
220
  @app.post("/diarize")
221
+ async def diarize_endpoint(diarize_request: DiarizeRequest, background_tasks: BackgroundTasks, credentials: HTTPAuthorizationCredentials = Depends(authenticate_bearer_token)):
222
  """FastAPI endpoint for diarization"""
223
  try:
224
  job_id = f"job_{int(time.time())}"