File size: 4,030 Bytes
14589fa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import subprocess

def restore_main():
    content = """import os
from fastapi import FastAPI, UploadFile, File
from fastapi.responses import HTMLResponse, JSONResponse
from dotenv import load_dotenv

# Import Agents
from agents.visual_analyst import VisualAnalyst
from agents.memory_agent import MemoryAgent
from agents.writer_agent import WriterAgent

load_dotenv()
app = FastAPI()

# Initialize All Agents
try:
    visual_agent = VisualAnalyst()
    memory_agent = MemoryAgent()
    writer_agent = WriterAgent()
    
    # Seed memory on startup
    memory_agent.seed_database()
    print("βœ… All Agents Online")
except Exception as e:
    print(f"⚠️ Warning: Some agents failed to load: {e}")

# 1. SERVE DASHBOARD AT ROOT
@app.get("/", response_class=HTMLResponse)
async def read_root():
    try:
        with open("dashboard.html", "r") as f:
            return f.read()
    except FileNotFoundError:
        return "Error: dashboard.html not found"

# 2. THE MAIN ORCHESTRATOR ENDPOINT
@app.post("/generate-catalog")
async def generate_catalog(file: UploadFile = File(...)):
    try:
        # A. Save Temp File
        os.makedirs("uploads", exist_ok=True)
        file_path = f"uploads/{file.filename}"
        with open(file_path, "wb") as f:
            f.write(await file.read())

        # B. Visual Analysis (The Eyes)
        visual_data = await visual_agent.analyze_image(file_path)
        
        # C. Memory Search (The Context)
        # Create a search query from visual data
        query = f"{visual_data.get('main_color', '')} {visual_data.get('product_type', 'product')}"
        seo_keywords = memory_agent.retrieve_keywords(query)
        
        # D. Write Copy (The Brain)
        listing = writer_agent.write_listing(visual_data, seo_keywords)
        
        # Cleanup
        if os.path.exists(file_path):
            os.remove(file_path)
            
        # Return Full Data Structure
        return JSONResponse(content={
            "visual_data": visual_data,
            "seo_keywords": seo_keywords,
            "listing": listing
        })
    except Exception as e:
        print(f"Error: {e}")
        return JSONResponse(content={"error": str(e)}, status_code=500)

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=7860)
"""
    with open("main.py", "w", encoding="utf-8") as f:
        f.write(content)
    print("βœ… main.py restored with full agent logic.")

def update_dashboard():
    try:
        with open("dashboard.html", "r", encoding="utf-8") as f:
            content = f.read()
        
        # Replace localhost URL with relative path
        new_content = content.replace("http://localhost:8000/generate-catalog", "/generate-catalog")
        
        with open("dashboard.html", "w", encoding="utf-8") as f:
            f.write(new_content)
        print("βœ… dashboard.html updated for cloud deployment.")
    except Exception as e:
        print(f"❌ Error updating dashboard.html: {e}")

def deploy():
    print("πŸš€ Starting Deployment...")
    commands = [
        ["git", "add", "main.py", "dashboard.html"],
        ["git", "commit", "-m", "Restore full brain logic and fix dashboard URL"],
        ["git", "push", "space", "clean_deploy:main"]
    ]
    
    for cmd in commands:
        try:
            print(f"Running: {' '.join(cmd)}")
            result = subprocess.run(cmd, check=True, capture_output=True, text=True)
            print(result.stdout)
        except subprocess.CalledProcessError as e:
            print(f"❌ Error running command: {' '.join(cmd)}")
            print(e.stderr)
            # Don't break on commit error as it might be empty
            if "nothing to commit" in e.stderr:
                continue
            # For other errors we might want to continue or stop, but let's try to proceed
    print("βœ… Deployment script finished.")

if __name__ == "__main__":
    print("πŸ”§ Restoring Full Brain...")
    restore_main()
    update_dashboard()
    deploy()