GmailTrust commited on
Commit
9fcb495
·
verified ·
1 Parent(s): 51299ae

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -83
app.py CHANGED
@@ -5,13 +5,14 @@ import random
5
  import pandas as pd
6
  from datetime import datetime
7
 
8
- # ==== Global variables ====
9
  OUTPUT_FILE_TXT = "output.txt"
10
  OUTPUT_FILE_XLSX = "output.xlsx"
11
  TEMP_FILE = "temp.txt"
12
  TAKEN_ROWS_FILE = "taken_rows.txt"
 
13
 
14
- # ==== File write helpers ====
15
  def write_file(file_path, data):
16
  with open(file_path, 'w', encoding='utf-8') as file:
17
  file.writelines(data)
@@ -30,9 +31,9 @@ def read_file(file):
30
  except Exception as e:
31
  return f"Lỗi khi đọc file: {str(e)}"
32
 
33
- # ==== Log viewer ====
34
  def view_log(password):
35
- if password != "hihihaha":
36
  return "❌ Wrong password!"
37
  if not os.path.exists(TEMP_FILE):
38
  return "No log file found."
@@ -43,6 +44,14 @@ def view_log(password):
43
  except Exception as e:
44
  return f"Error reading log: {e}"
45
 
 
 
 
 
 
 
 
 
46
  # ==== Utils ====
47
  def count_emails(data):
48
  if not data or (isinstance(data, str) and data.startswith("Lỗi")):
@@ -50,16 +59,6 @@ def count_emails(data):
50
  lines = [line.strip() for line in data.splitlines() if line.strip()]
51
  return len(lines)
52
 
53
- # ==== Core features ====
54
- def replace_char(input_data, uploaded_file):
55
- data = input_data if input_data else read_file(uploaded_file)
56
- if not data:
57
- return gr.update(value="", label="Output Text (Total: 0)"), "Lỗi: Không có dữ liệu."
58
- if isinstance(data, str) and data.startswith("Lỗi"):
59
- return gr.update(value="", label="Output Text (Total: 0)"), data
60
- output_text = re.sub(r'[|\t;:]', '\t', data)
61
- return gr.update(value=output_text, label=f"Output Text (Total: {count_emails(output_text)})"), "Thay thế thành công!"
62
-
63
  def add_random_dot(email):
64
  if "@gmail.com" in email:
65
  local, domain = email.split("@")
@@ -75,6 +74,14 @@ def remove_dots(email):
75
  return f"{local.replace('.', '')}@{domain}"
76
  return email
77
 
 
 
 
 
 
 
 
 
78
  def modify_dots(input_data, uploaded_file, action):
79
  data = input_data if input_data else read_file(uploaded_file)
80
  if not data:
@@ -129,64 +136,11 @@ def fill_missing_parts(input_data, uploaded_file):
129
  output_data = "".join(output_lines)
130
  return f"Đã lưu kết quả.", gr.update(value=data, label=f"Input Text (Total: {count_emails(data)})"), gr.update(value=output_data, label=f"Output Text (Total: {count_emails(output_data)})"), None
131
 
132
- def take_top_rows(input_data, uploaded_file, num_rows):
133
- data = input_data if input_data else read_file(uploaded_file)
134
- if not data:
135
- return "Cảnh báo: Không có dữ liệu.", gr.update(label="Input Text (Total: 0)"), gr.update(value="", label="Output Text (Total: 0)"), None
136
- lines = [line.strip() for line in data.splitlines() if line.strip()]
137
- try:
138
- num_rows = int(num_rows)
139
- if num_rows <= 0:
140
- return "Lỗi: Số hàng phải lớn hơn 0.", gr.update(label=f"Input Text (Total: {count_emails(data)})"), gr.update(value="", label="Output Text (Total: 0)"), None
141
- except:
142
- return "Lỗi: Vui lòng nhập số hợp lệ.", gr.update(label=f"Input Text (Total: {count_emails(data)})"), gr.update(value="", label="Output Text (Total: 0)"), None
143
- num_rows = min(num_rows, len(lines))
144
- output_lines = lines[:num_rows]
145
- output_data = "\n".join(output_lines) + "\n" if output_lines else ""
146
- write_file(TAKEN_ROWS_FILE, [line + '\n' for line in output_lines])
147
- return "Đã lấy các hàng đầu tiên!", gr.update(value=data, label=f"Input Text (Total: {count_emails(data)})"), gr.update(value=output_data, label=f"Output Text (Total: {count_emails(output_data)})"), None
148
-
149
- def get_remaining_rows(input_data, uploaded_file):
150
- data = input_data if input_data else read_file(uploaded_file)
151
- if not data:
152
- return "Cảnh báo: Không có dữ liệu.", gr.update(label="Input Text (Total: 0)"), gr.update(value="", label="Output Text (Total: 0)"), None
153
- if not os.path.exists(TAKEN_ROWS_FILE):
154
- return "Lỗi: Vui lòng chạy 'Take Top Rows' trước.", gr.update(label=f"Input Text (Total: {count_emails(data)})"), gr.update(value="", label="Output Text (Total: 0)"), None
155
- lines = [line.strip() for line in data.splitlines() if line.strip()]
156
- with open(TAKEN_ROWS_FILE, 'r', encoding='utf-8') as file:
157
- taken_rows = {line.strip() for line in file if line.strip()}
158
- remaining_lines = [line for line in lines if line not in taken_rows]
159
- output_data = "\n".join(remaining_lines) + "\n" if remaining_lines else ""
160
- if os.path.exists(TAKEN_ROWS_FILE):
161
- os.remove(TAKEN_ROWS_FILE)
162
- return "Đã lấy các hàng còn lại!", gr.update(value=data, label=f"Input Text (Total: {count_emails(data)})"), gr.update(value=output_data, label=f"Output Text (Total: {count_emails(output_data)})"), None
163
-
164
- def replace_text(input_data, uploaded_file, text_to_replace, replacement_text):
165
- data = input_data if input_data else read_file(uploaded_file)
166
- if not data:
167
- return gr.update(value="", label="Output Text (Total: 0)"), "Lỗi: Không có dữ liệu."
168
- if isinstance(data, str) and data.startswith("Lỗi"):
169
- return gr.update(value="", label="Output Text (Total: 0)"), data
170
- if not text_to_replace:
171
- return gr.update(value=data, label=f"Output Text (Total: {count_emails(data)})"), "Lỗi: Vui lòng nhập văn bản cần thay thế."
172
- output_text = re.sub(re.escape(text_to_replace), replacement_text, data)
173
- return gr.update(value=output_text, label=f"Output Text (Total: {count_emails(output_text)})"), "Thay thế văn bản thành công!"
174
-
175
- def clear_input(input_data, output_data, uploaded_file):
176
- return "", gr.update(value="", label="Input Text (Total: 0)"), gr.update(value=output_data, label=f"Output Text (Total: {count_emails(output_data)})"), None, "Đã xóa input."
177
-
178
- def clear_output(input_data, output_data, uploaded_file):
179
- if os.path.exists(OUTPUT_FILE_TXT):
180
- os.remove(OUTPUT_FILE_TXT)
181
- if os.path.exists(OUTPUT_FILE_XLSX):
182
- os.remove(OUTPUT_FILE_XLSX)
183
- return "Đã xóa output.", gr.update(value=input_data, label=f"Input Text (Total: {count_emails(input_data)})"), gr.update(value="", label="Output Text (Total: 0)"), None
184
-
185
  def clear_files():
186
- for file_path in [TEMP_FILE, OUTPUT_FILE_TXT, OUTPUT_FILE_XLSX, TAKEN_ROWS_FILE]:
187
  if os.path.exists(file_path):
188
  os.remove(file_path)
189
- return "Đã xóa các file.", gr.update(value="", label="Input Text (Total: 0)"), gr.update(value="", label="Output Text (Total: 0)"), None, None
190
 
191
  def download_output(input_data, output_data, uploaded_file):
192
  if not output_data:
@@ -214,7 +168,7 @@ def download_output(input_data, output_data, uploaded_file):
214
  [OUTPUT_FILE_TXT, OUTPUT_FILE_XLSX])
215
 
216
  # ==== UI ====
217
- with gr.Blocks(title="Email Processor Pro", theme=gr.themes.Soft()) as demo:
218
  with gr.Row(equal_height=True):
219
  with gr.Column(scale=1):
220
  input_text = gr.Textbox(label="Input Text", lines=10)
@@ -232,20 +186,13 @@ with gr.Blocks(title="Email Processor Pro", theme=gr.themes.Soft()) as demo:
232
  split_btn = gr.Button("✂️ Split Emails")
233
  fill_btn = gr.Button("🔍 Fill Missing")
234
  replace_char_btn = gr.Button("⇥ Replace Separators")
235
- with gr.Tab("✏️ Text Tools"):
236
- text_to_replace = gr.Textbox(label="Text to replace:")
237
- replacement_text = gr.Textbox(label="Replacement text:")
238
- replace_text_btn = gr.Button("🔄 Replace All")
239
  dot_action = gr.Radio(choices=["Add", "Remove"], label="Dot Action", value="Add")
240
  modify_dots_btn = gr.Button("• Modify Dots")
241
- with gr.Tab("📊 Data Management"):
242
- num_rows = gr.Number(label="Number of Rows", value=0, precision=0)
243
- take_top_rows_btn = gr.Button("⬇️ Take Top Rows")
244
- get_remaining_rows_btn = gr.Button("↕️ Get Remaining")
245
  with gr.Tab("📜 View Log"):
246
  log_password = gr.Textbox(label="Enter password:", type="password", placeholder="Password required")
247
  view_log_btn = gr.Button("View Log")
248
  log_output = gr.Textbox(label="Log Content", lines=15, interactive=False)
 
249
 
250
  clear_files_btn = gr.Button("🧹 Clear All Data")
251
 
@@ -253,13 +200,11 @@ with gr.Blocks(title="Email Processor Pro", theme=gr.themes.Soft()) as demo:
253
  fill_btn.click(fill_missing_parts, inputs=[input_text, upload_file], outputs=[status, input_text, output_text, download_file])
254
  replace_char_btn.click(replace_char, inputs=[input_text, upload_file], outputs=[output_text, status])
255
  modify_dots_btn.click(modify_dots, inputs=[input_text, upload_file, dot_action], outputs=[input_text, output_text, status])
256
- take_top_rows_btn.click(take_top_rows, inputs=[input_text, upload_file, num_rows], outputs=[status, input_text, output_text, download_file])
257
- get_remaining_rows_btn.click(get_remaining_rows, inputs=[input_text, upload_file], outputs=[status, input_text, output_text, download_file])
258
- replace_text_btn.click(replace_text, inputs=[input_text, upload_file, text_to_replace, replacement_text], outputs=[output_text, status])
259
  clear_files_btn.click(clear_files, outputs=[status, input_text, output_text, download_file, upload_file])
260
- clear_input_btn.click(clear_input, inputs=[input_text, output_text, upload_file], outputs=[input_text, input_text, output_text, upload_file, status])
261
- clear_output_btn.click(clear_output, inputs=[input_text, output_text, upload_file], outputs=[status, input_text, output_text, download_file])
262
  download_btn.click(download_output, inputs=[input_text, output_text, upload_file], outputs=[status, input_text, output_text, download_file])
263
  view_log_btn.click(view_log, inputs=[log_password], outputs=[log_output])
 
264
 
265
  demo.launch()
 
5
  import pandas as pd
6
  from datetime import datetime
7
 
8
+ # ==== Config ====
9
  OUTPUT_FILE_TXT = "output.txt"
10
  OUTPUT_FILE_XLSX = "output.xlsx"
11
  TEMP_FILE = "temp.txt"
12
  TAKEN_ROWS_FILE = "taken_rows.txt"
13
+ LOG_SECRET_KEY = os.environ.get("LOG_SECRET_KEY", "hihihaha") # đọc từ secret env
14
 
15
+ # ==== File helpers ====
16
  def write_file(file_path, data):
17
  with open(file_path, 'w', encoding='utf-8') as file:
18
  file.writelines(data)
 
31
  except Exception as e:
32
  return f"Lỗi khi đọc file: {str(e)}"
33
 
34
+ # ==== Log functions ====
35
  def view_log(password):
36
+ if password != LOG_SECRET_KEY:
37
  return "❌ Wrong password!"
38
  if not os.path.exists(TEMP_FILE):
39
  return "No log file found."
 
44
  except Exception as e:
45
  return f"Error reading log: {e}"
46
 
47
+ def clear_log(password):
48
+ if password != LOG_SECRET_KEY:
49
+ return "❌ Wrong password!"
50
+ if os.path.exists(TEMP_FILE):
51
+ os.remove(TEMP_FILE)
52
+ return "✅ Log cleared."
53
+ return "No log file to clear."
54
+
55
  # ==== Utils ====
56
  def count_emails(data):
57
  if not data or (isinstance(data, str) and data.startswith("Lỗi")):
 
59
  lines = [line.strip() for line in data.splitlines() if line.strip()]
60
  return len(lines)
61
 
 
 
 
 
 
 
 
 
 
 
62
  def add_random_dot(email):
63
  if "@gmail.com" in email:
64
  local, domain = email.split("@")
 
74
  return f"{local.replace('.', '')}@{domain}"
75
  return email
76
 
77
+ # ==== Core features ====
78
+ def replace_char(input_data, uploaded_file):
79
+ data = input_data if input_data else read_file(uploaded_file)
80
+ if not data:
81
+ return gr.update(value="", label="Output Text (Total: 0)"), "Lỗi: Không có dữ liệu."
82
+ output_text = re.sub(r'[|\t;:]', '\t', data)
83
+ return gr.update(value=output_text, label=f"Output Text (Total: {count_emails(output_text)})"), "Thay thế thành công!"
84
+
85
  def modify_dots(input_data, uploaded_file, action):
86
  data = input_data if input_data else read_file(uploaded_file)
87
  if not data:
 
136
  output_data = "".join(output_lines)
137
  return f"Đã lưu kết quả.", gr.update(value=data, label=f"Input Text (Total: {count_emails(data)})"), gr.update(value=output_data, label=f"Output Text (Total: {count_emails(output_data)})"), None
138
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
139
  def clear_files():
140
+ for file_path in [OUTPUT_FILE_TXT, OUTPUT_FILE_XLSX, TAKEN_ROWS_FILE]: # Không xóa log
141
  if os.path.exists(file_path):
142
  os.remove(file_path)
143
+ return "Đã xóa dữ liệu (log vẫn giữ).", gr.update(value="", label="Input Text (Total: 0)"), gr.update(value="", label="Output Text (Total: 0)"), None, None
144
 
145
  def download_output(input_data, output_data, uploaded_file):
146
  if not output_data:
 
168
  [OUTPUT_FILE_TXT, OUTPUT_FILE_XLSX])
169
 
170
  # ==== UI ====
171
+ with gr.Blocks(title="Email Processor", theme=gr.themes.Soft()) as demo:
172
  with gr.Row(equal_height=True):
173
  with gr.Column(scale=1):
174
  input_text = gr.Textbox(label="Input Text", lines=10)
 
186
  split_btn = gr.Button("✂️ Split Emails")
187
  fill_btn = gr.Button("🔍 Fill Missing")
188
  replace_char_btn = gr.Button("⇥ Replace Separators")
 
 
 
 
189
  dot_action = gr.Radio(choices=["Add", "Remove"], label="Dot Action", value="Add")
190
  modify_dots_btn = gr.Button("• Modify Dots")
 
 
 
 
191
  with gr.Tab("📜 View Log"):
192
  log_password = gr.Textbox(label="Enter password:", type="password", placeholder="Password required")
193
  view_log_btn = gr.Button("View Log")
194
  log_output = gr.Textbox(label="Log Content", lines=15, interactive=False)
195
+ clear_log_btn = gr.Button("🗑️ Clear Log")
196
 
197
  clear_files_btn = gr.Button("🧹 Clear All Data")
198
 
 
200
  fill_btn.click(fill_missing_parts, inputs=[input_text, upload_file], outputs=[status, input_text, output_text, download_file])
201
  replace_char_btn.click(replace_char, inputs=[input_text, upload_file], outputs=[output_text, status])
202
  modify_dots_btn.click(modify_dots, inputs=[input_text, upload_file, dot_action], outputs=[input_text, output_text, status])
 
 
 
203
  clear_files_btn.click(clear_files, outputs=[status, input_text, output_text, download_file, upload_file])
204
+ clear_input_btn.click(lambda *_: ("", "", "", None, "Đã xóa input."), outputs=[input_text, input_text, output_text, upload_file, status])
205
+ clear_output_btn.click(lambda *_: ("Đã xóa output.", "", "", None), outputs=[status, input_text, output_text, download_file])
206
  download_btn.click(download_output, inputs=[input_text, output_text, upload_file], outputs=[status, input_text, output_text, download_file])
207
  view_log_btn.click(view_log, inputs=[log_password], outputs=[log_output])
208
+ clear_log_btn.click(clear_log, inputs=[log_password], outputs=[status])
209
 
210
  demo.launch()