sofiamak commited on
Commit
bd1224a
·
verified ·
1 Parent(s): d62cdd4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -28
app.py CHANGED
@@ -90,9 +90,6 @@ def get_fallback_quote(language: str):
90
  return f'Quote: {text} — {author}'
91
 
92
  def ensure_two_lines_and_split(content: str, language: str):
93
- """
94
- 确保严格两行;若缺失名言则注入兜底。返回 (name_line, quote_line)
95
- """
96
  if not content:
97
  return None, None
98
  lines = [ln.strip() for ln in content.splitlines() if ln.strip()]
@@ -106,6 +103,19 @@ def ensure_two_lines_and_split(content: str, language: str):
106
  quote_line = get_fallback_quote(language)
107
  return name_line, quote_line
108
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109
  def generate_name(species, look, personality, gender, language, inspiration):
110
  try:
111
  prompt = build_prompt(
@@ -132,30 +142,11 @@ def generate_name(species, look, personality, gender, language, inspiration):
132
  except Exception as e:
133
  return render_result_html(f"⚠️ 出错:{html.escape(str(e))}", "")
134
 
135
- # ======= 渲染结果为“软件风格”的结果卡片(HTML) =======
136
- def render_result_html(name_line: str, quote_line: str) -> str:
137
- name_safe = html.escape(name_line or "")
138
- quote_safe = html.escape(quote_line or "")
139
- # 支持复制时保留纯文本(按钮用 JS 读隐藏文本)
140
- plain_text = (name_line + "\n" + quote_line).strip()
141
- plain_text = html.escape(plain_text)
142
- return f"""
143
- <div class="pm-result-card">
144
- <div class="pm-name">{name_safe}</div>
145
- <div class="pm-quote">{quote_safe}</div>
146
- <textarea id="pm-plain" style="position:absolute; left:-9999px; top:-9999px;">{plain_text}</textarea>
147
- </div>
148
- """
149
-
150
- # ======= 主题与样式(让它更像真正的软件) =======
151
  theme = gr.themes.Soft(
152
  primary_hue="pink",
153
  secondary_hue="cyan",
154
  neutral_hue="slate"
155
- ).set(
156
- radius_size="10px",
157
- block_title_text_size="20px",
158
- spacing_size="14px"
159
  )
160
 
161
  CUSTOM_CSS = """
@@ -163,8 +154,6 @@ CUSTOM_CSS = """
163
  --pm-bg:#faf7fb;
164
  --pm-surface:#ffffff;
165
  --pm-border:#eddcf3;
166
- --pm-accent:#ff4fa3;
167
- --pm-accent-2:#42c6d3;
168
  --pm-text:#111;
169
  --pm-muted:#6b7280;
170
  }
@@ -257,8 +246,7 @@ with gr.Blocks(title="Pawfect Match", theme=theme, css=CUSTOM_CSS) as demo:
257
  ["狸花猫","虎斑、黄眼睛","机灵好奇","male","日本語","山海经"],
258
  ["小型犬","白色卷毛","温柔、爱撒娇","female","Français","哈利波特"],
259
  ],
260
- inputs=[],
261
- outputs=[]
262
  )
263
 
264
  # 右侧:结果卡
@@ -274,6 +262,9 @@ with gr.Blocks(title="Pawfect Match", theme=theme, css=CUSTOM_CSS) as demo:
274
 
275
  gr.HTML('<div class="pm-footer">© Pawfect Match</div>')
276
 
 
 
 
277
  # 交互
278
  btn.click(
279
  generate_name,
@@ -286,7 +277,6 @@ with gr.Blocks(title="Pawfect Match", theme=theme, css=CUSTOM_CSS) as demo:
286
  outputs=[species, look, personality, gender, language, inspiration]
287
  )
288
 
289
- # 复制纯文本(从隐藏 textarea 读取)
290
  copy_btn.click(None, None, None, _js="""
291
  () => {
292
  const ta = document.getElementById('pm-plain');
 
90
  return f'Quote: {text} — {author}'
91
 
92
  def ensure_two_lines_and_split(content: str, language: str):
 
 
 
93
  if not content:
94
  return None, None
95
  lines = [ln.strip() for ln in content.splitlines() if ln.strip()]
 
103
  quote_line = get_fallback_quote(language)
104
  return name_line, quote_line
105
 
106
+ def render_result_html(name_line: str, quote_line: str) -> str:
107
+ name_safe = html.escape(name_line or "")
108
+ quote_safe = html.escape(quote_line or "")
109
+ plain_text = (name_line + "\n" + quote_line).strip()
110
+ plain_text = html.escape(plain_text)
111
+ return f"""
112
+ <div class="pm-result-card">
113
+ <div class="pm-name">{name_safe}</div>
114
+ <div class="pm-quote">{quote_safe}</div>
115
+ <textarea id="pm-plain" style="position:absolute; left:-9999px; top:-9999px;">{plain_text}</textarea>
116
+ </div>
117
+ """
118
+
119
  def generate_name(species, look, personality, gender, language, inspiration):
120
  try:
121
  prompt = build_prompt(
 
142
  except Exception as e:
143
  return render_result_html(f"⚠️ 出错:{html.escape(str(e))}", "")
144
 
145
+ # ======= 主题与样式(去掉 .set;用 CSS 控制圆角/间距) =======
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
146
  theme = gr.themes.Soft(
147
  primary_hue="pink",
148
  secondary_hue="cyan",
149
  neutral_hue="slate"
 
 
 
 
150
  )
151
 
152
  CUSTOM_CSS = """
 
154
  --pm-bg:#faf7fb;
155
  --pm-surface:#ffffff;
156
  --pm-border:#eddcf3;
 
 
157
  --pm-text:#111;
158
  --pm-muted:#6b7280;
159
  }
 
246
  ["狸花猫","虎斑、黄眼睛","机灵好奇","male","日本語","山海经"],
247
  ["小型犬","白色卷毛","温柔、爱撒娇","female","Français","哈利波特"],
248
  ],
249
+ inputs=[], # 先声明,后面用 .click 赋值也可以;最稳妥是直接绑定到下一行 inputs
 
250
  )
251
 
252
  # 右侧:结果卡
 
262
 
263
  gr.HTML('<div class="pm-footer">© Pawfect Match</div>')
264
 
265
+ # 绑定 Examples 到真实输入(有些版本需要明确绑定)
266
+ demo.children[-3].children[-1].children[-1].inputs = [species, look, personality, gender, language, inspiration]
267
+
268
  # 交互
269
  btn.click(
270
  generate_name,
 
277
  outputs=[species, look, personality, gender, language, inspiration]
278
  )
279
 
 
280
  copy_btn.click(None, None, None, _js="""
281
  () => {
282
  const ta = document.getElementById('pm-plain');