File size: 1,837 Bytes
7b5193c
 
305579a
 
7b5193c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
305579a
7b5193c
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
55
56
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
import gradio as gr

# Example data
train_queries = [
    "How do I activate my card?",
    "What is the age limit for opening an account?",
    "Do you support Apple Pay or Google Pay?",
]
train_labels = [0, 1, 2]

responses = {
    0: "To activate your card, please go to the app's settings.",
    1: "The age limit for opening an account is 18 years.",
    2: "Yes, we support Apple Pay and Google Pay.",
}

label_to_intent = {0: "activate_my_card", 1: "age_limit", 2: "apple_pay_or_google_pay"}

# Prepare the Naive Bayes model
vectorizer = CountVectorizer()
X_train = vectorizer.fit_transform(train_queries)

clf = MultinomialNB()
clf.fit(X_train, train_labels)

# Define the chatbot response function
def naive_bayes_response(user_input):
    vectorized_input = vectorizer.transform([user_input])
    predicted_label = clf.predict(vectorized_input)[0]
    return responses.get(predicted_label, "Sorry, I couldn't understand your query.")

# Define Gradio interface
def chatbot_interface(user_input):
    return naive_bayes_response(user_input)

# UI design with Gradio
with gr.Blocks() as demo:
    gr.Markdown("# Naive Bayes Chatbot")
    gr.Markdown("This is a chatbot powered by Naive Bayes that handles basic queries.")
    with gr.Row():
        with gr.Column():
            user_input = gr.Textbox(
                label="Your Query",
                placeholder="Type your question here...",
                lines=1,
            )
            submit_btn = gr.Button("Submit")
        with gr.Column():
            response = gr.Textbox(label="Chatbot Response", interactive=False)
    submit_btn.click(chatbot_interface, inputs=user_input, outputs=response)

# Run the app
if __name__ == "__main__":
    demo.launch()