tonyassi commited on
Commit
e7f0c14
·
verified ·
1 Parent(s): 63d78b8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -0
app.py CHANGED
@@ -31,6 +31,28 @@ SYSTEM_PROMPT = (
31
  "Don't repeat yourself too much.\n"
32
  )
33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  # STT (we chose base.en)
35
  WHISPER_MODEL_NAME = os.environ.get("WHISPER_MODEL", "base.en")
36
  WHISPER_DEVICE = os.environ.get("WHISPER_DEVICE", "cpu")
@@ -205,6 +227,10 @@ def health():
205
 
206
  @app.post("/v1/chat")
207
  def chat_text():
 
 
 
 
208
  t0 = time.time()
209
  ip = _client_ip()
210
 
@@ -249,6 +275,10 @@ def tts_only():
249
  Timing headers:
250
  X-TTS-MS, X-TOTAL-MS
251
  """
 
 
 
 
252
  ip = _client_ip()
253
  t0 = time.time()
254
 
@@ -301,6 +331,10 @@ def utterance_audio_to_audio():
301
  Timing headers:
302
  X-STT-MS, X-LLM-MS, X-TTS-MS, X-TOTAL-MS
303
  """
 
 
 
 
304
  t0 = time.time()
305
  ip = _client_ip()
306
 
@@ -413,6 +447,10 @@ def utterance_audio_to_audio():
413
 
414
  @app.post("/v1/reset")
415
  def reset():
 
 
 
 
416
  ip = _client_ip()
417
  print(f"[/v1/reset] {time.strftime('%Y-%m-%d %H:%M:%S')} ip={ip} clearing mem (was {len(HISTORY)}/{MAX_MESSAGES})")
418
  HISTORY.clear()
 
31
  "Don't repeat yourself too much.\n"
32
  )
33
 
34
+
35
+ # -------------------------
36
+ # Auth
37
+ # -------------------------
38
+ API_PASSWORD = os.environ.get("API_PASSWORD", "").strip()
39
+
40
+ def _require_auth():
41
+ """
42
+ Require a shared secret from the client.
43
+ Client must send header: X-API-PASSWORD: <secret>
44
+ """
45
+ if not API_PASSWORD:
46
+ # If you forget to set the secret, fail closed.
47
+ return jsonify({"error": "Server missing API_PASSWORD secret"}), 500
48
+
49
+ provided = (request.headers.get("X-API-PASSWORD") or "").strip()
50
+ if not provided or provided != API_PASSWORD:
51
+ return jsonify({"error": "Unauthorized"}), 401
52
+
53
+ return None
54
+
55
+
56
  # STT (we chose base.en)
57
  WHISPER_MODEL_NAME = os.environ.get("WHISPER_MODEL", "base.en")
58
  WHISPER_DEVICE = os.environ.get("WHISPER_DEVICE", "cpu")
 
227
 
228
  @app.post("/v1/chat")
229
  def chat_text():
230
+ auth_resp = _require_auth()
231
+ if auth_resp:
232
+ return auth_resp
233
+
234
  t0 = time.time()
235
  ip = _client_ip()
236
 
 
275
  Timing headers:
276
  X-TTS-MS, X-TOTAL-MS
277
  """
278
+ auth_resp = _require_auth()
279
+ if auth_resp:
280
+ return auth_resp
281
+
282
  ip = _client_ip()
283
  t0 = time.time()
284
 
 
331
  Timing headers:
332
  X-STT-MS, X-LLM-MS, X-TTS-MS, X-TOTAL-MS
333
  """
334
+ auth_resp = _require_auth()
335
+ if auth_resp:
336
+ return auth_resp
337
+
338
  t0 = time.time()
339
  ip = _client_ip()
340
 
 
447
 
448
  @app.post("/v1/reset")
449
  def reset():
450
+ auth_resp = _require_auth()
451
+ if auth_resp:
452
+ return auth_resp
453
+
454
  ip = _client_ip()
455
  print(f"[/v1/reset] {time.strftime('%Y-%m-%d %H:%M:%S')} ip={ip} clearing mem (was {len(HISTORY)}/{MAX_MESSAGES})")
456
  HISTORY.clear()