Spaces:
Running
Running
Update app.py
#6
by
reach-vb
- opened
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import gradio as gr
|
|
| 2 |
import os
|
| 3 |
import requests
|
| 4 |
import json
|
|
|
|
| 5 |
from huggingface_hub import HfApi, create_repo
|
| 6 |
|
| 7 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
|
@@ -30,9 +31,9 @@ def fetch_manifest(image, tag):
|
|
| 30 |
else:
|
| 31 |
return None
|
| 32 |
|
| 33 |
-
def upload_to_huggingface(repo_id, folder_path):
|
| 34 |
-
api = HfApi(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(
|
|
@@ -44,9 +45,11 @@ def upload_to_huggingface(repo_id, folder_path):
|
|
| 44 |
except Exception as e:
|
| 45 |
return f"Upload failed: {str(e)}"
|
| 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
|
|
@@ -73,23 +76,28 @@ def process_image_tag(image_tag, repo_id):
|
|
| 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 |
return f"Successfully fetched and downloaded files for {image}:{tag}\n{upload_result}\nBlobs folder deleted"
|
| 81 |
except Exception as e:
|
| 82 |
-
|
| 83 |
return f"Error found: {str(e)}"
|
| 84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
# Create the Gradio interface using gr.Blocks
|
| 86 |
-
with gr.Blocks() as demo:
|
| 87 |
gr.Markdown("# Ollama <> HF Hub 🤝")
|
| 88 |
gr.Markdown("Enter the image and tag to download the corresponding files from the Ollama registry and upload them to the Hugging Face Hub.")
|
| 89 |
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
|
|
|
| 93 |
|
| 94 |
result_output = gr.Textbox(label="Result")
|
| 95 |
|
|
|
|
| 2 |
import os
|
| 3 |
import requests
|
| 4 |
import json
|
| 5 |
+
import shutil
|
| 6 |
from huggingface_hub import HfApi, create_repo
|
| 7 |
|
| 8 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
|
|
|
| 31 |
else:
|
| 32 |
return None
|
| 33 |
|
| 34 |
+
def upload_to_huggingface(repo_id, folder_path, token=token):
|
| 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(
|
|
|
|
| 45 |
except Exception as e:
|
| 46 |
return f"Upload failed: {str(e)}"
|
| 47 |
|
| 48 |
+
def process_image_tag(image_tag, repo_id, oauth_token: Union[gr.OAuthToken, None]):
|
| 49 |
# Extract image and tag from the input
|
| 50 |
try:
|
| 51 |
+
token = oauth_token.token if oauth_token else None
|
| 52 |
+
|
| 53 |
image, tag = image_tag.split(':')
|
| 54 |
|
| 55 |
# Fetch the manifest JSON
|
|
|
|
| 76 |
download_file(config_digest, image)
|
| 77 |
|
| 78 |
# Upload to Hugging Face Hub
|
| 79 |
+
upload_result = upload_to_huggingface(repo_id, 'blobs/*', token=token)
|
| 80 |
|
| 81 |
# Delete the blobs folder
|
| 82 |
+
shutil.rmtree('blobs')
|
| 83 |
return f"Successfully fetched and downloaded files for {image}:{tag}\n{upload_result}\nBlobs folder deleted"
|
| 84 |
except Exception as e:
|
| 85 |
+
shutil.rmtree('blobs', ignore_errors=True)
|
| 86 |
return f"Error found: {str(e)}"
|
| 87 |
|
| 88 |
+
css = """
|
| 89 |
+
.main_ui_logged_out{opacity: 0.3; pointer-events: none}
|
| 90 |
+
"""
|
| 91 |
+
|
| 92 |
# Create the Gradio interface using gr.Blocks
|
| 93 |
+
with gr.Blocks(css=css) as demo:
|
| 94 |
gr.Markdown("# Ollama <> HF Hub 🤝")
|
| 95 |
gr.Markdown("Enter the image and tag to download the corresponding files from the Ollama registry and upload them to the Hugging Face Hub.")
|
| 96 |
|
| 97 |
+
gr.LoginButton()
|
| 98 |
+
|
| 99 |
+
image_tag_input = gr.Textbox(placeholder="Enter Ollama ID", label="Image and Tag")
|
| 100 |
+
repo_id_input = gr.Textbox(placeholder="Enter Hugging Face repo ID", label="Hugging Face Repo ID")
|
| 101 |
|
| 102 |
result_output = gr.Textbox(label="Result")
|
| 103 |
|