-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
56 lines (47 loc) · 1.67 KB
/
app.py
File metadata and controls
56 lines (47 loc) · 1.67 KB
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
from flask import Flask, render_template, request, jsonify
import pickle
import json
import random
app = Flask(__name__)
# Load the trained model and vectorizer
try:
with open('model/chatbot_model.pkl', 'rb') as f:
best_model = pickle.load(f)
with open('model/vectorizer.pkl', 'rb') as f:
vectorizer = pickle.load(f)
print("Model and vectorizer loaded successfully.")
except Exception as e:
print(f"Error loading model or vectorizer: {e}")
# Load the intents data
try:
with open('dataset/intents1.json', 'r') as f:
intents = json.load(f)
print("Intents data loaded successfully.")
except Exception as e:
print(f"Error loading intents file: {e}")
def chatbot_response(user_input):
try:
input_text = vectorizer.transform([user_input])
predicted_intent = best_model.predict(input_text)[0]
print(f"Predicted intent: {predicted_intent}")
for intent in intents['intents']:
if intent['tag'] == predicted_intent:
response = random.choice(intent['responses'])
print(f"Response: {response}")
return response
return "Sorry, I don't understand that."
except Exception as e:
print(f"Error generating response: {e}")
return "There was an error processing your request."
@app.route('/')
def home():
return render_template('index.html')
@app.route('/chat', methods=['POST'])
def chat():
user_input = request.form.get('user_input', '')
if not user_input:
return "Please provide input."
response = chatbot_response(user_input)
return jsonify({'response': response})
if __name__ == '__main__':
app.run(debug=True)