awacke1 commited on
Commit
089eee1
Β·
1 Parent(s): b459af6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -45
app.py CHANGED
@@ -3,22 +3,22 @@ import hashlib
3
  import streamlit as st
4
  import textflowsms as tf
5
  from datetime import datetime
6
-
7
- # Central Time Zone Adjustment
8
  import pytz
 
 
9
  central = pytz.timezone('US/Central')
10
 
11
- # Function to format phone number
12
  def format_phone_number(phone_number):
13
  if len(phone_number) == 10 and not phone_number.startswith('+'):
14
  return '+1' + phone_number
15
  return phone_number
16
 
17
- # Function to hash a password
18
  def hash_password(password):
19
  return hashlib.sha256(password.encode()).hexdigest()
20
 
21
- # Function to save user data to a file
22
  def save_user_data(phone_number, password_hash):
23
  timestamp = datetime.now(central).strftime('%d%m%y-%H-%M')
24
  file_name = f"phone-{timestamp}.txt"
@@ -26,7 +26,7 @@ def save_user_data(phone_number, password_hash):
26
  file.write(f"{password_hash}\n")
27
  return file_name
28
 
29
- # Function to check if user is authenticated
30
  def is_user_authenticated(phone_number, hash_value):
31
  for file_name in os.listdir():
32
  if file_name.startswith('phone-') and phone_number in file_name:
@@ -36,9 +36,14 @@ def is_user_authenticated(phone_number, hash_value):
36
  return True
37
  return False
38
 
39
- # Initialize session state
 
 
 
 
 
40
  if 'phone_number' not in st.session_state:
41
- st.session_state['phone_number'] = '+19522583980' # Default phone number
42
  if 'password' not in st.session_state:
43
  st.session_state['password'] = ''
44
  if 'hash' not in st.session_state:
@@ -46,66 +51,59 @@ if 'hash' not in st.session_state:
46
  if 'authenticated' not in st.session_state:
47
  st.session_state['authenticated'] = False
48
 
49
- # Sidebar inputs
50
  user_phone_input = st.sidebar.text_input("πŸ“± Mobile Phone", value=st.session_state.get('phone_number', ''))
51
  password_input = st.sidebar.text_input("πŸ”‘ Set Password", type='password')
52
 
53
- # Save button
54
  if st.sidebar.button('πŸ’Ύ Save Settings'):
55
  st.session_state['phone_number'] = format_phone_number(user_phone_input)
56
  hashed_password = hash_password(password_input)
57
  st.session_state['password'] = hashed_password
58
  file_name = save_user_data(st.session_state['phone_number'], hashed_password)
59
  st.sidebar.success(f"Settings saved successfully! Data saved in {file_name}")
60
- # Record save event with timestamp
61
- st.session_state['save_time'] = datetime.now(central).strftime('%Y-%m-%d %H:%M:%S')
62
-
63
 
64
- # Function to send verification SMS
65
  def send_verification_sms():
66
-
67
- # Load the API key from an environment variable
68
  api_key = os.getenv('API_KEY')
69
  tf.useKey(api_key)
70
- #tf.sendSMS("+19522583980", "Test 2")
71
 
72
  base_url = "https://huggingface.co/spaces/awacke1/RT-SMS-Phone-Verify"
73
- # Correct the sendSMS function call
74
- #phone=st.session_state['phone_number']
75
- phone=user_phone_input
76
- st.write('Sending SMS to phone number ' + phone)
77
-
78
- hashmessage=f"Verify here: {base_url}?hash={st.session_state['password']}"
79
- st.write('Hash message: ' + hashmessage)
80
-
81
- result = tf.sendSMS(phone, hashmessage)
82
-
83
- if(result.ok):
84
- print(result.data)
85
- st.sidebar.success("Verification link sent via SMS")
86
- else:
87
- print(result.message)
88
 
 
 
 
 
 
 
 
89
 
90
- # URL hash handling
91
  query_params = st.experimental_get_query_params()
92
  if 'hash' in query_params:
93
- st.session_state['hash'] = query_params['hash'][0]
94
- st.session_state['authenticated'] = is_user_authenticated(st.session_state['phone_number'], st.session_state['hash'])
95
- if st.session_state['authenticated']:
96
- st.write("βœ… User has authenticated using text")
 
 
 
 
 
97
 
98
- # Display the main area if authenticated
99
  if st.session_state['authenticated']:
100
  st.write("## Main Area")
101
-
102
  # Display history for the specific phone number
103
- st.write("## πŸ“œ Authenticated! Below is Your Verification History")
104
  history_files = [f for f in os.listdir() if f.startswith('phone-') and st.session_state['phone_number'] in f]
 
 
 
105
  for file_name in history_files:
106
  with open(file_name, 'r') as file:
107
- st.write(f"{file_name}: {file.read()}")
108
-
109
- else:
110
- if st.sidebar.button('πŸ“¨ Send Verify Request'):
111
- send_verification_sms()
 
3
  import streamlit as st
4
  import textflowsms as tf
5
  from datetime import datetime
 
 
6
  import pytz
7
+
8
+ # 1. Central Time Zone Adjustment
9
  central = pytz.timezone('US/Central')
10
 
11
+ # 2. Function to format phone number
12
  def format_phone_number(phone_number):
13
  if len(phone_number) == 10 and not phone_number.startswith('+'):
14
  return '+1' + phone_number
15
  return phone_number
16
 
17
+ # 3. Function to hash a password
18
  def hash_password(password):
19
  return hashlib.sha256(password.encode()).hexdigest()
20
 
21
+ # 4. Function to save user data to a file
22
  def save_user_data(phone_number, password_hash):
23
  timestamp = datetime.now(central).strftime('%d%m%y-%H-%M')
24
  file_name = f"phone-{timestamp}.txt"
 
26
  file.write(f"{password_hash}\n")
27
  return file_name
28
 
29
+ # 5. Function to check if user is authenticated
30
  def is_user_authenticated(phone_number, hash_value):
31
  for file_name in os.listdir():
32
  if file_name.startswith('phone-') and phone_number in file_name:
 
36
  return True
37
  return False
38
 
39
+ # 6. Function to log events using markdown
40
+ def log_event(message, emoji):
41
+ timestamp = datetime.now(central).strftime('%Y-%m-%d %H:%M:%S')
42
+ st.markdown(f"{emoji} **{timestamp}:** {message}")
43
+
44
+ # 7. Initialize session state
45
  if 'phone_number' not in st.session_state:
46
+ st.session_state['phone_number'] = '+19522583980'
47
  if 'password' not in st.session_state:
48
  st.session_state['password'] = ''
49
  if 'hash' not in st.session_state:
 
51
  if 'authenticated' not in st.session_state:
52
  st.session_state['authenticated'] = False
53
 
54
+ # 8. Sidebar inputs
55
  user_phone_input = st.sidebar.text_input("πŸ“± Mobile Phone", value=st.session_state.get('phone_number', ''))
56
  password_input = st.sidebar.text_input("πŸ”‘ Set Password", type='password')
57
 
58
+ # 9. Save button
59
  if st.sidebar.button('πŸ’Ύ Save Settings'):
60
  st.session_state['phone_number'] = format_phone_number(user_phone_input)
61
  hashed_password = hash_password(password_input)
62
  st.session_state['password'] = hashed_password
63
  file_name = save_user_data(st.session_state['phone_number'], hashed_password)
64
  st.sidebar.success(f"Settings saved successfully! Data saved in {file_name}")
65
+ log_event("Settings saved successfully", "πŸ’Ύ")
 
 
66
 
67
+ # 10. Function to send verification SMS
68
  def send_verification_sms():
 
 
69
  api_key = os.getenv('API_KEY')
70
  tf.useKey(api_key)
 
71
 
72
  base_url = "https://huggingface.co/spaces/awacke1/RT-SMS-Phone-Verify"
73
+ phone = format_phone_number(user_phone_input)
74
+ hash_message = f"Verify here: {base_url}?hash={st.session_state['password']}"
 
 
 
 
 
 
 
 
 
 
 
 
 
75
 
76
+ result = tf.sendSMS(phone, hash_message)
77
+ if result.ok:
78
+ st.sidebar.success("Verification link sent via SMS πŸ“¨")
79
+ log_event("Verification SMS sent", "πŸ“¨")
80
+ else:
81
+ st.sidebar.error("Failed to send SMS ❌")
82
+ log_event("Failed to send SMS", "❌")
83
 
84
+ # 11. URL hash handling
85
  query_params = st.experimental_get_query_params()
86
  if 'hash' in query_params:
87
+ received_hash = query_params['hash'][0]
88
+ st.session_state['hash'] = received_hash
89
+ is_auth = is_user_authenticated(st.session_state['phone_number'], received_hash)
90
+ st.session_state['authenticated'] = is_auth
91
+
92
+ if is_auth:
93
+ log_event("User authenticated using text", "βœ…")
94
+ else:
95
+ log_event("Authentication failed. Hash mismatch or user not found", "❌")
96
 
97
+ # 12. Display the main area if authenticated
98
  if st.session_state['authenticated']:
99
  st.write("## Main Area")
100
+
101
  # Display history for the specific phone number
102
+ st.write("## πŸ“œ Authenticated! Below is Your Verification History")
103
  history_files = [f for f in os.listdir() if f.startswith('phone-') and st.session_state['phone_number'] in f]
104
+
105
+ # Create a markdown table for history
106
+ history_markdown = "| Filename | Hash Value |\n| --- | --- |\n"
107
  for file_name in history_files:
108
  with open(file_name, 'r') as file:
109
+ history_markdown += f"| {file_name} | {file.read().strip()} |\n"