Spaces:
Runtime error
Runtime error
Merge pull request #11 from HunyyDev/dev/NMPhap
Browse files- .coverage +0 -0
- app/routers/video.py +48 -9
- tests/test_friend_request.py +3 -13
.coverage
CHANGED
|
Binary files a/.coverage and b/.coverage differ
|
|
|
app/routers/video.py
CHANGED
|
@@ -15,21 +15,22 @@ from fastapi import (
|
|
| 15 |
BackgroundTasks,
|
| 16 |
status,
|
| 17 |
)
|
| 18 |
-
import
|
|
|
|
| 19 |
from app import supabase
|
| 20 |
from app.dependencies import get_current_user
|
| 21 |
from app.routers.image import inferenceImage
|
|
|
|
| 22 |
|
| 23 |
router = APIRouter(prefix="/video", tags=["Video"])
|
| 24 |
|
| 25 |
|
| 26 |
-
@router.post("
|
| 27 |
async def handleVideoRequest(
|
| 28 |
-
artifactId: str,
|
| 29 |
file: UploadFile,
|
| 30 |
background_tasks: BackgroundTasks,
|
| 31 |
threshold: float = 0.3,
|
| 32 |
-
|
| 33 |
):
|
| 34 |
if re.search("^video\/", file.content_type) is None:
|
| 35 |
raise HTTPException(
|
|
@@ -38,12 +39,15 @@ async def handleVideoRequest(
|
|
| 38 |
)
|
| 39 |
|
| 40 |
try:
|
|
|
|
|
|
|
| 41 |
id = str(now())
|
|
|
|
| 42 |
os.mkdir(id)
|
| 43 |
async with aiofiles.open(os.path.join(id, "input.mp4"), "wb") as out_file:
|
| 44 |
while content := await file.read(1024):
|
| 45 |
await out_file.write(content)
|
| 46 |
-
background_tasks.add_task(inferenceVideo,
|
| 47 |
return id + ".mp4"
|
| 48 |
except ValueError as err:
|
| 49 |
print(err)
|
|
@@ -145,7 +149,42 @@ async def inferenceVideo(artifactId: str, inputDir: str, threshold: float):
|
|
| 145 |
|
| 146 |
|
| 147 |
def updateArtifact(artifactId: str, body):
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
BackgroundTasks,
|
| 16 |
status,
|
| 17 |
)
|
| 18 |
+
from firebase_admin import messaging
|
| 19 |
+
from app import db
|
| 20 |
from app import supabase
|
| 21 |
from app.dependencies import get_current_user
|
| 22 |
from app.routers.image import inferenceImage
|
| 23 |
+
from google.cloud.firestore_v1.base_query import FieldFilter
|
| 24 |
|
| 25 |
router = APIRouter(prefix="/video", tags=["Video"])
|
| 26 |
|
| 27 |
|
| 28 |
+
@router.post("")
|
| 29 |
async def handleVideoRequest(
|
|
|
|
| 30 |
file: UploadFile,
|
| 31 |
background_tasks: BackgroundTasks,
|
| 32 |
threshold: float = 0.3,
|
| 33 |
+
user=Depends(get_current_user),
|
| 34 |
):
|
| 35 |
if re.search("^video\/", file.content_type) is None:
|
| 36 |
raise HTTPException(
|
|
|
|
| 39 |
)
|
| 40 |
|
| 41 |
try:
|
| 42 |
+
if user["sub"] is None:
|
| 43 |
+
return HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="User not found")
|
| 44 |
id = str(now())
|
| 45 |
+
_, artifact_ref = db.collection("artifacts").add({"name": id + ".mp4", "status": "pending"})
|
| 46 |
os.mkdir(id)
|
| 47 |
async with aiofiles.open(os.path.join(id, "input.mp4"), "wb") as out_file:
|
| 48 |
while content := await file.read(1024):
|
| 49 |
await out_file.write(content)
|
| 50 |
+
background_tasks.add_task(inferenceVideo, artifact_ref.id, id, threshold)
|
| 51 |
return id + ".mp4"
|
| 52 |
except ValueError as err:
|
| 53 |
print(err)
|
|
|
|
| 149 |
|
| 150 |
|
| 151 |
def updateArtifact(artifactId: str, body):
|
| 152 |
+
artifact_snapshot = db.collection("artifacts").document(artifactId).get()
|
| 153 |
+
if not artifact_snapshot.exists:
|
| 154 |
+
artifact_snapshot.update(body)
|
| 155 |
+
sendMessage(artifactId)
|
| 156 |
+
# This function cannot be automation test because the requirement of another device to receive notification
|
| 157 |
+
def sendMessage(artifactId: str, message: str = None):
|
| 158 |
+
token = []
|
| 159 |
+
artifact = db.collection("artifacts").document(artifactId).get()
|
| 160 |
+
if not artifact.exists:
|
| 161 |
+
return
|
| 162 |
+
user_ref = db.collection("user").where(filter=FieldFilter("artifacts", "array-contains", "artifacts/" + artifactId))
|
| 163 |
+
for user in user_ref:
|
| 164 |
+
token.append(user.get().to_dict()['deviceId'])
|
| 165 |
+
if message is not None:
|
| 166 |
+
messaging.MulticastMessage(data={"notification": {
|
| 167 |
+
"title": message,
|
| 168 |
+
"body":
|
| 169 |
+
"Video " +
|
| 170 |
+
artifact.name +
|
| 171 |
+
" has done inference. Click here to see the video",
|
| 172 |
+
},}, android=messaging.AndroidConfig(
|
| 173 |
+
notification=messaging.AndroidNotification(
|
| 174 |
+
icon='stock_ticker_update',
|
| 175 |
+
color='#f45342'
|
| 176 |
+
),))
|
| 177 |
+
else:
|
| 178 |
+
messaging.MulticastMessage(data={"notification": {
|
| 179 |
+
"title": "Video " + artifact.name + " has done inference.",
|
| 180 |
+
"body":
|
| 181 |
+
"Video " +
|
| 182 |
+
artifact.name +
|
| 183 |
+
" has done inference. Click here to see the video",
|
| 184 |
+
},}, android=messaging.AndroidConfig(
|
| 185 |
+
notification=messaging.AndroidNotification(
|
| 186 |
+
icon='stock_ticker_update',
|
| 187 |
+
color='#f45342'
|
| 188 |
+
),))
|
| 189 |
+
response = messaging.send_multicast(message)
|
| 190 |
+
return response.success_count
|
tests/test_friend_request.py
CHANGED
|
@@ -11,6 +11,7 @@ from app.main import app
|
|
| 11 |
from app.constants import deviceId
|
| 12 |
from fastapi.routing import APIRoute
|
| 13 |
from firebase_admin import firestore
|
|
|
|
| 14 |
from google.cloud.firestore_v1.base_query import FieldFilter
|
| 15 |
def endpoints():
|
| 16 |
endpoints = []
|
|
@@ -54,16 +55,6 @@ def inviter():
|
|
| 54 |
data = response.json()
|
| 55 |
inviter = {"id": data['localId'], "token": data["idToken"]}
|
| 56 |
yield inviter
|
| 57 |
-
@pytest.fixture()
|
| 58 |
-
def firebase_app():
|
| 59 |
-
if "test" in firebase_admin._apps:
|
| 60 |
-
firebase_app = firebase_admin.get_app('test')
|
| 61 |
-
yield firebase_app
|
| 62 |
-
else:
|
| 63 |
-
firebase_app = firebase_admin.initialize_app( credential= credentials.Certificate(
|
| 64 |
-
json.loads(os.environ.get("FIREBASE_CREDENTIALS"))
|
| 65 |
-
),name='test')
|
| 66 |
-
yield firebase_app
|
| 67 |
|
| 68 |
@pytest.fixture()
|
| 69 |
def invitee():
|
|
@@ -83,9 +74,8 @@ def invitee():
|
|
| 83 |
yield invitee
|
| 84 |
class TestFriendRequest():
|
| 85 |
@pytest.mark.skipif("/friend_request" not in endpoints(),reason="Route not defined")
|
| 86 |
-
def test_post_friend(self,
|
| 87 |
# Call the firebase database
|
| 88 |
-
db = firestore.client(app=firebase_app)
|
| 89 |
friend_request_ref = db.collection('friend_request')
|
| 90 |
# Remove all the friend_request use for testing in the past
|
| 91 |
query = friend_request_ref.where(filter=FieldFilter("inviter", "==", inviter['id']))
|
|
@@ -160,4 +150,4 @@ class TestFriendRequest():
|
|
| 160 |
# Delete entity for next time test
|
| 161 |
user_ref.document(inviter['id']).delete()
|
| 162 |
user_ref.document(invitee['id']).delete()
|
| 163 |
-
|
|
|
|
| 11 |
from app.constants import deviceId
|
| 12 |
from fastapi.routing import APIRoute
|
| 13 |
from firebase_admin import firestore
|
| 14 |
+
from app import db
|
| 15 |
from google.cloud.firestore_v1.base_query import FieldFilter
|
| 16 |
def endpoints():
|
| 17 |
endpoints = []
|
|
|
|
| 55 |
data = response.json()
|
| 56 |
inviter = {"id": data['localId'], "token": data["idToken"]}
|
| 57 |
yield inviter
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
@pytest.fixture()
|
| 60 |
def invitee():
|
|
|
|
| 74 |
yield invitee
|
| 75 |
class TestFriendRequest():
|
| 76 |
@pytest.mark.skipif("/friend_request" not in endpoints(),reason="Route not defined")
|
| 77 |
+
def test_post_friend(self, client, inviter, invitee):
|
| 78 |
# Call the firebase database
|
|
|
|
| 79 |
friend_request_ref = db.collection('friend_request')
|
| 80 |
# Remove all the friend_request use for testing in the past
|
| 81 |
query = friend_request_ref.where(filter=FieldFilter("inviter", "==", inviter['id']))
|
|
|
|
| 150 |
# Delete entity for next time test
|
| 151 |
user_ref.document(inviter['id']).delete()
|
| 152 |
user_ref.document(invitee['id']).delete()
|
| 153 |
+
|