Spaces:
Sleeping
Sleeping
Download app.py from mkashanz/prophet-api: direct link, hf CLI and curl.
- Browser
- Download file 998 Bytes
-
https://huggingface.co/spaces/mkashanz/prophet-api/resolve/main/app.py
- Command line
-
hf download hf://spaces/mkashanz/prophet-api/app.py
-
curl -L -o app.py https://huggingface.co/spaces/mkashanz/prophet-api/resolve/main/app.py
998 Bytes
| import gradio as gr | |
| import pandas as pd | |
| from prophet import Prophet | |
| import json | |
| def forecast_api(json_data, periods): | |
| # 1. Parse Data | |
| try: | |
| data = json.loads(json_data) | |
| df = pd.DataFrame(data) | |
| # 2. Train Prophet (Runs on Hugging Face Server, not your machine) | |
| m = Prophet() | |
| m.fit(df) | |
| # 3. Predict | |
| future = m.make_future_dataframe(periods=int(periods)) | |
| forecast = m.predict(future) | |
| # 4. Return result as JSON | |
| result = forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail(int(periods)) | |
| # Convert timestamps to string for JSON serialization | |
| result['ds'] = result['ds'].astype(str) | |
| return result.to_json(orient='records') | |
| except Exception as e: | |
| return json.dumps({"error": str(e)}) | |
| # Create the API interface | |
| demo = gr.Interface( | |
| fn=forecast_api, | |
| inputs=[gr.Textbox(label="JSON Data"), gr.Number(label="Periods", value=30)], | |
| outputs="json" | |
| ) | |
| demo.launch() |