Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import pandas as pd
|
| 2 |
import numpy as np
|
| 3 |
-
from datetime import datetime
|
| 4 |
from prophet import Prophet
|
| 5 |
import matplotlib.pyplot as plt
|
| 6 |
import gradio as gr
|
|
@@ -8,22 +8,28 @@ import gradio as gr
|
|
| 8 |
def simulate_factory_data(days=30, freq='H'):
|
| 9 |
date_rng = pd.date_range(end=datetime.now(), periods=24 * days, freq=freq)
|
| 10 |
df = pd.DataFrame(date_rng, columns=['ds'])
|
| 11 |
-
df['temperature'] = np.random.normal(loc=70, scale=
|
| 12 |
df['vibration'] = np.random.normal(loc=20, scale=3, size=(len(date_rng)))
|
| 13 |
-
df['power_usage'] = np.random.normal(loc=120, scale=
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
return df
|
| 15 |
|
| 16 |
-
def forecast_temperature(
|
|
|
|
| 17 |
df = simulate_factory_data()
|
| 18 |
model = Prophet()
|
| 19 |
df_temp = df[['ds', 'temperature']].rename(columns={"temperature": "y"})
|
| 20 |
model.fit(df_temp)
|
| 21 |
-
future = model.make_future_dataframe(periods=
|
| 22 |
forecast = model.predict(future)
|
| 23 |
fig, ax = plt.subplots(figsize=(10, 5))
|
| 24 |
ax.plot(df['ds'], df['temperature'], label='Actual')
|
| 25 |
ax.plot(forecast['ds'], forecast['yhat'], label='Forecast')
|
| 26 |
ax.fill_between(forecast['ds'], forecast['yhat_lower'], forecast['yhat_upper'], alpha=0.2, label='Confidence')
|
|
|
|
| 27 |
ax.set_title("Machine Temperature Forecast")
|
| 28 |
ax.set_xlabel("Time")
|
| 29 |
ax.set_ylabel("Temperature (°C)")
|
|
@@ -33,10 +39,10 @@ def forecast_temperature(days):
|
|
| 33 |
|
| 34 |
demo = gr.Interface(
|
| 35 |
fn=forecast_temperature,
|
| 36 |
-
inputs=gr.Slider(12, 72, value=48, label="Forecast Hours"),
|
| 37 |
outputs=gr.Plot(label="Forecasted Temperature Chart"),
|
| 38 |
-
title="Smart Factory AI Pipeline
|
| 39 |
-
description="
|
| 40 |
)
|
| 41 |
|
| 42 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import pandas as pd
|
| 2 |
import numpy as np
|
| 3 |
+
from datetime import datetime
|
| 4 |
from prophet import Prophet
|
| 5 |
import matplotlib.pyplot as plt
|
| 6 |
import gradio as gr
|
|
|
|
| 8 |
def simulate_factory_data(days=30, freq='H'):
|
| 9 |
date_rng = pd.date_range(end=datetime.now(), periods=24 * days, freq=freq)
|
| 10 |
df = pd.DataFrame(date_rng, columns=['ds'])
|
| 11 |
+
df['temperature'] = np.random.normal(loc=70, scale=4, size=(len(date_rng)))
|
| 12 |
df['vibration'] = np.random.normal(loc=20, scale=3, size=(len(date_rng)))
|
| 13 |
+
df['power_usage'] = np.random.normal(loc=120, scale=8, size=(len(date_rng)))
|
| 14 |
+
anomaly_indices = np.random.choice(len(df), size=10, replace=False)
|
| 15 |
+
df.loc[anomaly_indices, 'temperature'] += np.random.uniform(10, 20, size=10)
|
| 16 |
+
df.loc[anomaly_indices, 'vibration'] += np.random.uniform(5, 10, size=10)
|
| 17 |
+
df.loc[anomaly_indices, 'power_usage'] += np.random.uniform(30, 50, size=10)
|
| 18 |
return df
|
| 19 |
|
| 20 |
+
def forecast_temperature(hours):
|
| 21 |
+
periods = int(hours) # Prophet expects integer periods
|
| 22 |
df = simulate_factory_data()
|
| 23 |
model = Prophet()
|
| 24 |
df_temp = df[['ds', 'temperature']].rename(columns={"temperature": "y"})
|
| 25 |
model.fit(df_temp)
|
| 26 |
+
future = model.make_future_dataframe(periods=periods, freq='H')
|
| 27 |
forecast = model.predict(future)
|
| 28 |
fig, ax = plt.subplots(figsize=(10, 5))
|
| 29 |
ax.plot(df['ds'], df['temperature'], label='Actual')
|
| 30 |
ax.plot(forecast['ds'], forecast['yhat'], label='Forecast')
|
| 31 |
ax.fill_between(forecast['ds'], forecast['yhat_lower'], forecast['yhat_upper'], alpha=0.2, label='Confidence')
|
| 32 |
+
ax.axhspan(85, 100, color='red', alpha=0.1, label='Danger Zone')
|
| 33 |
ax.set_title("Machine Temperature Forecast")
|
| 34 |
ax.set_xlabel("Time")
|
| 35 |
ax.set_ylabel("Temperature (°C)")
|
|
|
|
| 39 |
|
| 40 |
demo = gr.Interface(
|
| 41 |
fn=forecast_temperature,
|
| 42 |
+
inputs=gr.Slider(12.0, 72.0, value=48.0, step=0.5, label="Forecast Hours"),
|
| 43 |
outputs=gr.Plot(label="Forecasted Temperature Chart"),
|
| 44 |
+
title="Smart Factory AI Pipeline",
|
| 45 |
+
description="Forecast machine temperature using Prophet. Red zone shows potential overheating risk."
|
| 46 |
)
|
| 47 |
|
| 48 |
if __name__ == "__main__":
|