awacke1 commited on
Commit
62e25b3
1 Parent(s): 376619b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -11
app.py CHANGED
@@ -17,14 +17,42 @@ def search_hub(query: str, search_type: str) -> pd.DataFrame:
17
  data = [{"id": space.id, "author": space.author, "link": f"https://huggingface.co/spaces/{space.id}"} for space in results]
18
  else:
19
  data = []
 
 
 
 
 
 
20
  return pd.DataFrame(data)
21
 
22
- def open_url(evt: gr.SelectData, df: pd.DataFrame):
23
- if df is not None and not df.empty and evt.index[0] < len(df):
24
- url = df.iloc[evt.index[0]]['link']
25
- return f'<iframe src="{url}" width="100%" height="600px"></iframe>'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  else:
27
- return ""
28
 
29
  def load_metadata(evt: gr.SelectData, df: pd.DataFrame, search_type: str):
30
  if df is not None and not df.empty and evt.index[0] < len(df):
@@ -84,18 +112,16 @@ with gr.Blocks() as demo:
84
  search_query = gr.Textbox(label="Search Query", value="awacke1")
85
  search_type = gr.Radio(["Models", "Datasets", "Spaces"], label="Search Type", value="Models")
86
  search_button = gr.Button("Search")
87
- results_df = gr.DataFrame(label="Search Results", wrap=True, interactive=True)
88
- web_view = gr.HTML(label="Web View")
89
  metadata_output = gr.Textbox(label="Metadata", lines=10)
90
  aggregated_output = gr.JSON(label="Aggregated Content")
91
 
92
  def search_and_aggregate(query, search_type):
93
  df = search_hub(query, search_type)
94
  aggregated = SwarmyTime(df.to_dict('records'))
95
- return df, aggregated
 
96
 
97
- search_button.click(search_and_aggregate, inputs=[search_query, search_type], outputs=[results_df, aggregated_output])
98
- results_df.select(open_url, inputs=[results_df], outputs=[web_view])
99
- results_df.select(load_metadata, inputs=[results_df, search_type], outputs=[metadata_output])
100
 
101
  demo.launch(debug=True)
 
17
  data = [{"id": space.id, "author": space.author, "link": f"https://huggingface.co/spaces/{space.id}"} for space in results]
18
  else:
19
  data = []
20
+
21
+ # Add numbering and format the link
22
+ for i, item in enumerate(data, 1):
23
+ item['number'] = i
24
+ item['formatted_link'] = format_link(item, i, search_type)
25
+
26
  return pd.DataFrame(data)
27
 
28
+ def format_link(item: Dict, number: int, search_type: str) -> str:
29
+ link = item['link']
30
+ readme_link = f"{link}/blob/main/README.md"
31
+ title = f"{number}. {item['id']}"
32
+
33
+ metadata = f"Author: {item['author']}"
34
+ if 'downloads' in item:
35
+ metadata += f", Downloads: {item['downloads']}"
36
+
37
+ html = f"""
38
+ <div style="margin-bottom: 10px;">
39
+ <strong>{title}</strong><br>
40
+ <a href="{link}" target="_blank" style="color: #4a90e2; text-decoration: none;">View {search_type[:-1]}</a> |
41
+ <a href="{readme_link}" target="_blank" style="color: #4a90e2; text-decoration: none;">View README</a><br>
42
+ <small>{metadata}</small>
43
+ </div>
44
+ """
45
+ return html
46
+
47
+ def display_results(df: pd.DataFrame):
48
+ if df is not None and not df.empty:
49
+ html = "<div style='max-height: 400px; overflow-y: auto;'>"
50
+ for _, row in df.iterrows():
51
+ html += row['formatted_link']
52
+ html += "</div>"
53
+ return html
54
  else:
55
+ return "<p>No results found.</p>"
56
 
57
  def load_metadata(evt: gr.SelectData, df: pd.DataFrame, search_type: str):
58
  if df is not None and not df.empty and evt.index[0] < len(df):
 
112
  search_query = gr.Textbox(label="Search Query", value="awacke1")
113
  search_type = gr.Radio(["Models", "Datasets", "Spaces"], label="Search Type", value="Models")
114
  search_button = gr.Button("Search")
115
+ results_html = gr.HTML(label="Search Results")
 
116
  metadata_output = gr.Textbox(label="Metadata", lines=10)
117
  aggregated_output = gr.JSON(label="Aggregated Content")
118
 
119
  def search_and_aggregate(query, search_type):
120
  df = search_hub(query, search_type)
121
  aggregated = SwarmyTime(df.to_dict('records'))
122
+ html_results = display_results(df)
123
+ return html_results, aggregated
124
 
125
+ search_button.click(search_and_aggregate, inputs=[search_query, search_type], outputs=[results_html, aggregated_output])
 
 
126
 
127
  demo.launch(debug=True)