File size: 1,791 Bytes
a6bc8ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import nltk
from nltk.corpus import stopwords
from nltk.stem import PorterStemmer
from nltk.tokenize import word_tokenize
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
from flask import Flask, request, jsonify

# Initialize the Flask app
app = Flask(__name__)

# Download necessary NLTK data
nltk.download('punkt')
nltk.download('stopwords')

# Load customer inquiries dataset
with open('my_text_file.txt', 'r') as f:
    data = f.readlines()

# Preprocess data
def preprocess_text(text):
    tokens = word_tokenize(text.lower())
    stop_words = set(stopwords.words('english'))
    filtered_tokens = [word for word in tokens if word not in stop_words]
    stemmer = PorterStemmer()
    stemmed_tokens = [stemmer.stem(word) for word in filtered_tokens]
    return stemmed_tokens

# Create TF-IDF vectorizer
vectorizer = TfidfVectorizer(analyzer=preprocess_text)
tfidf_matrix = vectorizer.fit_transform(data)

# Define chatbot logic
def chatbot_response(user_input):
    preprocessed_input = preprocess_text(user_input)
    input_vector = vectorizer.transform([user_input])
    cosine_similarities = cosine_similarity(input_vector, tfidf_matrix)
    most_similar_index = cosine_similarities.argmax()
    return data[most_similar_index].strip()

# Define routes
@app.route('/')
def home():
    return "Welcome to the Chatbot! Send a POST request to /chat with your message."

@app.route('/chat', methods=['POST'])
def chat():
    user_input = request.json.get('message')
    if user_input:
        response = chatbot_response(user_input)
        return jsonify({'response': response})
    else:
        return jsonify({'error': 'No message provided'}), 400

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8080)