Spaces:
Sleeping
Sleeping
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,289 +0,0 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import re
|
| 3 |
-
import nltk
|
| 4 |
-
from nltk.corpus import stopwords
|
| 5 |
-
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 6 |
-
from sklearn.metrics.pairwise import cosine_similarity
|
| 7 |
-
|
| 8 |
-
# Download NLTK stopwords
|
| 9 |
-
nltk.download("stopwords")
|
| 10 |
-
|
| 11 |
-
# -------------------------------
|
| 12 |
-
# FAQ Dataset
|
| 13 |
-
# -------------------------------
|
| 14 |
-
|
| 15 |
-
faq_data = {
|
| 16 |
-
"What is Artificial Intelligence?": "Artificial Intelligence (AI) is the simulation of human intelligence by machines.",
|
| 17 |
-
"What is Machine Learning?": "Machine Learning is a subset of AI that enables computers to learn from data.",
|
| 18 |
-
"What is Deep Learning?": "Deep Learning is a branch of Machine Learning based on neural networks.",
|
| 19 |
-
"What is NLP?": "Natural Language Processing enables computers to understand human language.",
|
| 20 |
-
"What is Python?": "Python is a popular programming language used in AI, automation, and web development.",
|
| 21 |
-
"Who developed Python?": "Python was developed by Guido van Rossum.",
|
| 22 |
-
"What is Hugging Face?": "Hugging Face is a platform for sharing machine learning models and datasets.",
|
| 23 |
-
"What is Google Colab?": "Google Colab is a free cloud-based notebook environment for Python.",
|
| 24 |
-
"What is Gradio?": "Gradio is a Python library used to build interactive machine learning web applications.",
|
| 25 |
-
"What is Streamlit?": "Streamlit is a framework for creating web applications using Python.",
|
| 26 |
-
"What is Cosine Similarity?": "Cosine Similarity measures how similar two text vectors are.",
|
| 27 |
-
"Who is Donald Trump?": "Donald Trump is the 45th and 47th President of the United States.",
|
| 28 |
-
"Who is Elon Musk?": "Elon Musk is the CEO of Tesla and SpaceX.",
|
| 29 |
-
"What is ChatGPT?": "ChatGPT is an AI chatbot developed by OpenAI.",
|
| 30 |
-
"What is TensorFlow?": "TensorFlow is an open-source machine learning framework developed by Google.",
|
| 31 |
-
"What is Scikit-learn?": "Scikit-learn is a Python library for machine learning.",
|
| 32 |
-
"What is Data Science?": "Data Science is the field of extracting knowledge and insights from data.",
|
| 33 |
-
"What is Neural Network?": "A Neural Network is a machine learning model inspired by the human brain.",
|
| 34 |
-
"What is Computer Vision?": "Computer Vision enables computers to interpret and understand images and videos.",
|
| 35 |
-
"What is Generative AI?": "Generative AI creates new content such as text, images, and code."
|
| 36 |
-
}
|
| 37 |
-
|
| 38 |
-
# -------------------------------
|
| 39 |
-
# Text Preprocessing
|
| 40 |
-
# -------------------------------
|
| 41 |
-
|
| 42 |
-
stop_words = set(stopwords.words("english"))
|
| 43 |
-
|
| 44 |
-
def preprocess(text):
|
| 45 |
-
text = text.lower()
|
| 46 |
-
text = re.sub(r'[^a-zA-Z0-9 ]', '', text)
|
| 47 |
-
words = text.split()
|
| 48 |
-
words = [word for word in words if word not in stop_words]
|
| 49 |
-
return " ".join(words)
|
| 50 |
-
|
| 51 |
-
questions = list(faq_data.keys())
|
| 52 |
-
answers = list(faq_data.values())
|
| 53 |
-
|
| 54 |
-
processed_questions = [preprocess(q) for q in questions]
|
| 55 |
-
|
| 56 |
-
vectorizer = TfidfVectorizer()
|
| 57 |
-
|
| 58 |
-
question_vectors = vectorizer.fit_transform(processed_questions)
|
| 59 |
-
|
| 60 |
-
# -------------------------------
|
| 61 |
-
# Chatbot Function
|
| 62 |
-
# -------------------------------
|
| 63 |
-
|
| 64 |
-
def chatbot(message):
|
| 65 |
-
|
| 66 |
-
if not message.strip():
|
| 67 |
-
return "Please enter a question."
|
| 68 |
-
|
| 69 |
-
processed = preprocess(message)
|
| 70 |
-
|
| 71 |
-
user_vector = vectorizer.transform([processed])
|
| 72 |
-
|
| 73 |
-
similarity = cosine_similarity(user_vector, question_vectors)
|
| 74 |
-
|
| 75 |
-
index = similarity.argmax()
|
| 76 |
-
|
| 77 |
-
score = similarity[0][index]
|
| 78 |
-
|
| 79 |
-
if score < 0.25:
|
| 80 |
-
return (
|
| 81 |
-
"❌ Sorry, I couldn't find a relevant answer.\n\n"
|
| 82 |
-
"Try asking about:\n"
|
| 83 |
-
"• AI\n"
|
| 84 |
-
"• Machine Learning\n"
|
| 85 |
-
"• Python\n"
|
| 86 |
-
"• Hugging Face\n"
|
| 87 |
-
"• Google Colab\n"
|
| 88 |
-
"• NLP\n"
|
| 89 |
-
"• Elon Musk\n"
|
| 90 |
-
"• Donald Trump"
|
| 91 |
-
)
|
| 92 |
-
|
| 93 |
-
return answers[index]
|
| 94 |
-
# ==========================================
|
| 95 |
-
# Beautiful ChatGPT Style UI
|
| 96 |
-
# ==========================================
|
| 97 |
-
|
| 98 |
-
css = """
|
| 99 |
-
body{
|
| 100 |
-
background:linear-gradient(135deg,#0f172a,#1e293b);
|
| 101 |
-
}
|
| 102 |
-
|
| 103 |
-
.gradio-container{
|
| 104 |
-
max-width:1100px !important;
|
| 105 |
-
margin:auto;
|
| 106 |
-
background:transparent;
|
| 107 |
-
}
|
| 108 |
-
|
| 109 |
-
footer{
|
| 110 |
-
display:none;
|
| 111 |
-
}
|
| 112 |
-
|
| 113 |
-
#title{
|
| 114 |
-
text-align:center;
|
| 115 |
-
font-size:40px;
|
| 116 |
-
font-weight:700;
|
| 117 |
-
color:white;
|
| 118 |
-
margin-top:10px;
|
| 119 |
-
}
|
| 120 |
-
|
| 121 |
-
#subtitle{
|
| 122 |
-
text-align:center;
|
| 123 |
-
color:#cbd5e1;
|
| 124 |
-
font-size:18px;
|
| 125 |
-
margin-bottom:25px;
|
| 126 |
-
}
|
| 127 |
-
|
| 128 |
-
.gr-chatbot{
|
| 129 |
-
border-radius:20px !important;
|
| 130 |
-
border:2px solid #334155 !important;
|
| 131 |
-
background:#111827 !important;
|
| 132 |
-
}
|
| 133 |
-
|
| 134 |
-
.gr-textbox textarea{
|
| 135 |
-
font-size:17px !important;
|
| 136 |
-
}
|
| 137 |
-
|
| 138 |
-
button{
|
| 139 |
-
border-radius:14px !important;
|
| 140 |
-
font-size:17px !important;
|
| 141 |
-
font-weight:bold !important;
|
| 142 |
-
}
|
| 143 |
-
|
| 144 |
-
#send{
|
| 145 |
-
background:linear-gradient(90deg,#2563eb,#7c3aed);
|
| 146 |
-
color:white;
|
| 147 |
-
}
|
| 148 |
-
|
| 149 |
-
#clear{
|
| 150 |
-
background:#ef4444;
|
| 151 |
-
color:white;
|
| 152 |
-
}
|
| 153 |
-
|
| 154 |
-
#examples{
|
| 155 |
-
padding-top:10px;
|
| 156 |
-
}
|
| 157 |
-
|
| 158 |
-
.example-btn{
|
| 159 |
-
border-radius:25px;
|
| 160 |
-
}
|
| 161 |
-
"""
|
| 162 |
-
|
| 163 |
-
theme = gr.themes.Soft(
|
| 164 |
-
primary_hue="blue",
|
| 165 |
-
secondary_hue="violet",
|
| 166 |
-
neutral_hue="slate",
|
| 167 |
-
)
|
| 168 |
-
|
| 169 |
-
with gr.Blocks(theme=theme, css=css, title="AI FAQ Chatbot") as demo:
|
| 170 |
-
|
| 171 |
-
gr.Markdown(
|
| 172 |
-
"""
|
| 173 |
-
<div id="title">🤖 AI FAQ Chatbot</div>
|
| 174 |
-
|
| 175 |
-
<div id="subtitle">
|
| 176 |
-
Ask questions about AI, Python, Machine Learning, Hugging Face,
|
| 177 |
-
Google Colab and more.
|
| 178 |
-
</div>
|
| 179 |
-
"""
|
| 180 |
-
)
|
| 181 |
-
|
| 182 |
-
chatbot_ui = gr.Chatbot(
|
| 183 |
-
height=520,
|
| 184 |
-
bubble_full_width=False,
|
| 185 |
-
show_copy_button=True,
|
| 186 |
-
label="Conversation"
|
| 187 |
-
)
|
| 188 |
-
|
| 189 |
-
with gr.Row():
|
| 190 |
-
|
| 191 |
-
textbox = gr.Textbox(
|
| 192 |
-
placeholder="💬 Type your question here...",
|
| 193 |
-
lines=2,
|
| 194 |
-
scale=8
|
| 195 |
-
)
|
| 196 |
-
|
| 197 |
-
send_btn = gr.Button(
|
| 198 |
-
"🚀 Send",
|
| 199 |
-
elem_id="send",
|
| 200 |
-
scale=1
|
| 201 |
-
)
|
| 202 |
-
|
| 203 |
-
with gr.Row():
|
| 204 |
-
|
| 205 |
-
clear_btn = gr.Button(
|
| 206 |
-
"🗑 Clear Chat",
|
| 207 |
-
elem_id="clear"
|
| 208 |
-
)
|
| 209 |
-
|
| 210 |
-
gr.Examples(
|
| 211 |
-
examples=[
|
| 212 |
-
["What is AI?"],
|
| 213 |
-
["What is Machine Learning?"],
|
| 214 |
-
["Who is Elon Musk?"],
|
| 215 |
-
["Who is Donald Trump?"],
|
| 216 |
-
["What is Hugging Face?"],
|
| 217 |
-
["What is Google Colab?"],
|
| 218 |
-
["What is Python?"],
|
| 219 |
-
["What is NLP?"]
|
| 220 |
-
],
|
| 221 |
-
inputs=textbox,
|
| 222 |
-
elem_id="examples"
|
| 223 |
-
)
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
def respond(message, history):
|
| 227 |
-
|
| 228 |
-
if history is None:
|
| 229 |
-
history = []
|
| 230 |
-
|
| 231 |
-
answer = chatbot(message)
|
| 232 |
-
|
| 233 |
-
history.append(
|
| 234 |
-
{"role":"user","content":message}
|
| 235 |
-
)
|
| 236 |
-
|
| 237 |
-
history.append(
|
| 238 |
-
{"role":"assistant","content":answer}
|
| 239 |
-
)
|
| 240 |
-
|
| 241 |
-
return "", history
|
| 242 |
-
# ==========================================
|
| 243 |
-
# Event Handlers
|
| 244 |
-
# ==========================================
|
| 245 |
-
|
| 246 |
-
send_btn.click(
|
| 247 |
-
fn=respond,
|
| 248 |
-
inputs=[textbox, chatbot_ui],
|
| 249 |
-
outputs=[textbox, chatbot_ui]
|
| 250 |
-
)
|
| 251 |
-
|
| 252 |
-
textbox.submit(
|
| 253 |
-
fn=respond,
|
| 254 |
-
inputs=[textbox, chatbot_ui],
|
| 255 |
-
outputs=[textbox, chatbot_ui]
|
| 256 |
-
)
|
| 257 |
-
|
| 258 |
-
clear_btn.click(
|
| 259 |
-
lambda: (None, []),
|
| 260 |
-
outputs=[textbox, chatbot_ui]
|
| 261 |
-
)
|
| 262 |
-
|
| 263 |
-
# ==========================================
|
| 264 |
-
# Footer
|
| 265 |
-
# ==========================================
|
| 266 |
-
|
| 267 |
-
gr.Markdown(
|
| 268 |
-
"""
|
| 269 |
-
---
|
| 270 |
-
<center>
|
| 271 |
-
|
| 272 |
-
### 🚀 AI FAQ Chatbot
|
| 273 |
-
|
| 274 |
-
Built with ❤️ using
|
| 275 |
-
|
| 276 |
-
**Python • Gradio • NLP • TF-IDF • Scikit-learn • NLTK**
|
| 277 |
-
|
| 278 |
-
Google Colab ✔️ | Hugging Face Spaces ✔️
|
| 279 |
-
|
| 280 |
-
</center>
|
| 281 |
-
"""
|
| 282 |
-
)
|
| 283 |
-
|
| 284 |
-
# ==========================================
|
| 285 |
-
# Launch App
|
| 286 |
-
# ==========================================
|
| 287 |
-
|
| 288 |
-
if __name__ == "__main__":
|
| 289 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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