Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,45 +1,26 @@
|
|
| 1 |
-
import os
|
| 2 |
-
os.environ["TRANSFORMERS_CACHE"] = "/tmp/hf"
|
| 3 |
-
os.makedirs("/tmp/hf", exist_ok=True)
|
| 4 |
|
| 5 |
-
|
| 6 |
-
import torch
|
| 7 |
-
import requests
|
| 8 |
-
from fastapi import FastAPI, Request
|
| 9 |
|
| 10 |
-
|
| 11 |
-
SUPABASE_URL = "https://olbjfxlclotxtnpjvpfj.supabase.co"
|
| 12 |
-
SUPABASE_KEY = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
# π FastAPI app
|
| 18 |
app = FastAPI()
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
"Authorization": f"Bearer {SUPABASE_KEY}",
|
| 26 |
-
"Content-Type": "application/json"
|
| 27 |
-
}
|
| 28 |
-
try:
|
| 29 |
-
r = requests.get(url, headers=headers)
|
| 30 |
-
r.raise_for_status()
|
| 31 |
-
data = r.json()
|
| 32 |
-
return [{"q": d["question"], "a": d["answer"]} for d in data]
|
| 33 |
-
except Exception as e:
|
| 34 |
-
print("β Supabase error:", e)
|
| 35 |
-
return []
|
| 36 |
-
|
| 37 |
-
@app.post("/predict")
|
| 38 |
-
async def predict(request: Request):
|
| 39 |
-
body = await request.json()
|
| 40 |
-
uid, question = body.get("data", [None, None])
|
| 41 |
-
|
| 42 |
-
if not uid or not question:
|
| 43 |
return {"data": ["UID atau pertanyaan tidak valid."]}
|
| 44 |
|
| 45 |
faqs = get_faq_from_supabase(uid)
|
|
@@ -55,4 +36,8 @@ async def predict(request: Request):
|
|
| 55 |
similarity = util.pytorch_cos_sim(query_embedding, embeddings)
|
| 56 |
best_idx = torch.argmax(similarity).item()
|
| 57 |
|
| 58 |
-
return {"data": [answers[best_idx]]}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os from fastapi import FastAPI, Request from sentence_transformers import SentenceTransformer, util import torch import requests
|
|
|
|
|
|
|
| 2 |
|
| 3 |
+
β
Pastikan cache model tersimpan di lokasi yang bisa ditulis
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
os.environ["TRANSFORMERS_CACHE"] = "/tmp/hf" os.makedirs("/tmp/hf", exist_ok=True)
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
π Supabase config
|
| 8 |
+
|
| 9 |
+
SUPABASE_URL = "https://olbjfxlclotxtnpjvpfj.supabase.co" SUPABASE_KEY = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Im9sYmpmeGxjbG90eHRucGp2cGZqIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NTIyMzYwMDEsImV4cCI6MjA2NzgxMjAwMX0.7q_o5DCFEAAysnWXMChH4MI5qNhIVc4OgpT5JvgYxc0"
|
| 10 |
+
|
| 11 |
+
β
Load model (gunakan versi lebih kecil untuk pengujian)
|
| 12 |
+
|
| 13 |
+
model = SentenceTransformer("paraphrase-MiniLM-L3-v2")
|
| 14 |
+
|
| 15 |
+
π FastAPI app
|
| 16 |
|
|
|
|
| 17 |
app = FastAPI()
|
| 18 |
|
| 19 |
+
def get_faq_from_supabase(uid): url = f"{SUPABASE_URL}/rest/v1/faq_texts?uid=eq.{uid}" headers = { "apikey": SUPABASE_KEY, "Authorization": f"Bearer {SUPABASE_KEY}", "Content-Type": "application/json" } try: r = requests.get(url, headers=headers) r.raise_for_status() data = r.json() return [{"q": d["question"], "a": d["answer"]} for d in data] except Exception as e: print("β Supabase error:", e) return []
|
| 20 |
+
|
| 21 |
+
@app.post("/predict") async def predict(request: Request): try: body = await request.json() uid, question = body.get("data", [None, None])
|
| 22 |
+
|
| 23 |
+
if not uid or not question:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
return {"data": ["UID atau pertanyaan tidak valid."]}
|
| 25 |
|
| 26 |
faqs = get_faq_from_supabase(uid)
|
|
|
|
| 36 |
similarity = util.pytorch_cos_sim(query_embedding, embeddings)
|
| 37 |
best_idx = torch.argmax(similarity).item()
|
| 38 |
|
| 39 |
+
return {"data": [answers[best_idx]]}
|
| 40 |
+
|
| 41 |
+
except Exception as e:
|
| 42 |
+
print("β Error processing request:", e)
|
| 43 |
+
return {"data": ["Terjadi kesalahan pada server."]}
|