Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -15,13 +15,13 @@ html_content = f"""
|
|
| 15 |
</style>
|
| 16 |
<div style='display: flex; flex-direction: column; align-items: flex-start;'>
|
| 17 |
<div style='display: flex; align-items: center;'>
|
| 18 |
-
<div style='width: 20px; height: 4px; background-color: green; margin-right:
|
| 19 |
-
<div style='width: 20px; height: 4px; background-color: red; margin-right:
|
| 20 |
<div style='width: 20px; height: 4px; background-color: yellow; margin-right: 20px;'></div>
|
| 21 |
-
<span style='font-size:
|
| 22 |
</div>
|
| 23 |
<div style='text-align: left; width: 100%;'>
|
| 24 |
-
<span style='font-size:
|
| 25 |
Meta Prophet + Microsoft TAPEX</span>
|
| 26 |
</div>
|
| 27 |
</div>
|
|
@@ -65,8 +65,39 @@ def load_data(uploaded_file):
|
|
| 65 |
return df
|
| 66 |
|
| 67 |
def preprocess_data(df):
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
def apply_prophet(df_clean):
|
| 72 |
if df_clean.empty:
|
|
@@ -77,8 +108,41 @@ def apply_prophet(df_clean):
|
|
| 77 |
all_anomalies = pd.DataFrame()
|
| 78 |
# Processar cada linha no DataFrame
|
| 79 |
for index, row in df_clean.iterrows():
|
| 80 |
-
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
# Renomear colunas e aplicar filtros
|
| 84 |
return all_anomalies
|
|
|
|
| 15 |
</style>
|
| 16 |
<div style='display: flex; flex-direction: column; align-items: flex-start;'>
|
| 17 |
<div style='display: flex; align-items: center;'>
|
| 18 |
+
<div style='width: 20px; height: 4px; background-color: green; margin-right: 0px;'></div>
|
| 19 |
+
<div style='width: 20px; height: 4px; background-color: red; margin-right: 0px;'></div>
|
| 20 |
<div style='width: 20px; height: 4px; background-color: yellow; margin-right: 20px;'></div>
|
| 21 |
+
<span style='font-size: 40px; font-weight: normal; font-family: "Kanit", sans-serif;'>NOSTRADAMUS</span>
|
| 22 |
</div>
|
| 23 |
<div style='text-align: left; width: 100%;'>
|
| 24 |
+
<span style='font-size: 24px; font-weight: normal; color: #333; font-family: "Kanit", sans-serif'>
|
| 25 |
Meta Prophet + Microsoft TAPEX</span>
|
| 26 |
</div>
|
| 27 |
</div>
|
|
|
|
| 65 |
return df
|
| 66 |
|
| 67 |
def preprocess_data(df):
|
| 68 |
+
new_df = df.iloc[2:,9:-1].fillna(0)
|
| 69 |
+
new_df.columns = df.iloc[1,9:-1]
|
| 70 |
+
new_df.columns = new_df.columns.str.replace(r" \(\d+\)", "", regex=True)
|
| 71 |
+
month_dict = {
|
| 72 |
+
'Jan': '01', 'Fev': '02', 'Mar': '03', 'Abr': '04',
|
| 73 |
+
'Mai': '05', 'Jun': '06', 'Jul': '07', 'Ago': '08',
|
| 74 |
+
'Set': '09', 'Out': '10', 'Nov': '11', 'Dez': '12'
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
def convert_column_name(column_name):
|
| 78 |
+
# Check if the column name is 'R贸tulos de Linha'
|
| 79 |
+
if column_name == 'R贸tulos de Linha':
|
| 80 |
+
return column_name
|
| 81 |
+
|
| 82 |
+
# Otherwise, proceed to convert
|
| 83 |
+
parts = column_name.split('/')
|
| 84 |
+
month = parts[0].strip()
|
| 85 |
+
year = parts[1].strip()
|
| 86 |
+
|
| 87 |
+
# Clean year in case there are extra characters
|
| 88 |
+
year = ''.join(filter(str.isdigit, year))
|
| 89 |
+
|
| 90 |
+
# Get month number from the dictionary
|
| 91 |
+
month_number = month_dict.get(month, '00') # Default '00' if month is not found
|
| 92 |
+
|
| 93 |
+
# Return formatted date string
|
| 94 |
+
return f"{month_number}/{year}"
|
| 95 |
+
|
| 96 |
+
new_df.columns = [convert_column_name(col) for col in new_df.columns]
|
| 97 |
+
new_df.columns = pd.to_datetime(new_df.columns, errors='coerce')
|
| 98 |
+
new_df.rename(columns={new_df.columns[0]: 'Rotulo'}, inplace=True)
|
| 99 |
+
df_clean = new_df.copy()
|
| 100 |
+
return df_clean
|
| 101 |
|
| 102 |
def apply_prophet(df_clean):
|
| 103 |
if df_clean.empty:
|
|
|
|
| 108 |
all_anomalies = pd.DataFrame()
|
| 109 |
# Processar cada linha no DataFrame
|
| 110 |
for index, row in df_clean.iterrows():
|
| 111 |
+
data = pd.DataFrame({
|
| 112 |
+
'ds': [col for col in df_clean.columns if isinstance(col, pd.Timestamp)],
|
| 113 |
+
'y': row[[isinstance(col, pd.Timestamp) for col in df_clean.columns]].values
|
| 114 |
+
})
|
| 115 |
+
|
| 116 |
+
# Remove lines where 'y' is zero until the first non-zero value
|
| 117 |
+
data = data[data['y'] > 0].reset_index(drop=True)
|
| 118 |
+
if data.empty or len(data) < 2:
|
| 119 |
+
print(f"Skipping group {row['Rotulo']} because there are less than 2 non-zero observations.")
|
| 120 |
+
continue
|
| 121 |
+
|
| 122 |
+
# Try to create and train the model
|
| 123 |
+
try:
|
| 124 |
+
model = Prophet(interval_width=0.95) # Confidence interval
|
| 125 |
+
model.fit(data)
|
| 126 |
+
except ValueError as e:
|
| 127 |
+
print(f"Skipping group {row['Rotulo']} due to error: {e}")
|
| 128 |
+
continue
|
| 129 |
+
|
| 130 |
+
# Make future predictions
|
| 131 |
+
future = model.make_future_dataframe(periods=12, freq='M')
|
| 132 |
+
forecast = model.predict(future)
|
| 133 |
+
|
| 134 |
+
# Identify points outside the confidence interval
|
| 135 |
+
num_real = len(data)
|
| 136 |
+
num_forecast = len(forecast)
|
| 137 |
+
real_values = list(data['y']) + [None] * (num_forecast - num_real)
|
| 138 |
+
forecast['real'] = real_values
|
| 139 |
+
anomalies = forecast[(forecast['real'] < forecast['yhat_lower']) | (forecast['real'] > forecast['yhat_upper'])]
|
| 140 |
+
|
| 141 |
+
# Add group label to the anomalies
|
| 142 |
+
anomalies['group'] = row['Rotulo']
|
| 143 |
+
|
| 144 |
+
# Append anomalies to the all_anomalies DataFrame
|
| 145 |
+
all_anomalies = pd.concat([all_anomalies, anomalies[['ds', 'real', 'group']]], ignore_index=True)
|
| 146 |
|
| 147 |
# Renomear colunas e aplicar filtros
|
| 148 |
return all_anomalies
|