Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -157,33 +157,63 @@ def predict_volatility(date):
|
|
| 157 |
|
| 158 |
|
| 159 |
################### GRADIO INTERFACE
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
def gradio_predict(date):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
try:
|
| 162 |
pd.to_datetime(date)
|
| 163 |
except:
|
| 164 |
-
return "❌ Invalid date format.
|
| 165 |
|
|
|
|
| 166 |
try:
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
return (
|
| 170 |
-
f"NOWCAST (t)\n"
|
| 171 |
-
f"Volatility: {point:.4f}\n"
|
| 172 |
-
f"95% CI: [{low:.4f}, {high:.4f}]\n\n"
|
| 173 |
-
f"FORECAST (t+1)\n"
|
| 174 |
-
f"Volatility: {for_point:.4f}\n"
|
| 175 |
-
f"95% CI: [{for_low:.4f}, {for_high:.4f}]"
|
| 176 |
-
)
|
| 177 |
except Exception as e:
|
| 178 |
-
return f"⚠️ Error while computing prediction:\n{str(e)}"
|
| 179 |
|
|
|
|
| 180 |
demo = gr.Interface(
|
| 181 |
fn=gradio_predict,
|
| 182 |
inputs=gr.Textbox(label="Date (YYYY-MM-DD)"),
|
| 183 |
-
outputs=gr.
|
| 184 |
title="Crypto Volatility Predictor",
|
| 185 |
-
description=
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
)
|
| 187 |
|
| 188 |
if __name__ == "__main__":
|
| 189 |
demo.launch()
|
|
|
|
|
|
| 157 |
|
| 158 |
|
| 159 |
################### GRADIO INTERFACE
|
| 160 |
+
import gradio as gr
|
| 161 |
+
import pandas as pd
|
| 162 |
+
|
| 163 |
+
################### HELPER FUNCTION TO RETURN TABLE ###################
|
| 164 |
+
def predict_volatility_for_table(date):
|
| 165 |
+
"""
|
| 166 |
+
Returns a DataFrame with Nowcast and Forecast predictions for the given date.
|
| 167 |
+
"""
|
| 168 |
+
# Run your existing predict_volatility function
|
| 169 |
+
point, low, high, for_point, for_low, for_high = predict_volatility(date)
|
| 170 |
+
|
| 171 |
+
# Create a DataFrame to display nicely
|
| 172 |
+
data = {
|
| 173 |
+
"Type": ["Nowcast (t)", "Forecast (t+1)"],
|
| 174 |
+
"Volatility": [point, for_point],
|
| 175 |
+
"Low 95% CI": [low, for_low],
|
| 176 |
+
"High 95% CI": [high, for_high]
|
| 177 |
+
}
|
| 178 |
+
|
| 179 |
+
df = pd.DataFrame(data)
|
| 180 |
+
|
| 181 |
+
# Round values for better readability
|
| 182 |
+
df[["Volatility", "Low 95% CI", "High 95% CI"]] = df[["Volatility", "Low 95% CI", "High 95% CI"]].round(4)
|
| 183 |
+
|
| 184 |
+
return df
|
| 185 |
+
|
| 186 |
+
################### GRADIO WRAPPER ###################
|
| 187 |
def gradio_predict(date):
|
| 188 |
+
"""
|
| 189 |
+
Wrapper for Gradio. Returns a DataFrame for display.
|
| 190 |
+
"""
|
| 191 |
+
# Validate date format
|
| 192 |
try:
|
| 193 |
pd.to_datetime(date)
|
| 194 |
except:
|
| 195 |
+
return pd.DataFrame({"Error": ["❌ Invalid date format. Use YYYY-MM-DD."]})
|
| 196 |
|
| 197 |
+
# Attempt to predict
|
| 198 |
try:
|
| 199 |
+
df = predict_volatility_for_table(date)
|
| 200 |
+
return df
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
except Exception as e:
|
| 202 |
+
return pd.DataFrame({"Error": [f"⚠️ Error while computing prediction:\n{str(e)}"]})
|
| 203 |
|
| 204 |
+
################### GRADIO INTERFACE ###################
|
| 205 |
demo = gr.Interface(
|
| 206 |
fn=gradio_predict,
|
| 207 |
inputs=gr.Textbox(label="Date (YYYY-MM-DD)"),
|
| 208 |
+
outputs=gr.Dataframe(label="Volatility Predictions", headers=["Type", "Volatility", "Low 95% CI", "High 95% CI"]),
|
| 209 |
title="Crypto Volatility Predictor",
|
| 210 |
+
description=(
|
| 211 |
+
"Enter a date to get predicted BTC volatility and 95% confidence intervals.\n"
|
| 212 |
+
"Nowcast = today's volatility, Forecast = next day's volatility."
|
| 213 |
+
),
|
| 214 |
+
allow_flagging="never"
|
| 215 |
)
|
| 216 |
|
| 217 |
if __name__ == "__main__":
|
| 218 |
demo.launch()
|
| 219 |
+
|