Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
# CodeAlpha Task 2: Chatbot for FAQs - Fixed
|
| 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: Chat function
|
| 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)
|
|
@@ -38,8 +38,8 @@ def chatbot_response(message, history):
|
|
| 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(
|
| 42 |
-
return
|
| 43 |
|
| 44 |
# Step 4: Premium CSS
|
| 45 |
custom_css = """
|
|
@@ -91,7 +91,6 @@ custom_css = """
|
|
| 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 {
|
|
@@ -110,7 +109,7 @@ custom_css = """
|
|
| 110 |
}
|
| 111 |
"""
|
| 112 |
|
| 113 |
-
# Step 5: Create App
|
| 114 |
with gr.Blocks(css=custom_css, theme=gr.themes.Base()) as demo:
|
| 115 |
gr.HTML("""
|
| 116 |
<div id="header">
|
|
@@ -139,13 +138,12 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Base()) as demo:
|
|
| 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 |
-
#
|
| 147 |
-
send.click(chatbot_response, [msg, chatbot], [
|
| 148 |
-
msg.submit(chatbot_response, [msg, chatbot], [
|
| 149 |
|
| 150 |
gr.HTML("""
|
| 151 |
<div id="footer">
|
|
|
|
| 1 |
+
# CodeAlpha Task 2: Chatbot for FAQs - Final Fixed Version
|
| 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 - Fixed format
|
| 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)
|
|
|
|
| 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
|
| 43 |
|
| 44 |
# Step 4: Premium CSS
|
| 45 |
custom_css = """
|
|
|
|
| 91 |
font-weight: 600!important;
|
| 92 |
border-radius: 12px!important;
|
| 93 |
font-size: 1.1em!important;
|
|
|
|
| 94 |
}
|
| 95 |
|
| 96 |
#send-btn:hover {
|
|
|
|
| 109 |
}
|
| 110 |
"""
|
| 111 |
|
| 112 |
+
# Step 5: Create App
|
| 113 |
with gr.Blocks(css=custom_css, theme=gr.themes.Base()) as demo:
|
| 114 |
gr.HTML("""
|
| 115 |
<div id="header">
|
|
|
|
| 138 |
|
| 139 |
gr.Examples(
|
| 140 |
examples=["What is CodeAlpha?", "When is submission deadline?", "What is TF-IDF?", "What is cosine similarity?"],
|
| 141 |
+
inputs=msg
|
|
|
|
| 142 |
)
|
| 143 |
|
| 144 |
+
# Fixed: return textbox first, then chatbot
|
| 145 |
+
send.click(chatbot_response, [msg, chatbot], [msg, chatbot])
|
| 146 |
+
msg.submit(chatbot_response, [msg, chatbot], [msg, chatbot])
|
| 147 |
|
| 148 |
gr.HTML("""
|
| 149 |
<div id="footer">
|