uploader / app.py
Sakalti's picture
Create app.py
3b42b0c verified
raw
history blame contribute delete
No virus
2.21 kB
# 必要なライブラリのインストール
# !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()