Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
# CodeAlpha Task 2: Chatbot for FAQs
|
| 2 |
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 3 |
from sklearn.metrics.pairwise import cosine_similarity
|
| 4 |
import numpy as np
|
|
@@ -6,14 +6,14 @@ import gradio as gr
|
|
| 6 |
|
| 7 |
# Step 1: Collect FAQs
|
| 8 |
faqs = {
|
| 9 |
-
"What is CodeAlpha?": "CodeAlpha is a tech platform offering free virtual internships in AI, Web Development, and Data Science to help students gain practical experience.",
|
| 10 |
"How long is CodeAlpha internship?": "CodeAlpha internships last 1 month. You must complete minimum 2 out of 4 tasks to receive the completion certificate and Letter of Recommendation.",
|
| 11 |
"When is CodeAlpha submission deadline?": "Submission window: 10 May 2026 to 10 June 2026. Last date: 10 June 2026. Certificates issued: 11 June 2026.",
|
| 12 |
-
"Is CodeAlpha certificate valuable?": "Yes, CodeAlpha certificates are recognized
|
| 13 |
-
"How to contact CodeAlpha?": "Website: www.codealpha.tech | WhatsApp: +91 9336576683 |
|
| 14 |
-
"What is TF-IDF?": "TF-IDF
|
| 15 |
-
"What is cosine similarity?": "Cosine similarity measures
|
| 16 |
-
"What is NLTK used for?": "NLTK
|
| 17 |
}
|
| 18 |
|
| 19 |
questions = list(faqs.keys())
|
|
@@ -23,31 +23,152 @@ answers = list(faqs.values())
|
|
| 23 |
vectorizer = TfidfVectorizer(stop_words='english')
|
| 24 |
tfidf_matrix = vectorizer.fit_transform(questions)
|
| 25 |
|
| 26 |
-
# Step 3: Match user questions
|
| 27 |
def chatbot_response(message, history):
|
| 28 |
if not message.strip():
|
| 29 |
-
return "Please ask me a question about CodeAlpha or NLP concepts!"
|
| 30 |
|
| 31 |
user_vec = vectorizer.transform([message])
|
| 32 |
similarity = cosine_similarity(user_vec, tfidf_matrix)
|
| 33 |
idx = np.argmax(similarity)
|
| 34 |
confidence = similarity[0][idx]
|
| 35 |
|
| 36 |
-
# Step 4: Display best matching answer
|
| 37 |
if confidence > 0.3:
|
| 38 |
-
return f"π€ {answers[idx]}\n\
|
| 39 |
else:
|
| 40 |
-
return "
|
| 41 |
|
| 42 |
-
# Step
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
gr.ChatInterface(
|
| 48 |
fn=chatbot_response,
|
| 49 |
-
examples=[
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
)
|
| 52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
demo.launch()
|
|
|
|
| 1 |
+
# CodeAlpha Task 2: Chatbot for FAQs - Premium Design
|
| 2 |
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 3 |
from sklearn.metrics.pairwise import cosine_similarity
|
| 4 |
import numpy as np
|
|
|
|
| 6 |
|
| 7 |
# Step 1: Collect FAQs
|
| 8 |
faqs = {
|
| 9 |
+
"What is CodeAlpha?": "CodeAlpha is a tech platform offering free virtual internships in AI, Web Development, and Data Science to help students gain practical experience and build their portfolio.",
|
| 10 |
"How long is CodeAlpha internship?": "CodeAlpha internships last 1 month. You must complete minimum 2 out of 4 tasks to receive the completion certificate and Letter of Recommendation.",
|
| 11 |
"When is CodeAlpha submission deadline?": "Submission window: 10 May 2026 to 10 June 2026. Last date: 10 June 2026. Certificates issued: 11 June 2026.",
|
| 12 |
+
"Is CodeAlpha certificate valuable?": "Yes, CodeAlpha certificates are recognized by companies. The LOR highlights your practical skills and helps in job applications.",
|
| 13 |
+
"How to contact CodeAlpha?": "Website: www.codealpha.tech | WhatsApp: +91 9336576683 | Official tasks shared via WhatsApp group.",
|
| 14 |
+
"What is TF-IDF?": "TF-IDF = Term Frequency-Inverse Document Frequency. NLP technique that converts text to numbers. High TF-IDF = important word for that document.",
|
| 15 |
+
"What is cosine similarity?": "Cosine similarity measures angle between two text vectors. Score 1 = identical, 0 = completely different. Used to find best matching FAQ.",
|
| 16 |
+
"What is NLTK used for?": "NLTK = Natural Language Toolkit. Python library for text preprocessing: tokenization, stemming, stopword removal, and cleaning text data."
|
| 17 |
}
|
| 18 |
|
| 19 |
questions = list(faqs.keys())
|
|
|
|
| 23 |
vectorizer = TfidfVectorizer(stop_words='english')
|
| 24 |
tfidf_matrix = vectorizer.fit_transform(questions)
|
| 25 |
|
| 26 |
+
# Step 3: Match user questions
|
| 27 |
def chatbot_response(message, history):
|
| 28 |
if not message.strip():
|
| 29 |
+
return "π Please ask me a question about CodeAlpha or NLP concepts!"
|
| 30 |
|
| 31 |
user_vec = vectorizer.transform([message])
|
| 32 |
similarity = cosine_similarity(user_vec, tfidf_matrix)
|
| 33 |
idx = np.argmax(similarity)
|
| 34 |
confidence = similarity[0][idx]
|
| 35 |
|
|
|
|
| 36 |
if confidence > 0.3:
|
| 37 |
+
return f"π€ **Answer:** {answers[idx]}\n\nπ **Confidence:** {confidence:.0%} | β
High Match"
|
| 38 |
else:
|
| 39 |
+
return "π€ I don't have specific info on that. Try: 'What is CodeAlpha?' or 'What is TF-IDF?'"
|
| 40 |
|
| 41 |
+
# Step 4: Premium CSS with Animations
|
| 42 |
+
custom_css = """
|
| 43 |
+
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600;700&display=swap');
|
| 44 |
+
|
| 45 |
+
.gradio-container {
|
| 46 |
+
font-family: 'Poppins', sans-serif!important;
|
| 47 |
+
background: linear-gradient(-45deg, #667eea, #764ba2, #f093fb, #4facfe)!important;
|
| 48 |
+
background-size: 400% 400%!important;
|
| 49 |
+
animation: gradientBG 15s ease infinite!important;
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
@keyframes gradientBG {
|
| 53 |
+
0% { background-position: 0% 50%; }
|
| 54 |
+
50% { background-position: 100% 50%; }
|
| 55 |
+
100% { background-position: 0% 50%; }
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
#header {
|
| 59 |
+
text-align: center;
|
| 60 |
+
color: white;
|
| 61 |
+
padding: 40px 20px;
|
| 62 |
+
background: rgba(255, 255, 255, 0.1);
|
| 63 |
+
backdrop-filter: blur(20px);
|
| 64 |
+
border-radius: 30px;
|
| 65 |
+
margin: 20px;
|
| 66 |
+
border: 2px solid rgba(255, 255, 255, 0.3);
|
| 67 |
+
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
|
| 68 |
+
animation: fadeInDown 1s ease;
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
@keyframes fadeInDown {
|
| 72 |
+
from { opacity: 0; transform: translateY(-30px); }
|
| 73 |
+
to { opacity: 1; transform: translateY(0); }
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
#header h1 {
|
| 77 |
+
font-size: 3em;
|
| 78 |
+
font-weight: 700;
|
| 79 |
+
margin-bottom: 10px;
|
| 80 |
+
text-shadow: 2px 2px 10px rgba(0,0,0,0.3);
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
#header p {
|
| 84 |
+
font-size: 1.2em;
|
| 85 |
+
opacity: 0.9;
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
.gr-chatbot {
|
| 89 |
+
background: rgba(255, 255, 255, 0.95)!important;
|
| 90 |
+
border-radius: 20px!important;
|
| 91 |
+
border: 2px solid rgba(255, 255, 255, 0.5)!important;
|
| 92 |
+
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1)!important;
|
| 93 |
+
animation: fadeInUp 1s ease 0.3s both;
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
@keyframes fadeInUp {
|
| 97 |
+
from { opacity: 0; transform: translateY(30px); }
|
| 98 |
+
to { opacity: 1; transform: translateY(0); }
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
.gr-button {
|
| 102 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%)!important;
|
| 103 |
+
border: none!important;
|
| 104 |
+
color: white!important;
|
| 105 |
+
font-weight: 600!important;
|
| 106 |
+
border-radius: 12px!important;
|
| 107 |
+
transition: all 0.3s ease!important;
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
.gr-button:hover {
|
| 111 |
+
transform: translateY(-3px)!important;
|
| 112 |
+
box-shadow: 0 10px 25px rgba(102, 126, 234, 0.4)!important;
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
#footer {
|
| 116 |
+
text-align: center;
|
| 117 |
+
color: white;
|
| 118 |
+
padding: 25px;
|
| 119 |
+
margin-top: 30px;
|
| 120 |
+
background: rgba(0, 0, 0, 0.2);
|
| 121 |
+
backdrop-filter: blur(10px);
|
| 122 |
+
border-radius: 20px;
|
| 123 |
+
animation: fadeIn 1s ease 0.6s both;
|
| 124 |
+
}
|
| 125 |
+
|
| 126 |
+
@keyframes fadeIn {
|
| 127 |
+
from { opacity: 0; }
|
| 128 |
+
to { opacity: 1; }
|
| 129 |
+
}
|
| 130 |
+
|
| 131 |
+
.examples {
|
| 132 |
+
animation: fadeIn 1s ease 0.9s both;
|
| 133 |
+
}
|
| 134 |
+
"""
|
| 135 |
+
|
| 136 |
+
# Step 5: Create App with Premium UI
|
| 137 |
+
with gr.Blocks(css=custom_css, theme=gr.themes.Base()) as demo:
|
| 138 |
+
gr.HTML("""
|
| 139 |
+
<div id="header">
|
| 140 |
+
<h1>π¬ CodeAlpha AI Assistant</h1>
|
| 141 |
+
<p>Task 2: FAQ Chatbot | CodeAlpha AI Internship 2026</p>
|
| 142 |
+
<p>Powered by NLP: TF-IDF + Cosine Similarity</p>
|
| 143 |
+
</div>
|
| 144 |
+
""")
|
| 145 |
|
| 146 |
gr.ChatInterface(
|
| 147 |
fn=chatbot_response,
|
| 148 |
+
examples=[
|
| 149 |
+
["What is CodeAlpha?"],
|
| 150 |
+
["When is submission deadline?"],
|
| 151 |
+
["What is TF-IDF?"],
|
| 152 |
+
["What is cosine similarity?"]
|
| 153 |
+
],
|
| 154 |
+
chatbot=gr.Chatbot(
|
| 155 |
+
height=500,
|
| 156 |
+
label="AI Assistant",
|
| 157 |
+
show_label=False,
|
| 158 |
+
avatar_images=("https://i.imgur.com/8Km9tLL.png", "https://i.imgur.com/2oBz7ag.png")
|
| 159 |
+
),
|
| 160 |
+
textbox=gr.Textbox(
|
| 161 |
+
placeholder="π Ask me about CodeAlpha or NLP...",
|
| 162 |
+
label="Your Question",
|
| 163 |
+
show_label=False
|
| 164 |
+
),
|
| 165 |
)
|
| 166 |
|
| 167 |
+
gr.HTML("""
|
| 168 |
+
<div id="footer">
|
| 169 |
+
<p>Β© 2026 CodeAlpha AI Internship | Built with β€οΈ using Gradio + Scikit-learn</p>
|
| 170 |
+
<p>π Demonstrating NLP Skills: Text Preprocessing + Vector Similarity + Chat UI</p>
|
| 171 |
+
</div>
|
| 172 |
+
""")
|
| 173 |
+
|
| 174 |
demo.launch()
|