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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -39
app.py CHANGED
@@ -5,20 +5,20 @@ 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,7 +26,7 @@ def save_user_data(phone_number, password_hash):
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,12 +36,29 @@ def is_user_authenticated(phone_number, hash_value):
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:
@@ -51,50 +68,35 @@ 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
 
@@ -107,3 +109,40 @@ if st.session_state['authenticated']:
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"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  from datetime import datetime
6
  import pytz
7
 
8
+ # Central Time Zone Adjustment
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
  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
  return True
37
  return False
38
 
39
+ # 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
+ # Function to send verification SMS
45
+ def send_verification_sms(phone_number, password_hash):
46
+ api_key = os.getenv('API_KEY')
47
+ tf.useKey(api_key)
48
+
49
+ base_url = "https://huggingface.co/spaces/awacke1/RT-SMS-Phone-Verify"
50
+ phone = format_phone_number(phone_number)
51
+ hash_message = f"Verify here: {base_url}?hash={password_hash}"
52
+
53
+ result = tf.sendSMS(phone, hash_message)
54
+ if result.ok:
55
+ st.sidebar.success("Verification link sent via SMS πŸ“¨")
56
+ log_event("Verification SMS sent", "πŸ“¨")
57
+ else:
58
+ st.sidebar.error("Failed to send SMS ❌")
59
+ log_event("Failed to send SMS", "❌")
60
+
61
+ # Initialize session state
62
  if 'phone_number' not in st.session_state:
63
  st.session_state['phone_number'] = '+19522583980'
64
  if 'password' not in st.session_state:
 
68
  if 'authenticated' not in st.session_state:
69
  st.session_state['authenticated'] = False
70
 
71
+ # Sidebar inputs
72
  user_phone_input = st.sidebar.text_input("πŸ“± Mobile Phone", value=st.session_state.get('phone_number', ''))
73
  password_input = st.sidebar.text_input("πŸ”‘ Set Password", type='password')
74
 
75
+ # Save button
76
  if st.sidebar.button('πŸ’Ύ Save Settings'):
77
  st.session_state['phone_number'] = format_phone_number(user_phone_input)
78
+ if password_input:
79
+ hashed_password = hash_password(password_input)
80
+ st.session_state['password'] = hashed_password
81
+ file_name = save_user_data(st.session_state['phone_number'], hashed_password)
82
+ st.sidebar.success(f"Settings saved successfully! Data saved in {file_name}")
83
+ log_event("Settings saved successfully", "πŸ’Ύ")
84
+ send_verification_sms(st.session_state['phone_number'], hashed_password)
 
 
 
 
 
 
 
85
 
86
+ # URL hash handling
 
 
 
 
 
 
 
 
87
  query_params = st.experimental_get_query_params()
88
+ if 'phone' in query_params:
89
+ phone_from_url = format_phone_number(query_params['phone'][0])
90
+ st.session_state['phone_number'] = phone_from_url
91
+ is_auth = any(phone_from_url in file for file in os.listdir() if file.startswith('phone-'))
92
  st.session_state['authenticated'] = is_auth
93
 
94
  if is_auth:
95
+ log_event("User authenticated using phone number from URL", "βœ…")
96
  else:
97
+ log_event("No authentication records found for the phone number in URL", "❌")
98
 
99
+ # Display the main area if authenticated
100
  if st.session_state['authenticated']:
101
  st.write("## Main Area")
102
 
 
109
  for file_name in history_files:
110
  with open(file_name, 'r') as file:
111
  history_markdown += f"| {file_name} | {file.read().strip()} |\n"
112
+ st.markdown(history_markdown)
113
+
114
+ st.markdown(history_markdown)
115
+ else:
116
+ # If not authenticated, display a message
117
+ st.write("## ❗ Authentication Required")
118
+ st.write("Please authenticate using the link sent to your mobile phone.")
119
+
120
+
121
+ # Import additional libraries
122
+ import base64
123
+
124
+ # Function to create a base64-encoded download link
125
+ def create_download_link(file_name):
126
+ with open(file_name, 'rb') as f:
127
+ bytes = f.read()
128
+ b64 = base64.b64encode(bytes).decode()
129
+ href = f'<a href="data:file/txt;base64,{b64}" download="{file_name}">Download {file_name}</a>'
130
+ return href
131
+
132
+ # Display a list of history files with download links and contents
133
+ st.write("## πŸ—‚οΈ File History")
134
+
135
+ # Iterate over history files and display them
136
+ history_files = [f for f in os.listdir() if f.startswith('phone-')]
137
+ for file_name in history_files:
138
+ # Create download link
139
+ download_link = create_download_link(file_name)
140
+
141
+ # Read and display file contents
142
+ with open(file_name, 'r') as file:
143
+ file_contents = file.read().strip()
144
+
145
+ # Display file information and contents in markdown
146
+ st.markdown(f"### {file_name}")
147
+ st.markdown(download_link, unsafe_allow_html=True)
148
+ st.markdown("```python\n" + file_contents + "\n```")