SPI-EndPoint / main.py
Alexvatti's picture
Create main.py
3baee5b verified
from fastapi import FastAPI
from pydantic import BaseModel
from fastapi.middleware.cors import CORSMiddleware
import uvicorn
app = FastAPI()
# Enable CORS for frontend requests
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Request model
class SIPRequest(BaseModel):
P: float = 5000 # Monthly investment
i: float = 12 # Annual interest rate
n: int = 10 # Number of years
# SIP Calculation Function
def calculate_sip(P: float, i: float, n: int):
monthly_rate = i / 12 / 100 # Convert annual rate to monthly decimal
n_payments = n * 12 # Convert years to months
M = P * (((1 + monthly_rate) ** n_payments - 1) / monthly_rate) * (1 + monthly_rate)
total_invested = P * n_payments
estimated_returns = M - total_invested
return {"final_amount": M, "total_invested": total_invested, "estimated_returns": estimated_returns}
@app.post("/api/sip")
async def sip_api(data: SIPRequest):
return calculate_sip(data.P, data.i, data.n)
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=7860)