Spaces:
Sleeping
Sleeping
Create main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
+
import uvicorn
|
| 5 |
+
|
| 6 |
+
app = FastAPI()
|
| 7 |
+
|
| 8 |
+
# Enable CORS for frontend requests
|
| 9 |
+
app.add_middleware(
|
| 10 |
+
CORSMiddleware,
|
| 11 |
+
allow_origins=["*"],
|
| 12 |
+
allow_credentials=True,
|
| 13 |
+
allow_methods=["*"],
|
| 14 |
+
allow_headers=["*"],
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
# Request model
|
| 18 |
+
class SIPRequest(BaseModel):
|
| 19 |
+
P: float = 5000 # Monthly investment
|
| 20 |
+
i: float = 12 # Annual interest rate
|
| 21 |
+
n: int = 10 # Number of years
|
| 22 |
+
|
| 23 |
+
# SIP Calculation Function
|
| 24 |
+
def calculate_sip(P: float, i: float, n: int):
|
| 25 |
+
monthly_rate = i / 12 / 100 # Convert annual rate to monthly decimal
|
| 26 |
+
n_payments = n * 12 # Convert years to months
|
| 27 |
+
M = P * (((1 + monthly_rate) ** n_payments - 1) / monthly_rate) * (1 + monthly_rate)
|
| 28 |
+
total_invested = P * n_payments
|
| 29 |
+
estimated_returns = M - total_invested
|
| 30 |
+
return {"final_amount": M, "total_invested": total_invested, "estimated_returns": estimated_returns}
|
| 31 |
+
|
| 32 |
+
@app.post("/api/sip")
|
| 33 |
+
async def sip_api(data: SIPRequest):
|
| 34 |
+
return calculate_sip(data.P, data.i, data.n)
|
| 35 |
+
|
| 36 |
+
if __name__ == "__main__":
|
| 37 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|