File size: 7,873 Bytes
f93d5f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5e4e3fb
f93d5f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2262d15
f93d5f5
2262d15
f93d5f5
2262d15
 
 
 
f93d5f5
 
 
2262d15
 
 
 
f93d5f5
 
 
 
 
 
 
 
 
 
2262d15
f93d5f5
 
2262d15
 
f93d5f5
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
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()