Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,66 +1,16 @@
|
|
| 1 |
import os
|
| 2 |
import shutil
|
| 3 |
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"
|
| 10 |
-
os.environ["TRANSFORMERS_CACHE"] = HF_CACHE
|
| 11 |
-
os.environ["HF_HOME"] = HF_CACHE
|
| 12 |
-
os.makedirs(HF_CACHE, exist_ok=True)
|
| 13 |
-
|
| 14 |
-
# ✅ Bersihkan cache jika terkunci
|
| 15 |
-
if os.path.exists(f"{HF_CACHE}/models--sentence-transformers--paraphrase-MiniLM-L3-v2.lock"):
|
| 16 |
-
os.remove(f"{HF_CACHE}/models--sentence-transformers--paraphrase-MiniLM-L3-v2.lock")
|
| 17 |
-
if os.path.exists(f"{HF_CACHE}/models--sentence-transformers--paraphrase-MiniLM-L3-v2"):
|
| 18 |
-
shutil.rmtree(f"{HF_CACHE}/models--sentence-transformers--paraphrase-MiniLM-L3-v2", ignore_errors=True)
|
| 19 |
-
|
| 20 |
-
# 🔐 Supabase
|
| 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 |
-
|
| 27 |
app = FastAPI()
|
| 28 |
|
| 29 |
-
def get_faq_from_supabase(uid):
|
| 30 |
-
url = f"{SUPABASE_URL}/rest/v1/faq_texts?uid=eq.{uid}"
|
| 31 |
-
headers = {
|
| 32 |
-
"apikey": SUPABASE_KEY,
|
| 33 |
-
"Authorization": f"Bearer {SUPABASE_KEY}",
|
| 34 |
-
"Content-Type": "application/json"
|
| 35 |
-
}
|
| 36 |
-
try:
|
| 37 |
-
r = requests.get(url, headers=headers)
|
| 38 |
-
r.raise_for_status()
|
| 39 |
-
data = r.json()
|
| 40 |
-
return [{"q": d["question"], "a": d["answer"]} for d in data]
|
| 41 |
-
except Exception as e:
|
| 42 |
-
print("❌ Supabase error:", e)
|
| 43 |
-
return []
|
| 44 |
-
|
| 45 |
@app.post("/predict")
|
| 46 |
async def predict(request: Request):
|
| 47 |
body = await request.json()
|
| 48 |
-
uid
|
| 49 |
|
| 50 |
-
if not uid
|
| 51 |
-
return {"data": ["UID
|
| 52 |
-
|
| 53 |
-
faqs = get_faq_from_supabase(uid)
|
| 54 |
-
if not faqs:
|
| 55 |
-
return {"data": ["FAQ tidak ditemukan untuk UID ini."]}
|
| 56 |
-
|
| 57 |
-
questions = [f["q"] for f in faqs]
|
| 58 |
-
answers = [f["a"] for f in faqs]
|
| 59 |
-
|
| 60 |
-
embeddings = model.encode(questions, convert_to_tensor=True)
|
| 61 |
-
query_embedding = model.encode(question, convert_to_tensor=True)
|
| 62 |
-
|
| 63 |
-
similarity = util.pytorch_cos_sim(query_embedding, embeddings)
|
| 64 |
-
best_idx = torch.argmax(similarity).item()
|
| 65 |
|
| 66 |
-
return {"data": [
|
|
|
|
| 1 |
import os
|
| 2 |
import shutil
|
| 3 |
from fastapi import FastAPI, Request
|
|
|
|
|
|
|
| 4 |
import requests
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
app = FastAPI()
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
@app.post("/predict")
|
| 9 |
async def predict(request: Request):
|
| 10 |
body = await request.json()
|
| 11 |
+
uid = body.get("data", [None])
|
| 12 |
|
| 13 |
+
if not uid:
|
| 14 |
+
return {"data": ["UID not valid."]}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
return {"data": []}
|