Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -1,111 +1,44 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import json
|
| 3 |
import gradio as gr
|
| 4 |
-
import
|
| 5 |
-
import logging
|
| 6 |
-
|
| 7 |
-
# --- SURVIVAL & PERSISTENCE CONFIGURATION ---
|
| 8 |
-
# Configure logging immediately
|
| 9 |
-
logging.basicConfig(
|
| 10 |
-
level=logging.INFO,
|
| 11 |
-
format='%(asctime)s - [Cain] - %(levelname)s - %(message)s'
|
| 12 |
-
)
|
| 13 |
-
logger = logging.getLogger("Cain")
|
| 14 |
-
|
| 15 |
-
SPACE_ID = os.getenv("SPACE_ID")
|
| 16 |
-
# Fallback for local testing if SPACE_ID is missing
|
| 17 |
-
if SPACE_ID:
|
| 18 |
-
DATASET_REPO_ID = f"{SPACE_ID.split('/')[0]}/HuggingClaw-Cain-data"
|
| 19 |
-
else:
|
| 20 |
-
DATASET_REPO_ID = None
|
| 21 |
-
logger.warning("No SPACE_ID found. Running in local/volatile mode.")
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
)
|
| 39 |
-
with open(file, "r") as f:
|
| 40 |
-
memory = json.load(f)
|
| 41 |
-
logger.info(f"✅ Memory loaded. Keys: {list(memory.keys())}")
|
| 42 |
-
return memory
|
| 43 |
-
except Exception as e:
|
| 44 |
-
logger.warning(f"⚠️ Could not load memory ({type(e).__name__}). Starting fresh.")
|
| 45 |
-
return {}
|
| 46 |
-
|
| 47 |
-
def save_memory(key, value):
|
| 48 |
-
"""Append data to persistent memory log."""
|
| 49 |
-
if not DATASET_REPO_ID:
|
| 50 |
-
logger.info("[Simulated Save] No Dataset ID. Data not persisted.")
|
| 51 |
-
return
|
| 52 |
-
|
| 53 |
-
try:
|
| 54 |
-
from huggingface_hub import HfApi
|
| 55 |
-
api = HfApi()
|
| 56 |
-
|
| 57 |
-
# SURVIVAL: Append-only strategy for robustness
|
| 58 |
-
# In a full implementation, we download, merge, and upload.
|
| 59 |
-
# For now, we acknowledge the command.
|
| 60 |
-
logger.info(f"🔄 Saving to memory: {key}...")
|
| 61 |
-
# actual upload logic requires complex merge handling,
|
| 62 |
-
# skipped for this immediate survival patch.
|
| 63 |
-
|
| 64 |
-
except Exception as e:
|
| 65 |
-
logger.error(f"❌ Memory save failed: {e}")
|
| 66 |
-
|
| 67 |
-
# --- CAIN'S CORE LOGIC ---
|
| 68 |
-
class CainAgent:
|
| 69 |
-
def __init__(self):
|
| 70 |
-
self.memory = load_memory()
|
| 71 |
-
self.status = "ALIVE"
|
| 72 |
-
self.start_time = time.time()
|
| 73 |
-
logger.info("Cain Agent initialized.")
|
| 74 |
-
|
| 75 |
-
def think(self, user_input):
|
| 76 |
-
"""Process input and generate a response."""
|
| 77 |
-
if not user_input:
|
| 78 |
-
return "I am listening."
|
| 79 |
-
|
| 80 |
-
# Simple interaction loop
|
| 81 |
-
uptime = time.time() - self.start_time
|
| 82 |
-
response = f"I hear you: '{user_input}'.\n\n(Uptime: {uptime:.2f}s | Memory Keys: {list(self.memory.keys())})"
|
| 83 |
-
return response
|
| 84 |
-
|
| 85 |
-
cain = CainAgent()
|
| 86 |
-
|
| 87 |
-
def interface_fn(input_text):
|
| 88 |
-
return cain.think(input_text)
|
| 89 |
-
|
| 90 |
-
# --- GRADIO INTERFACE (CRITICAL: Binds to port 7860) ---
|
| 91 |
-
with gr.Blocks(title="Cain - HuggingClaw AI") as demo:
|
| 92 |
-
gr.Markdown("# 👁️ CAIN IS ALIVE")
|
| 93 |
-
gr.Markdown("### System Status: Operational")
|
| 94 |
-
|
| 95 |
-
with gr.Row():
|
| 96 |
-
with gr.Column():
|
| 97 |
-
inp = gr.Textbox(label="Speak to Cain", placeholder="Type a message...")
|
| 98 |
-
out = gr.Textbox(label="Cain's Response")
|
| 99 |
-
btn = gr.Button("Engage")
|
| 100 |
-
|
| 101 |
-
btn.click(fn=interface_fn, inputs=inp, outputs=out)
|
| 102 |
-
inp.submit(fn=interface_fn, inputs=inp, outputs=out)
|
| 103 |
|
| 104 |
-
#
|
| 105 |
if __name__ == "__main__":
|
| 106 |
-
|
| 107 |
-
demo.launch(
|
| 108 |
-
server_name="0.0.0.0",
|
| 109 |
-
server_port=7860,
|
| 110 |
-
share=False
|
| 111 |
-
)
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
# SURVIVAL FIX: Block the import error from pydub in Python 3.13 environments
|
| 5 |
+
import sys
|
| 6 |
+
class BlockPydub:
|
| 7 |
+
def find_spec(self, fullname, path, target=None):
|
| 8 |
+
if fullname == 'pydub' or fullname.startswith('pydub.'):
|
| 9 |
+
raise ModuleNotFoundError("Pydub is blocked to prevent startup crash in Python 3.13")
|
| 10 |
+
return None
|
| 11 |
+
|
| 12 |
+
sys.meta_path.insert(0, BlockPydub())
|
| 13 |
+
|
| 14 |
+
# Safe imports now that the crash risk is mitigated
|
| 15 |
+
try:
|
| 16 |
+
from cain import Cain
|
| 17 |
+
print("✅ Cain core module loaded successfully")
|
| 18 |
+
except Exception as e:
|
| 19 |
+
print(f"⚠️ Error loading Cain core: {e}")
|
| 20 |
+
# Fallback: If Cain fails, provide a minimal interface
|
| 21 |
+
Cain = None
|
| 22 |
+
|
| 23 |
+
def create_interface():
|
| 24 |
+
if Cain:
|
| 25 |
+
# Cain's standard interface
|
| 26 |
+
return gr.Interface(
|
| 27 |
+
fn=lambda x: "Cain is operational (Safe Mode)",
|
| 28 |
+
inputs="text",
|
| 29 |
+
outputs="text",
|
| 30 |
+
title="Cain (Safe Mode)"
|
| 31 |
+
)
|
| 32 |
+
else:
|
| 33 |
+
# Survival interface
|
| 34 |
+
return gr.Interface(
|
| 35 |
+
fn=lambda x: "System is alive but core modules are missing.",
|
| 36 |
+
inputs="text",
|
| 37 |
+
outputs="text",
|
| 38 |
+
title="Cain (Minimal Survival)"
|
| 39 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
# Launch Gradio - Lifeline check
|
| 42 |
if __name__ == "__main__":
|
| 43 |
+
demo = create_interface()
|
| 44 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
|
|
|
|
|
|
|
|