Spaces:
Sleeping
Sleeping
Update pyPDAF/mcp_output/mcp_plugin/mcp_service.py
Browse files
pyPDAF/mcp_output/mcp_plugin/mcp_service.py
CHANGED
|
@@ -584,6 +584,150 @@ def run_enoi_pipeline(
|
|
| 584 |
"localization_radius_km": localization_radius if localization_radius > 0 else "none"
|
| 585 |
}
|
| 586 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 587 |
|
| 588 |
# ============================================================================
|
| 589 |
# Create MCP Server App
|
|
|
|
| 584 |
"localization_radius_km": localization_radius if localization_radius > 0 else "none"
|
| 585 |
}
|
| 586 |
|
| 587 |
+
@mcp.tool(name="upload_to_hf_dataset", description="Upload generated file to HuggingFace Dataset for easy download")
|
| 588 |
+
def upload_to_hf_dataset(file_path: str, dataset_repo: str = None, hf_token: str = None) -> dict:
|
| 589 |
+
"""
|
| 590 |
+
Upload generated file to HuggingFace Dataset repository.
|
| 591 |
+
Users can then download files from the Dataset page.
|
| 592 |
+
|
| 593 |
+
Parameters:
|
| 594 |
+
file_path (str): Absolute path to the file on HF Space
|
| 595 |
+
dataset_repo (str): Dataset repo name (e.g., 'username/obspy-outputs').
|
| 596 |
+
If None, uses HF_DATASET_REPO environment variable
|
| 597 |
+
hf_token (str): HuggingFace token. If None, uses HF_TOKEN environment variable
|
| 598 |
+
|
| 599 |
+
Returns:
|
| 600 |
+
dict: Success status and download URL
|
| 601 |
+
"""
|
| 602 |
+
try:
|
| 603 |
+
from huggingface_hub import HfApi, create_repo
|
| 604 |
+
from huggingface_hub.utils import RepositoryNotFoundError
|
| 605 |
+
|
| 606 |
+
if dataset_repo is None:
|
| 607 |
+
dataset_repo = os.environ.get("HF_DATASET_REPO")
|
| 608 |
+
if not dataset_repo:
|
| 609 |
+
return {
|
| 610 |
+
"success": False,
|
| 611 |
+
"error": "Dataset repo not specified. Set HF_DATASET_REPO environment variable or pass dataset_repo parameter"
|
| 612 |
+
}
|
| 613 |
+
|
| 614 |
+
if hf_token is None:
|
| 615 |
+
hf_token = os.environ.get("HF_TOKEN")
|
| 616 |
+
if not hf_token:
|
| 617 |
+
return {
|
| 618 |
+
"success": False,
|
| 619 |
+
"error": "HF token not found. Set HF_TOKEN environment variable or pass hf_token parameter"
|
| 620 |
+
}
|
| 621 |
+
|
| 622 |
+
# Check if file exists and readable
|
| 623 |
+
if not os.path.exists(file_path):
|
| 624 |
+
return {
|
| 625 |
+
"success": False,
|
| 626 |
+
"error": f"File not found: {file_path}"
|
| 627 |
+
}
|
| 628 |
+
|
| 629 |
+
if not os.path.isfile(file_path):
|
| 630 |
+
return {
|
| 631 |
+
"success": False,
|
| 632 |
+
"error": f"Path is not a file: {file_path}"
|
| 633 |
+
}
|
| 634 |
+
|
| 635 |
+
if not os.access(file_path, os.R_OK):
|
| 636 |
+
return {
|
| 637 |
+
"success": False,
|
| 638 |
+
"error": f"File not readable (permission denied): {file_path}",
|
| 639 |
+
"hint": "Check file permissions in Docker container"
|
| 640 |
+
}
|
| 641 |
+
|
| 642 |
+
# Get file info for debugging
|
| 643 |
+
file_size = os.path.getsize(file_path)
|
| 644 |
+
file_stat = os.stat(file_path)
|
| 645 |
+
|
| 646 |
+
# Initialize HF API
|
| 647 |
+
api = HfApi()
|
| 648 |
+
|
| 649 |
+
# Try to create dataset repo if it doesn't exist
|
| 650 |
+
try:
|
| 651 |
+
repo_info = create_repo(
|
| 652 |
+
repo_id=dataset_repo,
|
| 653 |
+
repo_type="dataset",
|
| 654 |
+
token=hf_token,
|
| 655 |
+
exist_ok=True,
|
| 656 |
+
private=False
|
| 657 |
+
)
|
| 658 |
+
except Exception as e:
|
| 659 |
+
return {
|
| 660 |
+
"success": False,
|
| 661 |
+
"error": f"Failed to create/access dataset repo: {str(e)}",
|
| 662 |
+
"dataset_repo": dataset_repo,
|
| 663 |
+
"hint": "Check if HF_TOKEN has write permission and dataset_repo format is correct (username/repo-name)"
|
| 664 |
+
}
|
| 665 |
+
|
| 666 |
+
# Get filename and determine path in dataset
|
| 667 |
+
filename = os.path.basename(file_path)
|
| 668 |
+
|
| 669 |
+
# Determine subdirectory based on file location or file type
|
| 670 |
+
if "/output/" in file_path:
|
| 671 |
+
path_in_repo = f"output/{filename}"
|
| 672 |
+
elif "/plots/" in file_path:
|
| 673 |
+
path_in_repo = f"plots/{filename}"
|
| 674 |
+
elif "/wave_data/" in file_path:
|
| 675 |
+
path_in_repo = f"wave_data/{filename}"
|
| 676 |
+
elif filename.endswith('.nc'):
|
| 677 |
+
# NetCDF files from pyPDAF (restart, analysis, ensemble, obs)
|
| 678 |
+
if "restart" in filename or "analysis" in filename:
|
| 679 |
+
path_in_repo = f"pypdaf_analysis/{filename}"
|
| 680 |
+
elif "obs" in filename:
|
| 681 |
+
path_in_repo = f"pypdaf_observations/{filename}"
|
| 682 |
+
elif "member" in filename or "ensemble" in file_path:
|
| 683 |
+
path_in_repo = f"pypdaf_ensemble/{filename}"
|
| 684 |
+
else:
|
| 685 |
+
path_in_repo = f"pypdaf_data/{filename}"
|
| 686 |
+
else:
|
| 687 |
+
path_in_repo = filename
|
| 688 |
+
|
| 689 |
+
try:
|
| 690 |
+
with open(file_path, 'rb') as f:
|
| 691 |
+
file_content = f.read()
|
| 692 |
+
except Exception as e:
|
| 693 |
+
return {
|
| 694 |
+
"success": False,
|
| 695 |
+
"error": f"Failed to read file: {str(e)}",
|
| 696 |
+
"file_path": file_path,
|
| 697 |
+
"hint": "File may exist but not readable due to permission issues"
|
| 698 |
+
}
|
| 699 |
+
|
| 700 |
+
# Upload file from memory instead of path
|
| 701 |
+
import io
|
| 702 |
+
upload_result = api.upload_file(
|
| 703 |
+
path_or_fileobj=io.BytesIO(file_content),
|
| 704 |
+
path_in_repo=path_in_repo,
|
| 705 |
+
repo_id=dataset_repo,
|
| 706 |
+
repo_type="dataset",
|
| 707 |
+
token=hf_token
|
| 708 |
+
)
|
| 709 |
+
|
| 710 |
+
# Construct download URL
|
| 711 |
+
download_url = f"https://huggingface.co/datasets/{dataset_repo}/resolve/main/{path_in_repo}"
|
| 712 |
+
viewer_url = f"https://huggingface.co/datasets/{dataset_repo}/viewer/default/train?f%5Bfile%5D%5Bvalue%5D={path_in_repo}"
|
| 713 |
+
|
| 714 |
+
return {
|
| 715 |
+
"success": True,
|
| 716 |
+
"message": f"File uploaded successfully to {dataset_repo}",
|
| 717 |
+
"dataset_repo": dataset_repo,
|
| 718 |
+
"filename": filename,
|
| 719 |
+
"path_in_repo": path_in_repo,
|
| 720 |
+
"download_url": download_url,
|
| 721 |
+
"viewer_url": viewer_url,
|
| 722 |
+
"usage": f"Download directly from: {download_url}"
|
| 723 |
+
}
|
| 724 |
+
|
| 725 |
+
except Exception as e:
|
| 726 |
+
return {
|
| 727 |
+
"success": False,
|
| 728 |
+
"error": str(e),
|
| 729 |
+
"hint": "Make sure HF_TOKEN and HF_DATASET_REPO are set in Space secrets"
|
| 730 |
+
}
|
| 731 |
|
| 732 |
# ============================================================================
|
| 733 |
# Create MCP Server App
|