Rehman1603 commited on
Commit
34c34e9
·
verified ·
1 Parent(s): d811584

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +121 -0
app.py ADDED
@@ -0,0 +1,121 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+ from prophet import Prophet
4
+ import plotly.graph_objs as go
5
+ import numpy as np
6
+
7
+ # Function to replace null values with average
8
+ def replace_null_with_avg_values(df, column_name):
9
+ df[column_name] = pd.to_numeric(df[column_name], errors='coerce')
10
+ avg_value = round(df[column_name].mean(), 1)
11
+ df[column_name].fillna(avg_value, inplace=True)
12
+
13
+ # Load and process the data
14
+ broiler_data = pd.read_csv('Broiler market price.csv')
15
+ replace_null_with_avg_values(broiler_data, 'DOC')
16
+ replace_null_with_avg_values(broiler_data, 'Farm Rate')
17
+ replace_null_with_avg_values(broiler_data, 'Open')
18
+ replace_null_with_avg_values(broiler_data, 'Close')
19
+
20
+ broiler_data['Date'] = pd.to_datetime(broiler_data['Date'])
21
+
22
+ # Prepare dataframes for Prophet models
23
+ farm_rate_df = broiler_data[['Date', 'Farm Rate', 'DOC']].rename(columns={'Date': 'ds', 'Farm Rate': 'y', 'DOC': 'DOC'})
24
+ open_rate_df = broiler_data[['Date', 'Open', 'DOC']].rename(columns={'Date': 'ds', 'Open': 'y', 'DOC': 'DOC'})
25
+ close_rate_df = broiler_data[['Date', 'Close', 'DOC']].rename(columns={'Date': 'ds', 'Close': 'y', 'DOC': 'DOC'})
26
+
27
+ # Initialize Prophet models
28
+ farm_rate_model = Prophet(growth='linear', yearly_seasonality=True, daily_seasonality=True)
29
+ open_rate_model = Prophet(growth='linear', yearly_seasonality=True, daily_seasonality=True)
30
+ close_rate_model = Prophet(growth='linear', yearly_seasonality=True, daily_seasonality=True)
31
+
32
+ # Add DOC regressor and holidays
33
+ farm_rate_model.add_regressor('DOC')
34
+ open_rate_model.add_regressor('DOC')
35
+ close_rate_model.add_regressor('DOC')
36
+ farm_rate_model.add_country_holidays(country_name='PAK')
37
+ open_rate_model.add_country_holidays(country_name='PAK')
38
+ close_rate_model.add_country_holidays(country_name='PAK')
39
+
40
+ # Fit the models
41
+ farm_rate_model.fit(farm_rate_df)
42
+ open_rate_model.fit(open_rate_df)
43
+ close_rate_model.fit(close_rate_df)
44
+
45
+ # Function to generate predictions based on DOC range and selected days
46
+ def predict_values(start_doc, end_doc, days_option):
47
+ # Generate future dataframes
48
+ farm_future_rate = farm_rate_model.make_future_dataframe(periods=days_option, freq='D')
49
+ open_future_rate = open_rate_model.make_future_dataframe(periods=days_option, freq='D')
50
+ close_future_rate = close_rate_model.make_future_dataframe(periods=days_option, freq='D')
51
+
52
+ # Generate DOC values within the specified range
53
+ doc_values = np.linspace(start_doc, end_doc, num=len(farm_future_rate)) # Adjusted line
54
+
55
+ # Assign the generated DOC values to the future dataframes
56
+ farm_future_rate['DOC'] = doc_values
57
+ open_future_rate['DOC'] = doc_values
58
+ close_future_rate['DOC'] = doc_values
59
+
60
+ # Make predictions
61
+ farm_forecast_rate = farm_rate_model.predict(farm_future_rate)
62
+ open_forecast_rate = open_rate_model.predict(open_future_rate)
63
+ close_forecast_rate = close_rate_model.predict(close_future_rate)
64
+
65
+ # Filter to get only the future predictions (last `days_option` days)
66
+ farm_output = farm_forecast_rate[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail(days_option).copy()
67
+ farm_output['DOC'] = farm_future_rate['DOC'].tail(days_option)
68
+
69
+ open_output = open_forecast_rate[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail(days_option).copy()
70
+ open_output['DOC'] = open_future_rate['DOC'].tail(days_option)
71
+
72
+ close_output = close_forecast_rate[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail(days_option).copy()
73
+ close_output['DOC'] = close_future_rate['DOC'].tail(days_option)
74
+
75
+ # Create Plotly graphs
76
+ fig_farm = go.Figure()
77
+ fig_farm.add_trace(go.Scatter(x=farm_forecast_rate['ds'], y=farm_forecast_rate['yhat'], mode='lines+markers', name='Farm Rate Prediction'))
78
+ fig_farm.update_layout(title='Farm Rate Predictions over Time', xaxis_title='Date', yaxis_title='Predicted Farm Rate')
79
+
80
+ fig_open = go.Figure()
81
+ fig_open.add_trace(go.Scatter(x=open_forecast_rate['ds'], y=open_forecast_rate['yhat'], mode='lines+markers', name='Open Rate Prediction'))
82
+ fig_open.update_layout(title='Open Rate Predictions over Time', xaxis_title='Date', yaxis_title='Predicted Open Rate')
83
+
84
+ fig_close = go.Figure()
85
+ fig_close.add_trace(go.Scatter(x=close_forecast_rate['ds'], y=close_forecast_rate['yhat'], mode='lines+markers', name='Close Rate Prediction'))
86
+ fig_close.update_layout(title='Close Rate Predictions over Time', xaxis_title='Date', yaxis_title='Predicted Close Rate')
87
+
88
+ return farm_output, open_output, close_output, fig_farm, fig_open, fig_close
89
+
90
+ # Define Gradio interface
91
+ def interface(start_doc, end_doc, days_option):
92
+ # Check if a valid selection is made
93
+ if days_option is None:
94
+ return "Please select a valid option for days.", None, None, None, None, None
95
+
96
+ days_map = {'7 days': 7, '10 days': 10, '15 days': 15}
97
+ days_selected = days_map.get(days_option, 7) # Default to 7 days if no valid option is provided
98
+ results_farm, results_open, results_close, plot_farm, plot_open, plot_close = predict_values(start_doc, end_doc, days_selected)
99
+ return results_farm, results_open, results_close, plot_farm, plot_open, plot_close
100
+
101
+ # Create Gradio inputs and outputs
102
+ start_doc_input = gr.components.Number(label="Start DOC Value")
103
+ end_doc_input = gr.components.Number(label="End DOC Value")
104
+ days_dropdown = gr.components.Dropdown(choices=['7 days', '10 days', '15 days'], label="Select Number of Days",value='7 days')
105
+
106
+ # Define output components
107
+ output_table_farm = gr.components.Dataframe(label="Predicted Farm Rate Values")
108
+ output_table_open = gr.components.Dataframe(label="Predicted Open Rate Values")
109
+ output_table_close = gr.components.Dataframe(label="Predicted Close Rate Values")
110
+ output_plot_farm = gr.components.Plot(label="Farm Rate Predictions")
111
+ output_plot_open = gr.components.Plot(label="Open Rate Predictions")
112
+ output_plot_close = gr.components.Plot(label="Close Rate Predictions")
113
+
114
+ # Set up Gradio interface
115
+ gr.Interface(
116
+ fn=interface,
117
+ inputs=[start_doc_input, end_doc_input, days_dropdown],
118
+ outputs=[output_table_farm, output_table_open, output_table_close, output_plot_farm, output_plot_open, output_plot_close],
119
+ title="Farm Rate Prediction Tool",
120
+ description="Enter DOC range and select the number of days to generate predictions and plot."
121
+ ).launch(debug=True)