import gradio as gr import requests import torch from transformers import AutoModel, AutoTokenizer from huggingface_hub import HfApi def convert_and_deploy(url, model_name, hf_username, hf_token): # セーフテンソルファイルをダウンロード response = requests.get(url) if response.status_code != 200: return "ファイルのダウンロードに失敗しました。URLを確認してください。" # ファイルを保存 file_path = "model.safetensors" with open(file_path, "wb") as f: f.write(response.content) # モデルを読み込み model = AutoModel.from_pretrained("path_to_model", torch_dtype=torch.float16, use_safetensors=True) model.load_state_dict(torch.load(file_path)) # モデルをfloat16形式で保存 model.save_pretrained(f"{model_name}_float16", torch_dtype=torch.float16) # モデルをHugging Faceにデプロイ api = HfApi() api.upload_folder( folder_path=f"{model_name}_float16", repo_id=f"{hf_username}/{model_name}", token=hf_token, path_in_repo=f"{model_name}_float16", create_remote_repo=True ) return "モデルをfloat16に変換し、Hugging Faceにデプロイしました。" # Gradioインターフェースの作成 iface = gr.Interface( fn=convert_and_deploy, inputs=[ gr.inputs.Textbox(label="セーフテンソルURL"), gr.inputs.Textbox(label="モデル名"), gr.inputs.Textbox(label="Hugging Face ユーザー名"), gr.inputs.Textbox(label="Hugging Face Write Token") ], outputs=gr.outputs.Textbox(label="結果"), title="モデルの変換とデプロイ", description="セーフテンソルURL、モデル名、Hugging Face ユーザー名、およびHugging Face Write Tokenを入力して、モデルをfloat16に変換し、Hugging Faceにデプロイします。" ) # インターフェースの起動 iface.launch()