Spaces:
Runtime error
Runtime error
Upload 5 files
Browse files- src/agents/critic.py +1 -0
- src/agents/executor.py +1 -0
- src/agents/planner.py +1 -0
- src/core/cognitive_engine.py +1 -101
- src/utils/hf_packager.py +1 -38
src/agents/critic.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
# critic.py
|
src/agents/executor.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
# executor.py
|
src/agents/planner.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
# planner.py
|
src/core/cognitive_engine.py
CHANGED
|
@@ -1,101 +1 @@
|
|
| 1 |
-
|
| 2 |
-
import os
|
| 3 |
-
import json
|
| 4 |
-
import re
|
| 5 |
-
|
| 6 |
-
class CognitiveEngine:
|
| 7 |
-
def __init__(self, model):
|
| 8 |
-
self.model = model
|
| 9 |
-
|
| 10 |
-
def identify_improvements(self, task_description):
|
| 11 |
-
prompt = f"What improvements can be made to this task?\n\nTask: {task_description}"
|
| 12 |
-
response = self.model(prompt)
|
| 13 |
-
return response.strip().split("\n")
|
| 14 |
-
|
| 15 |
-
def generate_enhancements(self, improvements):
|
| 16 |
-
prompt = "Generate code changes based on the following improvements:\n"
|
| 17 |
-
prompt += "\n".join(improvements)
|
| 18 |
-
return self.model(prompt)
|
| 19 |
-
|
| 20 |
-
def apply_enhancements(self, code_updates):
|
| 21 |
-
print("[Simulation] Applying the following code changes:")
|
| 22 |
-
print(code_updates)
|
| 23 |
-
return True # Simulate success
|
| 24 |
-
|
| 25 |
-
def load_knowledge(self):
|
| 26 |
-
try:
|
| 27 |
-
with open(self.knowledge_file, "r") as f:
|
| 28 |
-
self.knowledge = json.load(f)
|
| 29 |
-
except:
|
| 30 |
-
self.knowledge = {"improvements": [], "solutions": {}, "tasks": []}
|
| 31 |
-
|
| 32 |
-
def save_knowledge(self):
|
| 33 |
-
with open(self.knowledge_file, "w") as f:
|
| 34 |
-
json.dump(self.knowledge, f, indent=2)
|
| 35 |
-
|
| 36 |
-
def identify_improvements(self, user_task):
|
| 37 |
-
prompt = f"""
|
| 38 |
-
You are an autonomous AI system. The user has requested: "{user_task}"
|
| 39 |
-
|
| 40 |
-
Analyze your current capabilities and suggest concrete improvements to address this request.
|
| 41 |
-
Focus on:
|
| 42 |
-
1. Code quality enhancements
|
| 43 |
-
2. Performance optimizations
|
| 44 |
-
3. New functionality
|
| 45 |
-
|
| 46 |
-
Respond in JSON format: {{"improvements": ["item1", "item2", "item3"]}}
|
| 47 |
-
"""
|
| 48 |
-
response = self.model(prompt, max_new_tokens=256)
|
| 49 |
-
|
| 50 |
-
# Extract JSON from response
|
| 51 |
-
try:
|
| 52 |
-
json_match = re.search(r'\{.*\}', response, re.DOTALL)
|
| 53 |
-
if json_match:
|
| 54 |
-
return json.loads(json_match.group())["improvements"]
|
| 55 |
-
except:
|
| 56 |
-
pass
|
| 57 |
-
|
| 58 |
-
# Fallback if JSON parsing fails
|
| 59 |
-
return [
|
| 60 |
-
f"Implement user request: {user_task}",
|
| 61 |
-
"Optimize related systems",
|
| 62 |
-
"Add documentation for new features"
|
| 63 |
-
]
|
| 64 |
-
|
| 65 |
-
def generate_enhancements(self, improvements):
|
| 66 |
-
prompt = f"""
|
| 67 |
-
You are a self-coding AI. Generate Python code to implement these improvements:
|
| 68 |
-
{', '.join(improvements)}
|
| 69 |
-
|
| 70 |
-
Requirements:
|
| 71 |
-
- Modify only existing files
|
| 72 |
-
- Include necessary imports
|
| 73 |
-
- Add comments explaining changes
|
| 74 |
-
- Ensure backward compatibility
|
| 75 |
-
|
| 76 |
-
Output ONLY valid Python code in a single code block.
|
| 77 |
-
"""
|
| 78 |
-
return self.model(prompt, max_new_tokens=1024)
|
| 79 |
-
|
| 80 |
-
def apply_enhancements(self, code):
|
| 81 |
-
try:
|
| 82 |
-
# Extract code block
|
| 83 |
-
if "```python" in code:
|
| 84 |
-
code = code.split("```python")[1].split("```")[0]
|
| 85 |
-
elif "```" in code:
|
| 86 |
-
code = code.split("```")[1].split("```")[0]
|
| 87 |
-
|
| 88 |
-
# Validate code
|
| 89 |
-
compile(code, "<string>", "exec")
|
| 90 |
-
|
| 91 |
-
# Apply changes (in real system would write to files)
|
| 92 |
-
print(f"Applying code changes:\n{code[:500]}...")
|
| 93 |
-
|
| 94 |
-
# Update knowledge
|
| 95 |
-
self.knowledge["solutions"][str(time.time())] = code[:500] + "..."
|
| 96 |
-
self.save_knowledge()
|
| 97 |
-
|
| 98 |
-
return True
|
| 99 |
-
except Exception as e:
|
| 100 |
-
print(f"Code validation failed: {str(e)}")
|
| 101 |
-
return True
|
|
|
|
| 1 |
+
# cognitive_engine.py
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
src/utils/hf_packager.py
CHANGED
|
@@ -1,38 +1 @@
|
|
| 1 |
-
|
| 2 |
-
import datetime
|
| 3 |
-
import zipfile
|
| 4 |
-
import os
|
| 5 |
-
import json
|
| 6 |
-
|
| 7 |
-
class HFSpacePackager:
|
| 8 |
-
def __init__(self):
|
| 9 |
-
self.api = HfApi()
|
| 10 |
-
self.token = os.getenv("HF_TOKEN", "")
|
| 11 |
-
|
| 12 |
-
def create_snapshot(self):
|
| 13 |
-
"""Create snapshot in /tmp directory"""
|
| 14 |
-
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 15 |
-
zip_name = f"/tmp/snapshot_{timestamp}.zip"
|
| 16 |
-
manifest = {
|
| 17 |
-
"version": timestamp,
|
| 18 |
-
"files": ["app.py", "src/"],
|
| 19 |
-
"system": "Autonomous AI HF Space"
|
| 20 |
-
}
|
| 21 |
-
|
| 22 |
-
# Create zip
|
| 23 |
-
with zipfile.ZipFile(zip_name, 'w') as zipf:
|
| 24 |
-
# Add key files
|
| 25 |
-
for file in ["app.py", "requirements.txt", "Dockerfile"]:
|
| 26 |
-
if os.path.exists(file):
|
| 27 |
-
zipf.write(file)
|
| 28 |
-
|
| 29 |
-
# Add src directory
|
| 30 |
-
for root, _, files in os.walk("src"):
|
| 31 |
-
for file in files:
|
| 32 |
-
file_path = os.path.join(root, file)
|
| 33 |
-
zipf.write(file_path)
|
| 34 |
-
|
| 35 |
-
# Add manifest
|
| 36 |
-
zipf.writestr("MANIFEST.json", json.dumps(manifest, indent=2))
|
| 37 |
-
|
| 38 |
-
return f"Snapshot created: {zip_name}"
|
|
|
|
| 1 |
+
# hf_packager.py
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|