Ogghey commited on
Commit
aad84fe
·
verified ·
1 Parent(s): fdb32c3

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -0
app.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from sentence_transformers import SentenceTransformer, util
3
+ import torch
4
+
5
+ model = SentenceTransformer('all-MiniLM-L6-v2')
6
+
7
+ # Dummy FAQ user
8
+ faq = {
9
+ "uid123": [
10
+ {"q": "Apakah bisa COD?", "a": "Bisa, seluruh Indonesia"},
11
+ {"q": "Apakah ada garansi?", "a": "Garansi 7 hari"},
12
+ ]
13
+ }
14
+
15
+ def chatbot(uid, question):
16
+ user_faq = faq.get(uid, [])
17
+ if not user_faq:
18
+ return "FAQ belum tersedia."
19
+
20
+ questions = [item["q"] for item in user_faq]
21
+ answers = [item["a"] for item in user_faq]
22
+
23
+ embeddings = model.encode(questions, convert_to_tensor=True)
24
+ query_embedding = model.encode(question, convert_to_tensor=True)
25
+
26
+ similarity = util.pytorch_cos_sim(query_embedding, embeddings)
27
+ best_idx = torch.argmax(similarity).item()
28
+
29
+ return answers[best_idx]
30
+
31
+ # Gradio Interface (bisa dipanggil via API juga)
32
+ iface = gr.Interface(
33
+ fn=chatbot,
34
+ inputs=["text", "text"],
35
+ outputs="text",
36
+ examples=[["uid123", "Apakah bisa bayar di tempat?"]],
37
+ allow_flagging="never",
38
+ title="Biruu Chatbot API"
39
+ )
40
+
41
+ iface.launch()