Spaces:
Sleeping
Sleeping
File size: 1,855 Bytes
37ed814 6fddb01 37ed814 6723584 37ed814 6723584 37ed814 6723584 37ed814 6723584 37ed814 6fddb01 6723584 |
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 |
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
import gradio as gr
# Load Model Pre-trained (BERT)
MODEL_NAME = "indobenchmark/indobert-base-p2"
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME, num_labels=2)
# Pipeline untuk prediksi teks
classifier = pipeline("text-classification", model=model, tokenizer=tokenizer)
# Fungsi Chatbot
def chatbot_respon(user_input):
# Predefined responses based on intent
predefined_responses = {
"halo": "Hai juga! Ada yang bisa aku bantu?",
"apa kabar": "Aku baik, bagaimana dengan kamu?",
"siapa namamu": "Aku adalah IndoBot AI, teman bicaramu.",
"ceritakan lelucon": "Kenapa ayam menyeberang jalan? Untuk ke sisi lain!"
}
# Cari respons di predefined
for key, response in predefined_responses.items():
if key in user_input.lower():
return response
# Jika tidak ada di predefined, gunakan model
prediction = classifier(user_input)[0]
label = prediction['label']
confidence = prediction['score']
if confidence > 0.7: # Threshold confidence
if label == "LABEL_0":
return "Aku tidak yakin dengan pertanyaanmu, bisakah kamu menjelaskannya lebih lanjut?"
elif label == "LABEL_1":
return "Tentu! Aku bisa membantu menjelaskan topik ini lebih jauh."
return "Maaf, aku tidak mengerti pertanyaanmu."
# Gradio Interface
interface = gr.Interface(
fn=chatbot_respon,
inputs=gr.Textbox(lines=2, placeholder="Tanyakan sesuatu..."),
outputs="text",
title="IndoBot AI - Lebih Pintar",
description="IndoBot AI adalah chatbot berbasis bahasa Indonesia dengan pemahaman lebih mendalam. Tanyakan sesuatu!"
)
if __name__ == "__main__":
interface.launch()
|