PlayerBPlaytime commited on
Commit
2e927f4
·
verified ·
1 Parent(s): 55df4f5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -19
app.py CHANGED
@@ -3,39 +3,46 @@ import gradio as gr
3
 
4
  HF_API_BASE = "https://huggingface.co/api/models"
5
  SEARCH_TERMS = [
6
- "MJ", "MichaelJackson", "Michael_Jackson", "Michael-Jackson", "michaeljackson",
7
- "michael_jackson", "MJackson", "M_Jackson", "m-jackson", "mjackson",
8
- "KingOfPop", "King_Of_Pop", "King-Of-Pop", "kingofpop"
9
  ]
10
 
11
  def get_models(user: str):
12
  url = f"{HF_API_BASE}?search={user}"
13
- response = requests.get(url)
14
- if response.status_code == 200:
15
- return response.json()
16
- else:
 
 
 
17
  return []
18
 
19
  def check_zip_files(model_id: str):
20
- url = f"https://huggingface.co/{model_id}/tree/main"
21
- response = requests.get(url)
22
  zip_links = []
23
-
24
- if response.status_code == 200:
25
- files = response.json()
26
- for file in files:
27
- if file['type'] == 'file' and file['path'].endswith('.zip'):
28
- for term in SEARCH_TERMS:
29
- if term.lower() in file['path'].lower():
 
 
30
  zip_links.append(f"https://huggingface.co/{model_id}/resolve/main/{file['path']}?download=true")
 
 
31
  return zip_links
32
 
33
  def search_huggingface_zips(user: str):
34
  models = get_models(user)
35
  all_links = []
36
  for model in models:
37
- model_id = model['modelId']
38
- all_links.extend(check_zip_files(model_id))
 
39
  return "\n".join(all_links) if all_links else "No ZIP files found."
40
 
41
  demo = gr.Interface(
@@ -46,4 +53,4 @@ demo = gr.Interface(
46
  description="Enter a Hugging Face username to search for ZIP files related to Michael Jackson.",
47
  )
48
 
49
- demo.launch()
 
3
 
4
  HF_API_BASE = "https://huggingface.co/api/models"
5
  SEARCH_TERMS = [
6
+ "mj", "michaeljackson", "michael_jackson", "michael-jackson", "mjackson",
7
+ "m_jackson", "m-jackson", "kingofpop", "king_of_pop", "king-of-pop"
 
8
  ]
9
 
10
  def get_models(user: str):
11
  url = f"{HF_API_BASE}?search={user}"
12
+ headers = {"User-Agent": "Mozilla/5.0"}
13
+ try:
14
+ response = requests.get(url, headers=headers)
15
+ response.raise_for_status()
16
+ return response.json() if response.text else []
17
+ except requests.exceptions.RequestException as e:
18
+ print(f"Error fetching models: {e}")
19
  return []
20
 
21
  def check_zip_files(model_id: str):
22
+ url = f"https://huggingface.co/api/models/{model_id}/tree/main"
23
+ headers = {"User-Agent": "Mozilla/5.0"}
24
  zip_links = []
25
+ try:
26
+ response = requests.get(url, headers=headers)
27
+ response.raise_for_status()
28
+ if response.text:
29
+ files = response.json()
30
+ for file in files:
31
+ file_path = file.get('path', '').lower()
32
+ if file.get('type') == 'file' and file_path.endswith('.zip'):
33
+ if any(term in file_path or term in model_id.lower() for term in SEARCH_TERMS):
34
  zip_links.append(f"https://huggingface.co/{model_id}/resolve/main/{file['path']}?download=true")
35
+ except requests.exceptions.RequestException as e:
36
+ print(f"Error accessing model {model_id}: {e}")
37
  return zip_links
38
 
39
  def search_huggingface_zips(user: str):
40
  models = get_models(user)
41
  all_links = []
42
  for model in models:
43
+ model_id = model.get('modelId', '')
44
+ if model_id and any(term in model_id.lower() for term in SEARCH_TERMS):
45
+ all_links.extend(check_zip_files(model_id))
46
  return "\n".join(all_links) if all_links else "No ZIP files found."
47
 
48
  demo = gr.Interface(
 
53
  description="Enter a Hugging Face username to search for ZIP files related to Michael Jackson.",
54
  )
55
 
56
+ demo.launch()