sdy

update app.js

Showing 1 changed file with 48 additions and 195 deletions
1 -import React, {Fragment, useRef, useEffect, useState} from "react"; 1 +import React, {Fragment, useState} from "react";
2 -import * as tf from "@tensorflow/tfjs"; 2 +import { Client } from "elasticsearch"
3 -import * as qna from "@tensorflow-models/qna";
4 -import "react-loader-spinner/dist/loader/css/react-spinner-loader.css";
5 -import Loader from "react-loader-spinner";
6 import "./App.css"; 3 import "./App.css";
7 -import {paragraph} from "./passage";
8 4
9 function App() { 5 function App() {
10 6
11 - // setup references, states 7 + const [answer, setAnswer] = useState('');
12 - //const passageRef = useRef(null); 8 + const [inputValue, setInputValue] = useState('');
13 - const questionRef = useRef(null);
14 - const [answer, setAnswer] = useState();
15 - const [model, setModel] = useState(null);
16 9
17 - // load tensorflow models 10 + const client = new Client({
18 - const loadModel = async () => { 11 + host: 'http://localhost:9200'
19 - const loadedModel = await qna.load(); 12 + });
20 - setModel(loadedModel);
21 - console.log("Model is loaded");
22 - }
23 13
24 - useEffect(() => { loadModel() }, []); 14 + const onKeyUpHandler = async (event) => {
15 + if (event.charCode === 13) {
16 + setInputValue(event.target.value);
25 17
26 - // define question function 18 + if (inputValue !== '' || inputValue !== undefined || inputValue.length !== 0) {
27 - const answerQuestion = async (e) => { 19 + console.log(inputValue);
28 - if (e.which === 13 && model !== null) { // if key is enter 20 + try {
29 - console.log("Question Submitted."); 21 + const result = await client.search({
30 - const passage = paragraph; 22 + index: "wiki-qna2",
31 - const question = questionRef.current.value; 23 + body: {
24 + query: {
25 + match: {
26 + "질문(원문, 하 난이도)": inputValue
27 + }
28 + }
29 + }
30 + });
32 31
33 - const answers = await model.findAnswers(question, passage); 32 + if (result !== null || result !== undefined || result.length !== 0) {
34 - setAnswer(answers); 33 + setAnswer(result.hits.hits);
35 - console.log(answers); 34 + answer.forEach(e => {
35 + console.log(e._source.정답);
36 + });
37 + }
38 + } catch (error) {
39 + console.log(error);
40 + }
41 + }
36 } 42 }
37 } 43 }
38 44
45 + const showAnswer = (answer) => {
39 return ( 46 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> 47 <div>
44 - <div>Model Loading</div> 48 + <b>{answer._source.정답}</b>
45 - <Loader
46 - type="Puff"
47 - color="#00BFFF"
48 - height={100}
49 - width={100}
50 - />
51 </div> 49 </div>
52 - : // if model is loaded, passage and answer will be shown in browser 50 + )
51 + }
52 +
53 + return (
54 + <div className="App">
55 + <header className="App-header">
53 <Fragment> 56 <Fragment>
54 Ask a Question 57 Ask a Question
55 - <input ref={questionRef} onKeyPress={answerQuestion} size="80"/> 58 + <input size="80" onKeyPress={onKeyUpHandler}/>
56 - Answers 59 + <div>
57 - {answer ? answer.map((ans, idx) => <div><b>Answer {idx + 1} - </b> {ans.text} </div>) : " : Can't Find any answers !"} 60 + -- Answers --
61 + <div>
62 + {answer === '' ? " loading answer..." : showAnswer(answer[0])}
63 + </div>
64 + </div>
58 </Fragment> 65 </Fragment>
59 - }
60 </header> 66 </header>
61 </div> 67 </div>
62 ); 68 );
63 } 69 }
64 70
65 export default App; 71 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 -/*
86 -import "@elastic/react-search-ui-views/lib/styles/styles.css";
87 -import './App.css';
88 -*/
89 -/*
90 -const connector = new AppSearchAPIConnector({
91 - searchKey: "search-4td7gan5kcasgygjcyexksrz",
92 - engineName: "video-games",
93 - endpointBase: "http://localhost:3002"
94 -});
95 - */
96 -
97 -/*
98 -const client = new Client({
99 - node: "http://localhost:9200"
100 -})
101 -
102 -console.log(client);
103 -*/
104 -/*
105 -const configurationOptions = { // search 쿼리와 api 연결선을 작성한 객체
106 - apiConnector: client,
107 - searchQuery: {
108 - search_fields: {
109 - name: {}
110 - },
111 - result_fields: {
112 - name: {
113 - snippet: {
114 - size: 75
115 - }
116 - },
117 - genre: {
118 - snippet: {
119 - size: 50,
120 - fallback: true
121 - }
122 - },
123 - publisher: {
124 - snippet: {
125 - size: 50,
126 - fallback: true
127 - }
128 - },
129 - critic_score: {
130 - raw: {}
131 - },
132 - user_score: {
133 - raw: {}
134 - },
135 - platform: {
136 - snippet: {
137 - size: 50,
138 - fallback: true
139 - }
140 - },
141 - image_url: {
142 - raw: {}
143 - }
144 - },
145 - facets: {
146 - user_score: {
147 - type: "range",
148 - ranges: [
149 - { from: 0, to: 5, name: "Not good" },
150 - { from: 5, to: 7, name: "Not bad" },
151 - { from: 7, to: 9, name: "Pretty good" },
152 - { from: 9, to: 10, name: "Must play!" }
153 - ]
154 - },
155 - critic_score: {
156 - type: "range",
157 - ranges: [
158 - { from: 0, to: 50, name: "Not good" },
159 - { from: 50, to: 70, name: "Not bad" },
160 - { from: 70, to: 90, name: "Pretty good" },
161 - { from: 90, to: 100, name: "Must play!" }
162 - ]
163 - },
164 - genre: { type: "value", size: 100 },
165 - publisher: { type: "value", size: 100},
166 - platform: { type: "value", size: 100}
167 - }
168 - },
169 - autocompleteQuery: {
170 - suggestions: {
171 - types: {
172 - documents: {
173 - fields: ["name"]
174 - }
175 - },
176 - size: 5
177 - }
178 - }
179 -};
180 -*/
181 -
182 -/* <SearchProvider config={configurationOptions}>
183 - <div className="App">
184 - <Layout
185 - header={<SearchBox autocompleteSuggestions={true} />}
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 - ]}
203 - />
204 - <Facet field="user_score" label="User Score" />
205 - </div>
206 - }
207 - bodyHeader={
208 - <>
209 - <PagingInfo />
210 - <ResultsPerPage />
211 - </>
212 - }
213 - bodyFooter={<Paging />}
214 - />
215 -
216 - </div>
217 -</SearchProvider>
218 -*/
......