|
|
|
|
|
|
|
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"An error occurred while downloading the file: {e}" |
|
|
|
|
|
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"File successfully uploaded to {model_name}." |
|
except Exception as e: |
|
return f"An error occurred while uploading the file: {e}" |
|
|
|
|
|
with gr.Blocks() as demo: |
|
gr.Markdown("# File Uploader") |
|
|
|
download_url = gr.Textbox(label="Download URL", placeholder="Enter the file download link") |
|
hf_write_token = gr.Textbox(label="Hugging Face Write Token", placeholder="Enter your Hugging Face write token", type="password") |
|
model_name = gr.Textbox(label="Model Name", placeholder="Enter the model name (e.g., username/model_name)") |
|
|
|
output = gr.Textbox(label="Output") |
|
|
|
upload_button = gr.Button("Download and Upload") |
|
|
|
upload_button.click(download_and_upload, inputs=[download_url, hf_write_token, model_name], outputs=output) |
|
|
|
|
|
demo.launch() |
|
|