Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -5,33 +5,79 @@ if root_dir not in sys.path:
|
|
5 |
sys.path.append(root_dir)
|
6 |
|
7 |
import gradio as gr
|
8 |
-
import os
|
9 |
-
import requests
|
10 |
|
11 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
|
14 |
# Build demo
|
15 |
with gr.Blocks(title="SaprotHub", fill_width=True) as demo:
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
#
|
20 |
-
gr.
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
# url = "https://www.baidu.com"
|
26 |
-
response = requests.get(url)
|
27 |
-
# print(response.text)
|
28 |
-
# gr.HTML(response.text)
|
29 |
-
# iframe = f'<iframe srcdoc="{response.text}" width="100%"></iframe>'
|
30 |
-
iframe = f'<iframe src="{url}" width="100%" height="800"></iframe>'
|
31 |
-
# HTML = gr.HTML(open("/root/PycharmProjects/SaprotHub/hub_demo/test.html").read())
|
32 |
-
HTML = gr.HTML(response.text)
|
33 |
|
34 |
|
35 |
if __name__ == '__main__':
|
36 |
# Run the demo
|
37 |
-
demo.launch(server_name="0.0.0.0")
|
|
|
5 |
sys.path.append(root_dir)
|
6 |
|
7 |
import gradio as gr
|
|
|
|
|
8 |
|
9 |
+
from utils import set_text_bg_color
|
10 |
+
from loop_retrieve_cards import get_models, get_datasets, get_readme_dict
|
11 |
+
|
12 |
+
|
13 |
+
def match_card(input: str, card_id: str, card_type: str) -> str:
|
14 |
+
"""
|
15 |
+
Search the input in a card. If the input string is contained in the card_id or its README, display this card.
|
16 |
+
|
17 |
+
Args:
|
18 |
+
input: Input string
|
19 |
+
card_id: HuggingFace card id
|
20 |
+
card_type: Type of card, either "model" or "dataset"
|
21 |
+
"""
|
22 |
+
display_str = ""
|
23 |
+
readme_dict = get_readme_dict()
|
24 |
+
|
25 |
+
if input.lower() in card_id.lower() or input.lower() in readme_dict[card_id].lower():
|
26 |
+
# Add card id
|
27 |
+
if card_type == "model":
|
28 |
+
display_str += f"## [{set_text_bg_color(input, card_id)}](https://huggingface.co/{card_id})\n\n"
|
29 |
+
else:
|
30 |
+
display_str += f"## [{set_text_bg_color(input, card_id)}](https://huggingface.co/datasets/{card_id})\n\n"
|
31 |
+
|
32 |
+
# Highlight lines that contain the input string
|
33 |
+
show_lines = []
|
34 |
+
for line in readme_dict[card_id].split("\n"):
|
35 |
+
if input.lower() in line.lower() and "<!--" not in line:
|
36 |
+
show_lines.append(set_text_bg_color(input, line))
|
37 |
+
|
38 |
+
# Add README
|
39 |
+
display_str += "\n\n".join(show_lines)
|
40 |
+
|
41 |
+
# Add a separator
|
42 |
+
display_str = f"\n\n{display_str}\n\n---\n\n"
|
43 |
+
|
44 |
+
# In case that the keyword is only contained in comments
|
45 |
+
if input.lower() not in card_id.lower() and len(show_lines) == 0:
|
46 |
+
display_str = ""
|
47 |
+
|
48 |
+
return display_str
|
49 |
+
|
50 |
+
|
51 |
+
def show_card_info(input: str):
|
52 |
+
retrieval_str = ""
|
53 |
+
|
54 |
+
if input != "":
|
55 |
+
# Search models
|
56 |
+
retrieval_str += "# Models\n\n"
|
57 |
+
for model in get_models():
|
58 |
+
retrieval_str += match_card(input, model, "model")
|
59 |
+
|
60 |
+
# Search datasets
|
61 |
+
retrieval_str += "# Datasets\n\n"
|
62 |
+
for dataset in get_datasets():
|
63 |
+
retrieval_str += match_card(input, dataset, "dataset")
|
64 |
+
|
65 |
+
return gr.Markdown(retrieval_str, visible=True)
|
66 |
|
67 |
|
68 |
# Build demo
|
69 |
with gr.Blocks(title="SaprotHub", fill_width=True) as demo:
|
70 |
+
gr.Label("SaprotHub search", visible=True, show_label=False)
|
71 |
+
search_box = gr.Textbox(label="Search box", placeholder="Input keywords to search", interactive=True, scale=0, container=True)
|
72 |
+
|
73 |
+
# Display search results
|
74 |
+
search_hint = gr.Markdown("# Search results:", visible=True)
|
75 |
+
items = gr.Markdown(visible=False)
|
76 |
+
|
77 |
+
# Set events
|
78 |
+
search_box.change(show_card_info, inputs=[search_box], outputs=[items])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
|
80 |
|
81 |
if __name__ == '__main__':
|
82 |
# Run the demo
|
83 |
+
demo.launch(server_name="0.0.0.0")
|