-
Notifications
You must be signed in to change notification settings - Fork 0
/
localping.py
45 lines (36 loc) · 1.64 KB
/
localping.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import grpc
import nsfw_detector_pb2
import nsfw_detector_pb2_grpc
import os
import jwt
import base64
token_fpath = "/Users/jaydhanwant/Documents/SS/nsfw_jwt_token.txt"
with open(token_fpath, 'r') as f:
_JWT_TOKEN = f.read()
server_url = 'localhost:50051'
def run():
channel = grpc.insecure_channel(server_url)
stub = nsfw_detector_pb2_grpc.NSFWDetectorStub(channel)
# Test video ID
video_id = "21727f4f6cbe496d9a410202537faff8"
# Test image URL
image_url = "https://img-cdn.pixlr.com/image-generator/history/65bb506dcb310754719cf81f/ede935de-1138-4f66-8ed7-44bd16efc709/medium.webp"
# Test image path for byte64
image_path = "/Users/jaydhanwant/Downloads/WhatsApp Image 2024-08-29 at 13.18.09.jpeg"
try:
print("Sending video ID request to server")
metadata = [('authorization', f'Bearer {_JWT_TOKEN}')]
video_response = stub.DetectNSFWVideoId(nsfw_detector_pb2.NSFWDetectorRequestVideoId(video_id=video_id), metadata=metadata)
print(video_response)
print("Sending image URL request to server")
url_response = stub.DetectNSFWURL(nsfw_detector_pb2.NSFWDetectorRequestURL(url=image_url), metadata=metadata)
print(url_response)
print("Sending image to byte64 request to server")
with open(image_path, "rb") as image_file:
image_byte64 = base64.b64encode(image_file.read()).decode('utf-8')
byte64_response = stub.DetectNSFWImg(nsfw_detector_pb2.NSFWDetectorRequestImg(image=image_byte64), metadata=metadata)
print(byte64_response)
except grpc.RpcError as e:
print(f"RPC failed: {e}")
if __name__ == '__main__':
run()