Ogghey's picture
Update app.py
d669e15 verified
raw
history blame
2 kB
import os from fastapi import FastAPI, Request from sentence_transformers import SentenceTransformer, util import torch import requests
βœ… Pastikan cache model tersimpan di lokasi yang bisa ditulis
os.environ["TRANSFORMERS_CACHE"] = "/tmp/hf" os.makedirs("/tmp/hf", exist_ok=True)
πŸ” Supabase config
SUPABASE_URL = "https://olbjfxlclotxtnpjvpfj.supabase.co" SUPABASE_KEY = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Im9sYmpmeGxjbG90eHRucGp2cGZqIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NTIyMzYwMDEsImV4cCI6MjA2NzgxMjAwMX0.7q_o5DCFEAAysnWXMChH4MI5qNhIVc4OgpT5JvgYxc0"
βœ… Load model (gunakan versi lebih kecil untuk pengujian)
model = SentenceTransformer("paraphrase-MiniLM-L3-v2")
πŸš€ FastAPI app
app = FastAPI()
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 []
@app.post("/predict") async def predict(request: Request): try: body = await request.json() uid, question = body.get("data", [None, None])
if not uid or not question:
return {"data": ["UID atau pertanyaan tidak valid."]}
faqs = get_faq_from_supabase(uid)
if not faqs:
return {"data": ["FAQ tidak ditemukan untuk UID ini."]}
questions = [f["q"] for f in faqs]
answers = [f["a"] for f in faqs]
embeddings = model.encode(questions, convert_to_tensor=True)
query_embedding = model.encode(question, convert_to_tensor=True)
similarity = util.pytorch_cos_sim(query_embedding, embeddings)
best_idx = torch.argmax(similarity).item()
return {"data": [answers[best_idx]]}
except Exception as e:
print("❌ Error processing request:", e)
return {"data": ["Terjadi kesalahan pada server."]}