judebebo32 commited on
Commit
a6bc8ab
1 Parent(s): d29252d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -0
app.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import nltk
2
+ from nltk.corpus import stopwords
3
+ from nltk.stem import PorterStemmer
4
+ from nltk.tokenize import word_tokenize
5
+ from sklearn.feature_extraction.text import TfidfVectorizer
6
+ from sklearn.metrics.pairwise import cosine_similarity
7
+ from flask import Flask, request, jsonify
8
+
9
+ # Initialize the Flask app
10
+ app = Flask(__name__)
11
+
12
+ # Download necessary NLTK data
13
+ nltk.download('punkt')
14
+ nltk.download('stopwords')
15
+
16
+ # Load customer inquiries dataset
17
+ with open('my_text_file.txt', 'r') as f:
18
+ data = f.readlines()
19
+
20
+ # Preprocess data
21
+ def preprocess_text(text):
22
+ tokens = word_tokenize(text.lower())
23
+ stop_words = set(stopwords.words('english'))
24
+ filtered_tokens = [word for word in tokens if word not in stop_words]
25
+ stemmer = PorterStemmer()
26
+ stemmed_tokens = [stemmer.stem(word) for word in filtered_tokens]
27
+ return stemmed_tokens
28
+
29
+ # Create TF-IDF vectorizer
30
+ vectorizer = TfidfVectorizer(analyzer=preprocess_text)
31
+ tfidf_matrix = vectorizer.fit_transform(data)
32
+
33
+ # Define chatbot logic
34
+ def chatbot_response(user_input):
35
+ preprocessed_input = preprocess_text(user_input)
36
+ input_vector = vectorizer.transform([user_input])
37
+ cosine_similarities = cosine_similarity(input_vector, tfidf_matrix)
38
+ most_similar_index = cosine_similarities.argmax()
39
+ return data[most_similar_index].strip()
40
+
41
+ # Define routes
42
+ @app.route('/')
43
+ def home():
44
+ return "Welcome to the Chatbot! Send a POST request to /chat with your message."
45
+
46
+ @app.route('/chat', methods=['POST'])
47
+ def chat():
48
+ user_input = request.json.get('message')
49
+ if user_input:
50
+ response = chatbot_response(user_input)
51
+ return jsonify({'response': response})
52
+ else:
53
+ return jsonify({'error': 'No message provided'}), 400
54
+
55
+ if __name__ == '__main__':
56
+ app.run(host='0.0.0.0', port=8080)