Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
from datetime import datetime, timedelta
|
| 5 |
+
from prophet import Prophet
|
| 6 |
+
import matplotlib.pyplot as plt
|
| 7 |
+
import gradio as gr
|
| 8 |
+
|
| 9 |
+
# Simulate factory sensor data
|
| 10 |
+
def simulate_factory_data(days=30, freq='H'):
|
| 11 |
+
date_rng = pd.date_range(end=datetime.now(), periods=24 * days, freq=freq)
|
| 12 |
+
df = pd.DataFrame(date_rng, columns=['ds'])
|
| 13 |
+
df['temperature'] = np.random.normal(loc=70, scale=5, size=(len(date_rng)))
|
| 14 |
+
df['vibration'] = np.random.normal(loc=20, scale=3, size=(len(date_rng)))
|
| 15 |
+
df['power_usage'] = np.random.normal(loc=120, scale=10, size=(len(date_rng)))
|
| 16 |
+
return df
|
| 17 |
+
|
| 18 |
+
# Forecast temperature using Prophet
|
| 19 |
+
def forecast_temperature(days):
|
| 20 |
+
df = simulate_factory_data()
|
| 21 |
+
model = Prophet()
|
| 22 |
+
df_temp = df[['ds', 'temperature']].rename(columns={"temperature": "y"})
|
| 23 |
+
model.fit(df_temp)
|
| 24 |
+
|
| 25 |
+
future = model.make_future_dataframe(periods=days, freq='H')
|
| 26 |
+
forecast = model.predict(future)
|
| 27 |
+
|
| 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.set_title("Machine Temperature Forecast")
|
| 33 |
+
ax.set_xlabel("Time")
|
| 34 |
+
ax.set_ylabel("Temperature (°C)")
|
| 35 |
+
ax.legend()
|
| 36 |
+
plt.tight_layout()
|
| 37 |
+
return fig
|
| 38 |
+
|
| 39 |
+
# Gradio Interface
|
| 40 |
+
demo = gr.Interface(
|
| 41 |
+
fn=forecast_temperature,
|
| 42 |
+
inputs=gr.Slider(12, 72, value=48, label="Forecast Hours"),
|
| 43 |
+
outputs=gr.Plot(label="Forecasted Temperature Chart"),
|
| 44 |
+
title="Smart Factory AI Pipeline",
|
| 45 |
+
description="Simulated machine temperature forecasting using Prophet. Drag the slider to choose how far to forecast."
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
if __name__ == "__main__":
|
| 49 |
+
demo.launch()
|