File size: 1,451 Bytes
f3c58bb
 
 
 
b384df6
6fddb01
f3c58bb
 
 
 
 
 
37ed814
f3c58bb
 
 
 
 
 
 
 
 
eb06370
f3c58bb
 
 
 
 
 
 
 
 
 
 
 
eb06370
f3c58bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pandas as pd
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
import gradio as gr
import csv

# Load Dataset
try:
    df = pd.read_csv("cleaned_dataset.csv")
except FileNotFoundError:
    data = {"pertanyaan": [], "jawaban": []}
    df = pd.DataFrame(data)

# Preprocessing Data
vectorizer = CountVectorizer()
if not df.empty:
    X = vectorizer.fit_transform(df['pertanyaan'])
    y = df['jawaban']
    model = MultinomialNB()
    model.fit(X, y)
else:
    model = None

# Fungsi Chatbot
def chatbot_respon(user_input):
    if model:
        try:
            input_vec = vectorizer.transform([user_input])
            response = model.predict(input_vec)[0]
        except:
            response = "Maaf, aku belum memahami pertanyaan ini."
            log_input(user_input)
    else:
        response = "Model belum dilatih. Silakan tambahkan dataset."
    return response

# Log Pertanyaan Baru
def log_input(user_input):
    with open("chat_log.csv", "a", newline="") as file:
        writer = csv.writer(file)
        writer.writerow([user_input, ""])

# Gradio Interface
interface = gr.Interface(
    fn=chatbot_respon, 
    inputs=gr.Textbox(lines=2, placeholder="Tanyakan sesuatu..."),
    outputs="text",
    title="IndoBot AI",
    description="Chatbot berbasis bahasa Indonesia dengan kemampuan belajar dari log percakapan."
)

if __name__ == "__main__":
    interface.launch()