File size: 2,205 Bytes
3b42b0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# 必要なライブラリのインストール
# !pip install gradio huggingface_hub requests

import gradio as gr
import requests
import os
from huggingface_hub import HfApi, HfFolder

def download_and_upload(download_url, hf_write_token, model_name):
    # ファイル名を取得
    file_name = download_url.split("/")[-1]
    save_path = file_name

    # ファイルをダウンロード
    try:
        response = requests.get(download_url, stream=True)
        response.raise_for_status()
        with open(save_path, 'wb') as f:
            for chunk in response.iter_content(chunk_size=8192):
                f.write(chunk)
    except requests.exceptions.RequestException as e:
        return f"ファイルのダウンロード中にエラーが発生しました: {e}"
    
    # ファイルをHugging Faceにアップロード
    try:
        api = HfApi()
        HfFolder.save_token(hf_write_token)
        api.upload_file(
            path_or_fileobj=save_path,
            path_in_repo=file_name,  # ダウンロードしたファイル名をそのまま使用
            repo_id=model_name,
            repo_type="model"
        )
        return f"ファイルを {model_name} に正常にアップロードしました。"
    except Exception as e:
        return f"ファイルのアップロード中にエラーが発生しました: {e}"

# Gradioインターフェース
with gr.Blocks() as demo:
    gr.Markdown("#ファイルアップローダー")

    download_url = gr.Textbox(label="ダウンロードURL", placeholder="ファイルのダウンロードリンクを入力してください")
    hf_write_token = gr.Textbox(label="Hugging Face Write Token", placeholder="Hugging Faceの書き込みトークンを入力してください", type="password")
    model_name = gr.Textbox(label="モデル名", placeholder="モデル名を入力してください(例:ユーザー名/モデル名)")

    output = gr.Textbox(label="出力")

    upload_button = gr.Button("ダウンロードしてアップロード")

    upload_button.click(download_and_upload, inputs=[download_url, hf_write_token, model_name], outputs=output)

# アプリの実行
demo.launch()