Sam McLeod commited on
Commit
3642047
·
1 Parent(s): 4f40f68

fix: update packages

Browse files
Files changed (3) hide show
  1. README.md +1 -1
  2. app.py +40 -29
  3. requirements.txt +4 -3
README.md CHANGED
@@ -4,7 +4,7 @@ emoji: 🐐
4
  colorFrom: gray
5
  colorTo: green
6
  sdk: gradio
7
- sdk_version: 4.44.1
8
  hf_oauth: true
9
  hf_oauth_scopes:
10
  - read-repos
 
4
  colorFrom: gray
5
  colorTo: green
6
  sdk: gradio
7
+ sdk_version: 5.50.0
8
  hf_oauth: true
9
  hf_oauth_scopes:
10
  - read-repos
app.py CHANGED
@@ -1,13 +1,17 @@
1
  import os
 
2
  import tempfile
 
3
 
4
  os.environ["HF_HUB_CACHE"] = "cache"
 
5
  os.environ["GRADIO_ANALYTICS_ENABLED"] = "False"
6
  import gradio as gr
7
 
8
  from huggingface_hub import HfApi
9
  from huggingface_hub import whoami
10
  from huggingface_hub import ModelCard
 
11
  from huggingface_hub import scan_cache_dir
12
  from huggingface_hub import logging
13
 
@@ -32,15 +36,6 @@ QUANT_PARAMS = {
32
  "Q8": 8,
33
  }
34
 
35
- def list_files_in_folder(folder_path):
36
- # List all files and directories in the specified folder
37
- all_items = os.listdir(folder_path)
38
-
39
- # Filter out only files
40
- files = [item for item in all_items if os.path.isfile(os.path.join(folder_path, item))]
41
-
42
- return files
43
-
44
  def clear_hf_cache_space():
45
  scan = scan_cache_dir()
46
  to_delete = []
@@ -89,29 +84,42 @@ def upload_to_hub(path, upload_repo, hf_path, oauth_token):
89
 
90
  api = HfApi(token=oauth_token.token)
91
  api.create_repo(repo_id=upload_repo, exist_ok=True)
 
92
 
93
- files = list_files_in_folder(path)
94
- print(files)
95
- for file in files:
96
- file_path = os.path.join(path, file)
97
- print(f"Uploading file: {file_path}")
98
- api.upload_file(
99
- path_or_fileobj=file_path,
100
- path_in_repo=file,
101
- repo_id=upload_repo,
102
- )
103
-
104
- print(f"Upload successful, go to https://huggingface.co/{upload_repo} for details.")
105
 
106
  def process_model(model_id, q_method, oauth_token: gr.OAuthToken | None):
107
- if oauth_token.token is None:
108
  raise ValueError("You must be logged in to use MLX-my-repo")
109
-
 
 
 
110
  model_name = model_id.split('/')[-1]
111
  username = whoami(oauth_token.token)["name"]
112
  try:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
113
  # Fail early if the tokenizer requires custom code
114
- _ = AutoTokenizer.from_pretrained(model_id, trust_remote_code=False)
115
 
116
  if q_method == "FP16":
117
  upload_repo = f"{username}/{model_name}-mlx-fp16"
@@ -137,17 +145,20 @@ def process_model(model_id, q_method, oauth_token: gr.OAuthToken | None):
137
  "llama.png",
138
  )
139
  except Exception as e:
140
- return (f"Error: {e}", "error.png")
 
 
 
141
  finally:
142
  clear_hf_cache_space()
143
  print("Folder cleaned up successfully!")
144
 
145
- css="""/* Custom CSS to allow scrolling */
146
  .gradio-container {overflow-y: auto;}
147
  """
148
 
149
  # Create Gradio interface
150
- with gr.Blocks(css=css) as demo:
151
  gr.Markdown("You must be logged in to use MLX-my-repo.")
152
  gr.LoginButton(min_width=250)
153
 
@@ -165,7 +176,7 @@ with gr.Blocks(css=css) as demo:
165
  filterable=False,
166
  visible=True
167
  )
168
-
169
  iface = gr.Interface(
170
  fn=process_model,
171
  inputs=[
@@ -182,7 +193,7 @@ with gr.Blocks(css=css) as demo:
182
  )
183
 
184
  def restart_space():
185
- HfApi().restart_space(repo_id="mlx-community/mlx-my-repo", token=HF_TOKEN, factory_reboot=True)
186
 
187
  scheduler = BackgroundScheduler()
188
  scheduler.add_job(restart_space, "interval", seconds=21600)
 
1
  import os
2
+ import shutil
3
  import tempfile
4
+ import traceback
5
 
6
  os.environ["HF_HUB_CACHE"] = "cache"
7
+ os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "1"
8
  os.environ["GRADIO_ANALYTICS_ENABLED"] = "False"
9
  import gradio as gr
10
 
11
  from huggingface_hub import HfApi
12
  from huggingface_hub import whoami
13
  from huggingface_hub import ModelCard
14
+ from huggingface_hub import model_info
15
  from huggingface_hub import scan_cache_dir
16
  from huggingface_hub import logging
17
 
 
36
  "Q8": 8,
37
  }
38
 
 
 
 
 
 
 
 
 
 
39
  def clear_hf_cache_space():
40
  scan = scan_cache_dir()
41
  to_delete = []
 
84
 
85
  api = HfApi(token=oauth_token.token)
86
  api.create_repo(repo_id=upload_repo, exist_ok=True)
87
+ api.upload_folder(folder_path=path, repo_id=upload_repo)
88
 
89
+ print(f"Upload successful, go to https://huggingface.co/{upload_repo} for details.")
 
 
 
 
 
 
 
 
 
 
 
90
 
91
  def process_model(model_id, q_method, oauth_token: gr.OAuthToken | None):
92
+ if oauth_token is None or oauth_token.token is None:
93
  raise ValueError("You must be logged in to use MLX-my-repo")
94
+
95
+ # Set token for mlx_lm.convert which doesn't accept a token parameter
96
+ os.environ["HF_TOKEN"] = oauth_token.token
97
+
98
  model_name = model_id.split('/')[-1]
99
  username = whoami(oauth_token.token)["name"]
100
  try:
101
+ # Check model size vs available disk (need ~2x for download + conversion)
102
+ info = model_info(model_id, token=oauth_token.token)
103
+ if info.safetensors and info.safetensors.total:
104
+ model_bytes = info.safetensors.total
105
+ elif info.siblings:
106
+ model_bytes = sum(s.size or 0 for s in info.siblings)
107
+ else:
108
+ model_bytes = 0
109
+ if model_bytes > 0:
110
+ free_bytes = shutil.disk_usage(".").free
111
+ required_bytes = model_bytes * 2
112
+ if free_bytes < required_bytes:
113
+ model_gb = model_bytes / (1024**3)
114
+ free_gb = free_bytes / (1024**3)
115
+ raise ValueError(
116
+ f"Not enough disk space. Model is ~{model_gb:.1f}GB, "
117
+ f"need ~{model_gb * 2:.1f}GB for conversion, "
118
+ f"but only {free_gb:.1f}GB available."
119
+ )
120
+
121
  # Fail early if the tokenizer requires custom code
122
+ _ = AutoTokenizer.from_pretrained(model_id, token=oauth_token.token, trust_remote_code=False)
123
 
124
  if q_method == "FP16":
125
  upload_repo = f"{username}/{model_name}-mlx-fp16"
 
145
  "llama.png",
146
  )
147
  except Exception as e:
148
+ traceback.print_exc()
149
+ error_type = type(e).__name__
150
+ error_msg = str(e) or "No details available"
151
+ return (f"**{error_type}**: {error_msg}", "error.png")
152
  finally:
153
  clear_hf_cache_space()
154
  print("Folder cleaned up successfully!")
155
 
156
+ CSS = """/* Custom CSS to allow scrolling */
157
  .gradio-container {overflow-y: auto;}
158
  """
159
 
160
  # Create Gradio interface
161
+ with gr.Blocks(css=CSS) as demo:
162
  gr.Markdown("You must be logged in to use MLX-my-repo.")
163
  gr.LoginButton(min_width=250)
164
 
 
176
  filterable=False,
177
  visible=True
178
  )
179
+
180
  iface = gr.Interface(
181
  fn=process_model,
182
  inputs=[
 
193
  )
194
 
195
  def restart_space():
196
+ HfApi().restart_space(repo_id="smcleod/mlx-my-repo", token=HF_TOKEN, factory_reboot=True)
197
 
198
  scheduler = BackgroundScheduler()
199
  scheduler.add_job(restart_space, "interval", seconds=21600)
requirements.txt CHANGED
@@ -1,6 +1,7 @@
1
  hf-transfer
2
- gradio[oauth]>=4.28.0
3
- gradio_huggingfacehub_search==0.0.7
 
4
  APScheduler
5
  mlx[cpu]
6
- mlx-lm==0.29.1
 
1
  hf-transfer
2
+ audioop-lts
3
+ gradio[oauth]>=5.50.0,<6.0.0
4
+ gradio_huggingfacehub_search>=0.0.12
5
  APScheduler
6
  mlx[cpu]
7
+ mlx-lm>=0.31.2