Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,27 +6,26 @@ API_KEY = os.getenv("OPENAI_API_KEY")
|
|
| 6 |
MODEL_NAME = "gpt-4o-mini"
|
| 7 |
client = OpenAI(api_key=API_KEY)
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
SYSTEM_PROMPT = """You are a
|
| 11 |
-
Goal: Produce EXACTLY ONE pet name + one
|
| 12 |
|
| 13 |
Creative balance:
|
| 14 |
-
- Inspiration source drives the
|
| 15 |
- User inputs (species / appearance / personality / gender) refine the vibe (~30%).
|
| 16 |
-
- Keep outputs fresh and imaginative; avoid being too literal to user traits.
|
| 17 |
|
| 18 |
NAME rules:
|
| 19 |
-
-
|
| 20 |
-
-
|
| 21 |
-
-
|
| 22 |
-
|
| 23 |
-
- Provide ONE short meaning/explanation (≤12 words)
|
| 24 |
-
- No numbers, no long descriptive phrases.
|
| 25 |
|
| 26 |
QUOTE rules:
|
| 27 |
-
- After the name line, provide ONE
|
| 28 |
-
-
|
| 29 |
-
- Keep
|
|
|
|
| 30 |
|
| 31 |
STRICT OUTPUT: exactly two lines
|
| 32 |
Name — short meaning
|
|
@@ -34,20 +33,18 @@ Quote: “...” — Attribution
|
|
| 34 |
"""
|
| 35 |
|
| 36 |
def build_prompt(species, look, personality, gender, language, inspiration):
|
| 37 |
-
# 允许用户特征为空;强调主题先行
|
| 38 |
return f"""
|
| 39 |
Requested language: {language}
|
| 40 |
Inspiration source (primary): {inspiration}
|
| 41 |
-
Refinement inputs (secondary
|
| 42 |
- Species: {species or '(unspecified)'}
|
| 43 |
- Appearance: {look or '(unspecified)'}
|
| 44 |
- Personality: {personality or '(unspecified)'}
|
| 45 |
- Gender: {gender}
|
| 46 |
|
| 47 |
Task:
|
| 48 |
-
1) Create EXACTLY ONE pet name
|
| 49 |
-
2) Then
|
| 50 |
-
Remember: be imaginative; do NOT overfit to the traits.
|
| 51 |
"""
|
| 52 |
|
| 53 |
def generate_name(species, look, personality, gender, language, inspiration):
|
|
@@ -60,9 +57,9 @@ def generate_name(species, look, personality, gender, language, inspiration):
|
|
| 60 |
)
|
| 61 |
resp = client.chat.completions.create(
|
| 62 |
model=MODEL_NAME,
|
| 63 |
-
temperature=0.
|
| 64 |
top_p=0.95,
|
| 65 |
-
max_tokens=
|
| 66 |
messages=[
|
| 67 |
{"role": "system", "content": SYSTEM_PROMPT},
|
| 68 |
{"role": "user", "content": prompt}
|
|
@@ -72,8 +69,9 @@ def generate_name(species, look, personality, gender, language, inspiration):
|
|
| 72 |
except Exception as e:
|
| 73 |
return f"⚠️ 出错:{html.escape(str(e))}"
|
| 74 |
|
|
|
|
| 75 |
with gr.Blocks(title="Pawfect Match") as demo:
|
| 76 |
-
gr.Markdown("## 🐾 Pawfect Match|宠物起名机\n灵感
|
| 77 |
|
| 78 |
species = gr.Textbox(label="宠物种类", placeholder="例:金毛 / 英短 / 仓鼠 / 鹦鹉(可留空)")
|
| 79 |
look = gr.Textbox(label="外表特征", placeholder="例:橘色短毛,大眼睛,白爪(可留空)")
|
|
|
|
| 6 |
MODEL_NAME = "gpt-4o-mini"
|
| 7 |
client = OpenAI(api_key=API_KEY)
|
| 8 |
|
| 9 |
+
# --- 系统提示,强化“可爱名字 + 真实温馨名言” ---
|
| 10 |
+
SYSTEM_PROMPT = """You are a playful yet creative pet name generator.
|
| 11 |
+
Goal: Produce EXACTLY ONE cute, pet-friendly name + one authentic quote line.
|
| 12 |
|
| 13 |
Creative balance:
|
| 14 |
+
- Inspiration source (山海经 / 迪士尼 / 哈利波特 / 生活) drives the theme (~70%).
|
| 15 |
- User inputs (species / appearance / personality / gender) refine the vibe (~30%).
|
|
|
|
| 16 |
|
| 17 |
NAME rules:
|
| 18 |
+
- Must feel CUTE and easy to call out loud (pet style).
|
| 19 |
+
- Prefer 2–3 syllables; round and soft sounds are good.
|
| 20 |
+
- Draw imagery from the inspiration source, but keep tone playful and adorable.
|
| 21 |
+
- Output in the requested language (Chinese/English/Japanese/Korean/French).
|
| 22 |
+
- Provide ONE short meaning/explanation (≤12 words).
|
|
|
|
| 23 |
|
| 24 |
QUOTE rules:
|
| 25 |
+
- After the name line, provide ONE real existing quote about animals, love, companionship, or playfulness.
|
| 26 |
+
- The quote must be authentic (from literature, films, proverbs, or famous people).
|
| 27 |
+
- Keep it short, warm, and not too formal.
|
| 28 |
+
- Translate the quote into the requested language if needed, attribution kept.
|
| 29 |
|
| 30 |
STRICT OUTPUT: exactly two lines
|
| 31 |
Name — short meaning
|
|
|
|
| 33 |
"""
|
| 34 |
|
| 35 |
def build_prompt(species, look, personality, gender, language, inspiration):
|
|
|
|
| 36 |
return f"""
|
| 37 |
Requested language: {language}
|
| 38 |
Inspiration source (primary): {inspiration}
|
| 39 |
+
Refinement inputs (secondary):
|
| 40 |
- Species: {species or '(unspecified)'}
|
| 41 |
- Appearance: {look or '(unspecified)'}
|
| 42 |
- Personality: {personality or '(unspecified)'}
|
| 43 |
- Gender: {gender}
|
| 44 |
|
| 45 |
Task:
|
| 46 |
+
1) Create EXACTLY ONE cute pet name (≤12-word meaning).
|
| 47 |
+
2) Then add ONE authentic warm quote about animals/companionship/playfulness.
|
|
|
|
| 48 |
"""
|
| 49 |
|
| 50 |
def generate_name(species, look, personality, gender, language, inspiration):
|
|
|
|
| 57 |
)
|
| 58 |
resp = client.chat.completions.create(
|
| 59 |
model=MODEL_NAME,
|
| 60 |
+
temperature=0.9, # 提高创造力
|
| 61 |
top_p=0.95,
|
| 62 |
+
max_tokens=280,
|
| 63 |
messages=[
|
| 64 |
{"role": "system", "content": SYSTEM_PROMPT},
|
| 65 |
{"role": "user", "content": prompt}
|
|
|
|
| 69 |
except Exception as e:
|
| 70 |
return f"⚠️ 出错:{html.escape(str(e))}"
|
| 71 |
|
| 72 |
+
# --- Gradio 界面 ---
|
| 73 |
with gr.Blocks(title="Pawfect Match") as demo:
|
| 74 |
+
gr.Markdown("## 🐾 Pawfect Match|宠物起名机\n选择灵感来源 + 输入信息 → 一键生成一个可爱的名字 + 一句真实名言。")
|
| 75 |
|
| 76 |
species = gr.Textbox(label="宠物种类", placeholder="例:金毛 / 英短 / 仓鼠 / 鹦鹉(可留空)")
|
| 77 |
look = gr.Textbox(label="外表特征", placeholder="例:橘色短毛,大眼睛,白爪(可留空)")
|