File size: 6,479 Bytes
34c34e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5c5e691
f2eeff5
 
5c5e691
34c34e9
 
497fa18
 
 
5c5e691
34c34e9
5c5e691
34c34e9
 
 
5c5e691
34c34e9
 
 
 
 
5c5e691
34c34e9
5c5e691
 
34c34e9
 
 
 
5c5e691
34c34e9
 
 
 
 
5c5e691
34c34e9
 
 
 
 
5c5e691
 
 
 
 
 
 
34c34e9
 
 
a83cfe4
34c34e9
 
 
a83cfe4
34c34e9
 
 
a83cfe4
34c34e9
 
5c5e691
 
 
 
 
34c34e9
 
5c5e691
34c34e9
 
5c5e691
34c34e9
7c719d0
34c34e9
5c5e691
 
34c34e9
 
7c719d0
34c34e9
 
 
 
 
5c5e691
34c34e9
 
 
5c5e691
34c34e9
 
 
 
5c5e691
 
34c34e9
5c5e691
34c34e9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import gradio as gr
import pandas as pd
from prophet import Prophet
import plotly.graph_objs as go
import numpy as np

# Function to replace null values with average
def replace_null_with_avg_values(df, column_name):
    df[column_name] = pd.to_numeric(df[column_name], errors='coerce')
    avg_value = round(df[column_name].mean(), 1)
    df[column_name].fillna(avg_value, inplace=True)

# Load and process the data
broiler_data = pd.read_csv('Broiler market price.csv')
replace_null_with_avg_values(broiler_data, 'DOC')
replace_null_with_avg_values(broiler_data, 'Farm Rate')
replace_null_with_avg_values(broiler_data, 'Open')
replace_null_with_avg_values(broiler_data, 'Close')

broiler_data['Date'] = pd.to_datetime(broiler_data['Date'])

# Prepare dataframes for Prophet models
farm_rate_df = broiler_data[['Date', 'Farm Rate']].rename(columns={'Date': 'ds', 'Farm Rate': 'y'})
open_rate_df = broiler_data[['Date', 'Open']].rename(columns={'Date': 'ds', 'Open': 'y'})
close_rate_df = broiler_data[['Date', 'Close']].rename(columns={'Date': 'ds', 'Close': 'y'})
doc_rate_df = broiler_data[['Date', 'DOC']].rename(columns={'Date': 'ds', 'DOC': 'y'})

# Initialize Prophet models
farm_rate_model = Prophet(daily_seasonality=True)
open_rate_model = Prophet(daily_seasonality=True)
close_rate_model = Prophet(daily_seasonality=True)
doc_rate_model = Prophet(daily_seasonality=True)

# Add holidays
farm_rate_model.add_country_holidays(country_name='PAK')
open_rate_model.add_country_holidays(country_name='PAK')
close_rate_model.add_country_holidays(country_name='PAK')
doc_rate_model.add_country_holidays(country_name='PAK')

# Fit the models
farm_rate_model.fit(farm_rate_df)
open_rate_model.fit(open_rate_df)
close_rate_model.fit(close_rate_df)
doc_rate_model.fit(doc_rate_df)

# Function to generate predictions based on selected days
def predict_values(days_option):
    # Generate future dataframes
    farm_future_rate = farm_rate_model.make_future_dataframe(periods=days_option, freq='D')
    open_future_rate = open_rate_model.make_future_dataframe(periods=days_option, freq='D')
    close_future_rate = close_rate_model.make_future_dataframe(periods=days_option, freq='D')
    doc_future_rate = doc_rate_model.make_future_dataframe(periods=days_option, freq='D')

    # Make predictions
    farm_forecast_rate = farm_rate_model.predict(farm_future_rate)
    open_forecast_rate = open_rate_model.predict(open_future_rate)
    close_forecast_rate = close_rate_model.predict(close_future_rate)
    doc_forecast_rate = doc_rate_model.predict(doc_future_rate)

    # Filter to get only the future predictions (last `days_option` days)
    farm_output = farm_forecast_rate[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail(days_option).copy()
    open_output = open_forecast_rate[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail(days_option).copy()
    close_output = close_forecast_rate[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail(days_option).copy()
    doc_output = doc_forecast_rate[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail(days_option).copy()

    # Round the predictions to 1 decimal place
    for output in [farm_output, open_output, close_output, doc_output]:
        output['yhat'] = np.round(output['yhat'], 1)
        output['yhat_lower'] = np.round(output['yhat_lower'], 1)
        output['yhat_upper'] = np.round(output['yhat_upper'], 1)

    # Create Plotly graphs
    fig_farm = go.Figure()
    fig_farm.add_trace(go.Scatter(x=farm_forecast_rate['ds'], y=farm_forecast_rate['yhat'], mode='lines+markers', name='Farm Rate Prediction'))
    fig_farm.update_layout(title='Farm Rate Predictions over Time', xaxis_title='Date', yaxis_title='Predicted Farm Rate')

    fig_open = go.Figure()
    fig_open.add_trace(go.Scatter(x=open_forecast_rate['ds'], y=open_forecast_rate['yhat'], mode='lines+markers', name='Open Rate Prediction'))
    fig_open.update_layout(title='Open Rate Predictions over Time', xaxis_title='Date', yaxis_title='Predicted Open Rate')

    fig_close = go.Figure()
    fig_close.add_trace(go.Scatter(x=close_forecast_rate['ds'], y=close_forecast_rate['yhat'], mode='lines+markers', name='Close Rate Prediction'))
    fig_close.update_layout(title='Close Rate Predictions over Time', xaxis_title='Date', yaxis_title='Predicted Close Rate')

    fig_doc = go.Figure()
    fig_doc.add_trace(go.Scatter(x=doc_forecast_rate['ds'], y=doc_forecast_rate['yhat'], mode='lines+markers', name='DOC Prediction'))
    fig_doc.update_layout(title='DOC Predictions over Time', xaxis_title='Date', yaxis_title='Predicted DOC')

    return farm_output, open_output, close_output, doc_output, fig_farm, fig_open, fig_close, fig_doc

# Define Gradio interface
def interface(days_option):
    # Check if a valid selection is made
    if days_option is None:
        return "Please select a valid option for days.", None, None, None, None, None, None
    
    days_map = {'7 days': 7, '10 days': 10, '15 days': 15, '40 days': 40}
    days_selected = days_map.get(days_option, 7)  # Default to 7 days if no valid option is provided
    results_farm, results_open, results_close, results_doc, plot_farm, plot_open, plot_close, plot_doc = predict_values(days_selected)
    return results_farm, results_open, results_close, results_doc, plot_farm, plot_open, plot_close, plot_doc

# Create Gradio inputs and outputs
days_dropdown = gr.components.Dropdown(choices=['7 days', '10 days', '15 days', '40 days'], label="Select Number of Days", value='7 days')

# Define output components
output_table_farm = gr.components.Dataframe(label="Predicted Farm Rate Values")
output_table_open = gr.components.Dataframe(label="Predicted Open Rate Values")
output_table_close = gr.components.Dataframe(label="Predicted Close Rate Values")
output_table_doc = gr.components.Dataframe(label="Predicted DOC Values")
output_plot_farm = gr.components.Plot(label="Farm Rate Predictions")
output_plot_open = gr.components.Plot(label="Open Rate Predictions")
output_plot_close = gr.components.Plot(label="Close Rate Predictions")
output_plot_doc = gr.components.Plot(label="DOC Predictions")

# Set up Gradio interface
gr.Interface(
    fn=interface,
    inputs=[days_dropdown],
    outputs=[output_table_farm, output_table_open, output_table_close, output_table_doc, output_plot_farm, output_plot_open, output_plot_close, output_plot_doc],
    title="Farm Rate Prediction Tool",
    description="Select the number of days to generate predictions and plot."
).launch(debug=True)