Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,18 +1,53 @@
|
|
|
|
|
|
|
|
|
|
1 |
import csv
|
2 |
|
3 |
-
#
|
4 |
-
|
5 |
-
|
|
|
|
|
|
|
6 |
|
7 |
-
#
|
8 |
-
|
9 |
-
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
from sklearn.feature_extraction.text import CountVectorizer
|
3 |
+
from sklearn.naive_bayes import MultinomialNB
|
4 |
+
import gradio as gr
|
5 |
import csv
|
6 |
|
7 |
+
# Load Dataset
|
8 |
+
try:
|
9 |
+
df = pd.read_csv("cleaned_dataset.csv")
|
10 |
+
except FileNotFoundError:
|
11 |
+
data = {"pertanyaan": [], "jawaban": []}
|
12 |
+
df = pd.DataFrame(data)
|
13 |
|
14 |
+
# Preprocessing Data
|
15 |
+
vectorizer = CountVectorizer()
|
16 |
+
if not df.empty:
|
17 |
+
X = vectorizer.fit_transform(df['pertanyaan'])
|
18 |
+
y = df['jawaban']
|
19 |
+
model = MultinomialNB()
|
20 |
+
model.fit(X, y)
|
21 |
+
else:
|
22 |
+
model = None
|
23 |
|
24 |
+
# Fungsi Chatbot
|
25 |
+
def chatbot_respon(user_input):
|
26 |
+
if model:
|
27 |
+
try:
|
28 |
+
input_vec = vectorizer.transform([user_input])
|
29 |
+
response = model.predict(input_vec)[0]
|
30 |
+
except:
|
31 |
+
response = "Maaf, aku belum memahami pertanyaan ini."
|
32 |
+
log_input(user_input)
|
33 |
+
else:
|
34 |
+
response = "Model belum dilatih. Silakan tambahkan dataset."
|
35 |
+
return response
|
36 |
|
37 |
+
# Log Pertanyaan Baru
|
38 |
+
def log_input(user_input):
|
39 |
+
with open("chat_log.csv", "a", newline="") as file:
|
40 |
+
writer = csv.writer(file)
|
41 |
+
writer.writerow([user_input, ""])
|
42 |
+
|
43 |
+
# Gradio Interface
|
44 |
+
interface = gr.Interface(
|
45 |
+
fn=chatbot_respon,
|
46 |
+
inputs=gr.Textbox(lines=2, placeholder="Tanyakan sesuatu..."),
|
47 |
+
outputs="text",
|
48 |
+
title="IndoBot AI",
|
49 |
+
description="Chatbot berbasis bahasa Indonesia dengan kemampuan belajar dari log percakapan."
|
50 |
+
)
|
51 |
+
|
52 |
+
if __name__ == "__main__":
|
53 |
+
interface.launch()
|