Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,59 +1,39 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
-
import matplotlib.pyplot as plt
|
| 4 |
from prophet import Prophet
|
| 5 |
-
import
|
| 6 |
-
import io
|
| 7 |
-
|
| 8 |
-
def extract_csv_from_pdf(pdf_file):
|
| 9 |
-
# Load and extract text from the PDF
|
| 10 |
-
with fitz.open(stream=pdf_file.read(), filetype="pdf") as doc:
|
| 11 |
-
text = ""
|
| 12 |
-
for page in doc:
|
| 13 |
-
text += page.get_text()
|
| 14 |
-
|
| 15 |
-
# Try to parse CSV-like data
|
| 16 |
-
from io import StringIO
|
| 17 |
-
lines = [line for line in text.split("\n") if "," in line]
|
| 18 |
-
data = "\n".join(lines)
|
| 19 |
-
df = pd.read_csv(StringIO(data))
|
| 20 |
-
return df
|
| 21 |
|
| 22 |
-
def forecast_energy(
|
| 23 |
-
|
|
|
|
| 24 |
|
| 25 |
-
|
| 26 |
-
return "Error: PDF must contain columns 'date' and 'energy_load'."
|
| 27 |
-
|
| 28 |
-
df = df[['date', 'energy_load']]
|
| 29 |
df.columns = ['ds', 'y']
|
|
|
|
|
|
|
| 30 |
df['ds'] = pd.to_datetime(df['ds'])
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
model = Prophet()
|
| 35 |
model.fit(df)
|
| 36 |
-
|
|
|
|
| 37 |
future = model.make_future_dataframe(periods=14)
|
| 38 |
forecast = model.predict(future)
|
| 39 |
-
|
| 40 |
-
#
|
| 41 |
-
forecast_14 = forecast.tail(14)
|
| 42 |
-
peak_day = forecast_14.loc[forecast_14['yhat'].idxmax()]
|
| 43 |
-
peak_msg = f"🔺 Peak Forecasted Demand: {peak_day['yhat']:.2f} on {peak_day['ds'].date()}"
|
| 44 |
-
|
| 45 |
-
# Plot
|
| 46 |
fig = model.plot(forecast)
|
| 47 |
plt.title("Energy Load Forecast")
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
demo = gr.Interface(
|
| 52 |
fn=forecast_energy,
|
| 53 |
-
inputs=gr.File(label="Upload Energy Load
|
| 54 |
-
outputs=
|
| 55 |
title="Smart Energy Load Forecasting",
|
| 56 |
-
description="Upload a
|
| 57 |
)
|
| 58 |
|
| 59 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
|
|
|
| 3 |
from prophet import Prophet
|
| 4 |
+
import matplotlib.pyplot as plt
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
def forecast_energy(file):
|
| 7 |
+
# Read CSV
|
| 8 |
+
df = pd.read_csv(file)
|
| 9 |
|
| 10 |
+
# Rename columns to fit Prophet's expected format
|
|
|
|
|
|
|
|
|
|
| 11 |
df.columns = ['ds', 'y']
|
| 12 |
+
|
| 13 |
+
# Convert date column to datetime
|
| 14 |
df['ds'] = pd.to_datetime(df['ds'])
|
| 15 |
+
|
| 16 |
+
# Build and train model
|
|
|
|
| 17 |
model = Prophet()
|
| 18 |
model.fit(df)
|
| 19 |
+
|
| 20 |
+
# Forecast next 14 days
|
| 21 |
future = model.make_future_dataframe(periods=14)
|
| 22 |
forecast = model.predict(future)
|
| 23 |
+
|
| 24 |
+
# Plot forecast
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
fig = model.plot(forecast)
|
| 26 |
plt.title("Energy Load Forecast")
|
| 27 |
+
|
| 28 |
+
return fig
|
| 29 |
|
| 30 |
+
# Gradio interface
|
|
|
|
| 31 |
demo = gr.Interface(
|
| 32 |
fn=forecast_energy,
|
| 33 |
+
inputs=gr.File(label="Upload Energy Load CSV", file_types=[".csv"]),
|
| 34 |
+
outputs=gr.Plot(label="Forecasted Load"),
|
| 35 |
title="Smart Energy Load Forecasting",
|
| 36 |
+
description="Upload a CSV file with columns 'ds' (date) and 'y' (energy load). This app predicts the next 2 weeks of load."
|
| 37 |
)
|
| 38 |
|
| 39 |
if __name__ == "__main__":
|