sofiamak commited on
Commit
168b2d6
·
verified ·
1 Parent(s): ad18c1a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -27
app.py CHANGED
@@ -6,43 +6,48 @@ API_KEY = os.getenv("OPENAI_API_KEY")
6
  MODEL_NAME = "gpt-4o-mini"
7
  client = OpenAI(api_key=API_KEY)
8
 
9
- SYSTEM_PROMPT = """You are a creative pet name generator.
 
10
  Goal: Produce EXACTLY ONE pet name + one short quote line.
11
- Balance:
12
- - 50% guided by user inputs (species, appearance, personality, gender).
13
- - 50% free creative exploration.
14
- Inspiration source (one of: 山海经 / 迪士尼 / 哈利波特 / 生活) must influence tone and imagery.
15
 
16
- Rules for NAME:
17
- - The name CAN directly borrow or echo well-known characters or mythic creatures if fitting.
18
- - Prefer 2–3 syllables; pleasant to pronounce; pet-friendly.
 
 
 
 
19
  - Output in requested language (Chinese/English/Japanese/Korean/French).
20
- - Provide ONE short meaning/explanation (≤12 words).
 
 
21
  - No numbers, no long descriptive phrases.
22
 
23
- Rules for QUOTE:
24
- - After the name line, provide ONE short quote related to this animal type or its spirit.
25
- - If you recall a suitable real quote and author reliably, use it with attribution.
26
- - If not, provide a brief thematic aphorism and attribute as “Unknown” (or “Proverb”).
27
  - Keep quote language the same as requested language.
28
 
29
- STRICT OUTPUT FORMAT (exactly two lines):
30
  Name — short meaning
31
  Quote: “...” — Attribution
32
  """
33
 
34
  def build_prompt(species, look, personality, gender, language, inspiration):
 
35
  return f"""
36
  Requested language: {language}
37
- Species: {species}
38
- Appearance: {look}
39
- Personality: {personality}
40
- Gender: {gender}
41
- Inspiration source: {inspiration}
 
42
 
43
- Task: Following the system rules, propose EXACTLY ONE pet name (with ≤12-word meaning),
44
- then ONE quote line about this kind of animal or its spirit.
45
- Remember: 50% user-guided, 50% free creativity; keep both lines in {language}.
 
46
  """
47
 
48
  def generate_name(species, look, personality, gender, language, inspiration):
@@ -55,7 +60,8 @@ def generate_name(species, look, personality, gender, language, inspiration):
55
  )
56
  resp = client.chat.completions.create(
57
  model=MODEL_NAME,
58
- temperature=0.9, # 更有创造性
 
59
  max_tokens=260,
60
  messages=[
61
  {"role": "system", "content": SYSTEM_PROMPT},
@@ -67,11 +73,11 @@ def generate_name(species, look, personality, gender, language, inspiration):
67
  return f"⚠️ 出错:{html.escape(str(e))}"
68
 
69
  with gr.Blocks(title="Pawfect Match") as demo:
70
- gr.Markdown("## 🐾 Pawfect Match|宠物起名机\n选择灵感来源,输入信息 → 一键生成一个名字 + 一句名言。")
71
 
72
- species = gr.Textbox(label="宠物种类", placeholder="例:金毛 / 英短 / 仓鼠 / 鹦鹉")
73
- look = gr.Textbox(label="外表特征", placeholder="例:橘色短毛,大眼睛,白爪")
74
- personality = gr.Textbox(label="性格特点", placeholder="例:粘人、活泼、聪明")
75
  gender = gr.Radio(["male","female"], value="male", label="宠物性别")
76
  language = gr.Radio(["中文","English","日本語","한국어","Français"], value="English", label="输出语言")
77
  inspiration = gr.Radio(["山海经","迪士尼","哈利波特","生活"], value="生活", label="灵感来源")
 
6
  MODEL_NAME = "gpt-4o-mini"
7
  client = OpenAI(api_key=API_KEY)
8
 
9
+ # —— 强化“灵感优先、创作度高”的系统提示 —— #
10
+ SYSTEM_PROMPT = """You are a high-creativity pet name generator.
11
  Goal: Produce EXACTLY ONE pet name + one short quote line.
 
 
 
 
12
 
13
+ Creative balance:
14
+ - Inspiration source drives the concept (~70%).
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
+ - Prefer 2–3 syllables (or compact in Chinese), pleasant to pronounce.
20
  - Output in requested language (Chinese/English/Japanese/Korean/French).
21
+ - You MAY echo famous/mythic names if truly fitting, but PREFER fresh coinages,
22
+ playful twists, or affectionate diminutives in the chosen theme.
23
+ - Provide ONE short meaning/explanation (≤12 words), themed first, traits second.
24
  - No numbers, no long descriptive phrases.
25
 
26
+ QUOTE rules:
27
+ - After the name line, provide ONE short quote about this animal type or spirit.
28
+ - If a reliable real quote is recalled, include author; otherwise write a brief thematic aphorism and attribute as “Unknown” (or “Proverb”).
 
29
  - Keep quote language the same as requested language.
30
 
31
+ STRICT OUTPUT: exactly two lines
32
  Name — short meaning
33
  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, may be empty):
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 led by the inspiration source (~70%), with a short meaning (≤12 words).
49
+ 2) Then output ONE quote line about this kind of animal/spirit (same language).
50
+ Remember: be imaginative; do NOT overfit to the traits.
51
  """
52
 
53
  def generate_name(species, look, personality, gender, language, inspiration):
 
60
  )
61
  resp = client.chat.completions.create(
62
  model=MODEL_NAME,
63
+ temperature=0.95, # 提高创造力
64
+ top_p=0.95,
65
  max_tokens=260,
66
  messages=[
67
  {"role": "system", "content": SYSTEM_PROMPT},
 
73
  return f"⚠️ 出错:{html.escape(str(e))}"
74
 
75
  with gr.Blocks(title="Pawfect Match") as demo:
76
+ gr.Markdown("## 🐾 Pawfect Match|宠物起名机\n灵感优先(70%)+ 用户特征(30%)→ 一个名字 + 一句名言。")
77
 
78
+ species = gr.Textbox(label="宠物种类", placeholder="例:金毛 / 英短 / 仓鼠 / 鹦鹉(可留空)")
79
+ look = gr.Textbox(label="外表特征", placeholder="例:橘色短毛,大眼睛,白爪(可留空)")
80
+ personality = gr.Textbox(label="性格特点", placeholder="例:粘人、活泼、聪明(可留空)")
81
  gender = gr.Radio(["male","female"], value="male", label="宠物性别")
82
  language = gr.Radio(["中文","English","日本語","한국어","Français"], value="English", label="输出语言")
83
  inspiration = gr.Radio(["山海经","迪士尼","哈利波特","生活"], value="生活", label="灵感来源")