from fastapi import FastAPI, Query from fastapi.middleware.cors import CORSMiddleware from fastapi.staticfiles import StaticFiles from fastapi.responses import FileResponse app = FastAPI() # CORS setup origins = [ "http://localhost:3000", "http://localhost:8000", "localhost:8000", "https://your-space-name.hf.space", ] app.add_middleware( CORSMiddleware, allow_origins=origins, allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) # Mount static files app.mount("/static", StaticFiles(directory="app/build/static", html=True), name="static") # Serve index.html at the root @app.get("/") def read_root(): return FileResponse("app/build/index.html") # Define API route @app.get("/api/sum_of_lengths") def sum_of_lengths(word1: str = Query(...), word2: str = Query(...)): return {"sum": len(word1) + len(word2)} if __name__ == "__main__": import uvicorn uvicorn.run(app, host="0.0.0.0", port=8000)