Skip to content

Commit

Permalink
add credentials.py to get user token
Browse files Browse the repository at this point in the history
  • Loading branch information
nee1k committed Feb 15, 2025
1 parent 0b99c75 commit 4fe08cb
Show file tree
Hide file tree
Showing 3 changed files with 246 additions and 13 deletions.
234 changes: 234 additions & 0 deletions examples/notebooks/Uploading Models to HuggingFace.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
{
"cells": [
{
"metadata": {},
"cell_type": "raw",
"source": [
"{\n",
" \"cells\": [\n",
" {\n",
" \"cell_type\": \"markdown\",\n",
" \"source\": [\n",
" \"# Patra Integration with Hugging Face\\n\",\n",
" \"\\n\",\n",
" \"[![Run in Google Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/Data-to-Insight-Center/patra-toolkit/blob/main/examples/notebooks/HuggingFace_Example.ipynb)\\n\"\n",
" ],\n",
" \"metadata\": {\n",
" \"id\": \"KFXMTro-ne3-\"\n",
" },\n",
" \"id\": \"KFXMTro-ne3-\"\n",
" },\n",
" {\n",
" \"cell_type\": \"code\",\n",
" \"source\": \"!pip install patra-toolkit -e .\",\n",
" \"metadata\": {\n",
" \"collapsed\": true,\n",
" \"colab\": {\n",
" \"base_uri\": \"https://localhost:8080/\"\n",
" },\n",
" \"id\": \"Gm1pg-KpnfaJ\",\n",
" \"outputId\": \"798399ae-2826-4940-9e78-1714e568cd51\",\n",
" \"ExecuteTime\": {\n",
" \"end_time\": \"2025-02-13T01:01:43.915193Z\",\n",
" \"start_time\": \"2025-02-13T01:01:43.314247Z\"\n",
" }\n",
" },\n",
" \"id\": \"Gm1pg-KpnfaJ\",\n",
" \"outputs\": [\n",
" {\n",
" \"name\": \"stdout\",\n",
" \"output_type\": \"stream\",\n",
" \"text\": [\n",
" \"Obtaining file:///Users/neeleshkarthikeyan/d2i/patra-toolkit/examples/notebooks\\r\\n\",\n",
" \"\\u001B[31mERROR: file:///Users/neeleshkarthikeyan/d2i/patra-toolkit/examples/notebooks does not appear to be a Python project: neither 'setup.py' nor 'pyproject.toml' found.\\u001B[0m\\u001B[31m\\r\\n\",\n",
" \"\\u001B[0m\\r\\n\",\n",
" \"\\u001B[1m[\\u001B[0m\\u001B[34;49mnotice\\u001B[0m\\u001B[1;39;49m]\\u001B[0m\\u001B[39;49m A new release of pip is available: \\u001B[0m\\u001B[31;49m23.2.1\\u001B[0m\\u001B[39;49m -> \\u001B[0m\\u001B[32;49m25.0.1\\u001B[0m\\r\\n\",\n",
" \"\\u001B[1m[\\u001B[0m\\u001B[34;49mnotice\\u001B[0m\\u001B[1;39;49m]\\u001B[0m\\u001B[39;49m To update, run: \\u001B[0m\\u001B[32;49mpip install --upgrade pip\\u001B[0m\\r\\n\"\n",
" ]\n",
" }\n",
" ],\n",
" \"execution_count\": 10\n",
" },\n",
" {\n",
" \"cell_type\": \"code\",\n",
" \"source\": [\n",
" \"import torchvision.models as models\\n\",\n",
" \"\\n\",\n",
" \"model = models.resnet50(pretrained=True)\\n\"\n",
" ],\n",
" \"metadata\": {\n",
" \"colab\": {\n",
" \"base_uri\": \"https://localhost:8080/\"\n",
" },\n",
" \"id\": \"3_qN6LTjAVOT\",\n",
" \"outputId\": \"501dd56f-231c-4845-94b2-559d0549c5d2\",\n",
" \"ExecuteTime\": {\n",
" \"end_time\": \"2025-02-13T01:01:44.821461Z\",\n",
" \"start_time\": \"2025-02-13T01:01:44.376241Z\"\n",
" }\n",
" },\n",
" \"id\": \"3_qN6LTjAVOT\",\n",
" \"outputs\": [],\n",
" \"execution_count\": 11\n",
" },\n",
" {\n",
" \"metadata\": {\n",
" \"colab\": {\n",
" \"base_uri\": \"https://localhost:8080/\"\n",
" },\n",
" \"id\": \"xwXQeWspo7kl\",\n",
" \"outputId\": \"ab051b67-60cd-4f73-bab3-ac40e5ca31f2\",\n",
" \"ExecuteTime\": {\n",
" \"end_time\": \"2025-02-13T01:01:45.287450Z\",\n",
" \"start_time\": \"2025-02-13T01:01:45.281271Z\"\n",
" }\n",
" },\n",
" \"cell_type\": \"code\",\n",
" \"source\": [\n",
" \"from patra_toolkit import ModelCard, AIModel\\n\",\n",
" \"\\n\",\n",
" \"# Initialize the Model Card\\n\",\n",
" \"mc = ModelCard(\\n\",\n",
" \" name=\\\"ResNet50 Image Classification Model\\\",\\n\",\n",
" \" version=\\\"1.0\\\",\\n\",\n",
" \" short_description=\\\"Pre-trained ResNet50 model from torchvision for image classification.\\\",\\n\",\n",
" \" full_description=(\\n\",\n",
" \" \\\"This ResNet50 model is pre-trained on ImageNet and can be used for a variety \\\"\\n\",\n",
" \" \\\"of image classification tasks. It demonstrates the use of Patra Model Cards to \\\"\\n\",\n",
" \" \\\"document model metadata, bias, and explainability metrics.\\\"\\n\",\n",
" \" ),\\n\",\n",
" \" keywords=\\\"ResNet50, image classification, pytorch, ImageNet, model card\\\",\\n\",\n",
" \" author=\\\"Neelesh Karthikeyan\\\",\\n\",\n",
" \" input_type=\\\"Image\\\",\\n\",\n",
" \" category=\\\"classification\\\",\\n\",\n",
" \" foundational_model=\\\"None\\\"\\n\",\n",
" \")\\n\",\n",
" \"\\n\",\n",
" \"# Define input and output data URLs\\n\",\n",
" \"mc.input_data = 'https://huggingface.co/datasets/cifar10'\\n\",\n",
" \"mc.output_data = 'https://huggingface.co/nkarthikeyan/ResNet50_Image_Classification_Model/blob/main/ResNet50_Image_Classification_Model.pt'\\n\",\n",
" \"\\n\",\n",
" \"# Initialize AI Model details\\n\",\n",
" \"ai_model = AIModel(\\n\",\n",
" \" name=\\\"ResNet50 Image Classification Model\\\",\\n\",\n",
" \" version=\\\"1.0\\\",\\n\",\n",
" \" description=(\\n\",\n",
" \" \\\"Pre-trained ResNet50 model from torchvision for image classification. \\\"\\n\",\n",
" \" \\\"This model achieves approximately 76% top-1 accuracy on ImageNet.\\\"\\n\",\n",
" \" ),\\n\",\n",
" \" owner=\\\"Neelesh Karthikeyan\\\",\\n\",\n",
" \" location='https://huggingface.co/nkarthikeyan/ResNet50_Image_Classification_Model/blob/main/ResNet50_Image_Classification_Model.pt',\\n\",\n",
" \" license=\\\"Apache-2.0\\\",\\n\",\n",
" \" framework=\\\"pytorch\\\",\\n\",\n",
" \" model_type=\\\"cnn\\\",\\n\",\n",
" \" test_accuracy=0.76\\n\",\n",
" \")\\n\",\n",
" \"\\n\",\n",
" \"ai_model.populate_model_structure(model)\\n\",\n",
" \"mc.ai_model = ai_model\\n\",\n",
" \"mc.populate_requirements()\\n\"\n",
" ],\n",
" \"id\": \"xwXQeWspo7kl\",\n",
" \"outputs\": [],\n",
" \"execution_count\": 12\n",
" },\n",
" {\n",
" \"metadata\": {\n",
" \"id\": \"y2Xfa42KqX9H\",\n",
" \"colab\": {\n",
" \"base_uri\": \"https://localhost:8080/\"\n",
" },\n",
" \"outputId\": \"4865c483-eaee-4da5-b58f-8a82fdcb145c\",\n",
" \"ExecuteTime\": {\n",
" \"end_time\": \"2025-02-13T01:01:46.431710Z\",\n",
" \"start_time\": \"2025-02-13T01:01:46.049438Z\"\n",
" }\n",
" },\n",
" \"cell_type\": \"code\",\n",
" \"source\": [\n",
" \"# Validate the Model Card\\n\",\n",
" \"mc.validate()\\n\",\n",
" \"\\n\",\n",
" \"# Submit the Model Card\\n\",\n",
" \"mc.submit(patra_server_url=\\\"http://10.20.227.55:5002\\\", model=model)\\n\"\n",
" ],\n",
" \"id\": \"y2Xfa42KqX9H\",\n",
" \"outputs\": [\n",
" {\n",
" \"data\": {\n",
" \"text/plain\": [\n",
" \"{'error': 'Failed to submit model card: 500 Server Error: INTERNAL SERVER ERROR for url: http://10.20.227.55:5002/upload_mc'}\"\n",
" ]\n",
" },\n",
" \"execution_count\": 13,\n",
" \"metadata\": {},\n",
" \"output_type\": \"execute_result\"\n",
" }\n",
" ],\n",
" \"execution_count\": 13\n",
" },\n",
" {\n",
" \"cell_type\": \"code\",\n",
" \"source\": [],\n",
" \"metadata\": {\n",
" \"id\": \"wMuGP-ogrKdA\"\n",
" },\n",
" \"id\": \"wMuGP-ogrKdA\",\n",
" \"execution_count\": null,\n",
" \"outputs\": []\n",
" }\n",
" ],\n",
" \"metadata\": {\n",
" \"kernelspec\": {\n",
" \"display_name\": \"Python 3\",\n",
" \"language\": \"python\",\n",
" \"name\": \"python3\"\n",
" },\n",
" \"language_info\": {\n",
" \"codemirror_mode\": {\n",
" \"name\": \"ipython\",\n",
" \"version\": 2\n",
" },\n",
" \"file_extension\": \".py\",\n",
" \"mimetype\": \"text/x-python\",\n",
" \"name\": \"python\",\n",
" \"nbconvert_exporter\": \"python\",\n",
" \"pygments_lexer\": \"ipython2\",\n",
" \"version\": \"2.7.6\"\n",
" },\n",
" \"colab\": {\n",
" \"provenance\": []\n",
" }\n",
" },\n",
" \"nbformat\": 4,\n",
" \"nbformat_minor\": 5\n",
"}\n"
],
"id": "ca26b3103b0e7ce7"
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
},
"colab": {
"provenance": []
}
},
"nbformat": 4,
"nbformat_minor": 5
}
6 changes: 3 additions & 3 deletions patra_toolkit/credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def get_huggingface_credentials(patra_server_url: str, timeout: int = 10) -> dic
response = requests.get(hf_creds_url, headers=headers, timeout=timeout)
response.raise_for_status()
creds = response.json()
logging.info(f"Successfully received credentials.")

if "username" not in creds or "token" not in creds:
raise Exception("Invalid Hugging Face credentials response from server.")
return creds
Expand All @@ -46,7 +46,7 @@ def get_github_credentials(patra_server_url: str, timeout: int = 10) -> dict:
response = requests.get(gh_creds_url, headers=headers, timeout=timeout)
response.raise_for_status()
creds = response.json()
logging.info(f"Successfully received credentials.")

if "username" not in creds or "token" not in creds:
raise Exception("Invalid GitHub credentials response from server.")
return creds
Expand All @@ -70,7 +70,7 @@ def get_ndp_credentials(patra_server_url: str, timeout: int = 10) -> dict:
response = requests.get(ndp_creds_url, headers=headers, timeout=timeout)
response.raise_for_status()
creds = response.json()
logging.info(f"Successfully received credentials.")

if "api_key" not in creds:
raise Exception("Invalid NDP credentials response from server.")
return creds
19 changes: 9 additions & 10 deletions tests/test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
if __name__ == '__main__':
import torchvision.models as models
from patra_toolkit import ModelCard, AIModel

import torchvision.models as models
from patra_toolkit import ModelCard, AIModel
if __name__ == '__main__':

# Initialize the Model Card
mc = ModelCard(
Expand All @@ -20,10 +20,6 @@
foundational_model="None"
)

# Define input and output data URLs
mc.input_data = 'https://huggingface.co/datasets/cifar10'
mc.output_data = 'https://huggingface.co/nkarthikeyan/ResNet50_Image_Classification_Model/blob/main/ResNet50_Image_Classification_Model.pt'

# Initialize AI Model details
ai_model = AIModel(
name="ResNet50 Image Classification Model",
Expand All @@ -40,14 +36,17 @@
test_accuracy=0.76
)

model = models.resnet50(pretrained=True)
resnet50_model = models.resnet50(pretrained=True)

ai_model.populate_model_structure(model)
ai_model.populate_model_structure(resnet50_model)
mc.ai_model = ai_model
mc.populate_requirements()

# Validate the Model Card
mc.validate()

# Submit the Model Card
mc.submit(patra_server_url="http://10.20.227.55:5002", model=model, storage_backend="huggingface", model_format="pt")
mc.submit(patra_server_url="http://10.20.227.55:5002",
model=resnet50_model,
storage_backend="huggingface",
model_format="pt")

0 comments on commit 4fe08cb

Please sign in to comment.