Elevi7 commited on
Commit
c278c76
·
verified ·
1 Parent(s): 663eb30

update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -24
app.py CHANGED
@@ -42,35 +42,38 @@ def pick_unique(idxs, goal, energy, max_minutes, ignore_goal=False, ignore_energ
42
  return out, used
43
 
44
  def fill_random(need, used, goal, energy, max_minutes):
45
- pool = [a for a in actions if a["title"] not in used and a["title"] not in recent
46
- and (not goal or a["goal"]==goal)
47
- and (not energy or a["energy"]==energy)
48
- and (not max_minutes or a["duration_min"]<=int(max_minutes))]
49
  if len(pool) < need:
50
  pool = [a for a in actions if a["title"] not in used and a["title"] not in recent]
51
  random.shuffle(pool)
52
  return pool[:need]
53
 
54
  def search(query, goal, energy, max_minutes):
55
- q = (query or "").strip()
56
- qx = f"{q} Goal:{goal or 'any'} Energy:{energy or 'any'} Max:{int(max_minutes) if max_minutes else ''} minutes"
57
- v = model.encode([qx], normalize_embeddings=True)
58
- D, I = index.search(np.asarray(v, dtype="float32"), 800)
59
- idxs = list(I[0]); random.shuffle(idxs)
60
-
61
- res, used = [], set()
62
- step, used = pick_unique(idxs, goal, energy, max_minutes, False, False, False, 3, used); res += step
63
- if len(res) < 3:
64
- step, used = pick_unique(idxs, goal, energy, max_minutes, False, True, False, 3-len(res), used); res += step
65
- if len(res) < 3:
66
- step, used = pick_unique(idxs, goal, energy, max_minutes, False, True, True, 3-len(res), used); res += step
67
- if len(res) < 3:
68
- step, used = pick_unique(idxs, goal, energy, max_minutes, True, True, True, 3-len(res), used); res += step
69
- if len(res) < 3:
70
- res += fill_random(3-len(res), used, goal, energy, max_minutes)
71
-
72
- recent.extend([a["title"] for a in res[:3]])
73
- return "\n\n---\n\n".join(render(a) for a in res[:3])
 
 
 
 
 
 
74
 
75
  goals = ["","calm","focus","productivity","wellbeing"]
76
  energies = ["","low","medium","high"]
@@ -86,4 +89,17 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="indigo", neutral_hue="slate"))
86
  e = gr.Radio(energies, label="Energy")
87
  m = gr.Slider(1, 15, step=1, value=5, label="Max minutes")
88
  out = gr.Markdown()
89
- btn.click(search,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  return out, used
43
 
44
  def fill_random(need, used, goal, energy, max_minutes):
45
+ pool = [a for a in actions if a["title"] not in used and a["title"] not in recent and (not goal or a["goal"]==goal) and (not energy or a["energy"]==energy) and (not max_minutes or a["duration_min"]<=int(max_minutes))]
 
 
 
46
  if len(pool) < need:
47
  pool = [a for a in actions if a["title"] not in used and a["title"] not in recent]
48
  random.shuffle(pool)
49
  return pool[:need]
50
 
51
  def search(query, goal, energy, max_minutes):
52
+ try:
53
+ q = (query or "").strip()
54
+ qx = f"{q} Goal:{goal or 'any'} Energy:{energy or 'any'} Max:{int(max_minutes) if max_minutes else ''} minutes"
55
+ v = model.encode([qx], normalize_embeddings=True)
56
+ D, I = index.search(np.asarray(v, dtype="float32"), 800)
57
+ idxs = list(I[0])
58
+ random.shuffle(idxs)
59
+ res, used = [], set()
60
+ step, used = pick_unique(idxs, goal, energy, max_minutes, False, False, False, 3, used); res += step
61
+ if len(res) < 3:
62
+ step, used = pick_unique(idxs, goal, energy, max_minutes, False, True, False, 3-len(res), used); res += step
63
+ if len(res) < 3:
64
+ step, used = pick_unique(idxs, goal, energy, max_minutes, False, True, True, 3-len(res), used); res += step
65
+ if len(res) < 3:
66
+ step, used = pick_unique(idxs, goal, energy, max_minutes, True, True, True, 3-len(res), used); res += step
67
+ if len(res) < 3:
68
+ res += fill_random(3-len(res), used, goal, energy, max_minutes)
69
+ recent.extend([a["title"] for a in res[:3]])
70
+ return "\n\n---\n\n".join(render(a) for a in res[:3])
71
+ except Exception:
72
+ pool = [a for a in actions if (not goal or a["goal"]==goal) and (not energy or a["energy"]==energy) and (not max_minutes or a["duration_min"]<=int(max_minutes))]
73
+ if len(pool) < 3:
74
+ pool = actions[:]
75
+ random.shuffle(pool)
76
+ return "\n\n---\n\n".join(render(a) for a in pool[:3])
77
 
78
  goals = ["","calm","focus","productivity","wellbeing"]
79
  energies = ["","low","medium","high"]
 
89
  e = gr.Radio(energies, label="Energy")
90
  m = gr.Slider(1, 15, step=1, value=5, label="Max minutes")
91
  out = gr.Markdown()
92
+ btn.click(search, [q, g, e, m], out)
93
+ gr.Examples(
94
+ [["Stressed before exam","calm","low",5],
95
+ ["No energy but need to start studying","focus","low",7],
96
+ ["Keep switching tabs while writing essay","focus","medium",10]],
97
+ inputs=[q, g, e, m],
98
+ outputs=out,
99
+ fn=search,
100
+ label="One-click examples",
101
+ cache_examples=False
102
+ )
103
+
104
+ demo.queue(concurrency_count=4, max_size=32)
105
+ demo.launch()