PoemForSmallFThings / NAIA /Beta /parquet_token_update_tool.py
baqu2213's picture
Upload 2 files
ad7e448 verified
import os
import pandas as pd
from transformers import CLIPTokenizer
import tkinter as tk
from tkinter import filedialog, scrolledtext
import threading
# CLIPTokenizer μ΄ˆκΈ°ν™”
s_token = CLIPTokenizer.from_pretrained("openai/clip-vit-base-patch32")
def rcs(text):
if text is None:
return None
token_ids = s_token.encode(text)
return len(token_ids)
def process_files(dflist, text_box, select_button, process_button):
# /tags ν•˜μœ„ 폴더 λ§Œλ“€κΈ°
output_dir = "processed"
os.makedirs(output_dir, exist_ok=True)
for i, _df in enumerate(dflist):
# parquet 파일 읽기
df = pd.read_parquet(_df, engine="pyarrow")
# 'tokens' μ—΄ μΆ”κ°€
tokens = []
total = len(df)
for idx, text in enumerate(df['general']):
if text is not None:
tokens.append(rcs(text))
# 진행 상황 좜λ ₯
if (idx + 1) % 100 == 0 or idx + 1 == total:
progress = f"Processing file {_df}: {idx + 1}/{total} ({(idx + 1) / total * 100:.2f}%)\n"
text_box.insert(tk.END, progress)
text_box.see(tk.END)
else:
tokens.append(None)
df['tokens'] = tokens
# 처리된 파일 μ €μž₯ 경둜
output_path = os.path.join(output_dir, os.path.basename(_df))
# parquet 파일둜 μ €μž₯
df.to_parquet(output_path, engine="pyarrow")
text_box.insert(tk.END, f"Finished processing {_df}\n")
text_box.see(tk.END)
text_box.insert(tk.END, "λͺ¨λ“  파일이 μ„±κ³΅μ μœΌλ‘œ μ²˜λ¦¬λ˜μ—ˆμŠ΅λ‹ˆλ‹€.\n")
text_box.see(tk.END)
# μž‘μ—…μ΄ λͺ¨λ‘ μ’…λ£Œλ˜λ©΄ output_dir μœˆλ„μš° 폴더가 μ—΄λ¦°λ‹€.
os.startfile(output_dir)
# dflist μ΄ˆκΈ°ν™”
dflist = []
# λ²„νŠΌ λ‹€μ‹œ ν™œμ„±ν™”
select_button.config(state=tk.NORMAL)
process_button.config(state=tk.NORMAL)
def select_files():
file_paths = filedialog.askopenfilenames(filetypes=[("Parquet files", "*.parquet")])
if file_paths:
dflist.extend(file_paths)
text_box.insert(tk.END, f"Selected files:\n{file_paths}\n")
text_box.see(tk.END)
def start_processing():
if not dflist:
return
select_button.config(state=tk.DISABLED)
process_button.config(state=tk.DISABLED)
# 파일 처리 μŠ€λ ˆλ“œ μ‹œμž‘
threading.Thread(target=process_files, args=(dflist, text_box, select_button, process_button)).start()
# Tkinter UI μ„€μ •
root = tk.Tk()
root.title("ν”„λ‘¬ν”„νŠΈ μŠ€νƒœμ»€μš© parquet 파일 토큰 μ—…λ°μ΄νŠΈ 도ꡬ")
frame = tk.Frame(root)
frame.pack(padx=10, pady=10)
select_button = tk.Button(frame, text="Parquet 파일 선택", command=select_files)
select_button.pack(side=tk.LEFT, padx=5, pady=5)
process_button = tk.Button(frame, text="토큰 계산 μ‹œμž‘", command=start_processing)
process_button.pack(side=tk.LEFT, padx=5, pady=5)
text_box = scrolledtext.ScrolledText(root, width=80, height=20)
text_box.pack(padx=10, pady=10)
# dflist μ΄ˆκΈ°ν™”
dflist = []
root.mainloop()