Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,55 +1,48 @@
|
|
1 |
-
import
|
2 |
-
from sklearn.feature_extraction.text import CountVectorizer
|
3 |
-
from sklearn.naive_bayes import MultinomialNB
|
4 |
import gradio as gr
|
5 |
|
6 |
-
#
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
"Ceritakan lelucon"
|
14 |
-
],
|
15 |
-
"jawaban": [
|
16 |
-
"Hai juga!",
|
17 |
-
"Aku baik, bagaimana dengan kamu?",
|
18 |
-
"Aku adalah IndoBot AI.",
|
19 |
-
"Hobiku membantu orang seperti kamu!",
|
20 |
-
"Kenapa ayam menyeberang jalan? Untuk ke sisi lain!"
|
21 |
-
]
|
22 |
-
}
|
23 |
-
df = pd.DataFrame(data)
|
24 |
-
|
25 |
-
# Preprocessing Data
|
26 |
-
vectorizer = CountVectorizer()
|
27 |
-
X = vectorizer.fit_transform(df['pertanyaan'])
|
28 |
-
y = df['jawaban']
|
29 |
-
|
30 |
-
# Model Klasifikasi
|
31 |
-
model = MultinomialNB()
|
32 |
-
model.fit(X, y)
|
33 |
|
34 |
# Fungsi Chatbot
|
35 |
-
def chatbot_respon(
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
#
|
44 |
-
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
|
|
|
47 |
interface = gr.Interface(
|
48 |
-
fn=
|
49 |
inputs=gr.Textbox(lines=2, placeholder="Tanyakan sesuatu..."),
|
50 |
outputs="text",
|
51 |
-
title="IndoBot AI",
|
52 |
-
description="IndoBot AI adalah chatbot
|
53 |
)
|
54 |
|
55 |
if __name__ == "__main__":
|
|
|
1 |
+
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
|
|
|
|
|
2 |
import gradio as gr
|
3 |
|
4 |
+
# Load Model Pre-trained (BERT)
|
5 |
+
MODEL_NAME = "indobenchmark/indobert-base-p2"
|
6 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
7 |
+
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME, num_labels=2)
|
8 |
+
|
9 |
+
# Pipeline untuk prediksi teks
|
10 |
+
classifier = pipeline("text-classification", model=model, tokenizer=tokenizer)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
# Fungsi Chatbot
|
13 |
+
def chatbot_respon(user_input):
|
14 |
+
# Predefined responses based on intent
|
15 |
+
predefined_responses = {
|
16 |
+
"halo": "Hai juga! Ada yang bisa aku bantu?",
|
17 |
+
"apa kabar": "Aku baik, bagaimana dengan kamu?",
|
18 |
+
"siapa namamu": "Aku adalah IndoBot AI, teman bicaramu.",
|
19 |
+
"ceritakan lelucon": "Kenapa ayam menyeberang jalan? Untuk ke sisi lain!"
|
20 |
+
}
|
21 |
+
# Cari respons di predefined
|
22 |
+
for key, response in predefined_responses.items():
|
23 |
+
if key in user_input.lower():
|
24 |
+
return response
|
25 |
+
|
26 |
+
# Jika tidak ada di predefined, gunakan model
|
27 |
+
prediction = classifier(user_input)[0]
|
28 |
+
label = prediction['label']
|
29 |
+
confidence = prediction['score']
|
30 |
+
|
31 |
+
if confidence > 0.7: # Threshold confidence
|
32 |
+
if label == "LABEL_0":
|
33 |
+
return "Aku tidak yakin dengan pertanyaanmu, bisakah kamu menjelaskannya lebih lanjut?"
|
34 |
+
elif label == "LABEL_1":
|
35 |
+
return "Tentu! Aku bisa membantu menjelaskan topik ini lebih jauh."
|
36 |
+
|
37 |
+
return "Maaf, aku tidak mengerti pertanyaanmu."
|
38 |
|
39 |
+
# Gradio Interface
|
40 |
interface = gr.Interface(
|
41 |
+
fn=chatbot_respon,
|
42 |
inputs=gr.Textbox(lines=2, placeholder="Tanyakan sesuatu..."),
|
43 |
outputs="text",
|
44 |
+
title="IndoBot AI - Lebih Pintar",
|
45 |
+
description="IndoBot AI adalah chatbot berbasis bahasa Indonesia dengan pemahaman lebih mendalam. Tanyakan sesuatu!"
|
46 |
)
|
47 |
|
48 |
if __name__ == "__main__":
|