app.py 1.0 KB

123456789101112131415161718192021222324252627
  1. import pika
  2. import json
  3. from correction import correct_grammar
  4. def callback(ch, method, properties, body):
  5. print("Received %r" % body)
  6. data = json.loads(body.decode("utf-8"))
  7. corrected_text = correct_grammar(data["message"])
  8. print("Corrected: %s" % corrected_text)
  9. ch.queue_declare(queue='fixedGrammar', durable=True)
  10. ch.basic_publish(
  11. exchange='',
  12. routing_key='fixedGrammar',
  13. body=corrected_text,
  14. properties=pika.BasicProperties(
  15. delivery_mode=2, # Make message persistent
  16. )
  17. )
  18. if __name__ == "__main__":
  19. credentials = pika.PlainCredentials('username', 'password')
  20. connection = pika.BlockingConnection(pika.ConnectionParameters('messageBroker', credentials=credentials))
  21. channel = connection.channel()
  22. channel.queue_declare(queue='fixGrammar', durable=True)
  23. channel.basic_consume(queue='fixGrammar', on_message_callback=callback, auto_ack=True)
  24. print('Waiting for messages. To exit press CTRL+C')
  25. channel.start_consuming()