Ogghey commited on
Commit
e97e0b7
Β·
verified Β·
1 Parent(s): 4aafef9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -0
app.py CHANGED
@@ -4,6 +4,16 @@ from fastapi import FastAPI, Request
4
  from sentence_transformers import SentenceTransformer, util
5
  import torch
6
  import requests
 
 
 
 
 
 
 
 
 
 
7
 
8
  # πŸ” Paksa semua cache ke path aman di Hugging Face Spaces
9
  HF_CACHE = "/tmp/hf"
@@ -21,6 +31,13 @@ if os.path.exists(f"{HF_CACHE}/models--sentence-transformers--paraphrase-MiniLM-
21
  SUPABASE_URL = "https://olbjfxlclotxtnpjvpfj.supabase.co"
22
  SUPABASE_KEY = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Im9sYmpmeGxjbG90eHRucGp2cGZqIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NTIyMzYwMDEsImV4cCI6MjA2NzgxMjAwMX0.7q_o5DCFEAAysnWXMChH4MI5qNhIVc4OgpT5JvgYxc0"
23
 
 
 
 
 
 
 
 
24
  # πŸ”„ Gunakan model kecil dan cepat
25
  model = SentenceTransformer("sentence-transformers/paraphrase-MiniLM-L3-v2")
26
 
@@ -43,6 +60,7 @@ def get_faq_from_supabase(uid):
43
  return []
44
 
45
  @app.post("/predict")
 
46
  async def predict(request: Request):
47
  body = await request.json()
48
  uid, question = body.get("data", [None, None])
 
4
  from sentence_transformers import SentenceTransformer, util
5
  import torch
6
  import requests
7
+ # Rate_limit
8
+ from slowapi import Limiter, _rate_limit_exceeded_handler
9
+ from slowapi.util import get_remote_address
10
+ from slowapi.errors import RateLimitExceeded
11
+
12
+ # Inisialisasi FastAPI dan Limiter
13
+ limiter = Limiter(key_func=get_remote_address)
14
+ app = FastAPI()
15
+ app.state.limiter = limiter
16
+ app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)
17
 
18
  # πŸ” Paksa semua cache ke path aman di Hugging Face Spaces
19
  HF_CACHE = "/tmp/hf"
 
31
  SUPABASE_URL = "https://olbjfxlclotxtnpjvpfj.supabase.co"
32
  SUPABASE_KEY = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Im9sYmpmeGxjbG90eHRucGp2cGZqIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NTIyMzYwMDEsImV4cCI6MjA2NzgxMjAwMX0.7q_o5DCFEAAysnWXMChH4MI5qNhIVc4OgpT5JvgYxc0"
33
 
34
+
35
+ # Inisialisasi rate limiter dan FastAPI
36
+ limiter = Limiter(key_func=get_remote_address)
37
+ app = FastAPI()
38
+ app.state.limiter = limiter
39
+ app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)
40
+
41
  # πŸ”„ Gunakan model kecil dan cepat
42
  model = SentenceTransformer("sentence-transformers/paraphrase-MiniLM-L3-v2")
43
 
 
60
  return []
61
 
62
  @app.post("/predict")
63
+ @limiter.limit("5/minute")
64
  async def predict(request: Request):
65
  body = await request.json()
66
  uid, question = body.get("data", [None, None])