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
|
|
@@ -23,10 +23,10 @@ answers = list(faqs.values())
|
|
| 23 |
vectorizer = TfidfVectorizer(stop_words='english')
|
| 24 |
tfidf_matrix = vectorizer.fit_transform(questions)
|
| 25 |
|
| 26 |
-
# Step 3:
|
| 27 |
def chatbot_response(message, history):
|
| 28 |
if not message.strip():
|
| 29 |
-
return
|
| 30 |
|
| 31 |
user_vec = vectorizer.transform([message])
|
| 32 |
similarity = cosine_similarity(user_vec, tfidf_matrix)
|
|
@@ -34,11 +34,14 @@ def chatbot_response(message, history):
|
|
| 34 |
confidence = similarity[0][idx]
|
| 35 |
|
| 36 |
if confidence > 0.3:
|
| 37 |
-
|
| 38 |
else:
|
| 39 |
-
|
| 40 |
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
| 42 |
custom_css = """
|
| 43 |
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600;700&display=swap');
|
| 44 |
|
|
@@ -65,12 +68,6 @@ custom_css = """
|
|
| 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 {
|
|
@@ -80,34 +77,24 @@ custom_css = """
|
|
| 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 |
-
|
| 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 |
-
|
|
|
|
| 108 |
}
|
| 109 |
|
| 110 |
-
|
| 111 |
transform: translateY(-3px)!important;
|
| 112 |
box-shadow: 0 10px 25px rgba(102, 126, 234, 0.4)!important;
|
| 113 |
}
|
|
@@ -120,20 +107,10 @@ custom_css = """
|
|
| 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
|
| 137 |
with gr.Blocks(css=custom_css, theme=gr.themes.Base()) as demo:
|
| 138 |
gr.HTML("""
|
| 139 |
<div id="header">
|
|
@@ -143,27 +120,33 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Base()) as demo:
|
|
| 143 |
</div>
|
| 144 |
""")
|
| 145 |
|
| 146 |
-
gr.
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 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>
|
|
|
|
| 1 |
+
# CodeAlpha Task 2: Chatbot for FAQs - Fixed UI with Send Button
|
| 2 |
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 3 |
from sklearn.metrics.pairwise import cosine_similarity
|
| 4 |
import numpy as np
|
|
|
|
| 23 |
vectorizer = TfidfVectorizer(stop_words='english')
|
| 24 |
tfidf_matrix = vectorizer.fit_transform(questions)
|
| 25 |
|
| 26 |
+
# Step 3: Chat function
|
| 27 |
def chatbot_response(message, history):
|
| 28 |
if not message.strip():
|
| 29 |
+
return history, ""
|
| 30 |
|
| 31 |
user_vec = vectorizer.transform([message])
|
| 32 |
similarity = cosine_similarity(user_vec, tfidf_matrix)
|
|
|
|
| 34 |
confidence = similarity[0][idx]
|
| 35 |
|
| 36 |
if confidence > 0.3:
|
| 37 |
+
bot_msg = f"🤖 **Answer:** {answers[idx]}\n\n📊 **Confidence:** {confidence:.0%} | ✅ High Match"
|
| 38 |
else:
|
| 39 |
+
bot_msg = "🤔 I don't have specific info on that. Try: 'What is CodeAlpha?' or 'What is TF-IDF?'"
|
| 40 |
|
| 41 |
+
history.append([message, bot_msg])
|
| 42 |
+
return history, "" # history updated + clear textbox
|
| 43 |
+
|
| 44 |
+
# Step 4: Premium CSS
|
| 45 |
custom_css = """
|
| 46 |
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600;700&display=swap');
|
| 47 |
|
|
|
|
| 68 |
margin: 20px;
|
| 69 |
border: 2px solid rgba(255, 255, 255, 0.3);
|
| 70 |
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
}
|
| 72 |
|
| 73 |
#header h1 {
|
|
|
|
| 77 |
text-shadow: 2px 2px 10px rgba(0,0,0,0.3);
|
| 78 |
}
|
| 79 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
.gr-chatbot {
|
| 81 |
background: rgba(255, 255, 255, 0.95)!important;
|
| 82 |
border-radius: 20px!important;
|
| 83 |
border: 2px solid rgba(255, 255, 255, 0.5)!important;
|
| 84 |
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1)!important;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
}
|
| 86 |
|
| 87 |
+
#send-btn {
|
| 88 |
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%)!important;
|
| 89 |
border: none!important;
|
| 90 |
color: white!important;
|
| 91 |
font-weight: 600!important;
|
| 92 |
border-radius: 12px!important;
|
| 93 |
+
font-size: 1.1em!important;
|
| 94 |
+
padding: 12px 30px!important;
|
| 95 |
}
|
| 96 |
|
| 97 |
+
#send-btn:hover {
|
| 98 |
transform: translateY(-3px)!important;
|
| 99 |
box-shadow: 0 10px 25px rgba(102, 126, 234, 0.4)!important;
|
| 100 |
}
|
|
|
|
| 107 |
background: rgba(0, 0, 0, 0.2);
|
| 108 |
backdrop-filter: blur(10px);
|
| 109 |
border-radius: 20px;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
}
|
| 111 |
"""
|
| 112 |
|
| 113 |
+
# Step 5: Create App with Send Button
|
| 114 |
with gr.Blocks(css=custom_css, theme=gr.themes.Base()) as demo:
|
| 115 |
gr.HTML("""
|
| 116 |
<div id="header">
|
|
|
|
| 120 |
</div>
|
| 121 |
""")
|
| 122 |
|
| 123 |
+
chatbot = gr.Chatbot(
|
| 124 |
+
height=500,
|
| 125 |
+
label="AI Assistant",
|
| 126 |
+
show_label=False,
|
| 127 |
+
avatar_images=("https://i.imgur.com/8Km9tLL.png", "https://i.imgur.com/2oBz7ag.png")
|
| 128 |
+
)
|
| 129 |
+
|
| 130 |
+
with gr.Row():
|
| 131 |
+
msg = gr.Textbox(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
placeholder="💭 Ask me about CodeAlpha or NLP...",
|
| 133 |
label="Your Question",
|
| 134 |
+
show_label=False,
|
| 135 |
+
scale=8,
|
| 136 |
+
container=False
|
| 137 |
+
)
|
| 138 |
+
send = gr.Button("Send 📤", elem_id="send-btn", scale=1)
|
| 139 |
+
|
| 140 |
+
gr.Examples(
|
| 141 |
+
examples=["What is CodeAlpha?", "When is submission deadline?", "What is TF-IDF?", "What is cosine similarity?"],
|
| 142 |
+
inputs=msg,
|
| 143 |
+
label="Click any example:"
|
| 144 |
)
|
| 145 |
|
| 146 |
+
# Actions: Send button + Enter key
|
| 147 |
+
send.click(chatbot_response, [msg, chatbot], [chatbot, msg])
|
| 148 |
+
msg.submit(chatbot_response, [msg, chatbot], [chatbot, msg])
|
| 149 |
+
|
| 150 |
gr.HTML("""
|
| 151 |
<div id="footer">
|
| 152 |
<p>© 2026 CodeAlpha AI Internship | Built with ❤️ using Gradio + Scikit-learn</p>
|