Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +72 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
import nltk
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from nltk.corpus import stopwords
|
| 5 |
+
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 6 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
| 7 |
+
|
| 8 |
+
nltk.download("stopwords", quiet=True)
|
| 9 |
+
|
| 10 |
+
faq_data = {
|
| 11 |
+
"What is Artificial Intelligence?":"Artificial Intelligence (AI) is the simulation of human intelligence by machines.",
|
| 12 |
+
"What is Machine Learning?":"Machine Learning is a subset of AI that enables computers to learn from data.",
|
| 13 |
+
"What is Deep Learning?":"Deep Learning is a branch of Machine Learning based on neural networks.",
|
| 14 |
+
"What is NLP?":"Natural Language Processing enables computers to understand human language.",
|
| 15 |
+
"What is Python?":"Python is a programming language widely used for AI and web development.",
|
| 16 |
+
"Who developed Python?":"Python was created by Guido van Rossum.",
|
| 17 |
+
"What is Hugging Face?":"Hugging Face is a platform for machine learning models and datasets.",
|
| 18 |
+
"What is Google Colab?":"Google Colab is a free cloud notebook for Python.",
|
| 19 |
+
"What is Gradio?":"Gradio lets you build web interfaces for Python models.",
|
| 20 |
+
"What is cosine similarity?":"Cosine similarity measures similarity between text vectors."
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
stop_words=set(stopwords.words("english"))
|
| 24 |
+
|
| 25 |
+
def preprocess(text):
|
| 26 |
+
text=text.lower()
|
| 27 |
+
text=re.sub(r"[^a-z0-9 ]"," ",text)
|
| 28 |
+
return " ".join([w for w in text.split() if w not in stop_words])
|
| 29 |
+
|
| 30 |
+
questions=list(faq_data.keys())
|
| 31 |
+
answers=list(faq_data.values())
|
| 32 |
+
|
| 33 |
+
vectorizer=TfidfVectorizer()
|
| 34 |
+
X=vectorizer.fit_transform([preprocess(q) for q in questions])
|
| 35 |
+
|
| 36 |
+
def chatbot(msg,history):
|
| 37 |
+
if not msg.strip():
|
| 38 |
+
return "",history
|
| 39 |
+
vec=vectorizer.transform([preprocess(msg)])
|
| 40 |
+
sims=cosine_similarity(vec,X)[0]
|
| 41 |
+
idx=sims.argmax()
|
| 42 |
+
if sims[idx] < 0.25:
|
| 43 |
+
ans="Sorry, I couldn't find a relevant answer."
|
| 44 |
+
else:
|
| 45 |
+
ans=answers[idx]
|
| 46 |
+
history=history or []
|
| 47 |
+
history.append({"role":"user","content":msg})
|
| 48 |
+
history.append({"role":"assistant","content":ans})
|
| 49 |
+
return "",history
|
| 50 |
+
|
| 51 |
+
css="""
|
| 52 |
+
.gradio-container{max-width:1000px!important}
|
| 53 |
+
footer{display:none}
|
| 54 |
+
"""
|
| 55 |
+
|
| 56 |
+
with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
|
| 57 |
+
gr.Markdown("# 🤖 FAQ Chatbot\nAsk a question from the FAQ database.")
|
| 58 |
+
chat=gr.Chatbot(type="messages",height=450)
|
| 59 |
+
with gr.Row():
|
| 60 |
+
txt=gr.Textbox(placeholder="Ask your question...",scale=8)
|
| 61 |
+
btn=gr.Button("Send")
|
| 62 |
+
clr=gr.Button("Clear")
|
| 63 |
+
gr.Examples(
|
| 64 |
+
examples=[["What is AI?"],["What is Python?"],["What is Hugging Face?"]],
|
| 65 |
+
inputs=txt
|
| 66 |
+
)
|
| 67 |
+
btn.click(chatbot,[txt,chat],[txt,chat])
|
| 68 |
+
txt.submit(chatbot,[txt,chat],[txt,chat])
|
| 69 |
+
clr.click(lambda:("",[]),outputs=[txt,chat])
|
| 70 |
+
|
| 71 |
+
if __name__=="__main__":
|
| 72 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==5.38.2
|
| 2 |
+
scikit-learn
|
| 3 |
+
nltk
|