Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os, html
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from openai import OpenAI
|
| 4 |
+
|
| 5 |
+
# 读取 API Key:优先环境变量;没设置也允许传入文本框(更灵活)
|
| 6 |
+
ENV_KEY = os.getenv("OPENAI_API_KEY", "").strip()
|
| 7 |
+
DEFAULT_MODEL = os.getenv("MODEL_NAME", "gpt-4o-mini")
|
| 8 |
+
|
| 9 |
+
SYSTEM_PROMPT = """You are a creative pet name generator.
|
| 10 |
+
Given species, appearance and personality, propose concise, pronounceable names.
|
| 11 |
+
Rules:
|
| 12 |
+
- Return diverse styles; avoid offensive, brand, or trademarked terms.
|
| 13 |
+
- Prefer 2–3 syllables; mix cute, cool, elegant, funny styles depending on user choice.
|
| 14 |
+
- Provide a short meaning/explanation (≤12 words).
|
| 15 |
+
- If language is Chinese/Japanese/Spanish, output in that language; add romanization when helpful.
|
| 16 |
+
- Respect forbidden terms; avoid near-homophones of them.
|
| 17 |
+
- No numbers, no long phrases, no public-figure names.
|
| 18 |
+
Output strictly as a clean Markdown list.
|
| 19 |
+
"""
|
| 20 |
+
|
| 21 |
+
def build_user_prompt(species, look, personality, style, language, count, gender, length_pref, forbid):
|
| 22 |
+
species = (species or "").strip()
|
| 23 |
+
look = (look or "").strip()
|
| 24 |
+
personality = (personality or "").strip()
|
| 25 |
+
style = style or "mixed"
|
| 26 |
+
language = language or "English"
|
| 27 |
+
gender = gender or "any"
|
| 28 |
+
length_pref = length_pref or "medium"
|
| 29 |
+
forbid = (forbid or "").strip()
|
| 30 |
+
|
| 31 |
+
lines = [
|
| 32 |
+
f"Target language: {language}",
|
| 33 |
+
f"Species: {species}",
|
| 34 |
+
f"Appearance: {look}",
|
| 35 |
+
f"Personality: {personality}",
|
| 36 |
+
f"Style preference: {style}",
|
| 37 |
+
f"Desired length: {length_pref}",
|
| 38 |
+
f"Gender vibe: {gender}",
|
| 39 |
+
f"Forbidden terms (avoid, including near-homophones): {forbid or 'None'}",
|
| 40 |
+
f"Count: {count}",
|
| 41 |
+
"Format example:",
|
| 42 |
+
"- Nala — warm, agile; evokes amber fur (pinyin/romaji if non-Latin).",
|
| 43 |
+
"- Kumo(くも)— playful 'cloud'; soft & fluffy vibe.",
|
| 44 |
+
]
|
| 45 |
+
return "\n".join(lines)
|
| 46 |
+
|
| 47 |
+
def generate_names(api_key, model_name, species, look, personality, style, language, count, gender, length_pref, forbid):
|
| 48 |
+
key = (api_key or ENV_KEY).strip()
|
| 49 |
+
if not key:
|
| 50 |
+
return "❗请先填写 API Key(或在环境变量设置 OPENAI_API_KEY)。"
|
| 51 |
+
client = OpenAI(api_key=key)
|
| 52 |
+
try:
|
| 53 |
+
prompt = build_user_prompt(species, look, personality, style, language, count, gender, length_pref, forbid)
|
| 54 |
+
resp = client.chat.completions.create(
|
| 55 |
+
model=(model_name or DEFAULT_MODEL),
|
| 56 |
+
temperature=0.75,
|
| 57 |
+
max_tokens=900,
|
| 58 |
+
messages=[
|
| 59 |
+
{"role": "system", "content": SYSTEM_PROMPT},
|
| 60 |
+
{"role": "user", "content": prompt}
|
| 61 |
+
]
|
| 62 |
+
)
|
| 63 |
+
content = resp.choices[0].message.content
|
| 64 |
+
# 轻度清洗,防止 Markdown 注入或空输出
|
| 65 |
+
return content or "(没有生成内容,请尝试调整参数)"
|
| 66 |
+
except Exception as e:
|
| 67 |
+
return f"⚠️ 出错:{html.escape(str(e))}"
|
| 68 |
+
|
| 69 |
+
with gr.Blocks(title="PetName Genie") as demo:
|
| 70 |
+
gr.Markdown("## 🐾 PetName Genie|宠物起名机\n输入宠物信息,一键生成名字 + 简短含义。")
|
| 71 |
+
|
| 72 |
+
with gr.Accordion("Advanced (可选) — API & 模型", open=False):
|
| 73 |
+
api_key = gr.Textbox(label="API Key(留空则用环境变量 OPENAI_API_KEY)", type="password", placeholder="sk-...")
|
| 74 |
+
model_name = gr.Textbox(label="模型名", value=DEFAULT_MODEL)
|
| 75 |
+
|
| 76 |
+
with gr.Row():
|
| 77 |
+
species = gr.Textbox(label="宠物种类", placeholder="例:金毛 / 英短 / 布偶猫 / 仓鼠 / 柯基")
|
| 78 |
+
gender = gr.Radio(["any","male-ish","female-ish"], value="any", label="性别气质")
|
| 79 |
+
style = gr.Radio(["mixed","cute","cool","elegant","funny","mythic"], value="mixed", label="风格")
|
| 80 |
+
look = gr.Textbox(label="外表特征", placeholder="例:橘色短毛,圆脸,大眼睛,白爪")
|
| 81 |
+
personality = gr.Textbox(label="性格特点", placeholder="例:粘人、爱玩、胆小、聪明")
|
| 82 |
+
with gr.Row():
|
| 83 |
+
language = gr.Radio(["English","中文","日本語","Español"], value="English", label="输出语言")
|
| 84 |
+
length_pref = gr.Radio(["short","medium","long"], value="medium", label="名字长度偏好")
|
| 85 |
+
count = gr.Slider(5, 20, value=12, step=1, label="生成数量")
|
| 86 |
+
forbid = gr.Textbox(label="需要避开的词(可选)", placeholder="例:小白, 旺财, Mimi")
|
| 87 |
+
|
| 88 |
+
btn = gr.Button("✨ 起名!")
|
| 89 |
+
out = gr.Markdown()
|
| 90 |
+
|
| 91 |
+
btn.click(
|
| 92 |
+
generate_names,
|
| 93 |
+
inputs=[api_key, model_name, species, look, personality, style, language, count, gender, length_pref, forbid],
|
| 94 |
+
outputs=out
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
if __name__ == "__main__":
|
| 98 |
+
demo.launch()
|