-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
113 lines (94 loc) · 3.82 KB
/
app.py
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
from flask import Flask, request, jsonify, redirect, render_template_string
import joblib
import random
import json
import os
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from nltk.stem import WordNetLemmatizer
import nltk
app = Flask(__name__)
# Load the trained model and vectorizer (corrected file name)
model = joblib.load("chatbot_model.pkl") # Corrected to chatbot_model.pkl
vectorizer = joblib.load("vectorizer.pkl")
# Load the intents file
with open("intents.json", "r") as file:
intents = json.load(file)
#initialize NLTK components
lemmatizer = WordNetLemmatizer()
stop_words = set(stopwords.words("english"))
#preprocess text to match the training data
def preprocess_text(text):
text = text.lower()
words = word_tokenize(text)
words = [lemmatizer.lemmatize(w) for w in words if w not in stop_words and w.isalnum()]
return " ".join(words)
@app.route("/", methods=["GET"])
def index():
# Simple HTML page with a button
return render_template_string('''
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>College Chatbot</title>
<style>
body { font-family: Arial, sans-serif; text-align: center; margin: 20%; background-color: #f4f4f4; }
button {
background-color: #007BFF; color: white;
border: none; padding: 15px 30px; font-size: 18px;
cursor: pointer; border-radius: 5px;
}
button:hover { background-color: #0056b3; }
</style>
</head>
<body>
<h1>Welcome to the College Chatbot</h1>
<p>Click the button below to open the chatbot interface.</p>
<form action="/redirect">
<button type="submit">Go to Chatbot</button>
</form>
</body>
</html>
''')
@app.route("/redirect", methods=["POST", "GET"])
def redirect_to_streamlit():
# Redirect to Streamlit frontend
return redirect("http://localhost:8501") # Streamlit frontend URL
@app.route("/chat", methods = ["POST"])
def chatbot_response():
#receive the user input and return bot responses
try:
data = request.get_json()
user_message = data.get("message","")
if not user_message:
return jsonify({"responses": "No message provided"}), 400
processed_message = preprocess_text(user_message)
input_vector = vectorizer.transform([processed_message])
#predict intent and get confidence
predicted_tag = model.predict(input_vector)[0]
confidence_scores = model.predict_proba(input_vector)[0]
max_confidence = max(confidence_scores)
#confidence threshold
confidence_threshold = 0.75
if max_confidence < confidence_threshold:
return jsonify({"responses": "I'm not sure I understand. Could you rephrase your question?"})
#match the prediction to the response in intents file
response_list = next()
input_vector = vectorizer.transform([user_message])
predicted_tag = model.predict(input_vector)[0]
#Match prediction to response in intents file
response_list = next(
(intent["responses"] for intent in intents["intents"] if intent["tag"] == predicted_tag), ["Sorry, I don't understand that."]
)
#randomly choose one response
response_text = random.choice(response_list)
return jsonify({"responses": response_text})
except Exception as e:
return jsonify({"responses": f"Error: {str(e)}"}), 500
if __name__ == "__main__":
app.run(debug=True)
# Get the port from the environment variable, default to 5000 if not set
port = int(os.environ.get("PORT", 5000))
app.run(host="0.0.0.0", port=port, debug=True)