Spaces:
Sleeping
Sleeping
Gaurav vashistha
commited on
Commit
·
9efc352
1
Parent(s):
feaab26
Fix dashboard 404 by serving HTML at root
Browse files
main.py
CHANGED
|
@@ -1,69 +1,43 @@
|
|
| 1 |
-
|
| 2 |
-
from fastapi
|
|
|
|
|
|
|
| 3 |
from agents.visual_analyst import VisualAnalyst
|
| 4 |
-
from
|
| 5 |
-
|
| 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 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
return
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
async def generate_catalog(file: UploadFile = File(...)):
|
| 36 |
try:
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
if
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 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":
|
| 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)
|