Showing
2 changed files
with
145 additions
and
21 deletions
1 | + | ||
1 | .App { | 2 | .App { |
3 | + height: 100%; | ||
2 | text-align: center; | 4 | text-align: center; |
3 | } | 5 | } |
4 | 6 | ||
... | @@ -14,14 +16,11 @@ | ... | @@ -14,14 +16,11 @@ |
14 | } | 16 | } |
15 | 17 | ||
16 | .App-header { | 18 | .App-header { |
17 | - background-color: #282c34; | 19 | + height: 100%; |
18 | - min-height: 100vh; | ||
19 | display: flex; | 20 | display: flex; |
20 | flex-direction: column; | 21 | flex-direction: column; |
21 | align-items: center; | 22 | align-items: center; |
22 | justify-content: center; | 23 | justify-content: center; |
23 | - font-size: calc(10px + 2vmin); | ||
24 | - color: white; | ||
25 | } | 24 | } |
26 | 25 | ||
27 | .App-link { | 26 | .App-link { | ... | ... |
1 | -import React from "react"; | 1 | +import React, {Fragment, useRef, useEffect, useState} from "react"; |
2 | -import AppSearchAPIConnector from "@elastic/search-ui-app-search-connector"; | 2 | +import * as tf from "@tensorflow/tfjs"; |
3 | -import { SearchProvider, Results, SearchBox } from "@elastic/react-search-ui"; | 3 | +import * as qna from "@tensorflow-models/qna"; |
4 | -import { Layout} from "@elastic/react-search-ui-views"; | 4 | +import "react-loader-spinner/dist/loader/css/react-spinner-loader.css"; |
5 | +import Loader from "react-loader-spinner"; | ||
6 | +import "./App.css"; | ||
7 | +import {paragraph} from "./passage"; | ||
8 | + | ||
9 | +function App() { | ||
10 | + | ||
11 | + // setup references, states | ||
12 | + //const passageRef = useRef(null); | ||
13 | + const questionRef = useRef(null); | ||
14 | + const [answer, setAnswer] = useState(); | ||
15 | + const [model, setModel] = useState(null); | ||
16 | + | ||
17 | + // load tensorflow models | ||
18 | + const loadModel = async () => { | ||
19 | + const loadedModel = await qna.load(); | ||
20 | + setModel(loadedModel); | ||
21 | + console.log("Model is loaded"); | ||
22 | + } | ||
23 | + | ||
24 | + useEffect(() => { loadModel() }, []); | ||
25 | + | ||
26 | + // define question function | ||
27 | + const answerQuestion = async (e) => { | ||
28 | + if (e.which === 13 && model !== null) { // if key is enter | ||
29 | + console.log("Question Submitted."); | ||
30 | + const passage = paragraph; | ||
31 | + const question = questionRef.current.value; | ||
32 | + | ||
33 | + const answers = await model.findAnswers(question, passage); | ||
34 | + setAnswer(answers); | ||
35 | + console.log(answers); | ||
36 | + } | ||
37 | + } | ||
38 | + | ||
39 | + return ( | ||
40 | + <div className="App"> | ||
41 | + <header className="App-header"> | ||
42 | + {model == null? // if model isn't loaded, react loader spinner will be upload in browser | ||
43 | + <div> | ||
44 | + <div>Model Loading</div> | ||
45 | + <Loader | ||
46 | + type="Puff" | ||
47 | + color="#00BFFF" | ||
48 | + height={100} | ||
49 | + width={100} | ||
50 | + /> | ||
51 | + </div> | ||
52 | + : // if model is loaded, passage and answer will be shown in browser | ||
53 | + <Fragment> | ||
54 | + Ask a Question | ||
55 | + <input ref={questionRef} onKeyPress={answerQuestion} size="80"/> | ||
56 | + Answers | ||
57 | + {answer ? answer.map((ans, idx) => <div><b>Answer {idx + 1} - </b> {ans.text} </div>) : " : Can't Find any answers !"} | ||
58 | + </Fragment> | ||
59 | + } | ||
60 | + </header> | ||
61 | + </div> | ||
62 | + ); | ||
63 | +} | ||
64 | + | ||
65 | +export default App; | ||
66 | + | ||
67 | + | ||
68 | +/* | ||
69 | +//import AppSearchAPIConnector from "@elastic/search-ui-app-search-connector"; | ||
70 | +import { Client } from "@elastic/elasticsearch"; | ||
71 | + */ | ||
72 | +/* | ||
73 | +import { | ||
74 | + SearchProvider, | ||
75 | + Results, | ||
76 | + SearchBox, | ||
77 | + PagingInfo, | ||
78 | + ResultsPerPage, | ||
79 | + Paging, | ||
80 | + Facet, | ||
81 | + Sorting | ||
82 | +} from "@elastic/react-search-ui"; | ||
83 | +import { Layout } from "@elastic/react-search-ui-views"; | ||
84 | +*/ | ||
85 | +/* | ||
5 | import "@elastic/react-search-ui-views/lib/styles/styles.css"; | 86 | import "@elastic/react-search-ui-views/lib/styles/styles.css"; |
6 | import './App.css'; | 87 | import './App.css'; |
7 | - | 88 | +*/ |
89 | +/* | ||
8 | const connector = new AppSearchAPIConnector({ | 90 | const connector = new AppSearchAPIConnector({ |
9 | - searchKey: "[search-4td7gan5kcasgygjcyexksrz]", | 91 | + searchKey: "search-4td7gan5kcasgygjcyexksrz", |
10 | engineName: "video-games", | 92 | engineName: "video-games", |
11 | - hostIdentifier: "[private-bhi6v1txusox87eag2c8qgu3]" | 93 | + endpointBase: "http://localhost:3002" |
12 | }); | 94 | }); |
95 | + */ | ||
96 | + | ||
97 | +/* | ||
98 | +const client = new Client({ | ||
99 | + node: "http://localhost:9200" | ||
100 | +}) | ||
13 | 101 | ||
14 | -const configurationOptions = { | 102 | +console.log(client); |
15 | - apiConnector: connector, | 103 | +*/ |
104 | +/* | ||
105 | +const configurationOptions = { // search 쿼리와 api 연결선을 작성한 객체 | ||
106 | + apiConnector: client, | ||
16 | searchQuery: { | 107 | searchQuery: { |
17 | search_fields: { | 108 | search_fields: { |
18 | name: {} | 109 | name: {} |
... | @@ -74,20 +165,54 @@ const configurationOptions = { | ... | @@ -74,20 +165,54 @@ const configurationOptions = { |
74 | publisher: { type: "value", size: 100}, | 165 | publisher: { type: "value", size: 100}, |
75 | platform: { type: "value", size: 100} | 166 | platform: { type: "value", size: 100} |
76 | } | 167 | } |
168 | + }, | ||
169 | + autocompleteQuery: { | ||
170 | + suggestions: { | ||
171 | + types: { | ||
172 | + documents: { | ||
173 | + fields: ["name"] | ||
174 | + } | ||
175 | + }, | ||
176 | + size: 5 | ||
177 | + } | ||
77 | } | 178 | } |
78 | }; | 179 | }; |
180 | +*/ | ||
79 | 181 | ||
80 | -function App() { | 182 | +/* <SearchProvider config={configurationOptions}> |
81 | - return ( | ||
82 | - <SearchProvider config={configurationOptions}> | ||
83 | <div className="App"> | 183 | <div className="App"> |
84 | <Layout | 184 | <Layout |
85 | - header={<SearchBox />} | 185 | + header={<SearchBox autocompleteSuggestions={true} />} |
86 | bodyContent={<Results titleField="name" urlField="image_url"/>} | 186 | bodyContent={<Results titleField="name" urlField="image_url"/>} |
187 | + sideContent={ | ||
188 | + <div> | ||
189 | + <Sorting | ||
190 | + label={"Sort by"} | ||
191 | + sortOptions={[ | ||
192 | + { | ||
193 | + name: "Relevance", | ||
194 | + value: "", | ||
195 | + direction: "" | ||
196 | + }, | ||
197 | + { | ||
198 | + name: "Name", | ||
199 | + value: "name", | ||
200 | + direction: "asc" | ||
201 | + } | ||
202 | + ]} | ||
87 | /> | 203 | /> |
204 | + <Facet field="user_score" label="User Score" /> | ||
88 | </div> | 205 | </div> |
89 | - </SearchProvider> | 206 | + } |
90 | - ); | 207 | + bodyHeader={ |
91 | -} | 208 | + <> |
209 | + <PagingInfo /> | ||
210 | + <ResultsPerPage /> | ||
211 | + </> | ||
212 | + } | ||
213 | + bodyFooter={<Paging />} | ||
214 | + /> | ||
92 | 215 | ||
93 | -export default App; | 216 | + </div> |
217 | +</SearchProvider> | ||
218 | +*/ | ... | ... |
-
Please register or login to post a comment