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("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()