File size: 11,369 Bytes
7035ccd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import os
import json
from pathlib import Path
from datetime import datetime
from typing import List, Dict, Any

import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity

try:
    import gradio as gr
    GRADIO_AVAILABLE = True
except Exception:
    GRADIO_AVAILABLE = False

try:
    from huggingface_hub import hf_hub_upload
    HF_AVAILABLE = True
except Exception:
    HF_AVAILABLE = False

DB_PATH = Path("synchronicities.json")


class SynchronicityDB:
    def __init__(self, path: Path = DB_PATH):
        self.path = path
        if not self.path.exists():
            self._write({"entries": []})
        self._data = self._read()

    def _read(self):
        with open(self.path, "r", encoding="utf-8") as f:
            return json.load(f)

    def _write(self, data):
        with open(self.path, "w", encoding="utf-8") as f:
            json.dump(data, f, indent=2, ensure_ascii=False)

    def add_entry(self, text: str, tags: List[str], outcome: str = "", witness: str = "Asset 448804922"):
        entry = {
            "id": len(self._data["entries"]) + 1,
            "timestamp": datetime.utcnow().isoformat() + "Z",
            "text": text,
            "tags": tags,
            "outcome": outcome,
            "witness": witness,
        }
        self._data["entries"].append(entry)
        self._write(self._data)
        return entry

    def all_texts(self) -> List[str]:
        return [e["text"] for e in self._data["entries"]]

    def all_entries(self) -> List[Dict[str, Any]]:
        return self._data["entries"]

    def export_json(self) -> str:
        return json.dumps(self._data, indent=2, ensure_ascii=False)

    def reset(self):
        self._write({"entries": []})
        return True


def extract_tfidf_matrix(texts: List[str]):
    if not texts:
        return None, None
    vect = TfidfVectorizer(max_features=2000, stop_words="english")
    mat = vect.fit_transform(texts)
    return mat, vect


def find_similar(new_text: str, db_texts: List[str], top_k: int = 5):
    if not db_texts:
        return []
    texts = db_texts + [new_text]
    mat, _ = extract_tfidf_matrix(texts)
    if mat is None:
        return []
    sims = cosine_similarity(mat[-1], mat[:-1]).flatten()
    idx_sorted = np.argsort(-sims)
    results = []
    for i in idx_sorted[:top_k]:
        results.append({"index": int(i), "score": float(sims[i])})
    return results


def coherence_score(matches: List[Dict[str, float]]):
    if not matches:
        return 0.0
    return float(np.mean([m["score"] for m in matches]))


def predict_outcomes(matches: List[Dict[str, Any]], db_entries: List[Dict[str, Any]]):
    if not matches:
        return "No prediction — not enough history."
    outcomes = []
    tag_counts: Dict[str, int] = {}
    for m in matches:
        idx = m.get("index")
        if idx is None:
            continue
        if idx < 0 or idx >= len(db_entries):
            continue
        e = db_entries[idx]
        if e.get("outcome"):
            outcomes.append(e["outcome"])
        for t in e.get("tags", []):
            tag_counts[t] = tag_counts.get(t, 0) + 1

    suggestion_parts: List[str] = []
    if outcomes:
        from collections import Counter
        c = Counter(outcomes)
        top_outcome, cnt = c.most_common(1)[0]
        suggestion_parts.append(f"Observed outcome pattern: '{top_outcome}' (seen {cnt} times among similar entries)")

    if tag_counts:
        sorted_tags = sorted(tag_counts.items(), key=lambda x: -x[1])
        top_tags = [t for t, _ in sorted_tags[:3]]
        suggestion_parts.append(f"Recurring tags among similar events: {', '.join(top_tags)}")

    if not suggestion_parts:
        return "No clear prediction from similar entries. Consider recording outcomes for better forecasts."

    return " | ".join(suggestion_parts)


def upload_db_to_hf(file_path: Path, repo_id: str, token: str, commit_message: str = "Update synchronicities.json"):
    if not HF_AVAILABLE:
        return False, "huggingface_hub not installed"
    if not token:
        return False, "No HF token provided"
    try:
        with open(file_path, "rb") as f:
            hf_hub_upload(repo_id=repo_id, path_or_fileobj=f, path_in_repo="synchronicities.json", token=token, repo_type="space")
        return True, "Uploaded to Hugging Face Hub"
    except Exception as e:
        return False, str(e)


# Core logic

db = SynchronicityDB()


def bot_response(user_message: str) -> str:
    lines = [l.strip() for l in user_message.splitlines() if l.strip()]
    tags: List[str] = []
    outcome = ""
    text_lines: List[str] = []
    for ln in lines:
        if ln.upper().startswith("TAGS:"):
            tags = [t.strip() for t in ln.split(":", 1)[1].split(",") if t.strip()]
        elif ln.upper().startswith("OUTCOME:"):
            outcome = ln.split(":", 1)[1].strip()
        else:
            text_lines.append(ln)

    text = " ".join(text_lines).strip()

    if not text:
        return "I didn't catch the event text. Please describe the synchronicity."

    entry = db.add_entry(text=text, tags=tags, outcome=outcome)

    db_texts = db.all_texts()[:-1]
    matches = find_similar(new_text=text, db_texts=db_texts, top_k=5)
    score = coherence_score(matches)

    assistant_parts: List[str] = []
    assistant_parts.append("🌙 — The Oracle records your entry into the ledger of coincidence.")
    assistant_parts.append(f"A coherence whisper: {score:.3f} (0–1, higher means more resonance with past entries)")

    if matches:
        assistant_parts.append("I perceive echoes from the archive:")
        for m in matches:
            idx = m.get("index")
            if idx is None:
                continue
            if idx < 0 or idx >= len(db.all_entries()):
                continue
            e = db.all_entries()[idx]
            snippet = e["text"][:180] + ("..." if len(e["text"]) > 180 else "")
            assistant_parts.append(f"— {snippet} (score {m['score']:.3f}) — tags: {', '.join(e.get('tags', []))}")

    prediction = predict_outcomes(matches, db.all_entries())
    assistant_parts.append("Possible suggestion & pattern note:")
    assistant_parts.append(prediction)

    assistant_parts.append("If you wish to tag this as an observation only, add 'OUTCOME: none'. To attach tags, write 'TAGS: tag1, tag2' on a new line.")

    assistant = "\n\n".join(assistant_parts)

    hf_token = os.environ.get("HF_TOKEN")
    hf_repo = os.environ.get("HF_REPO")
    if hf_token and hf_repo:
        ok, msg = upload_db_to_hf(DB_PATH, hf_repo, hf_token)
        if ok:
            assistant += "\n\n📡 The ledger was synchronized with your Hugging Face Space."
        else:
            assistant += f"\n\n⚠️ Sync to Hugging Face failed: {msg}"

    return assistant


def reset_db_action():
    db.reset()
    return "Database cleared."


def export_db_action():
    return db.export_json()


if GRADIO_AVAILABLE:
    with gr.Blocks(title="Quantum Synchronicity Chatbot") as demo:
        gr.Markdown("# Quantum Synchronicity Chatbot — Oracle Interface")
        gr.Markdown("A mystical-toned chat interface. To add an entry, simply paste the description. Optional lines:\nTAGS: mirror, 11:11\nOUTCOME: travel_home\n\nIf HF_TOKEN and HF_REPO are set as environment variables, the database will try to sync after each entry.")

        chatbot = gr.Chatbot(label="Oracle")
        msg = gr.Textbox(placeholder="Type your synchronicity or question here...\n(You can add TAGS: and OUTCOME: on separate lines)")
        clear = gr.Button("Clear chat")

        with gr.Row():
            add_btn = gr.Button("Add entry & analyze")
            export_btn = gr.Button("Export DB JSON")
            reset_btn = gr.Button("Reset DB")

        db_output = gr.Textbox(label="Database (JSON export)", lines=8)

        def user_submit(user_input, history):
            history = history or []
            assistant_text = bot_response(user_input)
            history.append((user_input, assistant_text))
            return history

        add_btn.click(fn=user_submit, inputs=[msg, chatbot], outputs=[chatbot])
        export_btn.click(fn=export_db_action, inputs=None, outputs=[db_output])
        reset_btn.click(fn=reset_db_action, inputs=None, outputs=[db_output])

        clear.click(lambda: [], None, chatbot)

    if __name__ == "__main__":
        demo.launch()
else:
    def cli_help():
        print("Gradio is not installed in this environment. Running in CLI fallback mode.")
        print("Commands:\n  add - Add a new synchronicity\n  export - Print DB JSON\n  reset - Clear the DB\n  tests - Run basic tests\n  exit - Quit")

    def cli_loop():
        cli_help()
        while True:
            cmd = input("> ").strip()
            if not cmd:
                continue
            if cmd == "exit":
                break
            if cmd == "help":
                cli_help()
                continue
            if cmd == "add":
                print("Enter your synchronicity text (end with a blank line):")
                lines = []
                while True:
                    try:
                        ln = input()
                    except EOFError:
                        ln = ""
                    if ln.strip() == "":
                        break
                    lines.append(ln)
                text = " ".join(lines).strip()
                print("Optional: enter TAGS: comma,separated or leave blank:")
                tags_line = input().strip()
                tags = [t.strip() for t in tags_line.split(",") if t.strip()] if tags_line else []
                print("Optional: enter OUTCOME: or leave blank:")
                outcome = input().strip()
                assistant = bot_response(f"{text}\nTAGS: {', '.join(tags)}\nOUTCOME: {outcome}")
                print("\n---\n")
                print(assistant)
                print("\n---\n")
                continue
            if cmd == "export":
                print(export_db_action())
                continue
            if cmd == "reset":
                print(reset_db_action())
                continue
            if cmd == "tests":
                run_tests()
                continue
            print("Unknown command. Type 'help' for options.")

    def run_tests():
        import tempfile
        print("Running basic tests...")
        with tempfile.TemporaryDirectory() as td:
            test_path = Path(td) / "test_db.json"
            test_db = SynchronicityDB(path=test_path)
            assert test_db.all_entries() == []
            e1 = test_db.add_entry("Saw mirror, 11:11 on the train", ["mirror", "11:11"], outcome="trip")
            assert e1["id"] == 1
            e2 = test_db.add_entry("Heard same song twice", ["song"], outcome="meeting")
            assert e2["id"] == 2
            texts = test_db.all_texts()
            assert len(texts) == 2
            sims = find_similar("Saw mirror again", texts, top_k=2)
            assert isinstance(sims, list)
            print("All tests passed.")

    if __name__ == "__main__":
        print("Gradio not available. To use the web UI, install gradio (`pip install gradio`).")
        print("If you'd like me to change expected behavior for any command, tell me in chat.")
        cli_loop()