Upload ghost_coder.py with huggingface_hub
Browse files- ghost_coder.py +83 -0
ghost_coder.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pickle
|
| 2 |
+
import sys
|
| 3 |
+
import os # <--- Added this to control Windows
|
| 4 |
+
import math
|
| 5 |
+
|
| 6 |
+
# ... (Keep the cosine_similarity_pure function as it is) ...
|
| 7 |
+
def cosine_similarity_pure(vec1, matrix):
|
| 8 |
+
# (Keep your existing math logic here)
|
| 9 |
+
best_score = -1
|
| 10 |
+
best_index = -1
|
| 11 |
+
query_indices = vec1.indices
|
| 12 |
+
query_data = vec1.data
|
| 13 |
+
query_map = dict(zip(query_indices, query_data))
|
| 14 |
+
|
| 15 |
+
num_rows = matrix.shape[0]
|
| 16 |
+
for i in range(num_rows):
|
| 17 |
+
row = matrix.getrow(i)
|
| 18 |
+
row_indices = row.indices
|
| 19 |
+
row_data = row.data
|
| 20 |
+
score = 0
|
| 21 |
+
for idx, val in zip(row_indices, row_data):
|
| 22 |
+
if idx in query_map:
|
| 23 |
+
score += val * query_map[idx]
|
| 24 |
+
if score > best_score:
|
| 25 |
+
best_score = score
|
| 26 |
+
best_index = i
|
| 27 |
+
return best_index
|
| 28 |
+
|
| 29 |
+
def load_brain():
|
| 30 |
+
try:
|
| 31 |
+
with open("ghost_brain.pkl", "rb") as f:
|
| 32 |
+
return pickle.load(f)
|
| 33 |
+
except FileNotFoundError:
|
| 34 |
+
print("❌ Error: 'ghost_brain.pkl' missing!")
|
| 35 |
+
input("Press Enter to exit...")
|
| 36 |
+
sys.exit()
|
| 37 |
+
|
| 38 |
+
def main():
|
| 39 |
+
print("---------------------------------------")
|
| 40 |
+
print(" 👻 GHOST CODEX (OFFLINE MODE) v1.1")
|
| 41 |
+
print("---------------------------------------")
|
| 42 |
+
|
| 43 |
+
print("Loading Neural Indices...", end="")
|
| 44 |
+
vectorizer, tfidf_matrix, codes = load_brain()
|
| 45 |
+
print(" DONE.")
|
| 46 |
+
print("\n[READY] Type 'exit' to quit.")
|
| 47 |
+
|
| 48 |
+
while True:
|
| 49 |
+
user_input = input("\n>> ").strip()
|
| 50 |
+
if user_input.lower() in ["exit", "quit"]:
|
| 51 |
+
break
|
| 52 |
+
if not user_input: continue
|
| 53 |
+
|
| 54 |
+
try:
|
| 55 |
+
# Match
|
| 56 |
+
user_vec = vectorizer.transform([user_input])
|
| 57 |
+
try:
|
| 58 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
| 59 |
+
best_idx = cosine_similarity(user_vec, tfidf_matrix).argmax()
|
| 60 |
+
except ImportError:
|
| 61 |
+
best_idx = cosine_similarity_pure(user_vec, tfidf_matrix)
|
| 62 |
+
|
| 63 |
+
result_code = codes[best_idx]
|
| 64 |
+
|
| 65 |
+
# Print to terminal
|
| 66 |
+
print("\n[GENERATED SOLUTION]")
|
| 67 |
+
print("-" * 30)
|
| 68 |
+
print(result_code)
|
| 69 |
+
print("-" * 30)
|
| 70 |
+
|
| 71 |
+
# Save AND Open
|
| 72 |
+
filename = "solution.txt"
|
| 73 |
+
with open(filename, "w", encoding="utf-8") as f:
|
| 74 |
+
f.write(result_code)
|
| 75 |
+
|
| 76 |
+
print(f" (Opening {filename} in Notepad...)")
|
| 77 |
+
os.startfile(filename) # <--- THIS IS THE MAGIC LINE
|
| 78 |
+
|
| 79 |
+
except Exception as e:
|
| 80 |
+
print(f"Error: {e}")
|
| 81 |
+
|
| 82 |
+
if __name__ == "__main__":
|
| 83 |
+
main()
|