Gaurav vashistha commited on
Commit
d288e3a
·
1 Parent(s): 9efc352

Restore full brain logic and fix dashboard URL

Browse files
Files changed (2) hide show
  1. dashboard.html +1 -1
  2. main.py +50 -21
dashboard.html CHANGED
@@ -412,7 +412,7 @@ SKU: JK-2024-WTR"></textarea>
412
 
413
  try {
414
  // Determine API URL - expecting localhost for this demo
415
- const response = await fetch('http://localhost:8000/generate-catalog', {
416
  method: 'POST',
417
  body: formData
418
  });
 
412
 
413
  try {
414
  // Determine API URL - expecting localhost for this demo
415
+ const response = await fetch('/generate-catalog', {
416
  method: 'POST',
417
  body: formData
418
  });
main.py CHANGED
@@ -1,43 +1,72 @@
1
  import os
2
- from fastapi import FastAPI, UploadFile, File, HTTPException
3
  from fastapi.responses import HTMLResponse, JSONResponse
4
- from fastapi.staticfiles import StaticFiles
5
- from agents.visual_analyst import VisualAnalyst
6
  from dotenv import load_dotenv
7
- # Load environment variables
 
 
 
 
 
8
  load_dotenv()
9
  app = FastAPI()
10
- # Initialize Agent
11
- visual_agent = VisualAnalyst()
12
- # 1. READ THE DASHBOARD HTML FILE INTO MEMORY
13
  try:
14
- with open("dashboard.html", "r") as f:
15
- dashboard_html = f.read()
16
- except FileNotFoundError:
17
- dashboard_html = "<h1>Error: dashboard.html not found. Please ensure the file exists.</h1>"
18
- # 2. SERVE DASHBOARD AT ROOT (Home Page)
 
 
 
 
 
 
19
  @app.get("/", response_class=HTMLResponse)
20
  async def read_root():
21
- return dashboard_html
22
- # 3. KEEP /dashboard ROUTE AS BACKUP
23
- @app.get("/dashboard", response_class=HTMLResponse)
24
- async def read_dashboard():
25
- return dashboard_html
26
- @app.post("/analyze")
27
- async def analyze_merch(file: UploadFile = File(...)):
28
  try:
 
 
 
 
 
 
 
 
 
 
29
  os.makedirs("uploads", exist_ok=True)
30
  file_path = f"uploads/{file.filename}"
31
  with open(file_path, "wb") as f:
32
  f.write(await file.read())
33
- result = await visual_agent.analyze_image(file_path)
 
 
 
 
 
 
 
 
 
 
34
 
 
35
  if os.path.exists(file_path):
36
  os.remove(file_path)
37
 
38
- return JSONResponse(content=result)
 
 
 
 
 
39
  except Exception as e:
 
40
  return JSONResponse(content={"error": str(e)}, status_code=500)
 
41
  if __name__ == "__main__":
42
  import uvicorn
43
  uvicorn.run(app, host="0.0.0.0", port=7860)
 
1
  import os
2
+ from fastapi import FastAPI, UploadFile, File
3
  from fastapi.responses import HTMLResponse, JSONResponse
 
 
4
  from dotenv import load_dotenv
5
+
6
+ # Import Agents
7
+ from agents.visual_analyst import VisualAnalyst
8
+ from agents.memory_agent import MemoryAgent
9
+ from agents.writer_agent import WriterAgent
10
+
11
  load_dotenv()
12
  app = FastAPI()
13
+
14
+ # Initialize All Agents
 
15
  try:
16
+ visual_agent = VisualAnalyst()
17
+ memory_agent = MemoryAgent()
18
+ writer_agent = WriterAgent()
19
+
20
+ # Seed memory on startup
21
+ memory_agent.seed_database()
22
+ print("✅ All Agents Online")
23
+ except Exception as e:
24
+ print(f"⚠️ Warning: Some agents failed to load: {e}")
25
+
26
+ # 1. SERVE DASHBOARD AT ROOT
27
  @app.get("/", response_class=HTMLResponse)
28
  async def read_root():
 
 
 
 
 
 
 
29
  try:
30
+ with open("dashboard.html", "r") as f:
31
+ return f.read()
32
+ except FileNotFoundError:
33
+ return "Error: dashboard.html not found"
34
+
35
+ # 2. THE MAIN ORCHESTRATOR ENDPOINT
36
+ @app.post("/generate-catalog")
37
+ async def generate_catalog(file: UploadFile = File(...)):
38
+ try:
39
+ # A. Save Temp File
40
  os.makedirs("uploads", exist_ok=True)
41
  file_path = f"uploads/{file.filename}"
42
  with open(file_path, "wb") as f:
43
  f.write(await file.read())
44
+
45
+ # B. Visual Analysis (The Eyes)
46
+ visual_data = await visual_agent.analyze_image(file_path)
47
+
48
+ # C. Memory Search (The Context)
49
+ # Create a search query from visual data
50
+ query = f"{visual_data.get('main_color', '')} {visual_data.get('product_type', 'product')}"
51
+ seo_keywords = memory_agent.retrieve_keywords(query)
52
+
53
+ # D. Write Copy (The Brain)
54
+ listing = writer_agent.write_listing(visual_data, seo_keywords)
55
 
56
+ # Cleanup
57
  if os.path.exists(file_path):
58
  os.remove(file_path)
59
 
60
+ # Return Full Data Structure
61
+ return JSONResponse(content={
62
+ "visual_data": visual_data,
63
+ "seo_keywords": seo_keywords,
64
+ "listing": listing
65
+ })
66
  except Exception as e:
67
+ print(f"Error: {e}")
68
  return JSONResponse(content={"error": str(e)}, status_code=500)
69
+
70
  if __name__ == "__main__":
71
  import uvicorn
72
  uvicorn.run(app, host="0.0.0.0", port=7860)