online_tools / modules /splitter.py
admin
sync ms
2000f4c
raw
history blame
1.38 kB
import math
import gradio as gr
from utils import EN_US
ZH2EN = {
"待分割字符串": "String to be split",
"分割步长": "Split step",
"状态栏": "Status",
"分割结果": "Split result",
}
def _L(zh_txt: str):
return ZH2EN[zh_txt] if EN_US else zh_txt
def infer(cookie: str, step: int):
status = "Success"
output = ""
try:
cookie = cookie.strip()
if not cookie:
raise ValueError("请输入 cookie !")
size = len(cookie)
count = math.ceil(size / step)
for i in range(count):
output += f"""
## {i + 1}
```txt
{cookie[i * step : min((i + 1) * step, size)]}
```
"""
except Exception as e:
status = f"{e}"
return status, output
def str_splitter():
return gr.Interface(
fn=infer,
inputs=[
gr.TextArea(label=_L("待分割字符串")),
gr.Slider(
label=_L("分割步长"),
minimum=1,
maximum=255959,
step=1,
value=1024,
),
],
outputs=[
gr.Textbox(label=_L("状态栏"), show_copy_button=True),
gr.Markdown(label=_L("分割结果"), container=True, show_copy_button=True),
],
flagging_mode="never",
)