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

Fix dashboard 404 by serving HTML at root

Browse files
Files changed (1) hide show
  1. main.py +37 -63
main.py CHANGED
@@ -1,69 +1,43 @@
1
- from fastapi import FastAPI, UploadFile, File
2
- from fastapi.middleware.cors import CORSMiddleware
 
 
3
  from agents.visual_analyst import VisualAnalyst
4
- from agents.memory_agent import MemoryAgent
5
- from agents.writer_agent import WriterAgent
6
-
7
  app = FastAPI()
8
-
9
- app.add_middleware(
10
- CORSMiddleware,
11
- allow_origins=["*"], # Allows all origins
12
- allow_credentials=True,
13
- allow_methods=["*"], # Allows all methods
14
- allow_headers=["*"], # Allows all headers
15
- )
16
-
17
- # Initialize Agents
18
  visual_agent = VisualAnalyst()
19
- memory_agent = MemoryAgent()
20
- memory_agent.seed_database()
21
- writer_agent = WriterAgent()
22
-
23
- @app.get("/")
24
- def read_root():
25
- return {"message": "MerchFlow AI API is running!"}
26
-
27
- @app.post("/analyze-image")
28
- async def analyze_image(file: UploadFile = File(...)):
29
- # Use global agent
30
- image_bytes = await file.read()
31
- analysis = visual_agent.analyze_image(image_bytes)
32
- return analysis
33
-
34
- @app.post("/generate-catalog")
35
- async def generate_catalog(file: UploadFile = File(...)):
36
  try:
37
- # Step 1: Visual Analysis (Eyes)
38
- image_bytes = await file.read()
39
- visual_data = visual_agent.analyze_image(image_bytes)
 
 
40
 
41
- if "error" in visual_data:
42
- return {"error": "Visual Analysis Failed", "details": visual_data}
43
-
44
- # Step 2: Memory Retrieval (Memory)
45
- # Construct query from visual attributes
46
- query_parts = []
47
- target_keys = ["Main Color", "color", "Material/Texture", "material", "Style/Vibe", "vibe"]
48
- for key in target_keys:
49
- if key in visual_data:
50
- query_parts.append(str(visual_data[key]))
51
-
52
- search_query = " ".join(query_parts)
53
- # Fallback: if no specific keys found, use all string values
54
- if not search_query:
55
- search_query = " ".join([str(v) for v in visual_data.values() if isinstance(v, str)])
56
-
57
- seo_keywords = memory_agent.retrieve_keywords(search_query)
58
-
59
- # Step 3: Copywriting (Brain)
60
- listing = writer_agent.write_listing(visual_data, seo_keywords)
61
-
62
- return {
63
- "visual_data": visual_data,
64
- "seo_keywords": seo_keywords,
65
- "listing": listing
66
- }
67
  except Exception as e:
68
- return {"error": f"Orchestration Error: {str(e)}"}
69
-
 
 
 
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)