Spaces:
Sleeping
Sleeping
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() |