reach-vb HF staff commited on
Commit
be16bab
·
verified ·
1 Parent(s): 17dc67f

Update app.py (#4)

Browse files

- Update app.py (8bf8e951b839aa33e382731d53443b73b02eb075)

Files changed (1) hide show
  1. app.py +38 -35
app.py CHANGED
@@ -32,7 +32,7 @@ def fetch_manifest(image, tag):
32
 
33
  def upload_to_huggingface(repo_id, folder_path):
34
  api = HfApi(token=HF_TOKEN)
35
- repo_path = create_repo(repo_id, "model")
36
  print(f"Repo created {repo_path}")
37
  try:
38
  api.upload_folder(
@@ -46,51 +46,54 @@ def upload_to_huggingface(repo_id, folder_path):
46
 
47
  def process_image_tag(image_tag, repo_id):
48
  # Extract image and tag from the input
49
- image, tag = image_tag.split(':')
50
-
51
- # Fetch the manifest JSON
52
- manifest_json = fetch_manifest(image, tag)
53
- if not manifest_json or 'errors' in manifest_json:
54
- return f"Failed to fetch the manifest for {image}:{tag}"
55
-
56
- # Save the manifest JSON to the blobs folder
57
- manifest_file_path = "blobs/manifest.json"
58
- os.makedirs(os.path.dirname(manifest_file_path), exist_ok=True)
59
- with open(manifest_file_path, 'w') as f:
60
- json.dump(manifest_json, f)
61
-
62
- # Extract the digest values from the JSON
63
- digests = [layer['digest'] for layer in manifest_json.get('layers', [])]
64
-
65
- # Download each file
66
- for digest in digests:
67
- download_file(digest, image)
68
-
69
- # Download the config file
70
- config_digest = manifest_json.get('config', {}).get('digest')
71
- if config_digest:
72
- download_file(config_digest, image)
73
-
74
- # Upload to Hugging Face Hub
75
- upload_result = upload_to_huggingface(repo_id, 'blobs/*')
76
-
77
- # Delete the blobs folder
78
  try:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
  os.rmtree('blobs')
80
  return f"Successfully fetched and downloaded files for {image}:{tag}\n{upload_result}\nBlobs folder deleted"
81
  except Exception as e:
82
- return f"Failed to delete blobs folder: {str(e)}"
 
83
 
84
  # Create the Gradio interface
85
  iface = gr.Interface(
86
  fn=process_image_tag,
87
  inputs=[
88
- gr.Textbox(placeholder="Enter image:tag", label="Image and Tag"),
89
- gr.Textbox(placeholder="Enter Hugging Face repo ID", label="Hugging Face Repo ID")
 
90
  ],
91
  outputs=gr.Textbox(label="Result"),
92
- title="Registry File Downloader and Uploader",
93
- description="Enter the image and tag to download the corresponding files from the registry and upload them to the Hugging Face Hub."
94
  )
95
 
96
  # Launch the Gradio app
 
32
 
33
  def upload_to_huggingface(repo_id, folder_path):
34
  api = HfApi(token=HF_TOKEN)
35
+ repo_path = api.create_repo(repo_id, "model", exist_ok=True,)
36
  print(f"Repo created {repo_path}")
37
  try:
38
  api.upload_folder(
 
46
 
47
  def process_image_tag(image_tag, repo_id):
48
  # Extract image and tag from the input
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  try:
50
+ image, tag = image_tag.split(':')
51
+
52
+ # Fetch the manifest JSON
53
+ manifest_json = fetch_manifest(image, tag)
54
+ if not manifest_json or 'errors' in manifest_json:
55
+ return f"Failed to fetch the manifest for {image}:{tag}"
56
+
57
+ # Save the manifest JSON to the blobs folder
58
+ manifest_file_path = "blobs/manifest.json"
59
+ os.makedirs(os.path.dirname(manifest_file_path), exist_ok=True)
60
+ with open(manifest_file_path, 'w') as f:
61
+ json.dump(manifest_json, f)
62
+
63
+ # Extract the digest values from the JSON
64
+ digests = [layer['digest'] for layer in manifest_json.get('layers', [])]
65
+
66
+ # Download each file
67
+ for digest in digests:
68
+ download_file(digest, image)
69
+
70
+ # Download the config file
71
+ config_digest = manifest_json.get('config', {}).get('digest')
72
+ if config_digest:
73
+ download_file(config_digest, image)
74
+
75
+ # Upload to Hugging Face Hub
76
+ upload_result = upload_to_huggingface(repo_id, 'blobs/*')
77
+
78
+ # Delete the blobs folder
79
+
80
  os.rmtree('blobs')
81
  return f"Successfully fetched and downloaded files for {image}:{tag}\n{upload_result}\nBlobs folder deleted"
82
  except Exception as e:
83
+ os.rmtree('blobs')
84
+ return f"Error found: {str(e)}"
85
 
86
  # Create the Gradio interface
87
  iface = gr.Interface(
88
  fn=process_image_tag,
89
  inputs=[
90
+ gr.Textbox(placeholder="Enter Ollama ID", label="Image and Tag"),
91
+ gr.Textbox(placeholder="Enter Hugging Face repo ID", label="Hugging Face Repo ID"),
92
+ )
93
  ],
94
  outputs=gr.Textbox(label="Result"),
95
+ title="Ollama <> HF Hub 🤝",
96
+ description="Enter the image and tag to download the corresponding files from the Ollama registry and upload them to the Hugging Face Hub."
97
  )
98
 
99
  # Launch the Gradio app