Spaces:
Sleeping
Sleeping
| # app.py | |
| # pip install gradio==4.43.0 pandas requests | |
| import re | |
| import random | |
| import gradio as gr | |
| import pandas as pd | |
| from difflib import SequenceMatcher | |
| # ----------------------------- | |
| # 工具函数 | |
| # ----------------------------- | |
| def normalize(s: str) -> str: | |
| s = s.strip().lower() | |
| s = re.sub(r"\s+", " ", s) | |
| return s | |
| def dedup_words(words): | |
| out, seen = [], set() | |
| for w in words: | |
| k = normalize(w) | |
| if k not in seen and w: | |
| seen.add(k) | |
| out.append(w) | |
| return out | |
| def rank_keywords(core_words, hotwords): | |
| items = [] | |
| for w in core_words: | |
| items.append((w, 1.0)) | |
| for i, w in enumerate(hotwords): | |
| items.append((w, 0.9 - i * 0.02)) | |
| uniq = {} | |
| for w, sc in items: | |
| k = normalize(w) | |
| if k not in uniq or sc > uniq[k]: | |
| uniq[k] = sc | |
| ranked = sorted([(w, sc) for w, sc in uniq.items()], key=lambda x: x[1], reverse=True) | |
| return [w for w, _ in ranked] | |
| def build_title(core, ranked, max_len): | |
| segments = dedup_words(core + ranked) | |
| title = " ".join(segments) | |
| if len(title) > max_len: | |
| title = title[:max_len] | |
| return title | |
| # ----------------------------- | |
| # 标题生成函数 | |
| # ----------------------------- | |
| def generate_titles(product_name, category, region, n_titles, hotwords_text, max_len): | |
| core_words = dedup_words(re.split(r"[,\|/;,、 ]+", product_name)) | |
| hotwords = dedup_words(re.split(r"[,\|/;,、\n]+", hotwords_text)) | |
| # 如果没有热词,使用核心词作为默认热词 | |
| if not hotwords: | |
| hotwords = core_words.copy() | |
| ranked = rank_keywords(core_words, hotwords) | |
| out = [] | |
| for _ in range(n_titles): | |
| random.shuffle(ranked) | |
| title = build_title(core_words, ranked, max_len) | |
| out.append(title) | |
| df = pd.DataFrame({"序号": list(range(1, len(out)+1)), "标题": out}) | |
| csv_bytes = df.to_csv(index=False).encode("utf-8-sig") | |
| return "\n".join(out), csv_bytes | |
| # ----------------------------- | |
| # Gradio UI | |
| # ----------------------------- | |
| REGIONS = [("美国", "US"), ("中文", "CN"), ("法国", "FR"), ("越南", "VN")] | |
| with gr.Blocks(theme=gr.themes.Soft()) as demo: | |
| gr.Markdown("## 商品标题优化工具(稳定版,不依赖 TikTok 接口)") | |
| with gr.Row(): | |
| product_name = gr.Textbox(label="商品名称与关键词", placeholder="例如:阻力带", lines=2) | |
| category = gr.Textbox(label="类目", value="健身用品/健身器材") | |
| with gr.Row(): | |
| region = gr.Dropdown(REGIONS, value="US", label="地区/语言") | |
| n_titles = gr.Slider(1, 10, value=3, step=1, label="生成数量") | |
| max_len = gr.Slider(30, 120, value=80, step=1, label="最大标题长度") | |
| hotwords_box = gr.Textbox(label="热门关键词(可选,自定义)", lines=2, placeholder="可手动填入关键词") | |
| btn_generate = gr.Button("⚡ 生成标题") | |
| titles_out = gr.Textbox(label="生成结果", lines=10) | |
| csv_file = gr.File(label="下载 CSV", file_count="single") | |
| btn_generate.click( | |
| generate_titles, | |
| [product_name, category, region, n_titles, hotwords_box, max_len], | |
| [titles_out, csv_file] | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() | |