import sys import os from pathlib import Path from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from typing import Dict, Any # 1. Simplified Imports: Everything is in the same folder now! try: from models import Action, Observation from ecommerce_bot_environment import EcommerceBotEnvironment except ImportError as e: print(f"CRITICAL ERROR: Missing modules. Ensure models.py and ecommerce_bot_environment.py are in the root. {e}") sys.exit(1) # 2. Initialize FastAPI app = FastAPI( title="Meta x Scalar: OpenEnv Ecommerce Bot", description="Agentic System for Real-World E-commerce Automation", version="1.0.0" ) # 3. Add CORS app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"], ) # 4. Initialize the Environment env = EcommerceBotEnvironment() # --- ROUTES --- @app.get("/") async def root(): return { "message": "E-commerce Bot API is running", "status": "online", "spec": "OpenEnv v1.0", "location": "Mumbai University Hackathon Entry" } @app.get("/health") async def health(): return {"status": "healthy", "database": "connected"} @app.get("/state") async def get_state(): return { "status": "ready", "tasks": ["create_order", "update_address", "issue_refund"], "persistance": "sqlite3" } @app.post("/reset") async def reset(): observation = env.reset() return observation @app.post("/step") async def step(action: Action): observation, reward, done, info = env.step(action) return { "observation": observation, "reward": float(reward), "done": bool(done), "info": info } @app.on_event("shutdown") def shutdown_event(): print("Shutting down E-commerce Bot Server...") if __name__ == "__main__": import uvicorn # Hardcode to 7860 to guarantee the HF Load Balancer connects uvicorn.run(app, host="0.0.0.0", port=7860)