-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
149 lines (120 loc) · 4.8 KB
/
app.py
File metadata and controls
149 lines (120 loc) · 4.8 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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
from flask import Flask, render_template, request, redirect, url_for, session, jsonify
from flask_cors import CORS
from chatbot.chatbot import chatbot_response, process_input
import webbrowser
import threading
# ✅ Initialize Flask App
app = Flask(__name__, template_folder="frontend/templates", static_folder="frontend/static")
app.secret_key = "temp_secret_key" # Required for session handling
# ✅ Enable CORS for handling cross-domain requests
CORS(app)
# ✅ Initialize state variables
step = 0
user_data = {}
session_active = True
# ✅ Temporary user storage (since we aren't using a database)
users = {
"testuser": {"password": "password123"}
}
# ✅ Redirect to login if not authenticated
@app.route("/")
def home():
if "user" in session:
return redirect(url_for("dashboard"))
return redirect(url_for("login"))
# ✅ Login Page
@app.route("/login", methods=["GET", "POST"])
def login():
if request.method == "POST":
username = request.form["username"]
password = request.form["password"]
# ✅ Check if user exists and password is correct
if username in users and users[username]["password"] == password:
session["user"] = username
return redirect(url_for("dashboard"))
else:
return render_template("login.html", error="❌ Invalid username or password. Try again.")
return render_template("login.html")
# ✅ Register Page
@app.route("/register", methods=["GET", "POST"])
def register():
if request.method == "POST":
username = request.form["username"]
password = request.form["password"]
# ✅ Check if username already exists
if username in users:
return render_template("register.html", error="⚠ Username already taken. Choose another.")
# ✅ Register user
users[username] = {"password": password}
# ✅ Redirect user to the login page after successful registration
return redirect(url_for("login"))
return render_template("register.html")
# ✅ Dashboard (only accessible after login)
@app.route("/dashboard")
def dashboard():
if "user" not in session:
return redirect(url_for("login")) # Redirect to login if not authenticated
username = session["user"]
return render_template("index.html", username=username)
# ✅ Chatbot Response Handling
@app.route("/get_response", methods=["POST"])
def get_response():
global step, user_data, session_active
try:
user_message = request.json.get("message", "").strip()
if not user_message:
return jsonify({"response": "⚠ Please type a message."}), 400
# ✅ Prevent further input if session is ended
if not session_active:
return jsonify({
"response": "✅ Session has ended. Please refresh the page to start a new diagnosis."
})
# ✅ Start new session if step = 0
if step == 0 and session_active:
user_data = {}
# ✅ Get chatbot response
bot_response = chatbot_response(user_message, step, user_data)
# ✅ Step Management
if step < 10:
step += 1
elif step == 10:
# ✅ Predict disease at step 10 (AFTER breathing issue input)
step = 11 # Move to restart/exit prompt
elif step == 11:
user_input = process_input(user_message).lower()
if user_input == "yes":
step = 0 # Restart diagnosis
else:
# ✅ End session and reset state (but don't restart)
step = 0
user_data = {}
session_active = False
return jsonify({
"response": "✅ Thank you for using the AI Medical Assistant. Session has ended. Please refresh the page to start a new session."
})
return jsonify({"response": bot_response})
except Exception as e:
print(f"❌ Error: {e}")
return jsonify({"response": "⚠ An error occurred. Please try again later."}), 500
# ✅ Logout Route
@app.route('/logout', methods=["POST"])
def logout():
"""
Handles user logout and redirects to the home page.
"""
global user_data, step, session_active
user_data = {}
step = 0
session_active = True
session.pop("user", None)
return redirect(url_for("login"))
# ✅ Auto Open Browser on Launch
def open_browser():
"""
Open the default web browser after Flask starts.
"""
webbrowser.open("http://127.0.0.1:8000/")
# ✅ Start Flask App
if __name__ == "__main__":
threading.Timer(1.5, open_browser).start() # Open browser after 1.5 seconds
app.run(debug=True, port=8000)