Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,128 +1,116 @@
|
|
| 1 |
-
|
| 2 |
-
import nltk
|
| 3 |
-
import gradio as gr
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 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 |
-
|
| 48 |
-
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
| 52 |
}
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
#
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
-
|
|
|
|
|
|
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
|
|
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
| 71 |
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
-
|
| 75 |
-
answers = list(faq_data.values())
|
| 76 |
|
| 77 |
-
|
|
|
|
|
|
|
| 78 |
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
-
|
| 86 |
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
|
|
|
|
|
|
| 90 |
|
| 91 |
-
|
| 92 |
|
| 93 |
-
|
| 94 |
-
return "Please enter a question."
|
| 95 |
|
| 96 |
-
processed_input = preprocess(user_input)
|
| 97 |
|
| 98 |
-
|
| 99 |
|
| 100 |
-
|
| 101 |
|
| 102 |
-
|
| 103 |
|
| 104 |
-
|
| 105 |
|
| 106 |
-
if score < 0.25:
|
| 107 |
-
return "Sorry, I couldn't find a relevant answer."
|
| 108 |
|
| 109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
|
|
|
|
|
|
| 114 |
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 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 |
-
|
| 128 |
-
demo.launch()
|
|
|
|
| 1 |
+
# -------------------- UI --------------------
|
|
|
|
|
|
|
| 2 |
|
| 3 |
+
css = """
|
| 4 |
+
body{
|
| 5 |
+
background: linear-gradient(135deg,#0f172a,#1e293b);
|
| 6 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
.gradio-container{
|
| 9 |
+
max-width:1000px !important;
|
| 10 |
+
margin:auto;
|
| 11 |
+
}
|
| 12 |
|
| 13 |
+
h1{
|
| 14 |
+
text-align:center;
|
| 15 |
+
font-size:42px;
|
| 16 |
+
font-weight:700;
|
| 17 |
+
color:white;
|
| 18 |
}
|
| 19 |
|
| 20 |
+
.subtitle{
|
| 21 |
+
text-align:center;
|
| 22 |
+
color:#cbd5e1;
|
| 23 |
+
font-size:18px;
|
| 24 |
+
margin-bottom:20px;
|
| 25 |
+
}
|
| 26 |
|
| 27 |
+
.chatbox{
|
| 28 |
+
border-radius:20px;
|
| 29 |
+
}
|
| 30 |
|
| 31 |
+
footer{
|
| 32 |
+
display:none;
|
| 33 |
+
}
|
| 34 |
|
| 35 |
+
button{
|
| 36 |
+
border-radius:15px !important;
|
| 37 |
+
font-size:18px !important;
|
| 38 |
+
font-weight:bold !important;
|
| 39 |
+
}
|
| 40 |
|
| 41 |
+
#send{
|
| 42 |
+
background:linear-gradient(90deg,#2563eb,#7c3aed);
|
| 43 |
+
color:white;
|
| 44 |
+
}
|
| 45 |
|
| 46 |
+
#clear{
|
| 47 |
+
background:#334155;
|
| 48 |
+
color:white;
|
| 49 |
+
}
|
| 50 |
|
| 51 |
+
#title{
|
| 52 |
+
text-align:center;
|
| 53 |
+
}
|
| 54 |
+
"""
|
| 55 |
|
| 56 |
+
with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue"), css=css) as demo:
|
|
|
|
| 57 |
|
| 58 |
+
gr.Markdown(
|
| 59 |
+
"""
|
| 60 |
+
# π€ AI FAQ Chatbot
|
| 61 |
|
| 62 |
+
<div class="subtitle">
|
| 63 |
+
Ask any FAQ and receive the most relevant answer using NLP,
|
| 64 |
+
TF-IDF and Cosine Similarity.
|
| 65 |
+
</div>
|
| 66 |
+
"""
|
| 67 |
+
)
|
| 68 |
|
| 69 |
+
chatbot_ui = gr.Chatbot(
|
| 70 |
+
height=500,
|
| 71 |
+
bubble_full_width=False,
|
| 72 |
+
show_copy_button=True,
|
| 73 |
+
avatar_images=("https://cdn-icons-png.flaticon.com/512/847/847969.png",
|
| 74 |
+
"https://cdn-icons-png.flaticon.com/512/4712/4712109.png")
|
| 75 |
+
)
|
| 76 |
|
| 77 |
+
with gr.Row():
|
| 78 |
|
| 79 |
+
textbox = gr.Textbox(
|
| 80 |
+
placeholder="π¬ Ask your question...",
|
| 81 |
+
scale=8,
|
| 82 |
+
container=False
|
| 83 |
+
)
|
| 84 |
|
| 85 |
+
send = gr.Button("Send π", elem_id="send", scale=1)
|
| 86 |
|
| 87 |
+
clear = gr.Button("π Clear Chat", elem_id="clear")
|
|
|
|
| 88 |
|
|
|
|
| 89 |
|
| 90 |
+
def respond(message, history):
|
| 91 |
|
| 92 |
+
answer = chatbot(message)
|
| 93 |
|
| 94 |
+
history.append((message, answer))
|
| 95 |
|
| 96 |
+
return "", history
|
| 97 |
|
|
|
|
|
|
|
| 98 |
|
| 99 |
+
send.click(
|
| 100 |
+
respond,
|
| 101 |
+
[textbox, chatbot_ui],
|
| 102 |
+
[textbox, chatbot_ui]
|
| 103 |
+
)
|
| 104 |
|
| 105 |
+
textbox.submit(
|
| 106 |
+
respond,
|
| 107 |
+
[textbox, chatbot_ui],
|
| 108 |
+
[textbox, chatbot_ui]
|
| 109 |
+
)
|
| 110 |
|
| 111 |
+
clear.click(
|
| 112 |
+
lambda: [],
|
| 113 |
+
outputs=chatbot_ui
|
| 114 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
+
demo.launch()
|
|
|