Sofa321 commited on
Commit
37ed814
·
verified ·
1 Parent(s): 3b3cc34

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -44
app.py CHANGED
@@ -1,55 +1,48 @@
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
 
6
- # Dataset Bahasa Indonesia
7
- data = {
8
- "pertanyaan": [
9
- "Halo",
10
- "Apa kabar?",
11
- "Siapa namamu?",
12
- "Apa hobi kamu?",
13
- "Ceritakan lelucon"
14
- ],
15
- "jawaban": [
16
- "Hai juga!",
17
- "Aku baik, bagaimana dengan kamu?",
18
- "Aku adalah IndoBot AI.",
19
- "Hobiku membantu orang seperti kamu!",
20
- "Kenapa ayam menyeberang jalan? Untuk ke sisi lain!"
21
- ]
22
- }
23
- df = pd.DataFrame(data)
24
-
25
- # Preprocessing Data
26
- vectorizer = CountVectorizer()
27
- X = vectorizer.fit_transform(df['pertanyaan'])
28
- y = df['jawaban']
29
-
30
- # Model Klasifikasi
31
- model = MultinomialNB()
32
- model.fit(X, y)
33
 
34
  # Fungsi Chatbot
35
- def chatbot_respon(input_text):
36
- input_vec = vectorizer.transform([input_text])
37
- try:
38
- response = model.predict(input_vec)[0]
39
- except:
40
- response = "Maaf, aku tidak mengerti pertanyaanmu."
41
- return response
42
-
43
- # Interface Gradio
44
- def gradio_chatbot(user_input):
45
- return chatbot_respon(user_input)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
 
 
47
  interface = gr.Interface(
48
- fn=gradio_chatbot,
49
  inputs=gr.Textbox(lines=2, placeholder="Tanyakan sesuatu..."),
50
  outputs="text",
51
- title="IndoBot AI",
52
- description="IndoBot AI adalah chatbot sederhana yang memahami bahasa Indonesia. Coba tanyakan sesuatu!"
53
  )
54
 
55
  if __name__ == "__main__":
 
1
+ from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
 
 
2
  import gradio as gr
3
 
4
+ # Load Model Pre-trained (BERT)
5
+ MODEL_NAME = "indobenchmark/indobert-base-p2"
6
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
7
+ model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME, num_labels=2)
8
+
9
+ # Pipeline untuk prediksi teks
10
+ classifier = pipeline("text-classification", model=model, tokenizer=tokenizer)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
  # Fungsi Chatbot
13
+ def chatbot_respon(user_input):
14
+ # Predefined responses based on intent
15
+ predefined_responses = {
16
+ "halo": "Hai juga! Ada yang bisa aku bantu?",
17
+ "apa kabar": "Aku baik, bagaimana dengan kamu?",
18
+ "siapa namamu": "Aku adalah IndoBot AI, teman bicaramu.",
19
+ "ceritakan lelucon": "Kenapa ayam menyeberang jalan? Untuk ke sisi lain!"
20
+ }
21
+ # Cari respons di predefined
22
+ for key, response in predefined_responses.items():
23
+ if key in user_input.lower():
24
+ return response
25
+
26
+ # Jika tidak ada di predefined, gunakan model
27
+ prediction = classifier(user_input)[0]
28
+ label = prediction['label']
29
+ confidence = prediction['score']
30
+
31
+ if confidence > 0.7: # Threshold confidence
32
+ if label == "LABEL_0":
33
+ return "Aku tidak yakin dengan pertanyaanmu, bisakah kamu menjelaskannya lebih lanjut?"
34
+ elif label == "LABEL_1":
35
+ return "Tentu! Aku bisa membantu menjelaskan topik ini lebih jauh."
36
+
37
+ return "Maaf, aku tidak mengerti pertanyaanmu."
38
 
39
+ # Gradio Interface
40
  interface = gr.Interface(
41
+ fn=chatbot_respon,
42
  inputs=gr.Textbox(lines=2, placeholder="Tanyakan sesuatu..."),
43
  outputs="text",
44
+ title="IndoBot AI - Lebih Pintar",
45
+ description="IndoBot AI adalah chatbot berbasis bahasa Indonesia dengan pemahaman lebih mendalam. Tanyakan sesuatu!"
46
  )
47
 
48
  if __name__ == "__main__":