Elevi7 commited on
Commit
2c09dae
·
verified ·
1 Parent(s): 87d083e

update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -9
app.py CHANGED
@@ -17,6 +17,10 @@ index = faiss.read_index(index_path)
17
  model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
18
  recent = deque(maxlen=120)
19
 
 
 
 
 
20
  def pick_unique(idxs, goal, energy, max_minutes, ignore_goal=False, ignore_energy=False, ignore_minutes=False, need=3, used=None):
21
  if used is None:
22
  used = set()
@@ -38,7 +42,10 @@ def pick_unique(idxs, goal, energy, max_minutes, ignore_goal=False, ignore_energ
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)
@@ -64,15 +71,42 @@ def search(query, goal, energy, max_minutes):
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]()_]()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
18
  recent = deque(maxlen=120)
19
 
20
+ def render(a):
21
+ ctx = ", ".join(a["context"])
22
+ return f"**{a['title']}** \n{a['instruction']} \nGoal: {a['goal']} • Duration: {a['duration_min']} min • Energy: {a['energy']} • Context: {ctx}"
23
+
24
  def pick_unique(idxs, goal, energy, max_minutes, ignore_goal=False, ignore_energy=False, ignore_minutes=False, need=3, used=None):
25
  if used is None:
26
  used = set()
 
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)
 
71
  res += fill_random(3-len(res), used, goal, energy, max_minutes)
72
 
73
  recent.extend([a["title"] for a in res[:3]])
74
+ return "\n\n---\n\n".join(render(a) for a in res[:3])
75
+
 
 
76
  except Exception:
77
+ pool = [a for a in actions if (not goal or a["goal"] == goal)
78
+ and (not energy or a["energy"] == energy)
79
+ and (not max_minutes or a["duration_min"] <= int(max_minutes))]
80
  if len(pool) < 3:
81
  pool = actions[:]
82
  random.shuffle(pool)
83
+ return "\n\n---\n\n".join(render(a) for a in pool[:3])
84
+
85
+ goals = ["","calm","focus","productivity","wellbeing"]
86
+ energies = ["","low","medium","high"]
87
+
88
+ with gr.Blocks(theme=gr.themes.Soft(primary_hue="indigo", neutral_hue="slate")) as demo:
89
+ gr.Markdown("# ActionMatch\nTop-3 micro-actions based on your situation, goal, energy and time.")
90
+ with gr.Row():
91
+ with gr.Column(scale=3):
92
+ q = gr.Textbox(lines=2, label="Your situation", placeholder="e.g., Stressed before exam")
93
+ btn = gr.Button("Recommend")
94
+ with gr.Column(scale=2):
95
+ g = gr.Dropdown(goals, label="Goal")
96
+ e = gr.Radio(energies, label="Energy")
97
+ m = gr.Slider(1, 15, step=1, value=5, label="Max minutes")
98
+ out = gr.Markdown()
99
+ btn.click(search, [q, g, e, m], out)
100
+ gr.Examples(
101
+ [["Stressed before exam","calm","low",5],
102
+ ["No energy but need to start studying","focus","low",7],
103
+ ["Keep switching tabs while writing essay","focus","medium",10]],
104
+ inputs=[q, g, e, m],
105
+ outputs=out,
106
+ fn=search,
107
+ label="One-click examples",
108
+ cache_examples=False
109
+ )
110
+ demo.queue(concurrency_count=4, max_size=32)
111
+
112
+ demo.launch()