Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import random, pandas as pd, tempfile, os | |
| SEEDS = { | |
| "3C 数码": [ | |
| "ventilateur portable","mini ventilateur","ventilateur de poche","ventilateur USB", | |
| "recharge USB","léger et compact","silencieux","puissant", | |
| "pour bureau","pour voyage","été","rafraîchissement" | |
| ], | |
| "默认": [ | |
| "portable","compact","léger","rechargeable","USB","silencieux", | |
| "idéale pour voyage","maison et bureau","été","rafraîchissement" | |
| ] | |
| } | |
| TEMPLATES = [ | |
| "{p} – {k1}, {k2}, {k3}", | |
| "Mini {p} {k1}, {k2} · idéal pour {k3}", | |
| "{p} rechargeable {k1} · {k2} et {k3}", | |
| "{p} {k1} · {k2} · parfait pour {k3}", | |
| "{p} – {k1} | {k2} | {k3}" | |
| ] | |
| def pick_keywords(category): | |
| base = SEEDS.get(category.strip(), []) + SEEDS["默认"] | |
| # 去重保持顺序 | |
| seen, uniq = set(), [] | |
| for w in base: | |
| if w not in seen: | |
| uniq.append(w); seen.add(w) | |
| return uniq | |
| def gen_titles(product, category, n_titles): | |
| if not product or not category: | |
| return "请填写商品名称和类目", pd.DataFrame(), None | |
| kws = pick_keywords(category) | |
| # 关键词展示 | |
| kw_text = ", ".join(kws) | |
| # 生成标题 | |
| titles = [] | |
| for _ in range(int(n_titles)): | |
| k1, k2, k3 = random.sample(kws, 3) if len(kws) >= 3 else (kws + ["portable","compact","USB"])[:3] | |
| tpl = random.choice(TEMPLATES) | |
| title = tpl.format(p=product.strip(), k1=k1, k2=k2, k3=k3) | |
| # 控制在大约 60-80 字符(法语场景常见) | |
| titles.append(title[:95]) | |
| df = pd.DataFrame({"Titre FR (pour TikTok Shop)": titles}) | |
| # 导出 CSV 文件 | |
| tmpdir = tempfile.mkdtemp() | |
| out_path = os.path.join(tmpdir, "tiktok_titles_fr.csv") | |
| df.to_csv(out_path, index=False, encoding="utf-8-sig") | |
| return kw_text, df, out_path | |
| with gr.Blocks(title="TikTok 商品标题优化(法语)") as demo: | |
| gr.Markdown("# 🇫🇷 TikTok 商品标题优化工具\n输入商品名和类目,自动生成**法语**热门关键词与标题模板。") | |
| with gr.Row(): | |
| product = gr.Textbox(label="商品名称(法语或中文均可)", placeholder="手持小风扇 / ventilateur de poche") | |
| category = gr.Textbox(label="类目", placeholder="3C 数码") | |
| n = gr.Slider(3, 10, value=6, step=1, label="生成标题数量") | |
| btn = gr.Button("生成") | |
| kw_out = gr.Textbox(label="热门关键词(可复制)", show_copy_button=True) | |
| table = gr.Dataframe(label="标题候选(可复制粘贴)") | |
| file_out = gr.File(label="下载 CSV", file_count="single") | |
| btn.click(fn=gen_titles, inputs=[product, category, n], outputs=[kw_out, table, file_out]) | |
| demo.launch() | |