Spaces:
Sleeping
Sleeping
update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import json, random
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
import faiss, gradio as gr
|
| 4 |
from sentence_transformers import SentenceTransformer
|
|
@@ -14,14 +15,15 @@ with open(actions_path, "r", encoding="utf-8") as f:
|
|
| 14 |
|
| 15 |
index = faiss.read_index(index_path)
|
| 16 |
model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
|
|
|
|
| 17 |
|
| 18 |
-
def pick_unique(idxs, goal, energy, max_minutes, ignore_goal=False, ignore_energy=False, ignore_minutes=False, need=3,
|
| 19 |
-
if
|
| 20 |
-
|
| 21 |
out = []
|
| 22 |
for i in idxs:
|
| 23 |
a = actions[i]
|
| 24 |
-
if a["title"] in
|
| 25 |
continue
|
| 26 |
if not ignore_goal and goal and a["goal"] != goal:
|
| 27 |
continue
|
|
@@ -29,36 +31,35 @@ def pick_unique(idxs, goal, energy, max_minutes, ignore_goal=False, ignore_energ
|
|
| 29 |
continue
|
| 30 |
if not ignore_minutes and max_minutes and a["duration_min"] > int(max_minutes):
|
| 31 |
continue
|
| 32 |
-
|
| 33 |
out.append(a)
|
| 34 |
if len(out) == need:
|
| 35 |
break
|
| 36 |
-
return out,
|
| 37 |
-
|
| 38 |
-
def fill_random(need, used_titles):
|
| 39 |
-
pool = [a for a in actions if a["title"] not in used_titles]
|
| 40 |
-
random.shuffle(pool)
|
| 41 |
-
return pool[:need]
|
| 42 |
|
| 43 |
def search(query, goal, energy, max_minutes):
|
| 44 |
-
q = query
|
| 45 |
-
|
|
|
|
| 46 |
D, I = index.search(np.asarray(v, dtype="float32"), 800)
|
| 47 |
idxs = list(I[0]); random.shuffle(idxs)
|
| 48 |
|
| 49 |
-
|
| 50 |
-
step, used = pick_unique(idxs, goal, energy, max_minutes, False, False, False, 3, used);
|
| 51 |
-
if len(
|
| 52 |
-
step, used = pick_unique(idxs, goal, energy, max_minutes, False, True, False, 3-len(
|
| 53 |
-
if len(
|
| 54 |
-
step, used = pick_unique(idxs, goal, energy, max_minutes, False, True, True, 3-len(
|
| 55 |
-
if len(
|
| 56 |
-
step, used = pick_unique(idxs, goal, energy, max_minutes, True, True, True, 3-len(
|
| 57 |
-
if len(
|
| 58 |
-
|
|
|
|
|
|
|
| 59 |
|
|
|
|
| 60 |
out = []
|
| 61 |
-
for a in
|
| 62 |
out.append(f"**{a['title']}** \n{a['instruction']} \nGoal: {a['goal']} • Duration: {a['duration_min']} min • Energy: {a['energy']} • Context: {', '.join(a['context'])}")
|
| 63 |
return "\n\n---\n\n".join(out)
|
| 64 |
|
|
|
|
| 1 |
import json, random
|
| 2 |
+
from collections import deque
|
| 3 |
import numpy as np
|
| 4 |
import faiss, gradio as gr
|
| 5 |
from sentence_transformers import SentenceTransformer
|
|
|
|
| 15 |
|
| 16 |
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()
|
| 23 |
out = []
|
| 24 |
for i in idxs:
|
| 25 |
a = actions[i]
|
| 26 |
+
if a["title"] in used or a["title"] in recent:
|
| 27 |
continue
|
| 28 |
if not ignore_goal and goal and a["goal"] != goal:
|
| 29 |
continue
|
|
|
|
| 31 |
continue
|
| 32 |
if not ignore_minutes and max_minutes and a["duration_min"] > int(max_minutes):
|
| 33 |
continue
|
| 34 |
+
used.add(a["title"])
|
| 35 |
out.append(a)
|
| 36 |
if len(out) == need:
|
| 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 |
|