File size: 1,624 Bytes
5638c25
 
 
 
 
 
9372c94
 
 
 
 
5638c25
9372c94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5638c25
9372c94
 
 
5638c25
9372c94
 
5638c25
9372c94
5638c25
9372c94
 
 
 
 
 
 
 
 
 
5638c25
9372c94
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
import gradio as gr
import torch
from safetensors.torch import save_file
import requests
import os

def convert_ckpt_to_safetensors(input_path, output_path):
    # Load the .ckpt file
    state_dict = torch.load(input_path, map_location='cpu')
    # Save as .safetensors
    save_file(state_dict, output_path)

def process(url, uploaded_file):
    if url:
        # Download the .ckpt file
        local_filename = 'model.ckpt'
        with requests.get(url, stream=True) as r:
            r.raise_for_status()
            with open(local_filename, 'wb') as f:
                for chunk in r.iter_content(chunk_size=8192):
                    f.write(chunk)
    elif uploaded_file is not None:
        # Save uploaded file
        local_filename = uploaded_file.name
        with open(local_filename, 'wb') as f:
            f.write(uploaded_file.read())
    else:
        return "Please provide a URL or upload a .ckpt file."

    output_filename = local_filename.replace('.ckpt', '.safetensors')
    # Convert the .ckpt to .safetensors
    convert_ckpt_to_safetensors(local_filename, output_filename)

    # Clean up the input file
    os.remove(local_filename)

    return output_filename

iface = gr.Interface(
    fn=process,
    inputs=[
        gr.Textbox(label="URL of .ckpt file", placeholder="Enter the URL here"),
        gr.File(label="Or upload a .ckpt file", file_types=['.ckpt'])
    ],
    outputs=gr.File(label="Converted .safetensors file"),
    title="CKPT to SafeTensors Converter",
    description="Convert .ckpt files to .safetensors format. Provide a URL or upload your .ckpt file."
)

iface.launch()