Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,112 +1,128 @@
|
|
| 1 |
import re
|
| 2 |
import nltk
|
| 3 |
import gradio as gr
|
|
|
|
| 4 |
from nltk.corpus import stopwords
|
| 5 |
-
from nltk.tokenize import word_tokenize
|
| 6 |
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 7 |
from sklearn.metrics.pairwise import cosine_similarity
|
| 8 |
|
| 9 |
-
# Download
|
| 10 |
-
nltk.download("punkt")
|
| 11 |
nltk.download("stopwords")
|
| 12 |
|
| 13 |
-
# -------------------------------
|
| 14 |
# FAQ Dataset
|
| 15 |
-
# -------------------------------
|
| 16 |
|
| 17 |
faq_data = {
|
| 18 |
-
"What is
|
| 19 |
-
"Artificial Intelligence is the simulation of human intelligence by machines.",
|
| 20 |
|
| 21 |
"What is Machine Learning?":
|
| 22 |
-
"Machine Learning is a subset of AI that
|
| 23 |
|
| 24 |
"What is Deep Learning?":
|
| 25 |
-
"Deep Learning is a branch of Machine Learning
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
"Who developed Python?":
|
| 28 |
"Python was created by Guido van Rossum.",
|
| 29 |
|
| 30 |
-
"What is Hugging Face?":
|
| 31 |
-
"Hugging Face is a platform for Machine Learning models and datasets.",
|
| 32 |
-
|
| 33 |
"What is Google Colab?":
|
| 34 |
-
"Google Colab is a free cloud-based Jupyter Notebook
|
| 35 |
-
|
| 36 |
-
"What is NLP?":
|
| 37 |
-
"Natural Language Processing enables computers to understand human language.",
|
| 38 |
|
| 39 |
-
"What is
|
| 40 |
-
"
|
| 41 |
|
| 42 |
"What is Gradio?":
|
| 43 |
-
"Gradio is a Python library for
|
| 44 |
|
| 45 |
"What is cosine similarity?":
|
| 46 |
-
"Cosine similarity measures how similar two
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
}
|
| 48 |
|
| 49 |
-
# -------------------------------
|
| 50 |
# Text Preprocessing
|
| 51 |
-
# -------------------------------
|
| 52 |
|
| 53 |
stop_words = set(stopwords.words("english"))
|
| 54 |
|
| 55 |
def preprocess(text):
|
| 56 |
text = text.lower()
|
| 57 |
-
text = re.sub(r'[^a-zA-Z ]', '', text)
|
| 58 |
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
|
|
|
| 61 |
tokens = [word for word in tokens if word not in stop_words]
|
| 62 |
|
| 63 |
return " ".join(tokens)
|
| 64 |
|
| 65 |
-
# Preprocess Questions
|
| 66 |
questions = list(faq_data.keys())
|
| 67 |
answers = list(faq_data.values())
|
| 68 |
|
| 69 |
processed_questions = [preprocess(q) for q in questions]
|
| 70 |
|
| 71 |
-
#
|
|
|
|
|
|
|
|
|
|
| 72 |
vectorizer = TfidfVectorizer()
|
| 73 |
|
| 74 |
question_vectors = vectorizer.fit_transform(processed_questions)
|
| 75 |
|
| 76 |
-
# -------------------------------
|
| 77 |
# Chatbot Function
|
| 78 |
-
# -------------------------------
|
| 79 |
|
| 80 |
def chatbot(user_input):
|
| 81 |
|
|
|
|
|
|
|
|
|
|
| 82 |
processed_input = preprocess(user_input)
|
| 83 |
|
| 84 |
input_vector = vectorizer.transform([processed_input])
|
| 85 |
|
| 86 |
-
|
| 87 |
|
| 88 |
-
best_match =
|
| 89 |
|
| 90 |
-
score =
|
| 91 |
|
| 92 |
if score < 0.25:
|
| 93 |
return "Sorry, I couldn't find a relevant answer."
|
| 94 |
|
| 95 |
return answers[best_match]
|
| 96 |
|
| 97 |
-
# -------------------------------
|
| 98 |
# Gradio UI
|
| 99 |
-
# -------------------------------
|
| 100 |
|
| 101 |
demo = gr.Interface(
|
| 102 |
fn=chatbot,
|
| 103 |
inputs=gr.Textbox(
|
| 104 |
lines=2,
|
| 105 |
-
placeholder="Ask
|
| 106 |
),
|
| 107 |
-
outputs=
|
| 108 |
title="🤖 FAQ Chatbot",
|
| 109 |
-
description="FAQ Chatbot using NLP, TF-IDF and Cosine Similarity"
|
|
|
|
| 110 |
)
|
| 111 |
|
| 112 |
-
|
|
|
|
|
|
| 1 |
import re
|
| 2 |
import nltk
|
| 3 |
import gradio as gr
|
| 4 |
+
|
| 5 |
from nltk.corpus import stopwords
|
|
|
|
| 6 |
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 7 |
from sklearn.metrics.pairwise import cosine_similarity
|
| 8 |
|
| 9 |
+
# Download only stopwords (no punkt needed)
|
|
|
|
| 10 |
nltk.download("stopwords")
|
| 11 |
|
| 12 |
+
# ---------------------------------
|
| 13 |
# FAQ Dataset
|
| 14 |
+
# ---------------------------------
|
| 15 |
|
| 16 |
faq_data = {
|
| 17 |
+
"What is Artificial Intelligence?":
|
| 18 |
+
"Artificial Intelligence (AI) is the simulation of human intelligence by machines.",
|
| 19 |
|
| 20 |
"What is Machine Learning?":
|
| 21 |
+
"Machine Learning is a subset of AI that enables computers to learn from data.",
|
| 22 |
|
| 23 |
"What is Deep Learning?":
|
| 24 |
+
"Deep Learning is a branch of Machine Learning based on neural networks.",
|
| 25 |
+
|
| 26 |
+
"What is NLP?":
|
| 27 |
+
"Natural Language Processing enables computers to understand human language.",
|
| 28 |
+
|
| 29 |
+
"What is Python?":
|
| 30 |
+
"Python is a popular programming language used in AI, web development, and automation.",
|
| 31 |
|
| 32 |
"Who developed Python?":
|
| 33 |
"Python was created by Guido van Rossum.",
|
| 34 |
|
|
|
|
|
|
|
|
|
|
| 35 |
"What is Google Colab?":
|
| 36 |
+
"Google Colab is a free cloud-based Jupyter Notebook for Python programming.",
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
"What is Hugging Face?":
|
| 39 |
+
"Hugging Face is a platform for Machine Learning models and datasets.",
|
| 40 |
|
| 41 |
"What is Gradio?":
|
| 42 |
+
"Gradio is a Python library for building interactive web interfaces.",
|
| 43 |
|
| 44 |
"What is cosine similarity?":
|
| 45 |
+
"Cosine similarity measures how similar two pieces of text are.",
|
| 46 |
+
|
| 47 |
+
"Who is Donald Trump?":
|
| 48 |
+
"Donald Trump is the 45th and 47th President of the United States.",
|
| 49 |
+
|
| 50 |
+
"Who is Elon Musk?":
|
| 51 |
+
"Elon Musk is the CEO of Tesla and SpaceX."
|
| 52 |
}
|
| 53 |
|
| 54 |
+
# ---------------------------------
|
| 55 |
# Text Preprocessing
|
| 56 |
+
# ---------------------------------
|
| 57 |
|
| 58 |
stop_words = set(stopwords.words("english"))
|
| 59 |
|
| 60 |
def preprocess(text):
|
| 61 |
text = text.lower()
|
|
|
|
| 62 |
|
| 63 |
+
# Remove punctuation
|
| 64 |
+
text = re.sub(r'[^a-zA-Z0-9 ]', '', text)
|
| 65 |
+
|
| 66 |
+
# Simple tokenization
|
| 67 |
+
tokens = text.split()
|
| 68 |
|
| 69 |
+
# Remove stopwords
|
| 70 |
tokens = [word for word in tokens if word not in stop_words]
|
| 71 |
|
| 72 |
return " ".join(tokens)
|
| 73 |
|
|
|
|
| 74 |
questions = list(faq_data.keys())
|
| 75 |
answers = list(faq_data.values())
|
| 76 |
|
| 77 |
processed_questions = [preprocess(q) for q in questions]
|
| 78 |
|
| 79 |
+
# ---------------------------------
|
| 80 |
+
# TF-IDF
|
| 81 |
+
# ---------------------------------
|
| 82 |
+
|
| 83 |
vectorizer = TfidfVectorizer()
|
| 84 |
|
| 85 |
question_vectors = vectorizer.fit_transform(processed_questions)
|
| 86 |
|
| 87 |
+
# ---------------------------------
|
| 88 |
# Chatbot Function
|
| 89 |
+
# ---------------------------------
|
| 90 |
|
| 91 |
def chatbot(user_input):
|
| 92 |
|
| 93 |
+
if not user_input.strip():
|
| 94 |
+
return "Please enter a question."
|
| 95 |
+
|
| 96 |
processed_input = preprocess(user_input)
|
| 97 |
|
| 98 |
input_vector = vectorizer.transform([processed_input])
|
| 99 |
|
| 100 |
+
similarity = cosine_similarity(input_vector, question_vectors)
|
| 101 |
|
| 102 |
+
best_match = similarity.argmax()
|
| 103 |
|
| 104 |
+
score = similarity[0][best_match]
|
| 105 |
|
| 106 |
if score < 0.25:
|
| 107 |
return "Sorry, I couldn't find a relevant answer."
|
| 108 |
|
| 109 |
return answers[best_match]
|
| 110 |
|
| 111 |
+
# ---------------------------------
|
| 112 |
# Gradio UI
|
| 113 |
+
# ---------------------------------
|
| 114 |
|
| 115 |
demo = gr.Interface(
|
| 116 |
fn=chatbot,
|
| 117 |
inputs=gr.Textbox(
|
| 118 |
lines=2,
|
| 119 |
+
placeholder="Ask your question here..."
|
| 120 |
),
|
| 121 |
+
outputs=gr.Textbox(),
|
| 122 |
title="🤖 FAQ Chatbot",
|
| 123 |
+
description="FAQ Chatbot using NLP, TF-IDF, and Cosine Similarity",
|
| 124 |
+
theme=gr.themes.Soft()
|
| 125 |
)
|
| 126 |
|
| 127 |
+
if __name__ == "__main__":
|
| 128 |
+
demo.launch()
|