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