Spaces:
Sleeping
Sleeping
update app.py
Browse files
app.py
CHANGED
|
@@ -37,56 +37,42 @@ def pick_unique(idxs, goal, energy, max_minutes, ignore_goal=False, ignore_energ
|
|
| 37 |
break
|
| 38 |
return out, used
|
| 39 |
|
| 40 |
-
def
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
res, used = [], set()
|
| 48 |
-
step, used = pick_unique(idxs, goal, energy, max_minutes, False, False, False, 3, used); res += step
|
| 49 |
-
if len(res) < 3:
|
| 50 |
-
step, used = pick_unique(idxs, goal, energy, max_minutes, False, True, False, 3-len(res), used); res += step
|
| 51 |
-
if len(res) < 3:
|
| 52 |
-
step, used = pick_unique(idxs, goal, energy, max_minutes, False, True, True, 3-len(res), used); res += step
|
| 53 |
-
if len(res) < 3:
|
| 54 |
-
step, used = pick_unique(idxs, goal, energy, max_minutes, True, True, True, 3-len(res), used); res += step
|
| 55 |
-
if len(res) < 3:
|
| 56 |
-
extras = [a for a in actions if a["title"] not in used and a["title"] not in recent]
|
| 57 |
-
random.shuffle(extras)
|
| 58 |
-
res += extras[:3-len(res)]
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
energies = ["","low","medium","high"]
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
out = gr.Markdown()
|
| 80 |
-
btn.click(search, [q, g, e, m], out)
|
| 81 |
-
gr.Examples(
|
| 82 |
-
[["Stressed before exam","calm","low",5],
|
| 83 |
-
["No energy but need to start studying","focus","low",7],
|
| 84 |
-
["Keep switching tabs while writing essay","focus","medium",10]],
|
| 85 |
-
inputs=[q, g, e, m],
|
| 86 |
-
outputs=out,
|
| 87 |
-
fn=search,
|
| 88 |
-
label="One-click examples",
|
| 89 |
-
cache_examples=False
|
| 90 |
-
)
|
| 91 |
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
break
|
| 38 |
return out, used
|
| 39 |
|
| 40 |
+
def fill_random(need, used, goal, energy, max_minutes):
|
| 41 |
+
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))]
|
| 42 |
+
if len(pool) < need:
|
| 43 |
+
pool = [a for a in actions if a["title"] not in used and a["title"] not in recent]
|
| 44 |
+
random.shuffle(pool)
|
| 45 |
+
return pool[:need]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
+
def search(query, goal, energy, max_minutes):
|
| 48 |
+
try:
|
| 49 |
+
q = (query or "").strip()
|
| 50 |
+
qx = f"{q} Goal:{goal or 'any'} Energy:{energy or 'any'} Max:{int(max_minutes) if max_minutes else ''} minutes"
|
| 51 |
+
v = model.encode([qx], normalize_embeddings=True)
|
| 52 |
+
D, I = index.search(np.asarray(v, dtype="float32"), 800)
|
| 53 |
+
idxs = list(I[0]); random.shuffle(idxs)
|
|
|
|
| 54 |
|
| 55 |
+
res, used = [], set()
|
| 56 |
+
step, used = pick_unique(idxs, goal, energy, max_minutes, False, False, False, 3, used); res += step
|
| 57 |
+
if len(res) < 3:
|
| 58 |
+
step, used = pick_unique(idxs, goal, energy, max_minutes, False, True, False, 3-len(res), used); res += step
|
| 59 |
+
if len(res) < 3:
|
| 60 |
+
step, used = pick_unique(idxs, goal, energy, max_minutes, False, True, True, 3-len(res), used); res += step
|
| 61 |
+
if len(res) < 3:
|
| 62 |
+
step, used = pick_unique(idxs, goal, energy, max_minutes, True, True, True, 3-len(res), used); res += step
|
| 63 |
+
if len(res) < 3:
|
| 64 |
+
res += fill_random(3-len(res), used, goal, energy, max_minutes)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
+
recent.extend([a["title"] for a in res[:3]])
|
| 67 |
+
out = []
|
| 68 |
+
for a in res[:3]:
|
| 69 |
+
out.append(f"**{a['title']}** \n{a['instruction']} \nGoal: {a['goal']} • Duration: {a['duration_min']} min • Energy: {a['energy']} • Context: {', '.join(a['context'])}")
|
| 70 |
+
return "\n\n---\n\n".join(out)
|
| 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 |
+
out = []
|
| 77 |
+
for a in pool[:3]:
|
| 78 |
+
out.append(f"**{a['title']}** \n{a['instruction']} \nGoal: {a['goal']} • Duration: {a['duration_min']} min • Energy: {a['energy']} • Context: {', '.join(a['context]()_]()
|