Replica / app.py
Sachin5112's picture
Update app.py
c6b956e verified
from agent.dataset_sync import init_dataset, push_file
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
import uvicorn
import os
from agent.fs import (
init_workspace,
list_projects,
create_project,
read_file,
write_file,
tree
)
# ----------- Schemas -----------
class ReadFileReq(BaseModel):
path: str
start: int = 0
limit: int = 2000
class WriteFileReq(BaseModel):
path: str
content: str
class CreateProjectReq(BaseModel):
name: str
# ----------- App -----------
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
# ----------- API -----------
@app.get("/api/projects")
def api_list_projects():
return list_projects()
@app.post("/api/projects")
def api_create_project(req: CreateProjectReq):
return create_project(req.name)
@app.post("/api/read")
def api_read_file(req: ReadFileReq):
return {
"content": read_file(req.path, req.start, req.limit)
}
@app.post("/api/write")
def api_write_file(req: WriteFileReq):
write_file(req.path, req.content)
local_path = f"/workspace/agent/{req.path}"
repo_path = f"agent_workspace/{req.path}"
push_file(local_path, repo_path, "write file")
return {"status": "ok"}
@app.get("/api/tree")
def api_tree(path: str = "projects", depth: int = 2):
return tree(path, depth)
# ----------- UI -----------
app.mount("/static", StaticFiles(directory="static"), name="static")
@app.get("/")
def root():
return FileResponse("static/index.html")
@app.get("/ping")
def ping():
return {"status": "ok"}
# ----------- HF ENTRY -----------
if __name__ == "__main__":
init_dataset() # 🔥 pull permanent files
init_workspace()
uvicorn.run(
"app:app",
host="0.0.0.0",
port=int(os.environ.get("PORT", 7860))
)