taishi-i commited on
Commit
94349ba
β€’
1 Parent(s): 1fdf118

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +95 -58
app.py CHANGED
@@ -3,61 +3,98 @@ import json
3
  import streamlit as st
4
  from pyserini.search.lucene import LuceneSearcher
5
 
6
- # Search engine
7
- searcher = LuceneSearcher("indexes/docs")
8
- searcher.set_language("ja")
9
-
10
- # UI
11
- st.set_page_config(
12
- page_title="awesome-japanese-nlp-resources-search",
13
- page_icon="😎",
14
- layout="centered",
15
- )
16
-
17
-
18
- cola, colb, colc = st.columns([5, 4, 5])
19
-
20
- st.header("awesome-japanese-nlp-resources-search 😎")
21
- st.markdown(
22
- "You can search for open-source software from [400+ Japanese NLP"
23
- " repositories](https://github.com/taishi-i/awesome-japanese-nlp-resources)."
24
- )
25
-
26
- col1, col2 = st.columns([9, 1])
27
- with col1:
28
- search_query = st.text_input(label="", placeholder="Search")
29
-
30
- with col2:
31
- st.write("#")
32
- button_clicked = st.button("πŸ”Ž")
33
-
34
-
35
- if search_query or button_clicked:
36
- search_results = searcher.search(search_query, k=100_000)
37
-
38
- st.write(
39
- '<p align="light" style="color:grey;">'
40
- f" {len(search_results):,.0f} repositories </p>",
41
- unsafe_allow_html=True,
42
- )
43
-
44
- for result in search_results:
45
- data_json = json.loads(result.raw)
46
- description = data_json["description"]
47
- url = data_json["url"]
48
- project_name = data_json["project_name"]
49
- main_topic = data_json["main_topic"]
50
- sub_topic = data_json["sub_topic"]
51
-
52
- try:
53
- st.markdown(f"### [{project_name}]({url})")
54
- st.markdown(f"{description}")
55
- if sub_topic is None:
56
- st.text(f"{main_topic}")
57
- else:
58
- st.text(f"{main_topic} / {sub_topic}")
59
- st.markdown("")
60
- st.markdown("")
61
-
62
- except:
63
- pass
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  import streamlit as st
4
  from pyserini.search.lucene import LuceneSearcher
5
 
6
+
7
+ class SearchApplication:
8
+ def __init__(self):
9
+ self.title = "Awesome Japanese NLP resources search"
10
+
11
+ self.set_page_config()
12
+ self.searcher = self.set_searcher()
13
+
14
+ st.header(self.title)
15
+ col1, col2 = st.columns(2)
16
+ with col1:
17
+ self.query = st.text_input(
18
+ "Search English or Japanese words", value=""
19
+ )
20
+
21
+ with col2:
22
+ st.write("#")
23
+ self.search_button = st.button("πŸ”Ž")
24
+
25
+ st.caption(
26
+ "You can search for open-source software from [400+ Japanese NLP"
27
+ " repositories](https://github.com/taishi-i/awesome-japanese-nlp-resources)."
28
+ )
29
+ st.write("#")
30
+
31
+ self.show_popular_words()
32
+ self.show_search_results()
33
+
34
+ def set_page_config(self):
35
+ st.set_page_config(
36
+ page_title=self.title,
37
+ page_icon="😎",
38
+ layout="centered",
39
+ )
40
+
41
+ def set_searcher(self):
42
+ searcher = LuceneSearcher("indexes/docs")
43
+ searcher.set_language("ja")
44
+ return searcher
45
+
46
+ def show_popular_words(self):
47
+ st.caption("Popular words")
48
+
49
+ word1, word2, word3, word4, _ = st.columns(5)
50
+ with word1:
51
+ button1 = st.button("Python")
52
+ if button1:
53
+ self.query = "Python"
54
+
55
+ with word2:
56
+ button2 = st.button("BERT")
57
+ if button2:
58
+ self.query = "BERT"
59
+
60
+ with word3:
61
+ button3 = st.button("θΎžζ›Έ")
62
+ if button3:
63
+ self.query = "θΎžζ›Έ"
64
+
65
+ with word4:
66
+ button4 = st.button("コーパス")
67
+ if button4:
68
+ self.query = "Corpus"
69
+
70
+ def show_search_results(self):
71
+ if self.query or self.search_button:
72
+ st.write("#")
73
+
74
+ search_results = self.searcher.search(self.query, k=500)
75
+ num_search_results = len(search_results)
76
+ st.write(f"{num_search_results} results")
77
+
78
+ for result in search_results:
79
+ data_json = json.loads(result.raw)
80
+ description = data_json["description"]
81
+ url = data_json["url"]
82
+ project_name = data_json["project_name"]
83
+ main_topic = data_json["main_topic"]
84
+ sub_topic = data_json["sub_topic"]
85
+
86
+ st.subheader(f"[{project_name}]({url})")
87
+ st.markdown(description)
88
+ if sub_topic is None:
89
+ st.caption(f"{main_topic}")
90
+ else:
91
+ st.caption(f"{main_topic} / {sub_topic}")
92
+ st.write("#")
93
+
94
+
95
+ def main():
96
+ SearchApplication()
97
+
98
+
99
+ if __name__ == "__main__":
100
+ main()