AIDev07 commited on
Commit
064c454
Β·
verified Β·
1 Parent(s): 9a91666

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -61
app.py CHANGED
@@ -1,95 +1,85 @@
1
  import os
2
  import requests
3
  import gradio as gr
4
- from huggingface_hub import HfApi, hf_hub_download
5
  from pathlib import Path
6
 
7
- # Initialize HF API
8
- # Ensure you have set 'HF_TOKEN' in your Space Secrets
9
  HF_TOKEN = os.environ.get("HF_TOKEN")
10
  api = HfApi(token=HF_TOKEN)
11
 
12
  def get_repo_info(url):
13
  """Extracts repo_id and repo_type from a standard HF URL."""
14
- parts = url.split("huggingface.co/")[1].split("/")
15
- repo_type = parts[0] # 'datasets' or 'models'
16
- repo_id = f"{parts[1]}/{parts[2]}"
17
- return repo_id, repo_type.rstrip('s')
 
 
 
 
18
 
19
  def download_and_upload(dataset_url, download_url, progress=gr.Progress()):
20
  if not HF_TOKEN:
21
- return "❌ Error: HF_TOKEN not found in Secrets. Please add it to your Space settings."
22
 
 
 
 
 
 
 
 
23
  try:
24
- # 1. Parse Target Repo
25
- progress(0, desc="Parsing repository info...")
26
- repo_id, repo_type = get_repo_info(dataset_url)
27
-
28
- # 2. Prepare File Info
29
- filename = download_url.split("/")[-1].split("?")[0]
30
- local_path = Path(filename)
31
-
32
- # 3. Download with Progress
33
- progress(0.1, desc=f"Starting download: {filename}")
34
- with requests.get(download_url, stream=True) as r:
35
  r.raise_for_status()
36
  total_size = int(r.headers.get('content-length', 0))
37
  downloaded = 0
38
 
39
  with open(local_path, 'wb') as f:
40
- for chunk in r.iter_content(chunk_size=8192):
41
  if chunk:
42
  f.write(chunk)
43
  downloaded += len(chunk)
44
  if total_size > 0:
45
  done = downloaded / total_size
46
- progress(0.1 + (done * 0.4), desc=f"Downloading... {int(done*100)}%")
 
47
 
48
- # 4. Upload to Hugging Face
49
  progress(0.6, desc="Uploading to Hugging Face...")
50
  api.upload_file(
51
  path_or_fileobj=str(local_path),
52
  path_in_repo=filename,
53
  repo_id=repo_id,
54
  repo_type=repo_type,
55
- commit_message=f"Uploaded {filename} via UpDownUrl"
 
 
56
  )
57
 
58
- # 5. Cleanup
59
- if local_path.exists():
60
- os.remove(local_path)
61
-
62
- return f"βœ… Success! File '{filename}' uploaded to {repo_id}"
63
 
64
  except Exception as e:
65
  return f"❌ Error: {str(e)}"
 
 
 
66
 
67
- # --- Professional UI Design ---
68
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
69
- gr.Markdown(
70
- """
71
- # πŸš€ UpDownUrl (v1.0)
72
- **Transfer files directly from the web to your Hugging Face Repositories.**
73
- """
74
- )
75
 
76
  with gr.Row():
77
- with gr.Column(scale=2):
78
- dataset_link = gr.Textbox(
79
- label="Target Dataset/Repo Link",
80
- placeholder="https://huggingface.co/datasets/USER/REPO",
81
- info="The URL of the repo where the file will be saved."
82
- )
83
- download_link = gr.Textbox(
84
- label="File Download URL",
85
- placeholder="https://.../model.gguf",
86
- info="Direct link to the file you want to download."
87
- )
88
- upload_btn = gr.Button("Start Transfer", variant="primary")
89
-
90
- with gr.Column(scale=1):
91
- gr.Markdown("### Status & Logs")
92
- output_log = gr.Code(label="Console Output", interactive=False)
93
 
94
  upload_btn.click(
95
  fn=download_and_upload,
@@ -97,13 +87,6 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
97
  outputs=output_log
98
  )
99
 
100
- gr.Markdown(
101
- """
102
- ---
103
- **Note:** This Space uses your `HF_TOKEN`. Larger files (up to 15GB) are supported depending on the Space's disk quota.
104
- If it crashes, consider using a 'Large' hardware tier.
105
- """
106
- )
107
-
108
  if __name__ == "__main__":
109
- demo.launch()
 
1
  import os
2
  import requests
3
  import gradio as gr
4
+ from huggingface_hub import HfApi
5
  from pathlib import Path
6
 
7
+ # Get token from Space Secrets
 
8
  HF_TOKEN = os.environ.get("HF_TOKEN")
9
  api = HfApi(token=HF_TOKEN)
10
 
11
  def get_repo_info(url):
12
  """Extracts repo_id and repo_type from a standard HF URL."""
13
+ try:
14
+ parts = url.strip().split("huggingface.co/")[1].split("/")
15
+ if parts[0] == "datasets":
16
+ return f"{parts[1]}/{parts[2]}", "dataset"
17
+ else:
18
+ return f"{parts[0]}/{parts[1]}", "model"
19
+ except:
20
+ return None, None
21
 
22
  def download_and_upload(dataset_url, download_url, progress=gr.Progress()):
23
  if not HF_TOKEN:
24
+ return "❌ Error: HF_TOKEN not found in Secrets."
25
 
26
+ repo_id, repo_type = get_repo_info(dataset_url)
27
+ if not repo_id:
28
+ return "❌ Error: Invalid Dataset/Repo URL format."
29
+
30
+ filename = download_url.split("/")[-1].split("?")[0]
31
+ local_path = Path(filename)
32
+
33
  try:
34
+ # Step 1: Download with status updates
35
+ progress(0, desc="Initializing stream...")
36
+ with requests.get(download_url, stream=True, timeout=60) as r:
 
 
 
 
 
 
 
 
37
  r.raise_for_status()
38
  total_size = int(r.headers.get('content-length', 0))
39
  downloaded = 0
40
 
41
  with open(local_path, 'wb') as f:
42
+ for chunk in r.iter_content(chunk_size=1024*1024): # 1MB chunks
43
  if chunk:
44
  f.write(chunk)
45
  downloaded += len(chunk)
46
  if total_size > 0:
47
  done = downloaded / total_size
48
+ # Update every few MBs to keep the connection alive
49
+ progress(done * 0.5, desc=f"Downloading: {downloaded/(1024**3):.2f}GB / {total_size/(1024**3):.2f}GB")
50
 
51
+ # Step 2: Upload to HF
52
  progress(0.6, desc="Uploading to Hugging Face...")
53
  api.upload_file(
54
  path_or_fileobj=str(local_path),
55
  path_in_repo=filename,
56
  repo_id=repo_id,
57
  repo_type=repo_type,
58
+ commit_message=f"Uploaded {filename} via UpDownUrl",
59
+ # This is key for large files:
60
+ run_as_future=False
61
  )
62
 
63
+ return f"βœ… Done! '{filename}' is now in {repo_id}"
 
 
 
 
64
 
65
  except Exception as e:
66
  return f"❌ Error: {str(e)}"
67
+ finally:
68
+ if local_path.exists():
69
+ os.remove(local_path)
70
 
71
+ # --- UI Setup ---
72
+ with gr.Blocks() as demo:
73
+ gr.Markdown("# πŸš€ UpDownUrl v1.1")
 
 
 
 
 
74
 
75
  with gr.Row():
76
+ with gr.Column():
77
+ dataset_link = gr.Textbox(label="Target Repo Link", placeholder="https://huggingface.co/datasets/AIDev07/AIModelsLoaded")
78
+ download_link = gr.Textbox(label="Download URL", placeholder="Direct link to file")
79
+ upload_btn = gr.Button("Boom! Upload", variant="primary")
80
+
81
+ with gr.Column():
82
+ output_log = gr.Textbox(label="Status", interactive=False)
 
 
 
 
 
 
 
 
 
83
 
84
  upload_btn.click(
85
  fn=download_and_upload,
 
87
  outputs=output_log
88
  )
89
 
90
+ # Fixed Gradio 6.0 syntax: Theme goes here
 
 
 
 
 
 
 
91
  if __name__ == "__main__":
92
+ demo.launch(theme=gr.themes.Soft())