Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,26 @@
|
|
| 1 |
import os
|
| 2 |
import re
|
| 3 |
import gradio as gr
|
|
|
|
| 4 |
import pandas as pd
|
|
|
|
| 5 |
|
| 6 |
# Các biến toàn cục
|
| 7 |
OUTPUT_FILE_TXT = "output.txt"
|
| 8 |
OUTPUT_FILE_XLSX = "output.xlsx"
|
| 9 |
-
TEMP_FILE = "temp.txt"
|
|
|
|
| 10 |
|
| 11 |
def write_file(file_path, data):
|
| 12 |
with open(file_path, 'w', encoding='utf-8') as file:
|
| 13 |
file.writelines(data)
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
def read_file(file):
|
| 16 |
if file is None:
|
| 17 |
return ""
|
|
@@ -27,6 +36,62 @@ def count_emails(data):
|
|
| 27 |
lines = [line.strip() for line in data.splitlines() if line.strip()]
|
| 28 |
return len(lines)
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
def split_emails(input_data, uploaded_file):
|
| 31 |
data = input_data if input_data else read_file(uploaded_file)
|
| 32 |
if not data:
|
|
@@ -43,13 +108,14 @@ def split_emails(input_data, uploaded_file):
|
|
| 43 |
if match:
|
| 44 |
gmail_addresses.append(match.group(1) + '\n')
|
| 45 |
|
| 46 |
-
#
|
| 47 |
-
|
| 48 |
-
|
| 49 |
output_data = "".join(gmail_addresses)
|
|
|
|
| 50 |
input_total = count_emails(data)
|
| 51 |
output_total = count_emails(output_data)
|
| 52 |
-
return f"Đã lưu kết quả.", gr.update(value=data, label=f"Input Text (Total: {input_total})"), gr.update(value=output_data, label=f"Output Text (Total: {output_total})"), None
|
| 53 |
|
| 54 |
def fill_missing_parts(input_data, uploaded_file):
|
| 55 |
data = input_data if input_data else read_file(uploaded_file)
|
|
@@ -75,44 +141,7 @@ def fill_missing_parts(input_data, uploaded_file):
|
|
| 75 |
output_total = count_emails(output_data)
|
| 76 |
return f"Đã lưu kết quả.", gr.update(value=data, label=f"Input Text (Total: {input_total})"), gr.update(value=output_data, label=f"Output Text (Total: {output_total})"), None
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
for file_path in [OUTPUT_FILE_TXT, OUTPUT_FILE_XLSX]:
|
| 81 |
-
if os.path.exists(file_path):
|
| 82 |
-
os.remove(file_path)
|
| 83 |
-
return "Đã xóa các file output.", gr.update(value="", label="Input Text (Total: 0)"), gr.update(value="", label="Output Text (Total: 0)"), None
|
| 84 |
-
|
| 85 |
-
# Giao diện Gradio (chỉ giữ lại 2 tab chính)
|
| 86 |
-
with gr.Blocks(title="Email Processor Pro", theme=gr.themes.Soft()) as demo:
|
| 87 |
-
with gr.Row(equal_height=True):
|
| 88 |
-
with gr.Column(scale=1):
|
| 89 |
-
input_text = gr.Textbox(label="Input Text", lines=10,
|
| 90 |
-
placeholder="Nhập dữ liệu hoặc tải file .txt lên...")
|
| 91 |
-
upload_file = gr.File(label="📤 Upload File (.txt)", file_types=[".txt"])
|
| 92 |
-
clear_input_btn = gr.Button("🧹 Clear Input")
|
| 93 |
-
|
| 94 |
-
with gr.Column(scale=1):
|
| 95 |
-
output_text = gr.Textbox(label="Output Text", lines=10, interactive=False)
|
| 96 |
-
download_file = gr.File(label="📥 Download Files", interactive=False, file_count="multiple")
|
| 97 |
-
clear_output_btn = gr.Button("🧹 Clear Output")
|
| 98 |
-
download_btn = gr.Button("💾 Download Output")
|
| 99 |
-
|
| 100 |
-
status = gr.Textbox(label="🔄 Status", interactive=False)
|
| 101 |
-
|
| 102 |
-
with gr.Tabs():
|
| 103 |
-
with gr.Tab("🔧 Email Tools"):
|
| 104 |
-
with gr.Row():
|
| 105 |
-
split_btn = gr.Button("✂️ Split Emails")
|
| 106 |
-
fill_btn = gr.Button("🔍 Fill Missing")
|
| 107 |
-
|
| 108 |
-
clear_files_btn = gr.Button("🧹 Clear All Data", size="lg")
|
| 109 |
-
|
| 110 |
-
# Kết nối sự kiện
|
| 111 |
-
split_btn.click(split_emails, inputs=[input_text, upload_file], outputs=[status, input_text, output_text, download_file])
|
| 112 |
-
fill_btn.click(fill_missing_parts, inputs=[input_text, upload_file], outputs=[status, input_text, output_text, download_file])
|
| 113 |
-
clear_files_btn.click(clear_files, outputs=[status, input_text, output_text, download_file])
|
| 114 |
-
clear_input_btn.click(lambda: ["", gr.update(value="", label="Input Text (Total: 0)"), gr.update(value="", label="Output Text (Total: 0)"), None, "Đã xóa input."], outputs=[input_text, input_text, output_text, upload_file, status])
|
| 115 |
-
clear_output_btn.click(clear_files, outputs=[status, input_text, output_text, download_file])
|
| 116 |
-
download_btn.click(download_output, inputs=[input_text, output_text, upload_file], outputs=[status, input_text, output_text, download_file])
|
| 117 |
|
| 118 |
-
demo.launch()
|
|
|
|
| 1 |
import os
|
| 2 |
import re
|
| 3 |
import gradio as gr
|
| 4 |
+
import random
|
| 5 |
import pandas as pd
|
| 6 |
+
from datetime import datetime # ✅ Thêm để ghi thời gian
|
| 7 |
|
| 8 |
# Các biến toàn cục
|
| 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 |
def write_file(file_path, data):
|
| 15 |
with open(file_path, 'w', encoding='utf-8') as file:
|
| 16 |
file.writelines(data)
|
| 17 |
|
| 18 |
+
# ✅ Hàm mới: ghi nối tiếp log
|
| 19 |
+
def append_log(file_path, data):
|
| 20 |
+
with open(file_path, 'a', encoding='utf-8') as file:
|
| 21 |
+
file.write(f"\n--- {datetime.now().strftime('%Y-%m-%d %H:%M:%S')} ---\n")
|
| 22 |
+
file.writelines(data)
|
| 23 |
+
|
| 24 |
def read_file(file):
|
| 25 |
if file is None:
|
| 26 |
return ""
|
|
|
|
| 36 |
lines = [line.strip() for line in data.splitlines() if line.strip()]
|
| 37 |
return len(lines)
|
| 38 |
|
| 39 |
+
def replace_char(input_data, uploaded_file):
|
| 40 |
+
data = input_data if input_data else read_file(uploaded_file)
|
| 41 |
+
if not data:
|
| 42 |
+
return gr.update(value="", label="Output Text (Total: 0)"), "Lỗi: Không có dữ liệu."
|
| 43 |
+
if isinstance(data, str) and data.startswith("Lỗi"):
|
| 44 |
+
return gr.update(value="", label="Output Text (Total: 0)"), data
|
| 45 |
+
|
| 46 |
+
output_text = re.sub(r'[|\t;:]', '\t', data)
|
| 47 |
+
return gr.update(value=output_text, label=f"Output Text (Total: {count_emails(output_text)})"), "Thay thế thành công!"
|
| 48 |
+
|
| 49 |
+
def add_random_dot(email):
|
| 50 |
+
if "@gmail.com" in email:
|
| 51 |
+
local, domain = email.split("@")
|
| 52 |
+
if len(local) < 2:
|
| 53 |
+
return email
|
| 54 |
+
pos = random.randint(1, len(local) - 1)
|
| 55 |
+
return f"{local[:pos]}.{local[pos:]}@{domain}"
|
| 56 |
+
return email
|
| 57 |
+
|
| 58 |
+
def remove_dots(email):
|
| 59 |
+
if "@gmail.com" in email:
|
| 60 |
+
local, domain = email.split("@")
|
| 61 |
+
return f"{local.replace('.', '')}@{domain}"
|
| 62 |
+
return email
|
| 63 |
+
|
| 64 |
+
def modify_dots(input_data, uploaded_file, action):
|
| 65 |
+
data = input_data if input_data else read_file(uploaded_file)
|
| 66 |
+
if not data:
|
| 67 |
+
return gr.update(label="Input Text (Total: 0)"), gr.update(value="", label="Output Text (Total: 0)"), "Lỗi: Không có dữ liệu."
|
| 68 |
+
|
| 69 |
+
lines = data.splitlines()
|
| 70 |
+
processed_lines = []
|
| 71 |
+
|
| 72 |
+
for line in lines:
|
| 73 |
+
if not line.strip():
|
| 74 |
+
continue
|
| 75 |
+
|
| 76 |
+
parts = re.split(r'([\t|;:])', line, 1)
|
| 77 |
+
if len(parts) < 3:
|
| 78 |
+
processed_lines.append(line)
|
| 79 |
+
continue
|
| 80 |
+
|
| 81 |
+
email = parts[0].strip()
|
| 82 |
+
separator = parts[1]
|
| 83 |
+
rest = parts[2] if len(parts) > 2 else ""
|
| 84 |
+
|
| 85 |
+
if action == "Add":
|
| 86 |
+
modified_email = add_random_dot(email)
|
| 87 |
+
else:
|
| 88 |
+
modified_email = remove_dots(email)
|
| 89 |
+
|
| 90 |
+
processed_lines.append(f"{modified_email}{separator}{rest}")
|
| 91 |
+
|
| 92 |
+
output_data = "\n".join(processed_lines)
|
| 93 |
+
return gr.update(label=f"Input Text (Total: {count_emails(data)})"), gr.update(value=output_data, label=f"Output Text (Total: {count_emails(output_data)})"), "Xử lý thành công!"
|
| 94 |
+
|
| 95 |
def split_emails(input_data, uploaded_file):
|
| 96 |
data = input_data if input_data else read_file(uploaded_file)
|
| 97 |
if not data:
|
|
|
|
| 108 |
if match:
|
| 109 |
gmail_addresses.append(match.group(1) + '\n')
|
| 110 |
|
| 111 |
+
# ✅ Lưu nối tiếp vào log thay vì ghi đè
|
| 112 |
+
append_log(TEMP_FILE, [line + '\n' for line in lines])
|
| 113 |
+
|
| 114 |
output_data = "".join(gmail_addresses)
|
| 115 |
+
|
| 116 |
input_total = count_emails(data)
|
| 117 |
output_total = count_emails(output_data)
|
| 118 |
+
return f"Đã lưu kết quả vào log.", gr.update(value=data, label=f"Input Text (Total: {input_total})"), gr.update(value=output_data, label=f"Output Text (Total: {output_total})"), None
|
| 119 |
|
| 120 |
def fill_missing_parts(input_data, uploaded_file):
|
| 121 |
data = input_data if input_data else read_file(uploaded_file)
|
|
|
|
| 141 |
output_total = count_emails(output_data)
|
| 142 |
return f"Đã lưu kết quả.", gr.update(value=data, label=f"Input Text (Total: {input_total})"), gr.update(value=output_data, label=f"Output Text (Total: {output_total})"), None
|
| 143 |
|
| 144 |
+
# Các hàm còn lại giữ nguyên như cũ...
|
| 145 |
+
# (phần code Data Management, Replace Text, Clear, Download, CSS, UI setup...)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
|
| 147 |
+
# Ở cuối file, hàm demo.launch() vẫn giữ nguyên
|