Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,79 +1,26 @@
|
|
| 1 |
-
import
|
| 2 |
-
import
|
| 3 |
-
|
| 4 |
-
from fastapi import FastAPI
|
| 5 |
-
from fastapi.middleware.cors import CORSMiddleware
|
| 6 |
-
from typing import Dict, Any
|
| 7 |
|
| 8 |
-
|
| 9 |
-
try:
|
| 10 |
-
from models import Action, Observation
|
| 11 |
-
from ecommerce_bot_environment import EcommerceBotEnvironment
|
| 12 |
-
except ImportError as e:
|
| 13 |
-
print(f"CRITICAL ERROR: Missing modules. Ensure models.py and ecommerce_bot_environment.py are in the root. {e}")
|
| 14 |
-
sys.exit(1)
|
| 15 |
-
|
| 16 |
-
# 2. Initialize FastAPI
|
| 17 |
-
app = FastAPI(
|
| 18 |
-
title="Meta x Scalar: OpenEnv Ecommerce Bot",
|
| 19 |
-
description="Agentic System for Real-World E-commerce Automation",
|
| 20 |
-
version="1.0.0"
|
| 21 |
-
)
|
| 22 |
-
|
| 23 |
-
# 3. Add CORS
|
| 24 |
-
app.add_middleware(
|
| 25 |
-
CORSMiddleware,
|
| 26 |
-
allow_origins=["*"],
|
| 27 |
-
allow_methods=["*"],
|
| 28 |
-
allow_headers=["*"],
|
| 29 |
-
)
|
| 30 |
-
|
| 31 |
-
# 4. Initialize the Environment
|
| 32 |
-
env = EcommerceBotEnvironment()
|
| 33 |
-
|
| 34 |
-
# --- ROUTES ---
|
| 35 |
|
|
|
|
| 36 |
@app.get("/")
|
| 37 |
-
async def
|
| 38 |
-
return {
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
@app.get("/state")
|
| 50 |
-
async def get_state():
|
| 51 |
-
return {
|
| 52 |
-
"status": "ready",
|
| 53 |
-
"tasks": ["create_order", "update_address", "issue_refund"],
|
| 54 |
-
"persistance": "sqlite3"
|
| 55 |
-
}
|
| 56 |
-
|
| 57 |
@app.post("/reset")
|
| 58 |
async def reset():
|
| 59 |
-
|
| 60 |
-
return observation
|
| 61 |
-
|
| 62 |
-
@app.post("/step")
|
| 63 |
-
async def step(action: Action):
|
| 64 |
-
observation, reward, done, info = env.step(action)
|
| 65 |
-
return {
|
| 66 |
-
"observation": observation,
|
| 67 |
-
"reward": float(reward),
|
| 68 |
-
"done": bool(done),
|
| 69 |
-
"info": info
|
| 70 |
-
}
|
| 71 |
-
|
| 72 |
-
@app.on_event("shutdown")
|
| 73 |
-
def shutdown_event():
|
| 74 |
-
print("Shutting down E-commerce Bot Server...")
|
| 75 |
|
| 76 |
if __name__ == "__main__":
|
| 77 |
-
import uvicorn
|
| 78 |
-
# Hardcode to 7860 to guarantee the HF Load Balancer connects
|
| 79 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request
|
| 2 |
+
from inference import run_inference # Importing from your root inference.py
|
| 3 |
+
import uvicorn
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
app = FastAPI()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
# Health Check Endpoint
|
| 8 |
@app.get("/")
|
| 9 |
+
async def health_check():
|
| 10 |
+
return {"status": "healthy", "message": "Ecommerce Bot API is live"}
|
| 11 |
+
|
| 12 |
+
# Prediction Endpoint
|
| 13 |
+
@app.post("/predict")
|
| 14 |
+
async def predict(request: Request):
|
| 15 |
+
data = await request.json()
|
| 16 |
+
# Assuming your inference.py has a function called run_inference
|
| 17 |
+
result = run_inference(data)
|
| 18 |
+
return {"prediction": result}
|
| 19 |
+
|
| 20 |
+
# The Reset Endpoint (which your logs show is already working)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
@app.post("/reset")
|
| 22 |
async def reset():
|
| 23 |
+
return {"status": "ok"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
if __name__ == "__main__":
|
|
|
|
|
|
|
| 26 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|