Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,6 @@ import nltk
|
|
| 4 |
import numpy as np
|
| 5 |
from gtts import gTTS
|
| 6 |
import os
|
| 7 |
-
import datetime
|
| 8 |
import tempfile
|
| 9 |
from sklearn.feature_extraction.text import CountVectorizer
|
| 10 |
from sklearn.preprocessing import LabelEncoder
|
|
@@ -12,22 +11,22 @@ from sklearn.naive_bayes import MultinomialNB
|
|
| 12 |
from nltk.stem import PorterStemmer
|
| 13 |
from textblob import TextBlob
|
| 14 |
import gradio as gr
|
| 15 |
-
import speech_recognition as sr
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
nltk.
|
|
|
|
| 19 |
|
| 20 |
-
# Load intents
|
| 21 |
with open('intents.json') as file:
|
| 22 |
intents = json.load(file)
|
| 23 |
|
| 24 |
-
#
|
| 25 |
stemmer = PorterStemmer()
|
| 26 |
all_words = []
|
| 27 |
tags = []
|
| 28 |
xy = []
|
| 29 |
|
| 30 |
-
#
|
| 31 |
for intent in intents["intents"]:
|
| 32 |
tag = intent["tag"]
|
| 33 |
tags.append(tag)
|
|
@@ -41,29 +40,29 @@ for intent in intents["intents"]:
|
|
| 41 |
vectorizer = CountVectorizer()
|
| 42 |
X = vectorizer.fit_transform([item[0] for item in xy])
|
| 43 |
|
| 44 |
-
#
|
| 45 |
label_encoder = LabelEncoder()
|
| 46 |
y = label_encoder.fit_transform([item[1] for item in xy])
|
| 47 |
|
| 48 |
-
# Train
|
| 49 |
model = MultinomialNB()
|
| 50 |
model.fit(X, y)
|
| 51 |
|
| 52 |
-
#
|
| 53 |
def chatbot_response(user_input):
|
| 54 |
-
|
| 55 |
-
|
| 56 |
|
| 57 |
-
#
|
| 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
|
| 63 |
predicted_label = model.predict(input_vec)[0]
|
| 64 |
tag = label_encoder.inverse_transform([predicted_label])[0]
|
| 65 |
|
| 66 |
-
#
|
| 67 |
for intent in intents["intents"]:
|
| 68 |
if intent["tag"] == tag:
|
| 69 |
response = random.choice(intent["responses"])
|
|
@@ -71,11 +70,8 @@ def chatbot_response(user_input):
|
|
| 71 |
else:
|
| 72 |
response = "I'm sorry, I didn't understand that."
|
| 73 |
|
| 74 |
-
#
|
| 75 |
tts = gTTS(text=response, lang='en')
|
| 76 |
-
with tempfile.NamedTemporaryFile(delete=
|
| 77 |
-
tts.save(fp.name)
|
| 78 |
-
os.system(f"start {fp.name}") # For Windows; use `afplay` for Mac or `mpg123` for Linux
|
| 79 |
|
| 80 |
-
return response
|
| 81 |
|
|
|
|
| 4 |
import numpy as np
|
| 5 |
from gtts import gTTS
|
| 6 |
import os
|
|
|
|
| 7 |
import tempfile
|
| 8 |
from sklearn.feature_extraction.text import CountVectorizer
|
| 9 |
from sklearn.preprocessing import LabelEncoder
|
|
|
|
| 11 |
from nltk.stem import PorterStemmer
|
| 12 |
from textblob import TextBlob
|
| 13 |
import gradio as gr
|
|
|
|
| 14 |
|
| 15 |
+
# Fix for Hugging Face/Cloud: Set download path
|
| 16 |
+
nltk.data.path.append('/tmp')
|
| 17 |
+
nltk.download('punkt', download_dir='/tmp')
|
| 18 |
|
| 19 |
+
# Load intents
|
| 20 |
with open('intents.json') as file:
|
| 21 |
intents = json.load(file)
|
| 22 |
|
| 23 |
+
# NLP Setup
|
| 24 |
stemmer = PorterStemmer()
|
| 25 |
all_words = []
|
| 26 |
tags = []
|
| 27 |
xy = []
|
| 28 |
|
| 29 |
+
# Tokenize and stem input patterns
|
| 30 |
for intent in intents["intents"]:
|
| 31 |
tag = intent["tag"]
|
| 32 |
tags.append(tag)
|
|
|
|
| 40 |
vectorizer = CountVectorizer()
|
| 41 |
X = vectorizer.fit_transform([item[0] for item in xy])
|
| 42 |
|
| 43 |
+
# Label encoding
|
| 44 |
label_encoder = LabelEncoder()
|
| 45 |
y = label_encoder.fit_transform([item[1] for item in xy])
|
| 46 |
|
| 47 |
+
# Train model
|
| 48 |
model = MultinomialNB()
|
| 49 |
model.fit(X, y)
|
| 50 |
|
| 51 |
+
# Chatbot function
|
| 52 |
def chatbot_response(user_input):
|
| 53 |
+
if user_input.strip() == "":
|
| 54 |
+
return "Please type something..."
|
| 55 |
|
| 56 |
+
# Tokenize and vectorize
|
| 57 |
words = nltk.word_tokenize(user_input)
|
| 58 |
stemmed_input = " ".join([stemmer.stem(w.lower()) for w in words])
|
| 59 |
input_vec = vectorizer.transform([stemmed_input])
|
| 60 |
|
| 61 |
+
# Predict
|
| 62 |
predicted_label = model.predict(input_vec)[0]
|
| 63 |
tag = label_encoder.inverse_transform([predicted_label])[0]
|
| 64 |
|
| 65 |
+
# Get response
|
| 66 |
for intent in intents["intents"]:
|
| 67 |
if intent["tag"] == tag:
|
| 68 |
response = random.choice(intent["responses"])
|
|
|
|
| 70 |
else:
|
| 71 |
response = "I'm sorry, I didn't understand that."
|
| 72 |
|
| 73 |
+
# Text-to-speech
|
| 74 |
tts = gTTS(text=response, lang='en')
|
| 75 |
+
with tempfile.NamedTemporaryFile(delete=False
|
|
|
|
|
|
|
| 76 |
|
|
|
|
| 77 |
|