Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
2
22_23-J 18
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
22_23-J 18
22_23-J 18
Commits
979b9747
Commit
979b9747
authored
Apr 20, 2023
by
Lelkada L L P S M
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
backend api call for flip card contents
parent
b5c46f5d
Changes
9
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
130 additions
and
90 deletions
+130
-90
App/Backend/__pycache__/flip_card_content.cpython-311.pyc
App/Backend/__pycache__/flip_card_content.cpython-311.pyc
+0
-0
App/Backend/__pycache__/word_card_game.cpython-311.pyc
App/Backend/__pycache__/word_card_game.cpython-311.pyc
+0
-0
App/Backend/__pycache__/word_generation.cpython-311.pyc
App/Backend/__pycache__/word_generation.cpython-311.pyc
+0
-0
App/Backend/flip_card_content.py
App/Backend/flip_card_content.py
+29
-0
App/Backend/server.py
App/Backend/server.py
+14
-2
App/Backend/word_card_game.py
App/Backend/word_card_game.py
+26
-14
App/Backend/word_generation.py
App/Backend/word_generation.py
+4
-3
App/Frontend/screens/FlipCardGameScreen.js
App/Frontend/screens/FlipCardGameScreen.js
+56
-70
App/Frontend/screens/WordGenerationScreen.js
App/Frontend/screens/WordGenerationScreen.js
+1
-1
No files found.
App/Backend/__pycache__/flip_card_content.cpython-311.pyc
0 → 100644
View file @
979b9747
File added
App/Backend/__pycache__/word_card_game.cpython-311.pyc
View file @
979b9747
No preview for this file type
App/Backend/__pycache__/word_generation.cpython-311.pyc
View file @
979b9747
No preview for this file type
App/Backend/flip_card_content.py
0 → 100644
View file @
979b9747
import
pymongo
def
getFlipCardContent
():
client
=
pymongo
.
MongoClient
(
"mongodb+srv://hearme:hearme678@cluster0.kz66vdr.mongodb.net"
)
db
=
client
[
'word_card'
]
collection
=
db
[
'card'
]
card_data
=
collection
.
find_one
()
if
card_data
:
main_image_data
=
{
'isMainImage'
:
True
,
'image'
:
card_data
[
'image'
],
'audio'
:
card_data
[
'audio'
]
}
other_images_data
=
[
{
'isMainImage'
:
False
,
'image'
:
card
[
'image'
],
'audio'
:
card
[
'audio'
]
}
for
card
in
card_data
[
'card_0'
]
]
images_data
=
[
main_image_data
]
+
other_images_data
return
images_data
return
[]
App/Backend/server.py
View file @
979b9747
import
numpy
as
np
from
flask
import
Flask
,
request
,
jsonify
,
request
from
flask_cors
import
CORS
#import pickle
from
word_card_game
import
wordGameData
from
word_generation
import
get_similar_words
from
fl
ask_cors
import
CORS
import
pymongo
from
fl
ip_card_content
import
getFlipCardContent
app
=
Flask
(
__name__
)
...
...
@@ -51,6 +53,16 @@ def similar_words_api():
similar_words
=
get_similar_words
(
word
)
return
jsonify
({
'similar_words'
:
similar_words
})
@
app
.
route
(
'/api/flip-card-content'
)
def
flip_card_content
():
data
=
getFlipCardContent
()
return
jsonify
(
data
)
@
app
.
route
(
'/api/images_data'
,
methods
=
[
'GET'
])
def
get_images_data
():
images_data
=
wordGameData
()
return
jsonify
(
images_data
)
if
__name__
==
'__main__'
:
CORS
(
app
.
run
(
host
=
'0.0.0.0'
,
port
=
5000
,
debug
=
True
))
#app.run(host='0.0.0.0', port=5000, debug=True)
\ No newline at end of file
App/Backend/word_card_game.py
View file @
979b9747
import
random
import
pymongo
def
wordGameData
(
w1
,
w2
,
w3
):
words
=
[
w1
,
w2
,
w3
]
main_word
=
random
.
choice
(
words
)
card_words
=
random
.
sample
(
words
,
len
(
words
))
client
=
pymongo
.
MongoClient
(
"mongodb+srv://hearme:hearme678@cluster0.kz66vdr.mongodb.net"
)
db
=
client
[
'word_card'
]
collection
=
db
[
'card'
]
card_word1
,
card_word2
,
card_word3
=
card_words
def
get_words
():
doc
=
collection
.
find_one
()
if
doc
and
'card_0'
in
doc
:
words
=
doc
[
'card_0'
]
#print(words)
return
words
else
:
return
[]
def
create_links
(
word
):
image_link
=
f
'https://fyp-word-images.s3.us-east-2.amazonaws.com/{word}.png'
audio_link
=
f
'https://fyp-word-audio.s3.us-east-2.amazonaws.com/{word}.m4a'
return
{
'word'
:
word
,
'image_link'
:
image_link
,
'audio_link'
:
audio_link
}
def
wordGameData
():
words_data
=
get_words
()
main_word_data
=
random
.
choice
(
words_data
)
card_words_data
=
random
.
sample
(
words_data
,
len
(
words_data
))
def
create_links
(
word_data
):
return
{
'word'
:
word_data
[
'word'
],
'image_link'
:
word_data
[
'image'
],
'audio_link'
:
word_data
[
'audio'
]}
return
{
'main_word'
:
create_links
(
main_word
),
'card_word1'
:
create_links
(
card_word1
),
'card_word2'
:
create_links
(
card_word2
),
'card_word3'
:
create_links
(
card_word3
)
}
\ No newline at end of file
'main_word'
:
create_links
(
main_word_data
),
'card_word1'
:
create_links
(
card_words_data
[
0
]),
'card_word2'
:
create_links
(
card_words_data
[
1
]),
'card_word3'
:
create_links
(
card_words_data
[
2
])
}
App/Backend/word_generation.py
View file @
979b9747
...
...
@@ -36,15 +36,16 @@ def get_similar_words(input_word, top_k=3):
'audio'
:
audio_url
})
print
(
result
)
#connect mongo
client
=
pymongo
.
MongoClient
(
"mongodb+srv://hearme:hearme678@cluster0.kz66vdr.mongodb.net"
)
db
=
client
[
'word_card'
]
collection
=
db
[
'card'
]
document
=
{
"card_0"
:
result
}
print
(
'---------------'
)
print
(
document
)
collection
.
delete_many
({})
collection
.
insert_one
(
document
)
...
...
App/Frontend/screens/FlipCardGameScreen.js
View file @
979b9747
import
React
,
{
useState
,
useEffect
}
from
'
react
'
;
import
{
StyleSheet
,
View
,
TouchableOpacity
,
Image
,
Text
,
Modal
,
Button
}
from
'
react-native
'
;
import
{
StyleSheet
,
View
,
TouchableOpacity
,
Image
,
Text
,
Modal
,
Button
}
from
'
react-native
'
;
import
{
Audio
}
from
'
expo-av
'
;
const
Card
=
({
image
,
audio
,
onFlip
,
gameOver
,
correct
})
=>
{
const
[
isFlipped
,
setIsFlipped
]
=
useState
(
false
);
const
[
isFlipped
,
setIsFlipped
]
=
useState
(
false
);
useEffect
(()
=>
{
if
(
gameOver
)
{
setIsFlipped
(
true
);
}
},
[
gameOver
]);
const
handleFlip
=
async
()
=>
{
if
(
!
isFlipped
&&
!
gameOver
)
{
useEffect
(()
=>
{
if
(
gameOver
)
{
setIsFlipped
(
true
);
}
},
[
gameOver
]);
const
handleFlip
=
async
()
=>
{
if
(
!
isFlipped
&&
!
gameOver
)
{
setIsFlipped
(
true
);
await
Audio
.
Sound
.
createAsync
(
audio
,
{
shouldPlay
:
true
});
onFlip
(
image
);
setTimeout
(()
=>
{
setIsFlipped
(
true
);
await
Audio
.
Sound
.
createAsync
(
audio
,
{
shouldPlay
:
true
});
onFlip
(
image
);
setTimeout
(()
=>
{
setIsFlipped
(
true
);
},
800
);
}
};
return
(
<
TouchableOpacity
style
=
{[
styles
.
card
,
correct
&&
gameOver
?
styles
.
correctCard
:
null
]}
onPress
=
{
handleFlip
}
>
<
Image
source
=
{
isFlipped
?
image
:
require
(
'
./assets/ST/owl.png
'
)}
style
=
{
styles
.
cardImage
}
/
>
<
/TouchableOpacity
>
);
};
},
800
);
}
};
return
(
<
TouchableOpacity
style
=
{[
styles
.
card
,
correct
&&
gameOver
?
styles
.
correctCard
:
null
]}
onPress
=
{
handleFlip
}
>
<
Image
source
=
{
isFlipped
?
image
:
require
(
'
./assets/ST/owl.png
'
)}
style
=
{
styles
.
cardImage
}
/
>
<
/TouchableOpacity
>
);
};
const
MessageBox
=
({
visible
,
onClose
})
=>
{
return
(
<
Modal
animationType
=
"
slide
"
transparent
=
{
true
}
visible
=
{
visible
}
>
<
View
style
=
{
styles
.
modalContainer
}
>
<
View
style
=
{
styles
.
modalContent
}
>
<
Text
style
=
{
styles
.
modalText
}
>
Game
Over
<
/Text
>
<
Button
title
=
"
Close
"
onPress
=
{
onClose
}
/
>
<
/View
>
return
(
<
Modal
animationType
=
"
slide
"
transparent
=
{
true
}
visible
=
{
visible
}
>
<
View
style
=
{
styles
.
modalContainer
}
>
<
View
style
=
{
styles
.
modalContent
}
>
<
Text
style
=
{
styles
.
modalText
}
>
Game
Over
<
/Text
>
<
Button
title
=
"
Close
"
onPress
=
{
onClose
}
/
>
<
/View
>
<
/Modal
>
);
};
<
/View
>
<
/Modal
>
);
};
export
default
function
FlipCardGame
({
route
})
{
const
[
gameOver
,
setGameOver
]
=
useState
(
false
);
const
{
cards
}
=
route
.
params
;
console
.
log
(
route
.
params
);
const
[
imagesData
,
setImagesData
]
=
useState
([]);
//const { cards } = route.params;
//console.log(route.params);
const
handleScroll
=
async
(
event
)
=>
{
const
newIndex
=
Math
.
round
(
event
.
nativeEvent
.
contentOffset
.
x
/
screenWidth
);
if
(
newIndex
!==
activeIndex
)
{
setActiveIndex
(
newIndex
);
if
(
cards
.
length
>
0
)
{
await
soundRef
.
current
.
unloadAsync
();
await
soundRef
.
current
.
loadAsync
({
uri
:
cards
[
newIndex
].
audio
});
await
soundRef
.
current
.
playAsync
();
}
}
};
const
handleAudioPress
=
async
()
=>
{
if
(
cards
.
length
>
0
)
{
await
soundRef
.
current
.
unloadAsync
();
await
soundRef
.
current
.
loadAsync
({
uri
:
cards
[
activeIndex
].
audio
});
await
soundRef
.
current
.
playAsync
();
const
fetchImagesData
=
async
()
=>
{
try
{
const
response
=
await
fetch
(
'
http://192.168.137.111:5000/api/images_data
'
);
const
data
=
await
response
.
json
();
setImagesData
(
data
);
}
catch
(
error
)
{
console
.
error
(
'
Error fetching images data:
'
,
error
);
}
};
const
images
=
[
{
image
:
require
(
'
./assets/CG/content/vegetable.png
'
),
audio
:
require
(
'
./assets/CG/content/vegetable.m4a
'
)
},
{
image
:
require
(
'
./assets/CG/content/kitchen.png
'
),
audio
:
require
(
'
./assets/CG/content/kitchen.m4a
'
)
},
{
image
:
require
(
'
./assets/CG/content/pot.png
'
),
audio
:
require
(
'
./assets/CG/content/pot.m4a
'
)
},
];
useEffect
(()
=>
{
fetchImagesData
();
},
[]);
const
mainImage
=
require
(
'
./assets/CG/content/kitchen.png
'
);
const
mainImage
=
imagesData
.
find
((
img
)
=>
img
.
isMainImage
);
const
handleCardFlip
=
(
image
)
=>
{
if
(
image
===
mainImage
)
{
...
...
@@ -88,16 +75,16 @@ export default function FlipCardGame({ route }) {
return
(
<
View
style
=
{
styles
.
container
}
>
<
Image
source
=
{
mainImage
}
style
=
{
styles
.
mainImage
}
/
>
{
mainImage
&&
<
Image
source
=
{{
uri
:
mainImage
.
image
}}
style
=
{
styles
.
mainImage
}
/>
}
<
View
style
=
{
styles
.
cardsContainer
}
>
{
images
.
map
((
img
,
index
)
=>
(
{
images
Data
.
map
((
img
,
index
)
=>
(
<
Card
key
=
{
index
}
image
=
{
img
.
image
}
audio
=
{
img
.
audio
}
image
=
{
{
uri
:
img
.
image
}
}
audio
=
{
{
uri
:
img
.
audio
}
}
onFlip
=
{
handleCardFlip
}
gameOver
=
{
gameOver
}
correct
=
{
img
.
i
mage
===
m
ainImage
}
correct
=
{
img
.
i
sM
ainImage
}
/
>
))}
<
/View
>
...
...
@@ -105,7 +92,6 @@ export default function FlipCardGame({ route }) {
<
/View
>
);
}
const
styles
=
StyleSheet
.
create
({
container
:
{
flex
:
1
,
...
...
App/Frontend/screens/WordGenerationScreen.js
View file @
979b9747
...
...
@@ -19,7 +19,7 @@ const screenHeight = Dimensions.get("window").height;
const
fetchCards
=
async
()
=>
{
try
{
const
response
=
await
fetch
(
'
http://192.168.
216
.111:5000/api/similar-words?word=school
'
);
const
response
=
await
fetch
(
'
http://192.168.
137
.111:5000/api/similar-words?word=school
'
);
const
data
=
await
response
.
json
();
console
.
log
(
data
)
return
data
.
similar_words
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment