Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -117,52 +117,50 @@ def get_faq_from_supabase(admin_id: str) -> List[Dict]:
|
|
| 117 |
|
| 118 |
# API Endpoints
|
| 119 |
@app.post("/predict")
|
| 120 |
-
async def predict(
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
if not question.strip():
|
| 126 |
-
return {"data": ["Pertanyaan tidak boleh kosong"]}
|
| 127 |
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
|
|
|
|
|
|
| 131 |
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
if
|
| 135 |
-
return {"data": ["
|
| 136 |
-
|
| 137 |
-
return {"data": [answers[best_idx]]}
|
| 138 |
-
except Exception:
|
| 139 |
-
return {"data": ["Sedang ada gangguan teknis, coba lagi nanti"]}
|
| 140 |
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
question_embeddings = model.encode(questions, convert_to_tensor=True)
|
| 148 |
query_embedding = model.encode(question, convert_to_tensor=True)
|
| 149 |
|
| 150 |
# Calculate similarity
|
| 151 |
-
similarity = util.pytorch_cos_sim(query_embedding,
|
| 152 |
best_idx = torch.argmax(similarity).item()
|
| 153 |
best_score = similarity[0][best_idx].item()
|
| 154 |
|
| 155 |
-
#
|
| 156 |
-
if best_score <
|
| 157 |
-
return {"data": ["Maaf, saya tidak mengerti pertanyaan Anda
|
| 158 |
|
| 159 |
return {"data": [answers[best_idx]]}
|
|
|
|
| 160 |
except Exception as e:
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
|
|
|
| 164 |
)
|
| 165 |
|
|
|
|
| 166 |
@app.post("/save_chat")
|
| 167 |
async def save_chat(chat: ChatMessage):
|
| 168 |
"""Save chat message to database"""
|
|
|
|
| 117 |
|
| 118 |
# API Endpoints
|
| 119 |
@app.post("/predict")
|
| 120 |
+
async def predict(request: Request):
|
| 121 |
+
try:
|
| 122 |
+
body = await request.json()
|
| 123 |
+
admin_id = body.get("data", [None, None])[0]
|
| 124 |
+
question = body.get("data", [None, None])[1]
|
|
|
|
|
|
|
| 125 |
|
| 126 |
+
if not admin_id or not question:
|
| 127 |
+
return JSONResponse(
|
| 128 |
+
{"data": ["Admin ID atau pertanyaan tidak valid"]},
|
| 129 |
+
status_code=400
|
| 130 |
+
)
|
| 131 |
|
| 132 |
+
# Get FAQs for this admin
|
| 133 |
+
faqs = get_faq_from_supabase(admin_id)
|
| 134 |
+
if not faqs:
|
| 135 |
+
return {"data": ["Maaf, belum ada FAQ yang tersedia."]}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
|
| 137 |
+
# Process question
|
| 138 |
+
questions = [f["question"] for f in faqs]
|
| 139 |
+
answers = [f["answer"] for f in faqs]
|
| 140 |
|
| 141 |
+
# Get embeddings
|
| 142 |
+
embeddings = model.encode(questions, convert_to_tensor=True)
|
|
|
|
| 143 |
query_embedding = model.encode(question, convert_to_tensor=True)
|
| 144 |
|
| 145 |
# Calculate similarity
|
| 146 |
+
similarity = util.pytorch_cos_sim(query_embedding, embeddings)
|
| 147 |
best_idx = torch.argmax(similarity).item()
|
| 148 |
best_score = similarity[0][best_idx].item()
|
| 149 |
|
| 150 |
+
# Threshold similarity (minimal 0.3)
|
| 151 |
+
if best_score < 0.3:
|
| 152 |
+
return {"data": ["Maaf, saya tidak mengerti pertanyaan Anda"]}
|
| 153 |
|
| 154 |
return {"data": [answers[best_idx]]}
|
| 155 |
+
|
| 156 |
except Exception as e:
|
| 157 |
+
print(f"Error in prediction: {str(e)}")
|
| 158 |
+
return JSONResponse(
|
| 159 |
+
{"data": ["Terjadi kesalahan saat memproses pertanyaan"]},
|
| 160 |
+
status_code=500
|
| 161 |
)
|
| 162 |
|
| 163 |
+
|
| 164 |
@app.post("/save_chat")
|
| 165 |
async def save_chat(chat: ChatMessage):
|
| 166 |
"""Save chat message to database"""
|