Springboardmen commited on
Commit
cf41e4c
·
verified ·
1 Parent(s): b1520a0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -41
app.py CHANGED
@@ -1,35 +1,45 @@
1
  import streamlit as st
2
 
3
- from database import create_tables, save_weight, get_weight_history
4
  from auth import signup_user, login_user, verify_user_otp, generate_otp
5
  from email_utils import send_otp_email
6
  from model_api import query_model
7
  from prompt_builder import build_prompt
8
- from database import create_tables
9
 
10
- # create tables automatically
 
11
  create_tables()
12
 
13
  st.set_page_config(page_title="FitPlan AI", layout="centered")
14
 
15
  st.title("🏋️ FitPlan AI - Personalized Fitness Planner")
16
 
17
- # session state
 
 
 
 
18
  if "user" not in st.session_state:
19
  st.session_state.user = None
20
 
21
- if "otp_sent" not in st.session_state:
22
- st.session_state.otp_sent = False
23
-
24
  if "email" not in st.session_state:
25
  st.session_state.email = None
26
 
 
 
 
 
 
 
27
 
28
  # -------------------------
29
- # LOGIN / SIGNUP PAGE
30
  # -------------------------
31
 
32
- menu = st.sidebar.selectbox("Menu", ["Login", "Signup"])
 
 
 
33
 
34
 
35
  # -------------------------
@@ -38,7 +48,7 @@ menu = st.sidebar.selectbox("Menu", ["Login", "Signup"])
38
 
39
  if menu == "Signup":
40
 
41
- st.subheader("Create Account")
42
 
43
  email = st.text_input("Email")
44
  password = st.text_input("Password", type="password")
@@ -59,43 +69,55 @@ if menu == "Signup":
59
 
60
  if menu == "Login" and st.session_state.user is None:
61
 
62
- st.subheader("Login")
63
 
64
  email = st.text_input("Email")
65
  password = st.text_input("Password", type="password")
66
 
67
- if st.button("Send OTP"):
 
68
 
69
  user = login_user(email, password)
70
 
71
  if user:
72
 
73
- otp = generate_otp()
74
-
75
- send_otp_email(email, otp)
76
-
77
- from database import save_otp
78
- save_otp(email, otp)
79
-
80
- st.session_state.otp_sent = True
81
  st.session_state.email = email
 
82
 
83
- st.success("OTP sent to your email")
84
 
85
  else:
86
  st.error("Invalid email or password")
87
 
88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
 
90
  # -------------------------
91
- # OTP VERIFICATION
92
  # -------------------------
93
 
94
  if st.session_state.otp_sent:
95
 
96
- st.subheader("Verify OTP")
97
 
98
- otp_input = st.text_input("Enter OTP")
99
 
100
  if st.button("Verify OTP"):
101
 
@@ -104,6 +126,7 @@ if st.session_state.otp_sent:
104
  if verified:
105
 
106
  st.session_state.user = st.session_state.email
 
107
  st.session_state.otp_sent = False
108
 
109
  st.success("Login successful")
@@ -112,9 +135,8 @@ if st.session_state.otp_sent:
112
  st.error("Invalid OTP")
113
 
114
 
115
-
116
  # -------------------------
117
- # MAIN DASHBOARD
118
  # -------------------------
119
 
120
  if st.session_state.user:
@@ -126,18 +148,12 @@ if st.session_state.user:
126
  ["Dashboard", "Weight Tracker", "Generate Fitness Plan"]
127
  )
128
 
129
-
130
-
131
- # -------------------------
132
- # DASHBOARD
133
- # -------------------------
134
-
135
  if page == "Dashboard":
136
 
137
  st.header("Welcome to FitPlan AI")
138
 
139
- st.write("Track your fitness journey and generate personalized plans.")
140
-
141
 
142
 
143
  # -------------------------
@@ -163,8 +179,7 @@ if st.session_state.user:
163
  if history:
164
  st.table(history)
165
  else:
166
- st.info("No weight history found")
167
-
168
 
169
 
170
  # -------------------------
@@ -193,14 +208,13 @@ if st.session_state.user:
193
 
194
  prompt = build_prompt(age, height, weight, goal, activity)
195
 
196
- with st.spinner("Generating plan..."):
197
 
198
- response = query_model(prompt)
199
 
200
  st.subheader("Your AI Fitness Plan")
201
 
202
- st.write(response)
203
-
204
 
205
 
206
  # -------------------------
@@ -211,6 +225,7 @@ if st.session_state.user:
211
 
212
  st.session_state.user = None
213
  st.session_state.email = None
 
214
  st.session_state.otp_sent = False
215
 
216
- st.experimental_rerun()
 
1
  import streamlit as st
2
 
3
+ from database import create_tables, save_weight, get_weight_history, save_otp
4
  from auth import signup_user, login_user, verify_user_otp, generate_otp
5
  from email_utils import send_otp_email
6
  from model_api import query_model
7
  from prompt_builder import build_prompt
 
8
 
9
+
10
+ # Create database tables automatically
11
  create_tables()
12
 
13
  st.set_page_config(page_title="FitPlan AI", layout="centered")
14
 
15
  st.title("🏋️ FitPlan AI - Personalized Fitness Planner")
16
 
17
+
18
+ # -------------------------
19
+ # SESSION STATE
20
+ # -------------------------
21
+
22
  if "user" not in st.session_state:
23
  st.session_state.user = None
24
 
 
 
 
25
  if "email" not in st.session_state:
26
  st.session_state.email = None
27
 
28
+ if "credentials_verified" not in st.session_state:
29
+ st.session_state.credentials_verified = False
30
+
31
+ if "otp_sent" not in st.session_state:
32
+ st.session_state.otp_sent = False
33
+
34
 
35
  # -------------------------
36
+ # SIDEBAR MENU
37
  # -------------------------
38
 
39
+ menu = st.sidebar.selectbox(
40
+ "Menu",
41
+ ["Signup", "Login"]
42
+ )
43
 
44
 
45
  # -------------------------
 
48
 
49
  if menu == "Signup":
50
 
51
+ st.header("Create Account")
52
 
53
  email = st.text_input("Email")
54
  password = st.text_input("Password", type="password")
 
69
 
70
  if menu == "Login" and st.session_state.user is None:
71
 
72
+ st.header("Login")
73
 
74
  email = st.text_input("Email")
75
  password = st.text_input("Password", type="password")
76
 
77
+ # Step 1: Verify credentials
78
+ if st.button("Login"):
79
 
80
  user = login_user(email, password)
81
 
82
  if user:
83
 
 
 
 
 
 
 
 
 
84
  st.session_state.email = email
85
+ st.session_state.credentials_verified = True
86
 
87
+ st.success("Credentials verified. Click Send OTP.")
88
 
89
  else:
90
  st.error("Invalid email or password")
91
 
92
 
93
+ # -------------------------
94
+ # SEND OTP
95
+ # -------------------------
96
+
97
+ if st.session_state.credentials_verified and not st.session_state.otp_sent:
98
+
99
+ if st.button("Send OTP"):
100
+
101
+ otp = generate_otp()
102
+
103
+ save_otp(st.session_state.email, otp)
104
+
105
+ send_otp_email(st.session_state.email, otp)
106
+
107
+ st.session_state.otp_sent = True
108
+
109
+ st.success("OTP sent to your email")
110
+
111
 
112
  # -------------------------
113
+ # VERIFY OTP
114
  # -------------------------
115
 
116
  if st.session_state.otp_sent:
117
 
118
+ st.subheader("Enter OTP")
119
 
120
+ otp_input = st.text_input("OTP")
121
 
122
  if st.button("Verify OTP"):
123
 
 
126
  if verified:
127
 
128
  st.session_state.user = st.session_state.email
129
+ st.session_state.credentials_verified = False
130
  st.session_state.otp_sent = False
131
 
132
  st.success("Login successful")
 
135
  st.error("Invalid OTP")
136
 
137
 
 
138
  # -------------------------
139
+ # DASHBOARD
140
  # -------------------------
141
 
142
  if st.session_state.user:
 
148
  ["Dashboard", "Weight Tracker", "Generate Fitness Plan"]
149
  )
150
 
151
+ # Dashboard
 
 
 
 
 
152
  if page == "Dashboard":
153
 
154
  st.header("Welcome to FitPlan AI")
155
 
156
+ st.write("Track your fitness progress and generate AI fitness plans.")
 
157
 
158
 
159
  # -------------------------
 
179
  if history:
180
  st.table(history)
181
  else:
182
+ st.info("No weight data available")
 
183
 
184
 
185
  # -------------------------
 
208
 
209
  prompt = build_prompt(age, height, weight, goal, activity)
210
 
211
+ with st.spinner("Generating AI plan..."):
212
 
213
+ result = query_model(prompt)
214
 
215
  st.subheader("Your AI Fitness Plan")
216
 
217
+ st.write(result)
 
218
 
219
 
220
  # -------------------------
 
225
 
226
  st.session_state.user = None
227
  st.session_state.email = None
228
+ st.session_state.credentials_verified = False
229
  st.session_state.otp_sent = False
230
 
231
+ st.rerun()