| import gradio as gr | |
| import numpy as np | |
| from tensorflow.keras.models import load_model | |
| from sklearn.model_selection import train_test_split | |
| from sklearn.preprocessing import MinMaxScaler | |
| import matplotlib.pyplot as plt | |
| from io import BytesIO | |
| from PIL import Image | |
| import pandas as pd | |
| model = load_model('models/2024-10-23-hurricane.keras') | |
| dataset_with_hurricanes = pd.read_csv('data/hurricane_sample.csv') | |
| features = ['Wind Speed', 'Damage', 'Pressure', 'Category', 'treturn', 'ireturn', 'areturn'] | |
| target = 'treturn' | |
| dataset_for_model = dataset_with_hurricanes[features].copy() | |
| dataset_for_model.fillna(0, inplace=True) | |
| scaler = MinMaxScaler() | |
| scaled_data = scaler.fit_transform(dataset_for_model) | |
| X = scaled_data[:, :-1] | |
| y = scaled_data[:, -1] | |
| X = X.reshape((X.shape[0], 1, X.shape[1])) | |
| X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) | |
| def predict_and_plot(): | |
| y_pred = model.predict(X_test) | |
| plt.figure(figsize=(10, 6)) | |
| plt.plot(np.arange(len(y_test)), y_test, label='Actual') | |
| plt.plot(np.arange(len(y_test)), y_pred, label='Predicted', linestyle='--') | |
| plt.title('Actual vs Predicted Values') | |
| plt.xlabel('Index') | |
| plt.ylabel('Total Return') | |
| plt.legend() | |
| plt.show() | |
| plt.xticks(rotation=90, fontsize=8) | |
| plt.gca().tick_params(axis='x', pad=15) | |
| plt.legend() | |
| plt.tight_layout() | |
| buf = BytesIO() | |
| plt.savefig(buf, format='png') | |
| buf.seek(0) | |
| img = Image.open(buf) | |
| img_array = np.array(img) | |
| return img_array | |
| with gr.Blocks() as demo: | |
| with gr.Row(): | |
| with gr.Column(): | |
| output_image = gr.Image(type="numpy", label="Actual vs Predicted Total Return") | |
| predict_button = gr.Button("Submit") | |
| with gr.Row(): | |
| with gr.Column(): | |
| gr.Markdown(""" | |
| **Total Return**: Total return is a measure of the performance of an asset or investment over a specific period, defined as the sum of **Income Return** and **Asset Return**. | |
| - **Income Return**: The net income generated by a property, calculated as rental income minus operating and capital expenditures. | |
| - **Asset Return**: The appreciation in the market value of a property from purchase to sale. | |
| **Hurricane Data**: This application uses hurricane-related data to analyze the impact of extreme weather events on total return. | |
| - **Key Features**: The dataset includes variables like Wind Speed, Damage Cost, Pressure, Hurricane Category, and historical return metrics. | |
| - **Hurricane Categories**: Categorized based on the Saffir-Simpson Hurricane Wind Scale, ranging from Category 1 (least severe) to Category 5 (most severe). | |
| - **Damage Cost**: Represents the economic impact caused by hurricanes, such as infrastructure damage and property loss. | |
| """) | |
| predict_button.click(fn=predict_and_plot, outputs=[output_image]) | |
| demo.launch() | |