def _tranfer_file(self, destination, filename): print("transferring file... " + filename + " to " + destination) try: msg_id = self._next_msg_id() self.messageTimes[msg_id] = time.time() total_chunk = get_total_file_chunks(filename) current_chunk = 1 for file_buffer in get_file_chunks(filename): message = chat.Message() message.id = msg_id message.type = chat.File message.data = file_buffer message.destination = destination message.origin = self.username message.timestamp = int(time.time()) message.hops = 0 message.seqnum = current_chunk message.seqmax = total_chunk current_chunk += 1 chats.append(message) except FileNotFoundError as e: print("File not found:", e)
def ListMessages(self, request, context): with self._tracer.start_span( "ListMessages", child_of=context.get_active_span().context) as scope: # redis doesn't automatically assign the child context (https://github.com/opentracing-contrib/python-redis/issues/10) with self._tracer.start_span("Fetching Messages", child_of=scope): messages = self._redis_client.get("messages") with self._tracer.start_span("Parsing Messages", child_of=scope): messages = json.loads(messages) with self._tracer.start_span("Serializing Messages", child_of=scope): messages = list( map( lambda message: chat_pb2.Message( id=message["id"], message=message["message"]), messages, )) with self._tracer.start_span("Serializing Response", child_of=scope): response = chat_pb2.ListMessagesResponse(messages=messages) return response
def message_decrypted(self, message): mess = chat.Message() mess.to = self.encrypt.decrypt(message.to) message.method = self.encrypt.encrypt(message.method) usr = chat.UserData() usr.name = self.encrypt.encrypt(message.user.name) usr.note = self.encrypt.encrypt(message.user.note) message.user = usr return mess
def PostMessage(self, request, context): logging.info('PostMessage(): nick="{0}", text="{1}"'\ .format(request.nick, request.text)) with self.lock: for queue in self.queues: message = chat_pb2.Message(nick=request.nick, text=request.text) queue.put(message) return chat_pb2.Empty()
def _post_messages(self): while self.running: element = self.send_queue.get() if element is None: break try: message = chat_pb2.Message(nick=element[0], text=element[1]) self.stub.PostMessage(message) except grpc.RpcError as e: logging.error('RPC error: {0}'.format(e))
def message_encrypted(self, to, method, user): message = chat.Message() message.to = self.encrypt.encrypt(to) message.method = self.encrypt.encrypt(method) usr = chat.UserData() usr.name = self.encrypt.encrypt(user.name) usr.note = self.encrypt.encrypt(user.note) chat.Message.user = usr message.user = usr return message
def GetMessages(self, request, context): # xxx: leaks queues! queue = self.get_queue() while True: # todo: completion from client? try: message = queue.get(timeout=10) except Queue.Empty: message = chat_pb2.Message(nick='server', text='ping') yield message self.put_queue(queue) # todo: with or try block
def send_message(self, chat_stub, user_stub): ''' This method is called when user enters something ''' while(True): m = input('[{}]'.format(self.username)) if m is not '': n = chat.Message() n.name = self.encrypt.encrypt(self.username) n.note = self.encrypt.encrypt(m) chat_stub.SendMsg(n)
def get_users(self, chat_stub, user_stub): self.userslist = [] for _usr in user_stub.GetUsers(chat.Empty()): self.userslist.append(self.encrypt.decrypt(_usr.name)) message = chat.Message() message.name = self.encrypt.encrypt('Spartan') print_string = 'User list:' for usr in self.userslist: print_string += '{},'.format(usr) message.note = self.encrypt.encrypt(print_string[:-1]) chat_stub.SendMsg(message)
def _send_message(self, msg, destination): message = chat.Message() message.id = 1 message.type = 1 message.data = str.encode(msg) message.destination = destination message.origin = self.username message.timestamp = int(time.time()) message.hops = 0 try: self.conn.Send(message) except grpc.RpcError as e: log_error("Unable to send msg: " + msg)
def _send_message(self, destination, msg): message = chat.Message() message.id = self._next_msg_id() message.type = chat.Text message.data = str.encode(" ".join(msg)) message.destination = destination message.origin = self.username message.timestamp = int(time.time()) message.hops = 0 message.seqnum = 1 message.seqmax = 1 chats.append(message)
def Chat(self, request, context): context = [request.text] input_ids = (torch.tensor([ token_id for utterance in context for token_id in self.tokenizer.encode(utterance, out_type=int) + [self.config.sept_token_id] ]).unsqueeze(0).to(self.device)) if self.decoding_method == "top_p": outputs = self.model.generate( input_ids=input_ids, max_length=48, min_length=8, temperature=1.0, do_sample=True, top_p=0.8, pad_token_id=self.config.pad_token_id, bos_token_id=self.config.bos_token_id, eos_token_id=self.config.eos_token_id, repetition_penalty=1.3, no_repeat_ngram_size=3, num_return_sequences=10, ) elif self.decoding_method == "beam_search": outputs = model.generate( input_ids=input_ids, max_length=48, min_length=8, num_beams=10, pad_token_id=self.config.pad_token_id, bos_token_id=self.config.bos_token_id, eos_token_id=self.config.eos_token_id, repetition_penalty=1.3, no_repeat_ngram_size=3, num_return_sequences=10, ) else: raise ValueError( f"Invalid decoding method: {self.decoding_method}") output = self.tokenizer.decode(outputs.tolist()[0]) output = postprocess(output) logger.info("%s <--> %s", request.text, output) response = chat_pb2.Message(text=output) return response
def CreateMessage(self, request, context): with self._tracer.start_span( "CreateMessage", child_of=context.get_active_span().context) as scope: id = uuid4() message = request.message with self._tracer.start_span("Publishing Message", child_of=scope) as publishing_scope: headers = {} self._tracer.inject(publishing_scope.context, opentracing.Format.TEXT_MAP, headers) headers = [(key, value.encode('utf-8')) for key, value in headers.items()] self._kafka_producer.send("message-created", { "id": str(id), "message": message }, headers=headers) with self._tracer.start_span("Serializing Response", child_of=scope): return chat_pb2.Message(id=str(id), message=message)
def send_message(self, msg): self.stub.Send(chat_pb2.Message(text=msg)) logging.info("client send message")
def Send(self, request, context): logging.info("client received message") display(request.text) return chat_pb2.Message(text="got it")
def run(): with grpc.insecure_channel("ai.vricarus.com:50081") as channel: stub = chat_pb2_grpc.ChattingStub(channel) my_message = chat_pb2.Message(text="안녕 뭐하니") response = stub.Chat(request=my_message) print("response:", response.text)