File size: 3,927 Bytes
38723d6
76a4a39
 
 
c82f60f
76a4a39
66a6283
38723d6
76a4a39
 
 
38723d6
76a4a39
 
38723d6
76a4a39
 
 
 
 
 
 
 
38723d6
76a4a39
 
 
 
 
 
 
38723d6
12cb0d4
dfd73b9
5808c09
dfd73b9
c82f60f
dfd73b9
c82f60f
dfd73b9
76a4a39
 
 
 
 
 
 
dfd73b9
76a4a39
 
38723d6
c82f60f
76a4a39
8164b1b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
be16bab
dfd73b9
be16bab
 
c82f60f
f1eb3e2
e63fb58
72d81ff
76a4a39
c82f60f
f1eb3e2
e63fb58
c82f60f
970e0ea
a3a70ce
970e0ea
 
 
c82f60f
 
 
 
970e0ea
dfd73b9
2c39275
970e0ea
 
2c39275
38723d6
76a4a39
970e0ea
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import gradio as gr
import os
import requests
import json
import shutil
from huggingface_hub import HfApi, create_repo
from typing import Union

def download_file(digest, image):
    url = f"https://registry.ollama.ai/v2/library/{image}/blobs/{digest}"
    file_name = f"blobs/{digest}"

    # Create the directory if it doesn't exist
    os.makedirs(os.path.dirname(file_name), exist_ok=True)

    # Download the file
    print(f"Downloading {url} to {file_name}")
    response = requests.get(url, allow_redirects=True)
    if response.status_code == 200:
        with open(file_name, 'wb') as f:
            f.write(response.content)
    else:
        print(f"Failed to download {url}")

def fetch_manifest(image, tag):
    manifest_url = f"https://registry.ollama.ai/v2/library/{image}/manifests/{tag}"
    response = requests.get(manifest_url)
    if response.status_code == 200:
        return response.json()
    else:
        return None

def upload_to_huggingface(repo_id, folder_path, oauth_token: Union[gr.OAuthToken, None]):

    token = oauth_token.token if oauth_token else None
    
    api = HfApi(token=token)
    
    repo_path = api.create_repo(repo_id=repo_id, repo_type="model", exist_ok=True)
    
    print(f"Repo created {repo_path}")
    try:
        api.upload_folder(
            folder_path=folder_path,
            repo_id=repo_id,
            repo_type="model",
        )
        return "Upload successful", repo_path
    except Exception as e:
        return f"Upload failed: {str(e)}"

def process_image_tag(image_tag, repo_id, oauth_token: Union[gr.OAuthToken, None]):
    try:
        # Extract image and tag from the input
        image, tag = image_tag.split(':')
    
        # Fetch the manifest JSON
        manifest_json = fetch_manifest(image, tag)
        if not manifest_json or 'errors' in manifest_json:
            return f"Failed to fetch the manifest for {image}:{tag}"
    
        # Save the manifest JSON to the blobs folder
        manifest_file_path = "blobs/manifest"
        os.makedirs(os.path.dirname(manifest_file_path), exist_ok=True)
        with open(manifest_file_path, 'w') as f:
            json.dump(manifest_json, f)
    
        # Extract the digest values from the JSON
        digests = [layer['digest'] for layer in manifest_json.get('layers', [])]
    
        # Download each file
        for digest in digests:
            download_file(digest, image)
    
        # Download the config file
        config_digest = manifest_json.get('config', {}).get('digest')
        if config_digest:
            download_file(config_digest, image)

        # Upload to Hugging Face Hub
        upload_result, repo_path = upload_to_huggingface(repo_id, 'blobs', oauth_token)
    
        # Delete the blobs folder
        shutil.rmtree('blobs')

        return (f'Find your repo <a href=\'{repo_path}\' target="_blank" style="text-decoration:underline">here</a>', "dramallama.jpg")
        
    except Exception as e:
        shutil.rmtree('blobs', ignore_errors=True)

        return (f"We got an error, my dude, here's what the error looks like: {str(e)}", "madllama.jpg")

# Create the Gradio interface using gr.Blocks
with gr.Blocks() as demo:
    gr.Markdown("# Ollama <> HF Hub 🤝")
    gr.Markdown("Enter the image and tag to download the corresponding files from the Ollama registry and upload them to the Hugging Face Hub.")
    
    gr.LoginButton()
    
    image_tag_input = gr.Textbox(placeholder="Enter Ollama ID", label="Image and Tag")
    repo_id_input = gr.Textbox(placeholder="Enter Hugging Face repo ID", label="Hugging Face Repo ID")
    
    result_output = gr.Markdown(label="Result")
    result_image = gr.Image(show_label=False)
    
    process_button = gr.Button("Process")
    process_button.click(fn=process_image_tag, inputs=[image_tag_input, repo_id_input], outputs=[result_output, result_image])

# Launch the Gradio app
demo.launch()