import nltk import numpy as np from nltk.stem.porter import PorterStemmer import json import torch import torch.nn as nn from torch.utils.data import Dataset, DataLoader import random import gradio as gr with open('dataset.json', 'r') as file: dataset = json.load(file) class NeuralNetwork(nn.Module): def __init__(self, input_size, hidden_size, num_classes): super(NeuralNetwork, self).__init__() self.l1 = nn.Linear(input_size, hidden_size) self.l2 = nn.Linear(hidden_size, hidden_size) self.l3 = nn.Linear(hidden_size, num_classes) self.relu = nn.ReLU() def forward(self, x): out = self.l1(x) out = self.relu(out) out = self.l2(out) out = self.relu(out) out = self.l3(out) return out data = torch.load('model_chatbot.pth', map_location=torch.device('cpu')) input_size = data["input_size"] hidden_size = data["hidden_size"] output_size = data["output_size"] all_words = data["all_words"] tags = data["tags"] model_state = data["model_state"] device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') model = NeuralNetwork(input_size, hidden_size, output_size).to(device) model.load_state_dict(model_state) model.eval() # Fungsi untuk tokenisasi, stemming, dan bag-of-words def tokenize(sentence): return nltk.word_tokenize(sentence) def stem(word): return PorterStemmer().stem(word.lower()) def bag_of_words(tokenized_sentence, all_words): tokenized_sentence = [stem(w) for w in tokenized_sentence] bag = np.zeros(len(all_words), dtype=np.float32) for idx, w in enumerate(all_words): if w in tokenized_sentence: bag[idx] = 1.0 return bag # Fungsi prediksi def predict(sentence): sentence = tokenize(sentence) X = bag_of_words(sentence, all_words) X = X.reshape(1, X.shape[0]) X = torch.from_numpy(X).to(device) output = model(X) _, predicted = torch.max(output, dim=1) tag = tags[predicted.item()] probs = torch.softmax(output, dim=1) prob = probs[0][predicted.item()] if prob.item() > 0.75: for intent in dataset['intents']: if tag == intent["tag"]: return random.choice(intent['response']) else: return "Saya tidak mengerti pertanyaan Anda." # Buat interface Gradio iface = gr.Interface(fn=predict, inputs="text", outputs="markdown", title="Chatbot Hukum Pajak") # Jalankan interface iface.launch(share=True)