Spaces:
Sleeping
Sleeping
added
Browse files- app.py +36 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from prophet import Prophet
|
| 4 |
+
import json
|
| 5 |
+
|
| 6 |
+
def forecast_api(json_data, periods):
|
| 7 |
+
# 1. Parse Data
|
| 8 |
+
try:
|
| 9 |
+
data = json.loads(json_data)
|
| 10 |
+
df = pd.DataFrame(data)
|
| 11 |
+
|
| 12 |
+
# 2. Train Prophet (Runs on Hugging Face Server, not your machine)
|
| 13 |
+
m = Prophet()
|
| 14 |
+
m.fit(df)
|
| 15 |
+
|
| 16 |
+
# 3. Predict
|
| 17 |
+
future = m.make_future_dataframe(periods=int(periods))
|
| 18 |
+
forecast = m.predict(future)
|
| 19 |
+
|
| 20 |
+
# 4. Return result as JSON
|
| 21 |
+
result = forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail(int(periods))
|
| 22 |
+
# Convert timestamps to string for JSON serialization
|
| 23 |
+
result['ds'] = result['ds'].astype(str)
|
| 24 |
+
return result.to_json(orient='records')
|
| 25 |
+
|
| 26 |
+
except Exception as e:
|
| 27 |
+
return json.dumps({"error": str(e)})
|
| 28 |
+
|
| 29 |
+
# Create the API interface
|
| 30 |
+
demo = gr.Interface(
|
| 31 |
+
fn=forecast_api,
|
| 32 |
+
inputs=[gr.Textbox(label="JSON Data"), gr.Number(label="Periods", value=30)],
|
| 33 |
+
outputs="json"
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
prophet
|
| 2 |
+
pandas
|
| 3 |
+
gradio
|