Niansuh commited on
Commit
1298ab7
1 Parent(s): 4e49059

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -0
app.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Install necessary libraries
2
+ # !pip install gradio huggingface_hub requests
3
+
4
+ import gradio as gr
5
+ import requests
6
+ import os
7
+ from huggingface_hub import HfApi, HfFolder
8
+
9
+ def download_and_upload(download_url, hf_write_token, model_name):
10
+ # Get the file name
11
+ file_name = download_url.split("/")[-1]
12
+ save_path = file_name
13
+
14
+ # Download the file
15
+ try:
16
+ response = requests.get(download_url, stream=True)
17
+ response.raise_for_status()
18
+ with open(save_path, 'wb') as f:
19
+ for chunk in response.iter_content(chunk_size=8192):
20
+ f.write(chunk)
21
+ except requests.exceptions.RequestException as e:
22
+ return f"An error occurred while downloading the file: {e}"
23
+
24
+ # Upload the file to Hugging Face
25
+ try:
26
+ api = HfApi()
27
+ HfFolder.save_token(hf_write_token)
28
+ api.upload_file(
29
+ path_or_fileobj=save_path,
30
+ path_in_repo=file_name, # Use the downloaded file name as is
31
+ repo_id=model_name,
32
+ repo_type="model"
33
+ )
34
+ return f"File successfully uploaded to {model_name}."
35
+ except Exception as e:
36
+ return f"An error occurred while uploading the file: {e}"
37
+
38
+ # Gradio Interface
39
+ with gr.Blocks() as demo:
40
+ gr.Markdown("# File Uploader")
41
+
42
+ download_url = gr.Textbox(label="Download URL", placeholder="Enter the file download link")
43
+ hf_write_token = gr.Textbox(label="Hugging Face Write Token", placeholder="Enter your Hugging Face write token", type="password")
44
+ model_name = gr.Textbox(label="Model Name", placeholder="Enter the model name (e.g., username/model_name)")
45
+
46
+ output = gr.Textbox(label="Output")
47
+
48
+ upload_button = gr.Button("Download and Upload")
49
+
50
+ upload_button.click(download_and_upload, inputs=[download_url, hf_write_token, model_name], outputs=output)
51
+
52
+ # Run the app
53
+ demo.launch()