Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import numpy as np, pandas as pd | |
| from huggingface_hub import hf_hub_download | |
| from sentence_transformers import SentenceTransformer | |
| DATASET_REPO = "samvlad/dream-decoder-dataset" | |
| def _dl(name): | |
| return hf_hub_download(repo_id=DATASET_REPO, filename=name, repo_type="dataset") | |
| # Load embeddings + a table (meta if present, else full dataset) | |
| emb_path = _dl("data/embeddings.npy") | |
| try: | |
| table_path = _dl("data/meta.parquet") | |
| meta = pd.read_parquet(table_path)[["dream_text","interpretation"]] | |
| except Exception: | |
| table_path = _dl("data/dreams.parquet") | |
| meta = pd.read_parquet(table_path)[["dream_text","interpretation"]] | |
| emb = np.load(emb_path) # already normalized vectors | |
| encoder = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2") | |
| def top3_similar(text): | |
| t = (text or "").strip() | |
| if len(t) < 10: | |
| return "Please paste a longer dream (>= 10 characters)." | |
| q = encoder.encode([t], normalize_embeddings=True) | |
| scores = (emb @ q.T).squeeze() | |
| idx = np.argsort(-scores)[:3] | |
| out = [] | |
| for i, j in enumerate(idx, 1): | |
| row = meta.iloc[j] | |
| out.append( | |
| f"### #{i} • Similarity: {scores[j]:.3f}\n" | |
| f"**Dream:** {row['dream_text']}\n\n" | |
| f"**Interpretation:** {row['interpretation']}\n" | |
| ) | |
| return "\n---\n".join(out) | |
| with gr.Blocks(fill_height=True) as demo: | |
| gr.Markdown("# 🌙 Dream Decoder\nPaste a dream. We’ll find the 3 most similar dreams and show their interpretations.") | |
| dream = gr.Textbox(label="Your dream", lines=7, placeholder="I was on a rooftop, running from a shadow...") | |
| btn = gr.Button("Find 3 similar dreams") | |
| out = gr.Markdown() | |
| gr.Examples( | |
| examples=[ | |
| "I was in a school hallway, hiding from a teacher. A mirror cracked while I was waiting, and I felt anxious yet hopeful.", | |
| "I was at a beach at night, searching for my phone. A storm rolled in and I felt excited yet confused.", | |
| "I was in an old house, escaping a fire. A baby cried and I felt lonely yet relieved.", | |
| ], | |
| inputs=[dream], | |
| label="Try examples", | |
| ) | |
| btn.click(top3_similar, dream, out) | |
| if __name__ == "__main__": | |
| demo.launch() | |