Spaces:
Sleeping
Sleeping
Update app.py
#17
by
reach-vb
- opened
app.py
CHANGED
|
@@ -31,9 +31,13 @@ def fetch_manifest(image, tag):
|
|
| 31 |
return None
|
| 32 |
|
| 33 |
def upload_to_huggingface(repo_id, folder_path, oauth_token: Union[gr.OAuthToken, None]):
|
|
|
|
| 34 |
token = oauth_token.token if oauth_token else None
|
|
|
|
| 35 |
api = HfApi(token=token)
|
|
|
|
| 36 |
repo_path = api.create_repo(repo_id=repo_id, repo_type="model", exist_ok=True)
|
|
|
|
| 37 |
print(f"Repo created {repo_path}")
|
| 38 |
try:
|
| 39 |
api.upload_folder(
|
|
@@ -41,7 +45,7 @@ def upload_to_huggingface(repo_id, folder_path, oauth_token: Union[gr.OAuthToken
|
|
| 41 |
repo_id=repo_id,
|
| 42 |
repo_type="model",
|
| 43 |
)
|
| 44 |
-
return "Upload successful"
|
| 45 |
except Exception as e:
|
| 46 |
return f"Upload failed: {str(e)}"
|
| 47 |
|
|
@@ -55,7 +59,7 @@ def process_image_tag(image_tag, repo_id, oauth_token: Union[gr.OAuthToken, None
|
|
| 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
|
| 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)
|
|
@@ -73,16 +77,16 @@ def process_image_tag(image_tag, repo_id, oauth_token: Union[gr.OAuthToken, None
|
|
| 73 |
download_file(config_digest, image)
|
| 74 |
try:
|
| 75 |
# Upload to Hugging Face Hub
|
| 76 |
-
upload_result = upload_to_huggingface(repo_id, 'blobs', oauth_token)
|
| 77 |
|
| 78 |
# Delete the blobs folder
|
| 79 |
shutil.rmtree('blobs')
|
| 80 |
|
| 81 |
-
return f"Successfully fetched and downloaded files for {image}:{tag}
|
| 82 |
except Exception as e:
|
| 83 |
shutil.rmtree('blobs', ignore_errors=True)
|
| 84 |
|
| 85 |
-
return f"
|
| 86 |
|
| 87 |
css = """
|
| 88 |
.main_ui_logged_out{opacity: 0.3; pointer-events: none}
|
|
@@ -98,7 +102,7 @@ with gr.Blocks() as demo:
|
|
| 98 |
image_tag_input = gr.Textbox(placeholder="Enter Ollama ID", label="Image and Tag")
|
| 99 |
repo_id_input = gr.Textbox(placeholder="Enter Hugging Face repo ID", label="Hugging Face Repo ID")
|
| 100 |
|
| 101 |
-
result_output = gr.
|
| 102 |
|
| 103 |
process_button = gr.Button("Process")
|
| 104 |
process_button.click(fn=process_image_tag, inputs=[image_tag_input, repo_id_input], outputs=result_output)
|
|
|
|
| 31 |
return None
|
| 32 |
|
| 33 |
def upload_to_huggingface(repo_id, folder_path, oauth_token: Union[gr.OAuthToken, None]):
|
| 34 |
+
|
| 35 |
token = oauth_token.token if oauth_token else None
|
| 36 |
+
|
| 37 |
api = HfApi(token=token)
|
| 38 |
+
|
| 39 |
repo_path = api.create_repo(repo_id=repo_id, repo_type="model", exist_ok=True)
|
| 40 |
+
|
| 41 |
print(f"Repo created {repo_path}")
|
| 42 |
try:
|
| 43 |
api.upload_folder(
|
|
|
|
| 45 |
repo_id=repo_id,
|
| 46 |
repo_type="model",
|
| 47 |
)
|
| 48 |
+
return "Upload successful", repo_path
|
| 49 |
except Exception as e:
|
| 50 |
return f"Upload failed: {str(e)}"
|
| 51 |
|
|
|
|
| 59 |
return f"Failed to fetch the manifest for {image}:{tag}"
|
| 60 |
|
| 61 |
# Save the manifest JSON to the blobs folder
|
| 62 |
+
manifest_file_path = "blobs/manifest"
|
| 63 |
os.makedirs(os.path.dirname(manifest_file_path), exist_ok=True)
|
| 64 |
with open(manifest_file_path, 'w') as f:
|
| 65 |
json.dump(manifest_json, f)
|
|
|
|
| 77 |
download_file(config_digest, image)
|
| 78 |
try:
|
| 79 |
# Upload to Hugging Face Hub
|
| 80 |
+
upload_result, repo_path = upload_to_huggingface(repo_id, 'blobs', oauth_token)
|
| 81 |
|
| 82 |
# Delete the blobs folder
|
| 83 |
shutil.rmtree('blobs')
|
| 84 |
|
| 85 |
+
return f"Successfully fetched and downloaded * uploaded files for {image}:{tag} <a href=\'{repo_path}\' target="_blank" style="text-decoration:underline">here</a> 🦙"
|
| 86 |
except Exception as e:
|
| 87 |
shutil.rmtree('blobs', ignore_errors=True)
|
| 88 |
|
| 89 |
+
return f"We got an error, my dude, here's what the error looks like: {str(e)}"
|
| 90 |
|
| 91 |
css = """
|
| 92 |
.main_ui_logged_out{opacity: 0.3; pointer-events: none}
|
|
|
|
| 102 |
image_tag_input = gr.Textbox(placeholder="Enter Ollama ID", label="Image and Tag")
|
| 103 |
repo_id_input = gr.Textbox(placeholder="Enter Hugging Face repo ID", label="Hugging Face Repo ID")
|
| 104 |
|
| 105 |
+
result_output = gr.Markdown(label="Result")
|
| 106 |
|
| 107 |
process_button = gr.Button("Process")
|
| 108 |
process_button.click(fn=process_image_tag, inputs=[image_tag_input, repo_id_input], outputs=result_output)
|