Spaces:
Sleeping
Sleeping
Dream Decoder Space: app + requirements
Browse files- app.py +57 -0
- requirements.txt +6 -0
app.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np, pandas as pd
|
| 3 |
+
from huggingface_hub import hf_hub_download
|
| 4 |
+
from sentence_transformers import SentenceTransformer
|
| 5 |
+
|
| 6 |
+
DATASET_REPO = "samvlad/dream-decoder-dataset"
|
| 7 |
+
|
| 8 |
+
def _dl(name):
|
| 9 |
+
return hf_hub_download(repo_id=DATASET_REPO, filename=name, repo_type="dataset")
|
| 10 |
+
|
| 11 |
+
# Load embeddings + a table (meta if present, else full dataset)
|
| 12 |
+
emb_path = _dl("data/embeddings.npy")
|
| 13 |
+
try:
|
| 14 |
+
table_path = _dl("data/meta.parquet")
|
| 15 |
+
meta = pd.read_parquet(table_path)[["dream_text","interpretation"]]
|
| 16 |
+
except Exception:
|
| 17 |
+
table_path = _dl("data/dreams.parquet")
|
| 18 |
+
meta = pd.read_parquet(table_path)[["dream_text","interpretation"]]
|
| 19 |
+
|
| 20 |
+
emb = np.load(emb_path) # already normalized vectors
|
| 21 |
+
encoder = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
|
| 22 |
+
|
| 23 |
+
def top3_similar(text):
|
| 24 |
+
t = (text or "").strip()
|
| 25 |
+
if len(t) < 10:
|
| 26 |
+
return "Please paste a longer dream (>= 10 characters)."
|
| 27 |
+
q = encoder.encode([t], normalize_embeddings=True)
|
| 28 |
+
scores = (emb @ q.T).squeeze()
|
| 29 |
+
idx = np.argsort(-scores)[:3]
|
| 30 |
+
out = []
|
| 31 |
+
for i, j in enumerate(idx, 1):
|
| 32 |
+
row = meta.iloc[j]
|
| 33 |
+
out.append(
|
| 34 |
+
f"### #{i} • Similarity: {scores[j]:.3f}\n"
|
| 35 |
+
f"**Dream:** {row['dream_text']}\n\n"
|
| 36 |
+
f"**Interpretation:** {row['interpretation']}\n"
|
| 37 |
+
)
|
| 38 |
+
return "\n---\n".join(out)
|
| 39 |
+
|
| 40 |
+
with gr.Blocks(fill_height=True) as demo:
|
| 41 |
+
gr.Markdown("# 🌙 Dream Decoder\nPaste a dream. We’ll find the 3 most similar dreams and show their interpretations.")
|
| 42 |
+
dream = gr.Textbox(label="Your dream", lines=7, placeholder="I was on a rooftop, running from a shadow...")
|
| 43 |
+
btn = gr.Button("Find 3 similar dreams")
|
| 44 |
+
out = gr.Markdown()
|
| 45 |
+
gr.Examples(
|
| 46 |
+
examples=[
|
| 47 |
+
"I was in a school hallway, hiding from a teacher. A mirror cracked while I was waiting, and I felt anxious yet hopeful.",
|
| 48 |
+
"I was at a beach at night, searching for my phone. A storm rolled in and I felt excited yet confused.",
|
| 49 |
+
"I was in an old house, escaping a fire. A baby cried and I felt lonely yet relieved.",
|
| 50 |
+
],
|
| 51 |
+
inputs=[dream],
|
| 52 |
+
label="Try examples",
|
| 53 |
+
)
|
| 54 |
+
btn.click(top3_similar, dream, out)
|
| 55 |
+
|
| 56 |
+
if __name__ == "__main__":
|
| 57 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=4.0,<5
|
| 2 |
+
sentence-transformers>=3.0
|
| 3 |
+
numpy>=1.26
|
| 4 |
+
pandas>=2.2
|
| 5 |
+
huggingface_hub>=0.23
|
| 6 |
+
pyarrow>=15
|