Snaseem2026 commited on
Commit
15fc4c3
·
verified ·
1 Parent(s): 3e0f5c8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -56
app.py CHANGED
@@ -1,15 +1,13 @@
1
  import gradio as gr
2
  from huggingface_hub import list_models
3
- from reportlab.lib.pagesizes import letter
4
- from reportlab.pdfgen import canvas
5
  import tempfile
6
- import os
7
 
8
  def get_models(username):
9
  if not username.strip():
10
- result = [["-", "Please enter a valid username or organization.", "-", "-"]]
11
- pdf_path = create_pdf(result)
12
- return result, pdf_path
13
 
14
  try:
15
  models = list_models(author=username.strip())
@@ -33,70 +31,45 @@ def get_models(username):
33
  model_url
34
  ])
35
  if results:
36
- pdf_path = create_pdf(results)
37
- table_results = [row[:4] for row in results]
38
- return table_results, pdf_path
39
  else:
40
- result = [["-", f"No models found for '{username}'.", "-", "-"]]
41
- pdf_path = create_pdf(result)
42
- return result, pdf_path
43
  except Exception as e:
44
- result = [["-", f"Error: {str(e)}", "-", "-"]]
45
- pdf_path = create_pdf(result)
46
- return result, pdf_path
47
 
48
- def create_pdf(data_rows):
49
- # Create a temporary file for PDF
50
- tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf")
51
- p = canvas.Canvas(tmp.name, pagesize=letter)
52
- width, height = letter
53
- x_margin = 50
54
- y_margin = 30
55
- max_rows_per_page = 40
56
- line_height = 20
57
- p.setFont("Helvetica-Bold", 16)
58
- p.drawString(x_margin, height - y_margin, "Hugging Face Model Explorer Results")
59
- p.setFont("Helvetica", 11)
60
- y = height - y_margin - 30
61
  headers = ["Model Name", "Description", "Downloads", "Tags", "URL"]
62
- p.setFont("Helvetica-Bold", 12)
63
- p.drawString(x_margin, y, " | ".join(headers))
64
- y -= line_height
65
- p.setFont("Helvetica", 10)
66
- row_count = 0
67
  for row in data_rows:
68
- printable = [str(cell).replace('\n', ' ') for cell in row]
69
- if len(printable) < 5:
70
- printable += [""] * (5 - len(printable))
71
- p.drawString(x_margin, y, " | ".join(printable))
72
- y -= line_height
73
- row_count += 1
74
- if row_count % max_rows_per_page == 0:
75
- p.showPage()
76
- y = height - y_margin - 30
77
- p.setFont("Helvetica-Bold", 12)
78
- p.drawString(x_margin, y, " | ".join(headers))
79
- y -= line_height
80
- p.setFont("Helvetica", 10)
81
- p.save()
82
  tmp.close()
83
- return tmp.name # Return path for gr.File or gr.Download
84
 
85
  with gr.Blocks() as demo:
86
- gr.Markdown("## 🤗 Hugging Face Model Explorer\nEnter a username/org to see all public models and download info as PDF!")
87
  username = gr.Textbox(label="HuggingFace Username or Organization", placeholder="e.g. Snaseem2026")
88
  table = gr.Dataframe(headers=["Name", "Description", "Downloads", "Tags"], interactive=False)
89
- download_pdf = gr.File(label="Download PDF") # gr.File for static files
90
 
91
- def search_and_pdf(user):
92
- df, pdf_path = get_models(user)
93
- return df, pdf_path
94
 
95
- search_btn = gr.Button("Search & Generate PDF")
96
  search_btn.click(
97
- search_and_pdf,
98
  inputs=username,
99
- outputs=[table, download_pdf]
100
  )
101
 
102
  if __name__ == "__main__":
 
1
  import gradio as gr
2
  from huggingface_hub import list_models
3
+ from openpyxl import Workbook
 
4
  import tempfile
 
5
 
6
  def get_models(username):
7
  if not username.strip():
8
+ result = [["-", "Please enter a valid username or organization.", "-", "-", "-"]]
9
+ xlsx_path = create_excel(result)
10
+ return [row[:-1] for row in result], xlsx_path
11
 
12
  try:
13
  models = list_models(author=username.strip())
 
31
  model_url
32
  ])
33
  if results:
34
+ xlsx_path = create_excel(results)
35
+ table_results = [row[:-1] for row in results] # Do not show URL column in Gradio table
36
+ return table_results, xlsx_path
37
  else:
38
+ result = [["-", f"No models found for '{username}'.", "-", "-", "-"]]
39
+ xlsx_path = create_excel(result)
40
+ return [row[:-1] for row in result], xlsx_path
41
  except Exception as e:
42
+ result = [["-", f"Error: {str(e)}", "-", "-", "-"]]
43
+ xlsx_path = create_excel(result)
44
+ return [row[:-1] for row in result], xlsx_path
45
 
46
+ def create_excel(data_rows):
 
 
 
 
 
 
 
 
 
 
 
 
47
  headers = ["Model Name", "Description", "Downloads", "Tags", "URL"]
48
+ wb = Workbook()
49
+ ws = wb.active
50
+ ws.append(headers)
 
 
51
  for row in data_rows:
52
+ ws.append(row)
53
+ tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".xlsx")
54
+ wb.save(tmp.name)
 
 
 
 
 
 
 
 
 
 
 
55
  tmp.close()
56
+ return tmp.name
57
 
58
  with gr.Blocks() as demo:
59
+ gr.Markdown("## 🤗 Hugging Face Model Explorer\nEnter a username/org to see all public models and download info as Excel!")
60
  username = gr.Textbox(label="HuggingFace Username or Organization", placeholder="e.g. Snaseem2026")
61
  table = gr.Dataframe(headers=["Name", "Description", "Downloads", "Tags"], interactive=False)
62
+ download_excel = gr.File(label="Download Excel") # Shows a download link/button
63
 
64
+ def search_and_excel(user):
65
+ df, excel_path = get_models(user)
66
+ return df, excel_path
67
 
68
+ search_btn = gr.Button("Search & Generate Excel")
69
  search_btn.click(
70
+ search_and_excel,
71
  inputs=username,
72
+ outputs=[table, download_excel]
73
  )
74
 
75
  if __name__ == "__main__":