Spaces:
Sleeping
Sleeping
from typing import List | |
import streamlit as st | |
import extra_streamlit_components as stx | |
from streamlit_searchbox import st_searchbox | |
from pathlib import Path | |
from streamlit_tags import st_tags | |
from src.tools import hf_pull_skill, time_ago | |
from src.skill_db import SkillsDB | |
from src.schema import Skill | |
st.title("Skill Library Hub") | |
skill_db = SkillsDB('skills.db') | |
all_skills = skill_db.get_skills() | |
all_tags = skill_db.get_tags() | |
if all_skills is None: | |
all_skills = [] | |
def search(searchterm: str) -> List[any]: | |
global all_skills | |
global skill_db | |
if all_skills is None: | |
if skill_db is None: | |
skill_db = SkillsDB('skills.db') | |
all_skills = skill_db.get_skills() | |
result = [] | |
if all_skills is not None and len(all_skills) > 0: | |
for skill in all_skills: | |
if searchterm.lower() in skill.skill_name.lower(): | |
result.append(skill.skill_name.lower()) | |
if searchterm.lower() in skill.author.lower(): | |
result.append(skill.author.lower()) | |
if len(result)>10: | |
return result | |
if len(result)<3: | |
for skill in all_skills: | |
if searchterm.lower() in skill.skill_description.lower(): | |
result.append(skill.skill_description.lower()) | |
if len(result)>10: | |
return result | |
return result | |
return result | |
program_languages = ["python", "r", "julia", "javascript", "html", "shell", "applescript"] | |
def main_page(): | |
global all_skills | |
global skill_db | |
if all_skills is None: | |
if skill_db is None: | |
skill_db = SkillsDB('skills.db') | |
all_skills = skill_db.get_skills() | |
selected_languages = [] | |
selected_tags = [] | |
with st.sidebar: | |
st.title("π Search Skills") | |
with st.expander("β¨ PROGRAM", True): | |
if st.checkbox("Python"): | |
selected_languages.append("python") | |
if st.checkbox("Julia"): | |
selected_languages.append("julia") | |
if st.checkbox("R"): | |
selected_languages.append("r") | |
if st.checkbox("Javascript"): | |
selected_languages.append("javascript") | |
if st.checkbox("Html"): | |
selected_languages.append("html") | |
if st.checkbox("Shell"): | |
selected_languages.append("shell") | |
if st.checkbox("AppleScript"): | |
selected_languages.append("applescript") | |
with st.expander("π TAGS", True): | |
if all_tags is not None: | |
for tag in all_tags: | |
if st.checkbox(tag): | |
selected_tags.append(tag) | |
if not selected_languages: | |
selected_languages = program_languages | |
all_skills = [item for item in all_skills if item.skill_program_language in selected_languages] | |
if not selected_tags and all_tags: | |
selected_tags = all_tags | |
all_skills = [item for item in all_skills if set(item.skill_tags) & set(selected_tags)] | |
selected_value = st_searchbox( | |
search, | |
key="", | |
) | |
print(f"selected_value: {selected_value}") | |
selected_skills = all_skills | |
if selected_value is None and len(selected_skills)!= len(all_skills): | |
selected_skills = all_skills | |
if selected_value is not None: | |
all_skills = [item for item in all_skills if selected_value.lower() in item.skill_name.lower() or | |
selected_value.lower() in item.author.lower() or | |
selected_value.lower() in item.skill_description.lower()] | |
if all_skills is not None and len(all_skills) > 0: | |
for skill in all_skills: | |
with st.expander(f"{skill.author}/{skill.skill_name}", expanded=True): | |
tags = "`{}`".format("`\t\t`".join(skill.skill_tags)) | |
st.markdown(f"{tags}") | |
st.markdown(f"**<a href='https://huggingface.co/spaces/{skill.repo_id}?page=detail_page&skill_name={skill.skill_name}' style='font-size: 22px;'>{skill.author}/{skill.skill_name}</a>**", unsafe_allow_html=True) | |
st.markdown(f">**{skill.skill_description}**") | |
st.markdown("Install:") | |
install = f"import creator\ncreator.create(huggingface_repo_id=\"{skill.repo_id}\", huggingface_skill_path=\"{skill.skill_name}\")" | |
st.code(install, language=skill.skill_program_language) | |
st.markdown("Usage:") | |
usage = f"{skill.skill_usage_example}" | |
if skill.skill_program_language.lower() == "python": | |
usage = f"from {skill.repo_id.replace('/', '.')}.skill_code import {skill.skill_name}\n"+usage | |
st.code(usage, language=skill.skill_program_language) | |
st.markdown(f"`Created {time_ago(skill.created_at)}` `{skill.skill_program_language}`") | |
else: | |
st.markdown(f"### π Search results for `{selected_value}`") | |
st.markdown(f"**No results found**") | |
def about_page(): | |
file_path = "About.md" | |
with open(file_path, 'r', encoding='utf-8') as file: | |
content = file.read() | |
st.write(content, unsafe_allow_html=True) | |
def submit_page(): | |
global all_skills | |
global skill_db | |
if all_skills is None: | |
if skill_db is None: | |
skill_db = SkillsDB('skills.db') | |
all_skills = skill_db.get_skills() | |
st.markdown(f"### βοΈβ¨ Submit your skill here!") | |
with st.form(key='my_form'): | |
skill_repository = st.text_input('Skill Repo Id:') | |
skill_names = st_tags( | |
label="Skills' name:", | |
text='Press enter to add every skill folder', | |
value=[], | |
suggestions=[], | |
maxtags = 10, | |
key='1') | |
if st.form_submit_button('Submit'): | |
error = False | |
skills = [] | |
if skill_names is None or len(skill_names)<1: | |
st.error("Please enter at least one skill name! And remeber to press 'Enter' after each skill name.") | |
error = True | |
return | |
if skill_names is not None and len(skill_names) > 0: | |
for skill_name in skill_names: | |
skill = hf_pull_skill(skill_repository, skill_name) | |
if isinstance(skill, Exception): | |
st.error(skill) | |
error = True | |
return | |
skill["repo_id"] = skill_repository | |
skills.append(skill) | |
if skills is not None and len(skills) > 0: | |
for skill in skills: | |
if skill is not None: | |
if skill not in all_skills: | |
skill_obj = Skill(skill["repo_id"], skill["skill_name"], skill["skill_description"], skill["skill_metadata"]["author"], skill["skill_metadata"]["created_at"], skill["skill_usage_example"], skill["skill_program_language"], skill["skill_tags"]) | |
result = skill_db.add_skill(skill_obj) | |
if result != "ok": | |
st.error(result) | |
error = True | |
continue | |
all_skills.append(skill_obj) | |
if not error: | |
st.success("Skill(s) submitted successfully!") | |
chosen_id = stx.tab_bar(data=[ | |
stx.TabBarItemData(id=1, title="π Skill Library", description=""), | |
stx.TabBarItemData(id=2, title="π About", description=""), | |
stx.TabBarItemData(id=3, title="π Submit here!", description=""), | |
], default=1) | |
if chosen_id == '1': | |
main_page() | |
elif chosen_id == '2': | |
about_page() | |
elif chosen_id == '3': | |
submit_page() |