Spaces:
Sleeping
Sleeping
Update pyfolio/mcp_output/mcp_plugin/mcp_service.py
Browse files
pyfolio/mcp_output/mcp_plugin/mcp_service.py
CHANGED
|
@@ -37,7 +37,7 @@ from pyfolio.plotting import (
|
|
| 37 |
|
| 38 |
mcp = FastMCP("pyfolio_service")
|
| 39 |
|
| 40 |
-
def _save_plot_as_base64(
|
| 41 |
"""Save plot as base64 string for HuggingFace deployment"""
|
| 42 |
import base64
|
| 43 |
from io import BytesIO
|
|
@@ -53,19 +53,22 @@ def _save_plot_as_base64(plt, plot_name):
|
|
| 53 |
import tempfile
|
| 54 |
temp_dir = tempfile.gettempdir()
|
| 55 |
filepath = f"{temp_dir}/{filename}"
|
| 56 |
-
|
| 57 |
except Exception as e:
|
| 58 |
# If file saving fails, we still have the base64 data
|
| 59 |
filepath = f"Unable to save file: {str(e)}"
|
| 60 |
|
| 61 |
# Convert to base64 (this is the main output for HuggingFace)
|
| 62 |
buffer = BytesIO()
|
| 63 |
-
|
| 64 |
buffer.seek(0)
|
| 65 |
image_base64_bytes = base64.b64encode(buffer.getvalue())
|
| 66 |
image_base64 = image_base64_bytes.decode('utf-8')
|
| 67 |
buffer.close()
|
| 68 |
|
|
|
|
|
|
|
|
|
|
| 69 |
# Create data URI for direct display
|
| 70 |
data_uri = f"data:image/png;base64,{image_base64}"
|
| 71 |
|
|
@@ -232,17 +235,22 @@ def generate_returns_tear_sheet(returns: list) -> dict:
|
|
| 232 |
# Calculate performance statistics
|
| 233 |
stats = _calculate_performance_stats(returns_series)
|
| 234 |
|
| 235 |
-
# Capture tear sheet output
|
| 236 |
output = io.StringIO()
|
| 237 |
with contextlib.redirect_stdout(output):
|
| 238 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 239 |
|
| 240 |
return {
|
| 241 |
"success": True,
|
| 242 |
"result": {
|
| 243 |
"statistics": stats,
|
| 244 |
"message": "Returns tear sheet generated successfully.",
|
| 245 |
-
"tear_sheet_output": output.getvalue()[:1000] if output.getvalue() else
|
| 246 |
},
|
| 247 |
"error": None
|
| 248 |
}
|
|
|
|
| 37 |
|
| 38 |
mcp = FastMCP("pyfolio_service")
|
| 39 |
|
| 40 |
+
def _save_plot_as_base64(fig, plot_name):
|
| 41 |
"""Save plot as base64 string for HuggingFace deployment"""
|
| 42 |
import base64
|
| 43 |
from io import BytesIO
|
|
|
|
| 53 |
import tempfile
|
| 54 |
temp_dir = tempfile.gettempdir()
|
| 55 |
filepath = f"{temp_dir}/{filename}"
|
| 56 |
+
fig.savefig(filepath, dpi=300, bbox_inches='tight')
|
| 57 |
except Exception as e:
|
| 58 |
# If file saving fails, we still have the base64 data
|
| 59 |
filepath = f"Unable to save file: {str(e)}"
|
| 60 |
|
| 61 |
# Convert to base64 (this is the main output for HuggingFace)
|
| 62 |
buffer = BytesIO()
|
| 63 |
+
fig.savefig(buffer, format='png', dpi=300, bbox_inches='tight')
|
| 64 |
buffer.seek(0)
|
| 65 |
image_base64_bytes = base64.b64encode(buffer.getvalue())
|
| 66 |
image_base64 = image_base64_bytes.decode('utf-8')
|
| 67 |
buffer.close()
|
| 68 |
|
| 69 |
+
# Close the figure to free memory
|
| 70 |
+
plt.close(fig)
|
| 71 |
+
|
| 72 |
# Create data URI for direct display
|
| 73 |
data_uri = f"data:image/png;base64,{image_base64}"
|
| 74 |
|
|
|
|
| 235 |
# Calculate performance statistics
|
| 236 |
stats = _calculate_performance_stats(returns_series)
|
| 237 |
|
| 238 |
+
# Capture tear sheet output without plotting to avoid date issues
|
| 239 |
output = io.StringIO()
|
| 240 |
with contextlib.redirect_stdout(output):
|
| 241 |
+
try:
|
| 242 |
+
# Try to create the tear sheet, but catch plotting errors
|
| 243 |
+
create_returns_tear_sheet(returns_series)
|
| 244 |
+
except Exception as plot_error:
|
| 245 |
+
# If plotting fails, still provide the statistics
|
| 246 |
+
output.write(f"Returns tear sheet generation completed with plotting issue: {str(plot_error)}")
|
| 247 |
|
| 248 |
return {
|
| 249 |
"success": True,
|
| 250 |
"result": {
|
| 251 |
"statistics": stats,
|
| 252 |
"message": "Returns tear sheet generated successfully.",
|
| 253 |
+
"tear_sheet_output": output.getvalue()[:1000] if output.getvalue() else "Tear sheet generated with statistics only"
|
| 254 |
},
|
| 255 |
"error": None
|
| 256 |
}
|