Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,7 +5,6 @@ APP_TITLE = "100个不"
|
|
| 5 |
TASKS_FILE = "tasks.json"
|
| 6 |
TARGET_PERCENT = 100
|
| 7 |
|
| 8 |
-
# 难度映射与奖励
|
| 9 |
LEVEL_LABELS = {"easy": "简单", "medium": "进阶", "hard": "挑战"}
|
| 10 |
LABEL_TO_KEY = {v: k for k, v in LEVEL_LABELS.items()}
|
| 11 |
LEVEL_REWARDS = {"easy": 1, "medium": 5, "hard": 10}
|
|
@@ -24,27 +23,24 @@ def load_tasks_from_file():
|
|
| 24 |
except Exception as e:
|
| 25 |
return {"easy": [], "medium": [], "hard": []}, f"{TASKS_FILE} 解析失败:{e}。已使用空题库。"
|
| 26 |
|
| 27 |
-
# ====== 进度状态 ======
|
| 28 |
def init_state():
|
| 29 |
tasks, msg = load_tasks_from_file()
|
| 30 |
return {
|
| 31 |
"done_percent": 0,
|
| 32 |
-
"history": [],
|
| 33 |
"current": None,
|
| 34 |
"current_level": None,
|
| 35 |
"tasks": tasks,
|
| 36 |
-
"load_msg": msg,
|
| 37 |
-
"celebrated": False
|
| 38 |
}
|
| 39 |
|
| 40 |
-
# ====== 业务逻辑 ======
|
| 41 |
def progress_text(state):
|
| 42 |
pct = min(100, int(state["done_percent"]))
|
| 43 |
-
bar = f"{'█' * (pct//5)}{'░' * (20 - pct//5)}"
|
| 44 |
return f"进度:{pct}%\n{bar}"
|
| 45 |
|
| 46 |
def draw_task(state, level_cn):
|
| 47 |
-
# 若已满 100%,提示先庆祝或重置
|
| 48 |
if state["done_percent"] >= TARGET_PERCENT:
|
| 49 |
return state, "🎉 你已达成 100%,可以重置开始新循环或导出记录。", progress_text(state), state.get("load_msg") or "", ""
|
| 50 |
level = LABEL_TO_KEY.get(level_cn, "easy")
|
|
@@ -57,7 +53,6 @@ def draw_task(state, level_cn):
|
|
| 57 |
return state, f"难度:{LEVEL_LABELS[level]}\n\n任务:{task}", progress_text(state), "", ""
|
| 58 |
|
| 59 |
def _celebration_overlay():
|
| 60 |
-
# 纯 CSS/HTML 彩带 + 祝贺语;点击遮罩或按钮关闭
|
| 61 |
return """
|
| 62 |
<div class="cele-overlay" onclick="this.remove()">
|
| 63 |
<div class="cele-modal" onclick="event.stopPropagation()">
|
|
@@ -88,7 +83,6 @@ def complete_task(state, note):
|
|
| 88 |
msg = f"✅ 已完成:{state['current']}(+{reward}%)"
|
| 89 |
state["current"], state["current_level"] = None, None
|
| 90 |
|
| 91 |
-
# 触发庆祝:从 <100 提升到 >=100 且未庆祝过
|
| 92 |
cele_html = ""
|
| 93 |
if before < TARGET_PERCENT and state["done_percent"] >= TARGET_PERCENT and not state["celebrated"]:
|
| 94 |
state["celebrated"] = True
|
|
@@ -126,7 +120,6 @@ def export_progress(state):
|
|
| 126 |
content = "\n".join(lines) if len(lines) > 3 else "暂无记录"
|
| 127 |
return gr.File.update(value=("progress.txt", content))
|
| 128 |
|
| 129 |
-
# ====== 样式 ======
|
| 130 |
theme = gr.themes.Soft(primary_hue="pink", secondary_hue="cyan", neutral_hue="slate")
|
| 131 |
CSS = """
|
| 132 |
body,.gradio-container{background:#faf7fb!important;}
|
|
@@ -141,8 +134,6 @@ body,.gradio-container{background:#faf7fb!important;}
|
|
| 141 |
.hero p{margin:0;color:#374151}
|
| 142 |
.hero .cta{padding:10px 16px;border-radius:10px;border:1px solid #eddcf3;background:#fff;cursor:pointer;font-weight:700}
|
| 143 |
.hero .cta:hover{border-color:#ff4fa3;color:#ff4fa3}
|
| 144 |
-
|
| 145 |
-
/* === 庆祝叠加层 === */
|
| 146 |
.cele-overlay{position:fixed;inset:0;background:rgba(0,0,0,.35);display:flex;align-items:center;justify-content:center;z-index:9999;}
|
| 147 |
.cele-modal{width:min(560px,92vw);background:#fff;border:1px solid #eddcf3;border-radius:16px;
|
| 148 |
box-shadow:0 20px 60px rgba(0,0,0,.18);padding:20px 18px 16px;text-align:center;position:relative;overflow:hidden;}
|
|
@@ -150,32 +141,16 @@ body,.gradio-container{background:#faf7fb!important;}
|
|
| 150 |
.cele-sub{color:#374151;margin-bottom:12px;}
|
| 151 |
.cele-btn{padding:10px 14px;border-radius:10px;border:1px solid #eddcf3;background:#fff;cursor:pointer;font-weight:700;}
|
| 152 |
.cele-btn:hover{border-color:#ff4fa3;color:#ff4fa3}
|
| 153 |
-
|
| 154 |
-
/* 简易 confetti 动效(纯 CSS) */
|
| 155 |
.confetti{position:absolute;inset:0;pointer-events:none;}
|
| 156 |
.confetti span{position:absolute;top:-10px;width:10px;height:14px;border-radius:2px;animation:fall 1.8s linear infinite;opacity:.9}
|
| 157 |
-
.confetti span:nth-child(3n){width:8px;height:12px}
|
| 158 |
-
.confetti span:nth-child(5n){width:6px;height:10px}
|
| 159 |
.confetti span:nth-child(odd){background:#ff4fa3}
|
| 160 |
.confetti span:nth-child(even){background:#5ee0ff}
|
| 161 |
-
.confetti span:nth-child(3n+2){background:#ffd166}
|
| 162 |
-
.confetti span:nth-child(1){left:8%}
|
| 163 |
-
.confetti span:nth-child(2){left:18%}
|
| 164 |
-
.confetti span:nth-child(3){left:28%}
|
| 165 |
-
.confetti span:nth-child(4){left:38%}
|
| 166 |
-
.confetti span:nth-child(5){left:48%}
|
| 167 |
-
.confetti span:nth-child(6){left:58%}
|
| 168 |
-
.confetti span:nth-child(7){left:68%}
|
| 169 |
-
.confetti span:nth-child(8){left:78%}
|
| 170 |
-
.confetti span:nth-child(9){left:88%}
|
| 171 |
-
.confetti span:nth-child(10){left:96%}
|
| 172 |
@keyframes fall{
|
| 173 |
0%{transform:translateY(-20px) rotate(0deg)}
|
| 174 |
100%{transform:translateY(520px) rotate(360deg)}
|
| 175 |
}
|
| 176 |
"""
|
| 177 |
|
| 178 |
-
# ====== UI ======
|
| 179 |
with gr.Blocks(title=APP_TITLE, theme=theme, css=CSS) as demo:
|
| 180 |
state = gr.State(init_state())
|
| 181 |
|
|
@@ -186,14 +161,13 @@ with gr.Blocks(title=APP_TITLE, theme=theme, css=CSS) as demo:
|
|
| 186 |
""")
|
| 187 |
gr.HTML("""
|
| 188 |
<div class="card hero">
|
| 189 |
-
<h1>
|
| 190 |
-
<p>
|
| 191 |
-
<p>完成 100 次小暴露,社交恐惧会一点点消退。</p>
|
| 192 |
</div>
|
| 193 |
""")
|
| 194 |
start_btn = gr.Button("开始挑战 ▶️", variant="primary")
|
| 195 |
|
| 196 |
-
# --- 主界面
|
| 197 |
with gr.Group(visible=False) as main:
|
| 198 |
gr.HTML(f"""<div class='topbar'><div class='title'>{APP_TITLE}</div></div>""")
|
| 199 |
|
|
@@ -224,26 +198,21 @@ with gr.Blocks(title=APP_TITLE, theme=theme, css=CSS) as demo:
|
|
| 224 |
progress = gr.Markdown("进度:0%\n░░░░░░░░░░░░░░░░░░░░░░")
|
| 225 |
tip = gr.Markdown("", elem_classes=["tip"])
|
| 226 |
file_out = gr.File(label="下载导出记录", visible=True)
|
|
|
|
| 227 |
|
| 228 |
-
|
| 229 |
-
cele = gr.HTML(value="", visible=True)
|
| 230 |
-
|
| 231 |
-
# ====== 行为函数 ======
|
| 232 |
def on_start(st):
|
| 233 |
first_tip = st.get("load_msg") or ""
|
| 234 |
-
st["load_msg"] = ""
|
| 235 |
-
return (gr.update(visible=False),
|
| 236 |
-
gr.update(visible=True), # 显示 main
|
| 237 |
-
first_tip, # 主界面右侧提示
|
| 238 |
-
"") # 清空庆祝层
|
| 239 |
|
| 240 |
def on_draw(st, lvl_cn):
|
| 241 |
st, msg, prog, info, cele_html = draw_task(st, lvl_cn)
|
| 242 |
-
return st, msg, prog,
|
| 243 |
|
| 244 |
def on_complete(st, note):
|
| 245 |
st, msg, prog, cele_html = complete_task(st, note)
|
| 246 |
-
return st, msg, prog, "", "", cele_html
|
| 247 |
|
| 248 |
def on_skip(st):
|
| 249 |
st, msg, prog, cele_html = skip_task(st)
|
|
@@ -260,7 +229,6 @@ with gr.Blocks(title=APP_TITLE, theme=theme, css=CSS) as demo:
|
|
| 260 |
st, info = reload_tasks(st)
|
| 261 |
return st, info
|
| 262 |
|
| 263 |
-
# ====== 事件绑定 ======
|
| 264 |
start_btn.click(on_start, inputs=[state], outputs=[landing, main, tip, cele])
|
| 265 |
btn_draw.click(on_draw, inputs=[state, level_choice], outputs=[state, current_task, progress, tip, cele])
|
| 266 |
btn_complete.click(on_complete, inputs=[state, reflect], outputs=[state, current_task, progress, tip, reflect, cele])
|
|
|
|
| 5 |
TASKS_FILE = "tasks.json"
|
| 6 |
TARGET_PERCENT = 100
|
| 7 |
|
|
|
|
| 8 |
LEVEL_LABELS = {"easy": "简单", "medium": "进阶", "hard": "挑战"}
|
| 9 |
LABEL_TO_KEY = {v: k for k, v in LEVEL_LABELS.items()}
|
| 10 |
LEVEL_REWARDS = {"easy": 1, "medium": 5, "hard": 10}
|
|
|
|
| 23 |
except Exception as e:
|
| 24 |
return {"easy": [], "medium": [], "hard": []}, f"{TASKS_FILE} 解析失败:{e}。已使用空题库。"
|
| 25 |
|
|
|
|
| 26 |
def init_state():
|
| 27 |
tasks, msg = load_tasks_from_file()
|
| 28 |
return {
|
| 29 |
"done_percent": 0,
|
| 30 |
+
"history": [],
|
| 31 |
"current": None,
|
| 32 |
"current_level": None,
|
| 33 |
"tasks": tasks,
|
| 34 |
+
"load_msg": msg,
|
| 35 |
+
"celebrated": False
|
| 36 |
}
|
| 37 |
|
|
|
|
| 38 |
def progress_text(state):
|
| 39 |
pct = min(100, int(state["done_percent"]))
|
| 40 |
+
bar = f"{'█' * (pct//5)}{'░' * (20 - pct//5)}"
|
| 41 |
return f"进度:{pct}%\n{bar}"
|
| 42 |
|
| 43 |
def draw_task(state, level_cn):
|
|
|
|
| 44 |
if state["done_percent"] >= TARGET_PERCENT:
|
| 45 |
return state, "🎉 你已达成 100%,可以重置开始新循环或导出记录。", progress_text(state), state.get("load_msg") or "", ""
|
| 46 |
level = LABEL_TO_KEY.get(level_cn, "easy")
|
|
|
|
| 53 |
return state, f"难度:{LEVEL_LABELS[level]}\n\n任务:{task}", progress_text(state), "", ""
|
| 54 |
|
| 55 |
def _celebration_overlay():
|
|
|
|
| 56 |
return """
|
| 57 |
<div class="cele-overlay" onclick="this.remove()">
|
| 58 |
<div class="cele-modal" onclick="event.stopPropagation()">
|
|
|
|
| 83 |
msg = f"✅ 已完成:{state['current']}(+{reward}%)"
|
| 84 |
state["current"], state["current_level"] = None, None
|
| 85 |
|
|
|
|
| 86 |
cele_html = ""
|
| 87 |
if before < TARGET_PERCENT and state["done_percent"] >= TARGET_PERCENT and not state["celebrated"]:
|
| 88 |
state["celebrated"] = True
|
|
|
|
| 120 |
content = "\n".join(lines) if len(lines) > 3 else "暂无记录"
|
| 121 |
return gr.File.update(value=("progress.txt", content))
|
| 122 |
|
|
|
|
| 123 |
theme = gr.themes.Soft(primary_hue="pink", secondary_hue="cyan", neutral_hue="slate")
|
| 124 |
CSS = """
|
| 125 |
body,.gradio-container{background:#faf7fb!important;}
|
|
|
|
| 134 |
.hero p{margin:0;color:#374151}
|
| 135 |
.hero .cta{padding:10px 16px;border-radius:10px;border:1px solid #eddcf3;background:#fff;cursor:pointer;font-weight:700}
|
| 136 |
.hero .cta:hover{border-color:#ff4fa3;color:#ff4fa3}
|
|
|
|
|
|
|
| 137 |
.cele-overlay{position:fixed;inset:0;background:rgba(0,0,0,.35);display:flex;align-items:center;justify-content:center;z-index:9999;}
|
| 138 |
.cele-modal{width:min(560px,92vw);background:#fff;border:1px solid #eddcf3;border-radius:16px;
|
| 139 |
box-shadow:0 20px 60px rgba(0,0,0,.18);padding:20px 18px 16px;text-align:center;position:relative;overflow:hidden;}
|
|
|
|
| 141 |
.cele-sub{color:#374151;margin-bottom:12px;}
|
| 142 |
.cele-btn{padding:10px 14px;border-radius:10px;border:1px solid #eddcf3;background:#fff;cursor:pointer;font-weight:700;}
|
| 143 |
.cele-btn:hover{border-color:#ff4fa3;color:#ff4fa3}
|
|
|
|
|
|
|
| 144 |
.confetti{position:absolute;inset:0;pointer-events:none;}
|
| 145 |
.confetti span{position:absolute;top:-10px;width:10px;height:14px;border-radius:2px;animation:fall 1.8s linear infinite;opacity:.9}
|
|
|
|
|
|
|
| 146 |
.confetti span:nth-child(odd){background:#ff4fa3}
|
| 147 |
.confetti span:nth-child(even){background:#5ee0ff}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
@keyframes fall{
|
| 149 |
0%{transform:translateY(-20px) rotate(0deg)}
|
| 150 |
100%{transform:translateY(520px) rotate(360deg)}
|
| 151 |
}
|
| 152 |
"""
|
| 153 |
|
|
|
|
| 154 |
with gr.Blocks(title=APP_TITLE, theme=theme, css=CSS) as demo:
|
| 155 |
state = gr.State(init_state())
|
| 156 |
|
|
|
|
| 161 |
""")
|
| 162 |
gr.HTML("""
|
| 163 |
<div class="card hero">
|
| 164 |
+
<h1>A gentle social challenge for shy souls.</h1>
|
| 165 |
+
<p>Collect 100 “no’s,” build real courage, and discover the world isn’t that scary after all.</p>
|
|
|
|
| 166 |
</div>
|
| 167 |
""")
|
| 168 |
start_btn = gr.Button("开始挑战 ▶️", variant="primary")
|
| 169 |
|
| 170 |
+
# --- 主界面 ---
|
| 171 |
with gr.Group(visible=False) as main:
|
| 172 |
gr.HTML(f"""<div class='topbar'><div class='title'>{APP_TITLE}</div></div>""")
|
| 173 |
|
|
|
|
| 198 |
progress = gr.Markdown("进度:0%\n░░░░░░░░░░░░░░░░░░░░░░")
|
| 199 |
tip = gr.Markdown("", elem_classes=["tip"])
|
| 200 |
file_out = gr.File(label="下载导出记录", visible=True)
|
| 201 |
+
cele = gr.HTML(value="", visible=True)
|
| 202 |
|
| 203 |
+
# ====== 事件绑定 ======
|
|
|
|
|
|
|
|
|
|
| 204 |
def on_start(st):
|
| 205 |
first_tip = st.get("load_msg") or ""
|
| 206 |
+
st["load_msg"] = ""
|
| 207 |
+
return (gr.update(visible=False), gr.update(visible=True), first_tip, "")
|
|
|
|
|
|
|
|
|
|
| 208 |
|
| 209 |
def on_draw(st, lvl_cn):
|
| 210 |
st, msg, prog, info, cele_html = draw_task(st, lvl_cn)
|
| 211 |
+
return st, msg, prog, info, cele_html
|
| 212 |
|
| 213 |
def on_complete(st, note):
|
| 214 |
st, msg, prog, cele_html = complete_task(st, note)
|
| 215 |
+
return st, msg, prog, "", "", cele_html
|
| 216 |
|
| 217 |
def on_skip(st):
|
| 218 |
st, msg, prog, cele_html = skip_task(st)
|
|
|
|
| 229 |
st, info = reload_tasks(st)
|
| 230 |
return st, info
|
| 231 |
|
|
|
|
| 232 |
start_btn.click(on_start, inputs=[state], outputs=[landing, main, tip, cele])
|
| 233 |
btn_draw.click(on_draw, inputs=[state, level_choice], outputs=[state, current_task, progress, tip, cele])
|
| 234 |
btn_complete.click(on_complete, inputs=[state, reflect], outputs=[state, current_task, progress, tip, reflect, cele])
|