Spaces:
Running
Running
File size: 2,546 Bytes
2c2a43f b4e040e 2c2a43f f060548 2c2a43f 54af0d0 b4e040e 54af0d0 2c2a43f 7d8e922 b4e040e 7d8e922 2c2a43f 54af0d0 f060548 54af0d0 2c2a43f 64807d8 f060548 64807d8 2c2a43f 64807d8 2c2a43f f060548 2c2a43f |
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 |
import gradio as gr
import requests
import torch
from transformers import AutoModel
from huggingface_hub import HfApi
def convert_and_deploy(url, repo_id, 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)
# モデルを読み込み
try:
# セーフテンソルファイルからモデルの状態を読み込み
state_dict = torch.load(file_path)
# モデルを初期化
model = AutoModel.from_pretrained("path_to_model", torch_dtype=torch.float16, token=hf_token)
# モデルの状態を設定
model.load_state_dict(state_dict)
except Exception as e:
return f"モデルの読み込みに失敗しました。エラー: {e}"
# モデルをfloat16形式で保存
try:
model_name = repo_id.split('/')[-1] # モデル名を取得
model.save_pretrained(f"{model_name}_float16", torch_dtype=torch.float16)
except Exception as e:
return f"モデルの保存に失敗しました。エラー: {e}"
# モデルをHugging Faceにデプロイ
api = HfApi()
try:
api.upload_folder(
folder_path=f"{model_name}_float16",
repo_id=repo_id,
token=hf_token,
path_in_repo=f"{model_name}_float16",
create_remote_repo=True
)
except Exception as e:
return f"モデルのデプロイに失敗しました。エラー: {e}"
return "モデルをfloat16に変換し、Hugging Faceにデプロイしました。"
# Gradioインターフェースの作成
iface = gr.Interface(
fn=convert_and_deploy,
inputs=[
gr.Text(label="セーフテンソルURL"),
gr.Text(label="Hugging Face リポジトリID (ユーザー名/モデル名)"),
gr.Text(label="Hugging Face Write Token")
],
outputs=gr.Text(label="結果"),
title="モデルの変換とデプロイ",
description="セーフテンソルURL、Hugging Face リポジトリID (ユーザー名/モデル名)、およびHugging Face Write Tokenを入力して、モデルをfloat16に変換し、Hugging Faceにデプロイします。"
)
# インターフェースの起動
iface.launch() |