Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -39,7 +39,40 @@ for intent in intents["intents"]:
|
|
| 39 |
|
| 40 |
# Feature extraction
|
| 41 |
vectorizer = CountVectorizer()
|
| 42 |
-
X = vectorizer.fit_transform([item[0] for item in
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
|
|
|
|
| 39 |
|
| 40 |
# Feature extraction
|
| 41 |
vectorizer = CountVectorizer()
|
| 42 |
+
X = vectorizer.fit_transform([item[0] for item in xy])
|
| 43 |
|
| 44 |
+
# Encode the tags (labels)
|
| 45 |
+
label_encoder = LabelEncoder()
|
| 46 |
+
y = label_encoder.fit_transform([item[1] for item in xy])
|
| 47 |
+
|
| 48 |
+
# Train the model
|
| 49 |
+
model = MultinomialNB()
|
| 50 |
+
model.fit(X, y)
|
| 51 |
+
|
| 52 |
+
# Prediction function
|
| 53 |
+
def chatbot_response(user_input):
|
| 54 |
+
# Sentiment check (optional)
|
| 55 |
+
sentiment = TextBlob(user_input).sentiment.polarity
|
| 56 |
+
|
| 57 |
+
# Process input
|
| 58 |
+
words = nltk.word_tokenize(user_input)
|
| 59 |
+
stemmed_input = " ".join([stemmer.stem(w.lower()) for w in words])
|
| 60 |
+
input_vec = vectorizer.transform([stemmed_input])
|
| 61 |
+
|
| 62 |
+
# Predict tag
|
| 63 |
+
predicted_label = model.predict(input_vec)[0]
|
| 64 |
+
tag = label_encoder.inverse_transform([predicted_label])[0]
|
| 65 |
+
|
| 66 |
+
# Select a random response
|
| 67 |
+
for intent in intents["intents"]:
|
| 68 |
+
if intent["tag"] == tag:
|
| 69 |
+
response = random.choice(intent["responses"])
|
| 70 |
+
break
|
| 71 |
+
else:
|
| 72 |
+
response = "I'm sorry, I didn't understand that."
|
| 73 |
+
|
| 74 |
+
# Convert text to speech
|
| 75 |
+
tts = gTTS(text=response, lang='en')
|
| 76 |
+
with tempfile.NamedTemporaryFile(dele
|
| 77 |
|
| 78 |
|