AIDev07 commited on
Commit
9a91666
Β·
verified Β·
1 Parent(s): 5803d68

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +99 -27
app.py CHANGED
@@ -1,37 +1,109 @@
1
  import os
2
  import requests
3
- from huggingface_hub import HfApi
 
 
4
 
5
- # 1. Configuration
6
- token = os.getenv("HF_TOKEN")
7
- dest_repo = "AIDev07/AIModelsLoaded"
8
- filename = "starcoder2-15b-instruct-v0.1-Q4_K_M.gguf"
9
 
10
- # Manually defined clean URL to avoid f-string errors
11
- source_url = "https://huggingface.co"
12
-
13
- if not token:
14
- print("❌ ERROR: Set 'HF_TOKEN' in Space Secrets!")
15
- else:
16
- api = HfApi(token=token)
17
- print(f"πŸš€ Starting Transfer for: {filename}")
18
 
 
 
 
 
19
  try:
20
- # 2. Use the 'run_as_future' or standard upload with the stream
21
- with requests.get(source_url, stream=True) as r:
 
 
 
 
 
 
 
 
 
22
  r.raise_for_status()
 
 
23
 
24
- # 3. Upload using the raw stream
25
- api.upload_file(
26
- path_or_fileobj=r.raw,
27
- path_in_repo=filename,
28
- repo_id=dest_repo,
29
- repo_type="dataset",
30
- commit_message=f"Direct cloud transfer of {filename}"
31
- )
32
-
33
- print(f"βœ… DONE! File is now in {dest_repo}")
 
 
 
 
 
 
 
 
34
 
 
 
 
 
 
 
35
  except Exception as e:
36
- print(f"❌ Transfer Failed: {e}")
37
- print("Tip: If it says 'Connection Reset', the file is too big for a basic stream.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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,
96
+ inputs=[dataset_link, download_link],
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()