File size: 3,588 Bytes
be85197
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import torch
import coremltools as ct
from huggingface_hub import snapshot_download
from transformers import AutoModel, AutoTokenizer
import os
import tempfile
import shutil

def convert_to_coreml(model_name, input_length=128, ios_version="iOS15"):
    
    try:
        print(f"Downloading model {model_name}...")
        # Download model from Huggingface
        model_path = snapshot_download(repo_id=model_name)
        
        print("Loading model...")
        # Load model and tokenizer
        model = AutoModel.from_pretrained(model_path)
        tokenizer = AutoTokenizer.from_pretrained(model_path)
        
        # Set model to evaluation mode
        model.eval()
        
        print("Creating example input...")
        # Create example input
        if hasattr(tokenizer, "pad_token") and tokenizer.pad_token is None:
            tokenizer.pad_token = tokenizer.eos_token
            
        sample_text = "This is a sample input for conversion"
        inputs = tokenizer(sample_text, return_tensors="pt", padding="max_length", max_length=input_length)
        
        print("Tracing model...")
        # Trace the model
        traced_model = torch.jit.trace(model, [inputs["input_ids"], inputs["attention_mask"]])
        
        # Define Core ML target version
        if ios_version == "iOS15":
            target = ct.target.iOS15
        elif ios_version == "iOS16":
            target = ct.target.iOS16
        else:
            target = ct.target.iOS17
        
        print(f"Converting to Core ML (targeting {ios_version})...")
        # Convert to Core ML
        mlmodel = ct.convert(
            traced_model,
            inputs=[
                ct.TensorType(name="input_ids", shape=inputs["input_ids"].shape),
                ct.TensorType(name="attention_mask", shape=inputs["attention_mask"].shape)
            ],
            minimum_deployment_target=target
        )
        
        # Create a temporary directory to save the model
        temp_dir = tempfile.mkdtemp()
        model_file = os.path.join(temp_dir, f"{model_name.split('/')[-1]}.mlmodel")
        
        print(f"Saving model to {model_file}...")
        # Save the model
        mlmodel.save(model_file)
        
        return model_file, "Conversion successful!"
    
    except Exception as e:
        return None, f"Error: {str(e)}"

def process(model_name, input_length, ios_version):
    model_file, message = convert_to_coreml(model_name, int(input_length), ios_version)
    if model_file:
        return message, model_file
    else:
        return message, None

# Create Gradio interface
with gr.Blocks(title="Huggingface to Core ML Converter") as demo:
    gr.Markdown("# Huggingface to Core ML Model Converter")
    gr.Markdown("Enter a Huggingface model ID and convert it to Core ML format.")
    
    with gr.Row():
        with gr.Column():
            model_name = gr.Textbox(label="Huggingface Model ID (e.g., 'your-username/your-model')")
            input_length = gr.Slider(minimum=16, maximum=512, value=128, step=16, label="Input Length")
            ios_version = gr.Dropdown(choices=["iOS15", "iOS16", "iOS17"], value="iOS15", label="Target iOS Version")
            convert_button = gr.Button("Convert Model")
        
        with gr.Column():
            output_message = gr.Textbox(label="Status")
            file_output = gr.File(label="Converted Model")
    
    convert_button.click(
        process,
        inputs=[model_name, input_length, ios_version],
        outputs=[output_message, file_output]
    )

demo.launch()