Spaces:
Sleeping
Sleeping
Durand D'souza
commited on
Added share feature
Browse files
main.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
from typing import Annotated, Dict, List, Tuple
|
| 2 |
from urllib.parse import urlencode
|
| 3 |
-
from fastapi import FastAPI, Query
|
| 4 |
from fastapi.middleware.cors import CORSMiddleware
|
| 5 |
from fastapi.responses import RedirectResponse
|
| 6 |
import pandas as pd
|
|
@@ -24,9 +24,16 @@ app.add_middleware(
|
|
| 24 |
app = gr.mount_gradio_app(app, interface, path="/ui")
|
| 25 |
|
| 26 |
@app.get("/")
|
| 27 |
-
def read_main():
|
| 28 |
-
#
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
@app.get("/solarpv/")
|
| 32 |
def get_lcoe(pv_assumptions: Annotated[SolarPVAssumptions, Query()]):
|
|
|
|
| 1 |
from typing import Annotated, Dict, List, Tuple
|
| 2 |
from urllib.parse import urlencode
|
| 3 |
+
from fastapi import FastAPI, Query, Request
|
| 4 |
from fastapi.middleware.cors import CORSMiddleware
|
| 5 |
from fastapi.responses import RedirectResponse
|
| 6 |
import pandas as pd
|
|
|
|
| 24 |
app = gr.mount_gradio_app(app, interface, path="/ui")
|
| 25 |
|
| 26 |
@app.get("/")
|
| 27 |
+
async def read_main(request: Request):
|
| 28 |
+
# Get query parameters as dictionary
|
| 29 |
+
query_params = dict(request.query_params)
|
| 30 |
+
|
| 31 |
+
# Create new URL with parameters
|
| 32 |
+
redirect_url = "/ui"
|
| 33 |
+
if query_params:
|
| 34 |
+
redirect_url += "?" + urlencode(query_params)
|
| 35 |
+
|
| 36 |
+
return RedirectResponse(redirect_url)
|
| 37 |
|
| 38 |
@app.get("/solarpv/")
|
| 39 |
def get_lcoe(pv_assumptions: Annotated[SolarPVAssumptions, Query()]):
|
schema.py
CHANGED
|
@@ -13,9 +13,9 @@ class SolarPVAssumptions(BaseModel):
|
|
| 13 |
description="Capacity factor as a decimal, e.g., 0.2 for 20%",
|
| 14 |
),
|
| 15 |
] = 0.10
|
| 16 |
-
|
| 17 |
-
float, Field(ge=
|
| 18 |
-
] =
|
| 19 |
o_m_cost_pct_of_capital_cost: Annotated[
|
| 20 |
float,
|
| 21 |
Field(
|
|
@@ -78,7 +78,7 @@ class SolarPVAssumptions(BaseModel):
|
|
| 78 |
title="Debt Service Coverage Ratio",
|
| 79 |
description="Debt service coverage ratio",
|
| 80 |
),
|
| 81 |
-
]
|
| 82 |
|
| 83 |
@model_validator(mode="after")
|
| 84 |
def check_sum_of_parts(self):
|
|
@@ -91,7 +91,7 @@ class SolarPVAssumptions(BaseModel):
|
|
| 91 |
@property
|
| 92 |
def capital_cost(self) -> Annotated[float,
|
| 93 |
Field(title="Capital Cost ($)", description="Total capital cost")]:
|
| 94 |
-
return self.capacity_mw * self.
|
| 95 |
|
| 96 |
@computed_field
|
| 97 |
@property
|
|
@@ -105,7 +105,7 @@ class SolarPVAssumptions(BaseModel):
|
|
| 105 |
@computed_field
|
| 106 |
@property
|
| 107 |
def wacc(self) -> Annotated[Optional[float], Field(title="WACC (%)", description="Weighted average cost of capital")]:
|
| 108 |
-
if self.debt_pct_of_capital_cost is not None:
|
| 109 |
return self.debt_pct_of_capital_cost * self.cost_of_debt + self.equity_pct_of_capital_cost * self.cost_of_equity
|
| 110 |
|
| 111 |
@computed_field
|
|
@@ -116,6 +116,13 @@ class SolarPVAssumptions(BaseModel):
|
|
| 116 |
if self.debt_pct_of_capital_cost is not None:
|
| 117 |
return 1 - self.debt_pct_of_capital_cost
|
| 118 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
# @model_validator(mode='after')
|
| 120 |
# def check_dcsr_or_debt_pct(self):
|
| 121 |
# """
|
|
|
|
| 13 |
description="Capacity factor as a decimal, e.g., 0.2 for 20%",
|
| 14 |
),
|
| 15 |
] = 0.10
|
| 16 |
+
capital_expenditure_per_kw: Annotated[
|
| 17 |
+
float, Field(ge=1e2, le=1e4, title="Capital expenditure ($/kW)")
|
| 18 |
+
] = 670
|
| 19 |
o_m_cost_pct_of_capital_cost: Annotated[
|
| 20 |
float,
|
| 21 |
Field(
|
|
|
|
| 78 |
title="Debt Service Coverage Ratio",
|
| 79 |
description="Debt service coverage ratio",
|
| 80 |
),
|
| 81 |
+
] = 1.3
|
| 82 |
|
| 83 |
@model_validator(mode="after")
|
| 84 |
def check_sum_of_parts(self):
|
|
|
|
| 91 |
@property
|
| 92 |
def capital_cost(self) -> Annotated[float,
|
| 93 |
Field(title="Capital Cost ($)", description="Total capital cost")]:
|
| 94 |
+
return self.capacity_mw * self.capital_expenditure_per_kw * 1000
|
| 95 |
|
| 96 |
@computed_field
|
| 97 |
@property
|
|
|
|
| 105 |
@computed_field
|
| 106 |
@property
|
| 107 |
def wacc(self) -> Annotated[Optional[float], Field(title="WACC (%)", description="Weighted average cost of capital")]:
|
| 108 |
+
if self.debt_pct_of_capital_cost is not None and self.equity_pct_of_capital_cost is not None:
|
| 109 |
return self.debt_pct_of_capital_cost * self.cost_of_debt + self.equity_pct_of_capital_cost * self.cost_of_equity
|
| 110 |
|
| 111 |
@computed_field
|
|
|
|
| 116 |
if self.debt_pct_of_capital_cost is not None:
|
| 117 |
return 1 - self.debt_pct_of_capital_cost
|
| 118 |
|
| 119 |
+
@model_validator(mode="before")
|
| 120 |
+
@classmethod
|
| 121 |
+
def empty_str_to_none(cls, values):
|
| 122 |
+
if isinstance(values, dict):
|
| 123 |
+
return {k: (None if v == '' or v == "None" else v) for k, v in values.items()}
|
| 124 |
+
return values
|
| 125 |
+
|
| 126 |
# @model_validator(mode='after')
|
| 127 |
# def check_dcsr_or_debt_pct(self):
|
| 128 |
# """
|
share.svg
ADDED
|
|
ui.py
CHANGED
|
@@ -26,7 +26,7 @@ def process_inputs(
|
|
| 26 |
assumptions = SolarPVAssumptions(
|
| 27 |
capacity_mw=capacity_mw,
|
| 28 |
capacity_factor=capacity_factor,
|
| 29 |
-
|
| 30 |
o_m_cost_pct_of_capital_cost=o_m_cost_pct_of_capital_cost,
|
| 31 |
debt_pct_of_capital_cost=debt_pct_of_capital_cost if financing_mode == "Manual Debt/Equity Split" else None,
|
| 32 |
cost_of_debt=cost_of_debt,
|
|
@@ -65,28 +65,66 @@ def process_inputs(
|
|
| 65 |
def update_equity_from_debt(debt_pct):
|
| 66 |
return 1 - debt_pct
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
with gr.Blocks() as interface:
|
| 69 |
with gr.Row():
|
| 70 |
gr.Markdown("# Solar PV Project Cashflow Model")
|
|
|
|
| 71 |
with gr.Row():
|
| 72 |
with gr.Column():
|
| 73 |
with gr.Row():
|
| 74 |
capacity_mw = gr.Slider(
|
| 75 |
-
value=30,
|
| 76 |
minimum=1,
|
| 77 |
maximum=1000,
|
| 78 |
step=10,
|
| 79 |
label="Capacity (MW)",
|
| 80 |
)
|
| 81 |
capacity_factor = gr.Slider(
|
| 82 |
-
value=0.10,
|
| 83 |
label="Capacity factor (%)",
|
| 84 |
minimum=0,
|
| 85 |
maximum=0.6,
|
| 86 |
step=0.01,
|
| 87 |
)
|
| 88 |
project_lifetime_years = gr.Slider(
|
| 89 |
-
value=25,
|
| 90 |
label="Project Lifetime (years)",
|
| 91 |
minimum=5,
|
| 92 |
maximum=50,
|
|
@@ -94,14 +132,12 @@ with gr.Blocks() as interface:
|
|
| 94 |
)
|
| 95 |
with gr.Row():
|
| 96 |
capital_expenditure_per_kw = gr.Slider(
|
| 97 |
-
value=670,
|
| 98 |
label="Capital expenditure ($/kW)",
|
| 99 |
minimum=1e2,
|
| 100 |
maximum=1e3,
|
| 101 |
step=10,
|
| 102 |
)
|
| 103 |
o_m_cost_pct_of_capital_cost = gr.Slider(
|
| 104 |
-
value=0.02,
|
| 105 |
label="O&M as % of total cost (%)",
|
| 106 |
minimum=0,
|
| 107 |
maximum=0.5,
|
|
@@ -109,22 +145,20 @@ with gr.Blocks() as interface:
|
|
| 109 |
)
|
| 110 |
with gr.Row():
|
| 111 |
cost_of_debt = gr.Slider(
|
| 112 |
-
|
| 113 |
)
|
| 114 |
cost_of_equity = gr.Slider(
|
| 115 |
-
value=0.10,
|
| 116 |
label="Cost of Equity (%)",
|
| 117 |
minimum=0,
|
| 118 |
maximum=0.5,
|
| 119 |
step=0.01,
|
| 120 |
)
|
| 121 |
tax_rate = gr.Slider(
|
| 122 |
-
|
| 123 |
)
|
| 124 |
with gr.Row():
|
| 125 |
with gr.Row():
|
| 126 |
debt_pct_of_capital_cost = gr.Slider(
|
| 127 |
-
value=0.8,
|
| 128 |
label="Debt Percentage (%)",
|
| 129 |
minimum=0,
|
| 130 |
maximum=1,
|
|
@@ -133,14 +167,12 @@ with gr.Blocks() as interface:
|
|
| 133 |
interactive=False,
|
| 134 |
)
|
| 135 |
equity_pct_of_capital_cost = gr.Number(
|
| 136 |
-
value=0.2,
|
| 137 |
label="Equity Percentage (%)",
|
| 138 |
visible=True,
|
| 139 |
interactive=False,
|
| 140 |
precision=2
|
| 141 |
)
|
| 142 |
dcsr = gr.Slider(
|
| 143 |
-
value=1.3,
|
| 144 |
label="Debt Service Coverage Ratio",
|
| 145 |
minimum=1,
|
| 146 |
maximum=2,
|
|
@@ -158,6 +190,7 @@ with gr.Blocks() as interface:
|
|
| 158 |
with gr.Row():
|
| 159 |
model_output = gr.Matrix(headers=None, max_height=800)
|
| 160 |
|
|
|
|
| 161 |
# Set up event handlers for all inputs
|
| 162 |
input_components = [
|
| 163 |
capacity_mw,
|
|
@@ -177,14 +210,39 @@ with gr.Blocks() as interface:
|
|
| 177 |
fn=process_inputs,
|
| 178 |
inputs=input_components + [financing_mode],
|
| 179 |
outputs=[json_output, line_chart, debt_pct_of_capital_cost, equity_pct_of_capital_cost, model_output],
|
|
|
|
| 180 |
)
|
| 181 |
|
| 182 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
interface.load(
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
|
|
|
| 187 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
|
| 189 |
def toggle_financing_inputs(choice):
|
| 190 |
if choice == "Target DSCR":
|
|
|
|
| 26 |
assumptions = SolarPVAssumptions(
|
| 27 |
capacity_mw=capacity_mw,
|
| 28 |
capacity_factor=capacity_factor,
|
| 29 |
+
capital_expenditure_per_kw=capital_expenditure_per_kw,
|
| 30 |
o_m_cost_pct_of_capital_cost=o_m_cost_pct_of_capital_cost,
|
| 31 |
debt_pct_of_capital_cost=debt_pct_of_capital_cost if financing_mode == "Manual Debt/Equity Split" else None,
|
| 32 |
cost_of_debt=cost_of_debt,
|
|
|
|
| 65 |
def update_equity_from_debt(debt_pct):
|
| 66 |
return 1 - debt_pct
|
| 67 |
|
| 68 |
+
def get_params(request: gr.Request) -> Dict:
|
| 69 |
+
params = SolarPVAssumptions.model_validate(dict(request.query_params))
|
| 70 |
+
return {capacity_mw: params.capacity_mw,
|
| 71 |
+
capacity_factor: params.capacity_factor,
|
| 72 |
+
capital_expenditure_per_kw: params.capital_expenditure_per_kw,
|
| 73 |
+
o_m_cost_pct_of_capital_cost: params.o_m_cost_pct_of_capital_cost,
|
| 74 |
+
cost_of_debt: params.cost_of_debt,
|
| 75 |
+
cost_of_equity: params.cost_of_equity,
|
| 76 |
+
tax_rate: params.tax_rate,
|
| 77 |
+
project_lifetime_years: params.project_lifetime_years,
|
| 78 |
+
dcsr: params.dcsr,
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
def get_share_url(
|
| 82 |
+
capacity_mw,
|
| 83 |
+
capacity_factor,
|
| 84 |
+
capital_expenditure_per_kw,
|
| 85 |
+
o_m_cost_pct_of_capital_cost,
|
| 86 |
+
cost_of_debt,
|
| 87 |
+
cost_of_equity,
|
| 88 |
+
tax_rate,
|
| 89 |
+
project_lifetime_years,
|
| 90 |
+
dcsr,
|
| 91 |
+
financing_mode,
|
| 92 |
+
request: gr.Request
|
| 93 |
+
):
|
| 94 |
+
params = {
|
| 95 |
+
"capacity_mw": capacity_mw,
|
| 96 |
+
"capacity_factor": capacity_factor,
|
| 97 |
+
"capital_expenditure_per_kw": capital_expenditure_per_kw,
|
| 98 |
+
"o_m_cost_pct_of_capital_cost": o_m_cost_pct_of_capital_cost,
|
| 99 |
+
"cost_of_debt": cost_of_debt,
|
| 100 |
+
"cost_of_equity": cost_of_equity,
|
| 101 |
+
"tax_rate": tax_rate,
|
| 102 |
+
"project_lifetime_years": project_lifetime_years,
|
| 103 |
+
"dcsr": dcsr,
|
| 104 |
+
}
|
| 105 |
+
base_url = "/ui?" # Replace with actual base URL
|
| 106 |
+
return gr.Button(link=base_url + urlencode(params))
|
| 107 |
+
|
| 108 |
with gr.Blocks() as interface:
|
| 109 |
with gr.Row():
|
| 110 |
gr.Markdown("# Solar PV Project Cashflow Model")
|
| 111 |
+
share_url = gr.Button(icon="share.svg", value="", size="sm", variant="secondary")
|
| 112 |
with gr.Row():
|
| 113 |
with gr.Column():
|
| 114 |
with gr.Row():
|
| 115 |
capacity_mw = gr.Slider(
|
|
|
|
| 116 |
minimum=1,
|
| 117 |
maximum=1000,
|
| 118 |
step=10,
|
| 119 |
label="Capacity (MW)",
|
| 120 |
)
|
| 121 |
capacity_factor = gr.Slider(
|
|
|
|
| 122 |
label="Capacity factor (%)",
|
| 123 |
minimum=0,
|
| 124 |
maximum=0.6,
|
| 125 |
step=0.01,
|
| 126 |
)
|
| 127 |
project_lifetime_years = gr.Slider(
|
|
|
|
| 128 |
label="Project Lifetime (years)",
|
| 129 |
minimum=5,
|
| 130 |
maximum=50,
|
|
|
|
| 132 |
)
|
| 133 |
with gr.Row():
|
| 134 |
capital_expenditure_per_kw = gr.Slider(
|
|
|
|
| 135 |
label="Capital expenditure ($/kW)",
|
| 136 |
minimum=1e2,
|
| 137 |
maximum=1e3,
|
| 138 |
step=10,
|
| 139 |
)
|
| 140 |
o_m_cost_pct_of_capital_cost = gr.Slider(
|
|
|
|
| 141 |
label="O&M as % of total cost (%)",
|
| 142 |
minimum=0,
|
| 143 |
maximum=0.5,
|
|
|
|
| 145 |
)
|
| 146 |
with gr.Row():
|
| 147 |
cost_of_debt = gr.Slider(
|
| 148 |
+
label="Cost of Debt (%)", minimum=0, maximum=0.5, step=0.01
|
| 149 |
)
|
| 150 |
cost_of_equity = gr.Slider(
|
|
|
|
| 151 |
label="Cost of Equity (%)",
|
| 152 |
minimum=0,
|
| 153 |
maximum=0.5,
|
| 154 |
step=0.01,
|
| 155 |
)
|
| 156 |
tax_rate = gr.Slider(
|
| 157 |
+
label="Tax Rate (%)", minimum=0, maximum=0.5, step=0.01
|
| 158 |
)
|
| 159 |
with gr.Row():
|
| 160 |
with gr.Row():
|
| 161 |
debt_pct_of_capital_cost = gr.Slider(
|
|
|
|
| 162 |
label="Debt Percentage (%)",
|
| 163 |
minimum=0,
|
| 164 |
maximum=1,
|
|
|
|
| 167 |
interactive=False,
|
| 168 |
)
|
| 169 |
equity_pct_of_capital_cost = gr.Number(
|
|
|
|
| 170 |
label="Equity Percentage (%)",
|
| 171 |
visible=True,
|
| 172 |
interactive=False,
|
| 173 |
precision=2
|
| 174 |
)
|
| 175 |
dcsr = gr.Slider(
|
|
|
|
| 176 |
label="Debt Service Coverage Ratio",
|
| 177 |
minimum=1,
|
| 178 |
maximum=2,
|
|
|
|
| 190 |
with gr.Row():
|
| 191 |
model_output = gr.Matrix(headers=None, max_height=800)
|
| 192 |
|
| 193 |
+
|
| 194 |
# Set up event handlers for all inputs
|
| 195 |
input_components = [
|
| 196 |
capacity_mw,
|
|
|
|
| 210 |
fn=process_inputs,
|
| 211 |
inputs=input_components + [financing_mode],
|
| 212 |
outputs=[json_output, line_chart, debt_pct_of_capital_cost, equity_pct_of_capital_cost, model_output],
|
| 213 |
+
trigger_mode="always_last"
|
| 214 |
)
|
| 215 |
|
| 216 |
+
json_output.change(
|
| 217 |
+
fn=get_share_url,
|
| 218 |
+
inputs=[
|
| 219 |
+
capacity_mw,
|
| 220 |
+
capacity_factor,
|
| 221 |
+
capital_expenditure_per_kw,
|
| 222 |
+
o_m_cost_pct_of_capital_cost,
|
| 223 |
+
cost_of_debt,
|
| 224 |
+
cost_of_equity,
|
| 225 |
+
tax_rate,
|
| 226 |
+
project_lifetime_years,
|
| 227 |
+
dcsr,
|
| 228 |
+
financing_mode
|
| 229 |
+
],
|
| 230 |
+
outputs=share_url,
|
| 231 |
+
trigger_mode="always_last"
|
| 232 |
+
)
|
| 233 |
+
|
| 234 |
interface.load(
|
| 235 |
+
get_params,
|
| 236 |
+
None,
|
| 237 |
+
input_components,
|
| 238 |
+
trigger_mode="always_last"
|
| 239 |
)
|
| 240 |
+
# # Run the model on first load
|
| 241 |
+
# interface.load(
|
| 242 |
+
# process_inputs,
|
| 243 |
+
# inputs=input_components + [financing_mode],
|
| 244 |
+
# outputs=[json_output, line_chart, debt_pct_of_capital_cost, equity_pct_of_capital_cost, model_output],
|
| 245 |
+
# )
|
| 246 |
|
| 247 |
def toggle_financing_inputs(choice):
|
| 248 |
if choice == "Target DSCR":
|