Elevi7 commited on
Commit
87d083e
·
verified ·
1 Parent(s): 9ea0ebc

update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -50
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 search(query, goal, energy, max_minutes):
41
- q = (query or "").strip()
42
- qx = f"{q} Goal:{goal or 'any'} Energy:{energy or 'any'} Max:{int(max_minutes) if max_minutes else ''} minutes"
43
- v = model.encode([qx], normalize_embeddings=True)
44
- D, I = index.search(np.asarray(v, dtype="float32"), 800)
45
- idxs = list(I[0]); random.shuffle(idxs)
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
- recent.extend([a["title"] for a in res[:3]])
61
- out = []
62
- for a in res[:3]:
63
- out.append(f"**{a['title']}** \n{a['instruction']} \nGoal: {a['goal']} Duration: {a['duration_min']} min • Energy: {a['energy']} • Context: {', '.join(a['context'])}")
64
- return "\n\n---\n\n".join(out)
65
-
66
- goals = ["","calm","focus","productivity","wellbeing"]
67
- energies = ["","low","medium","high"]
68
 
69
- with gr.Blocks(theme=gr.themes.Soft(primary_hue="indigo", neutral_hue="slate")) as demo:
70
- gr.Markdown("# ActionMatch\nTop-3 micro-actions based on your situation, goal, energy and time.")
71
- with gr.Row():
72
- with gr.Column(scale=3):
73
- q = gr.Textbox(lines=2, label="Your situation", placeholder="e.g., Stressed before exam")
74
- btn = gr.Button("Recommend")
75
- with gr.Column(scale=2):
76
- g = gr.Dropdown(goals, label="Goal")
77
- e = gr.Radio(energies, label="Energy")
78
- m = gr.Slider(1, 15, step=1, value=5, label="Max minutes")
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
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
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]()_]()