File size: 8,118 Bytes
a7eb20d
d411917
3ea2bf9
57cd8cf
3ea2bf9
a7eb20d
 
 
3ea2bf9
 
57cd8cf
3ea2bf9
e97e0b7
 
 
 
3ea2bf9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e97e0b7
 
 
345d10b
3ea2bf9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a7eb20d
3ea2bf9
 
a7eb20d
 
3ea2bf9
a7eb20d
3ea2bf9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a7eb20d
 
3ea2bf9
a7eb20d
74b0cea
9d56b27
74b0cea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9d56b27
3ea2bf9
 
74b0cea
9d56b27
3ea2bf9
9d56b27
74b0cea
 
 
9d56b27
 
74b0cea
9d56b27
74b0cea
 
 
 
3ea2bf9
9d56b27
74b0cea
9d56b27
3ea2bf9
 
9d56b27
3ea2bf9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9d56b27
3ea2bf9
 
 
 
9d56b27
 
3ea2bf9
 
9d56b27
3ea2bf9
 
 
 
 
 
 
 
 
 
 
 
9d56b27
3ea2bf9
 
 
 
57cd8cf
3ea2bf9
 
 
 
 
 
 
 
57cd8cf
 
3ea2bf9
 
 
 
 
 
 
57cd8cf
3ea2bf9
 
 
 
57cd8cf
 
3ea2bf9
 
 
 
 
 
 
57cd8cf
 
3ea2bf9
 
 
 
 
 
 
57cd8cf
3ea2bf9
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import os
import shutil
from fastapi import FastAPI, Request, HTTPException, Depends
from fastapi.responses import JSONResponse
from fastapi.middleware.cors import CORSMiddleware
from sentence_transformers import SentenceTransformer, util
import torch
import requests
from typing import List, Dict, Optional
from pydantic import BaseModel

# Rate limiting
from slowapi import Limiter, _rate_limit_exceeded_handler
from slowapi.util import get_remote_address
from slowapi.errors import RateLimitExceeded

# Configuration
class Config:
    SUPABASE_URL = "https://olbjfxlclotxtnpjvpfj.supabase.co"
    SUPABASE_KEY = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Im9sYmpmeGxjbG90eHRucGp2cGZqIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NTIyMzYwMDEsImV4cCI6MjA2NzgxMjAwMX0.7q_o5DCFEAAysnWXMChH4MI5qNhIVc4OgpT5JvgYxc0"
    MODEL_NAME = "sentence-transformers/paraphrase-MiniLM-L3-v2"
    SIMILARITY_THRESHOLD = 0.7
    HF_CACHE = "/tmp/hf"
    RATE_LIMIT = "10/minute"

# Initialize FastAPI
app = FastAPI(title="Biruu Chatbot API", version="1.0.0")

# CORS Middleware
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_methods=["*"],
    allow_headers=["*"],
)

# Rate Limiter
limiter = Limiter(key_func=get_remote_address)
app.state.limiter = limiter
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)

# Setup Hugging Face cache
os.makedirs(Config.HF_CACHE, exist_ok=True)
os.environ["TRANSFORMERS_CACHE"] = Config.HF_CACHE
os.environ["HF_HOME"] = Config.HF_CACHE

# Clean up locked cache
lock_file = f"{Config.HF_CACHE}/models--{Config.MODEL_NAME.replace('/', '--')}.lock"
if os.path.exists(lock_file):
    os.remove(lock_file)
model_cache = f"{Config.HF_CACHE}/models--{Config.MODEL_NAME.replace('/', '--')}"
if os.path.exists(model_cache):
    shutil.rmtree(model_cache, ignore_errors=True)

# Initialize model
try:
    model = SentenceTransformer(Config.MODEL_NAME)
except Exception as e:
    raise RuntimeError(f"Failed to load model: {str(e)}")

# Pydantic Models
class ChatMessage(BaseModel):
    admin_id: str
    session_id: str
    is_bot: bool
    is_admin: bool
    message: str

class DeleteRequest(BaseModel):
    id: Optional[str] = None
    uid: Optional[str] = None

# Helper Functions
def make_supabase_request(
    method: str,
    endpoint: str,
    params: Optional[Dict] = None,
    data: Optional[Dict] = None
) -> requests.Response:
    """Generic function to make Supabase API requests"""
    url = f"{Config.SUPABASE_URL}{endpoint}"
    headers = {
        "apikey": Config.SUPABASE_KEY,
        "Authorization": f"Bearer {Config.SUPABASE_KEY}",
        "Content-Type": "application/json"
    }
    
    try:
        if method == "GET":
            response = requests.get(url, headers=headers, params=params)
        elif method == "POST":
            response = requests.post(url, headers=headers, json=data)
        elif method == "DELETE":
            response = requests.delete(url, headers=headers)
        else:
            raise ValueError("Unsupported HTTP method")
        
        response.raise_for_status()
        return response
    except requests.exceptions.RequestException as e:
        raise HTTPException(
            status_code=500,
            detail=f"Supabase request failed: {str(e)}"
        )

def get_faq_from_supabase(admin_id: str) -> List[Dict]:
    """Get FAQ items for a specific admin"""
    try:
        response = make_supabase_request(
            "GET",
            "/rest/v1/faq_items",
            params={"admin_id": f"eq.{admin_id}"}
        )
        return response.json()
    except HTTPException:
        return []

# API Endpoints
@app.post("/predict")
async def predict(request: Request):
    try:
        body = await request.json()
        admin_id = body.get("data", [None, None])[0]
        question = body.get("data", [None, None])[1]

        if not admin_id or not question:
            return JSONResponse(
                {"data": ["Admin ID atau pertanyaan tidak valid"]},
                status_code=400
            )

        # Get FAQs for this admin
        faqs = get_faq_from_supabase(admin_id)
        if not faqs:
            return {"data": ["Maaf, belum ada FAQ yang tersedia."]}

        # Process question
        questions = [f["question"] for f in faqs]
        answers = [f["answer"] for f in faqs]

        # Get embeddings
        embeddings = model.encode(questions, convert_to_tensor=True)
        query_embedding = model.encode(question, convert_to_tensor=True)
        
        # Calculate similarity
        similarity = util.pytorch_cos_sim(query_embedding, embeddings)
        best_idx = torch.argmax(similarity).item()
        best_score = similarity[0][best_idx].item()
        
        # Threshold similarity (minimal 0.3)
        if best_score < 0.3:
            return {"data": ["Maaf, saya tidak mengerti pertanyaan Anda"]}
            
        return {"data": [answers[best_idx]]}

    except Exception as e:
        print(f"Error in prediction: {str(e)}")
        return JSONResponse(
            {"data": ["Terjadi kesalahan saat memproses pertanyaan"]},
            status_code=500
        )


@app.post("/save_chat")
async def save_chat(chat: ChatMessage):
    """Save chat message to database"""
    try:
        response = make_supabase_request(
            "POST",
            "/rest/v1/chat_logs",
            data={
                "admin_id": chat.admin_id,
                "session_id": chat.session_id,
                "is_bot": chat.is_bot,
                "is_admin": chat.is_admin,
                "message": chat.message
            }
        )
        saved_data = response.json()[0]
        return {
            "message": "Pesan berhasil disimpan",
            "id": saved_data["id"]
        }
    except HTTPException as e:
        raise e
    except Exception as e:
        raise HTTPException(
            status_code=500,
            detail=f"Failed to save chat: {str(e)}"
        )

@app.get("/chat_history")
async def get_chat_history(admin_id: str, session_id: str):
    """Get chat history for specific session"""
    try:
        response = make_supabase_request(
            "GET",
            "/rest/v1/chat_logs",
            params={
                "admin_id": f"eq.{admin_id}",
                "or": f"(session_id.eq.{session_id},is_bot.eq.true)",
                "order": "created_at.asc"
            }
        )
        return response.json()
    except HTTPException as e:
        raise e
    except Exception as e:
        raise HTTPException(
            status_code=500,
            detail=f"Failed to get chat history: {str(e)}"
        )

@app.post("/delete_chat")
async def delete_chat(request: DeleteRequest):
    """Delete specific chat message"""
    if not request.id:
        raise HTTPException(
            status_code=400,
            detail="Message ID is required"
        )

    try:
        make_supabase_request(
            "DELETE",
            f"/rest/v1/chat_logs?id=eq.{request.id}"
        )
        return {"message": f"Pesan dengan ID {request.id} berhasil dihapus."}
    except HTTPException as e:
        raise e
    except Exception as e:
        raise HTTPException(
            status_code=500,
            detail=f"Failed to delete message: {str(e)}"
        )

@app.post("/delete_all_by_uid")
async def delete_all_by_uid(request: DeleteRequest):
    """Delete all messages for specific user"""
    if not request.uid:
        raise HTTPException(
            status_code=400,
            detail="UID is required"
        )

    try:
        make_supabase_request(
            "DELETE",
            f"/rest/v1/chat_logs?admin_id=eq.{request.uid}"
        )
        return {"message": f"Semua pesan untuk UID {request.uid} berhasil dihapus."}
    except HTTPException as e:
        raise e
    except Exception as e:
        raise HTTPException(
            status_code=500,
            detail=f"Failed to delete messages: {str(e)}"
        )

@app.get("/health")
async def health_check():
    """Health check endpoint"""
    return {"status": "healthy", "version": "1.0.0"}