Эх сурвалжийг харах

grammar correction AI integration

Mehmet Kırkoca 1 жил өмнө
parent
commit
63af1f2f66

+ 19 - 18
docker-compose.yml

@@ -16,23 +16,33 @@ services:
       - ui
 
   messageBroker:
-    image: rabbitmq:management
-    environment:
-      RABBITMQ_DEFAULT_USER: username
-      RABBITMQ_DEFAULT_PASS: password
     container_name: messageBroker
+    image: rabbitmq:management
     restart: unless-stopped
     ports:
       - 5672:5672
       - 15672:15672
+    environment:
+      RABBITMQ_DEFAULT_USER: username
+      RABBITMQ_DEFAULT_PASS: password
     networks:
       - socialMediaManagerNetwork
 
+  ai-grammer-correction:
+    build: ./services/ai_grammar_correction
+    volumes:
+      - ./services/ai_grammar_correction:/services/grammar_correction
+    networks:
+      - socialMediaManagerNetwork
+    depends_on:
+      - messageBroker
+    command: tail -f /dev/null
+
   gateway:
     build: ./services/gateway
     volumes:
+      - ./services/utils:/services/gateway/utils
       - ./services/gateway:/services/gateway
-      - rabbitmq-utils:/services/gateway/utils
     networks:
       - socialMediaManagerNetwork
     depends_on:
@@ -41,8 +51,8 @@ services:
   socket:
     build: ./services/socket
     volumes:
+      - ./services/utils:/services/socket/utils
       - ./services/socket:/services/socket
-      - rabbitmq-utils:/services/socket/utils
     networks:
       - socialMediaManagerNetwork
     depends_on:
@@ -51,8 +61,8 @@ services:
   formatter:
     build: ./services/formatter
     volumes:
+      - ./services/utils:/services/formatter/utils
       - ./services/formatter:/services/formatter
-      - rabbitmq-utils:/services/formatter/utils
     restart: unless-stopped
     networks:
       - socialMediaManagerNetwork
@@ -62,8 +72,8 @@ services:
   twitter:
     build: ./services/twitter
     volumes:
+      - ./services/utils:/services/twitter/utils
       - ./services/twitter:/services/twitter
-      - rabbitmq-utils:/services/twitter/utils
     restart: unless-stopped
     networks:
       - socialMediaManagerNetwork
@@ -73,8 +83,8 @@ services:
   linkedin:
     build: ./services/linkedin
     volumes:
+      - ./services/utils:/services/linkedin/utils
       - ./services/linkedin:/services/linkedin
-      - rabbitmq-utils:/services/linkedin/utils
     restart: unless-stopped
     networks:
       - socialMediaManagerNetwork
@@ -90,15 +100,6 @@ services:
     depends_on:
       - gateway
 
-volumes:
-  rabbitmq-utils:
-    name: "rabbitmq-utils-volume"
-    driver: local
-    driver_opts:
-      type: 'none'
-      o: 'bind'
-      device: '${PWD}/services/utils'
-
 networks:
   socialMediaManagerNetwork:
     driver: bridge

+ 27 - 0
services/ai_grammar_correction/app.py

@@ -0,0 +1,27 @@
+import pika
+import json
+from correction import correct_grammar
+
+def callback(ch, method, properties, body):
+    print("Received %r" % body)
+    data = json.loads(body.decode("utf-8"))
+    corrected_text = correct_grammar(data["message"])
+    print("Corrected: %s" % corrected_text)
+    ch.queue_declare(queue='fixedGrammar', durable=True)
+    ch.basic_publish(
+        exchange='',
+        routing_key='fixedGrammar',
+        body=corrected_text,
+        properties=pika.BasicProperties(
+            delivery_mode=2,  # Make message persistent
+        )
+    )
+            
+if __name__ == "__main__":
+    credentials = pika.PlainCredentials('username', 'password')
+    connection = pika.BlockingConnection(pika.ConnectionParameters('messageBroker', credentials=credentials))
+    channel = connection.channel()
+    channel.queue_declare(queue='fixGrammar', durable=True)
+    channel.basic_consume(queue='fixGrammar', on_message_callback=callback, auto_ack=True)
+    print('Waiting for messages. To exit press CTRL+C')
+    channel.start_consuming()

+ 11 - 0
services/ai_grammar_correction/correction.py

@@ -0,0 +1,11 @@
+from happytransformer import HappyTextToText, TTSettings
+
+# Initialize the model
+happy_tt = HappyTextToText("T5", "vennify/t5-base-grammar-correction")
+
+# Define the settings
+args = TTSettings(num_beams=5, min_length=1)
+
+def correct_grammar(text: str) -> str:
+    result = happy_tt.generate_text(f"grammar: {text}", args=args)
+    return result.text

+ 13 - 0
services/ai_grammar_correction/dockerfile

@@ -0,0 +1,13 @@
+# Use an official Python runtime as a parent image
+FROM python:3.9-slim
+
+COPY . /services/grammar_correction
+
+# Set the working directory in the container
+WORKDIR /services/grammar_correction
+
+# Install dependencies
+RUN python -m pip install pika --upgrade
+RUN pip install happytransformer
+
+CMD ["python", "app.py"]

+ 7 - 2
services/formatter/index.js

@@ -48,8 +48,13 @@ async function formatMessage(message) {
 }
 
 (async () => {
-  await rabbitMQListener.listenToQueue('formatter', (message) => {
-    console.log('Received message:', message);
+  rabbitMQListener.listenToQueue('fixedGrammar', (message) => {
+    console.log('Received message from fixedGrammar ', message);
     formatMessage(message);
   });
+  rabbitMQListener.listenToQueue('formatter', (message) => {
+    console.log('Received message formatter:', message);
+    console.log('Sending for fixing grammar:', message);
+    rabbitMQProducer.sendMessage('fixGrammar', JSON.stringify({message: message}));
+  });
 })();

+ 10 - 1
services/utils/RabbitMQListener.js

@@ -36,16 +36,25 @@ class RabbitMQListener {
       });
   
       console.log(`Listening for messages on queue "${queueName}"`);
+      return true;
     } catch (error) {
       console.error('Error consuming messages:', error.message);
+      return false;
     }
   }
 
   async listenToQueue(queueName, callback) {
     try {
-      await this.consumeFromQueue(queueName, async (message) => {
+      let connected = await this.consumeFromQueue(queueName, async (message) => {
         await callback(message);
       });
+      // retry with timer if failed
+      if(!connected) {  
+        setTimeout(() => {
+          this.listenToQueue(queueName, callback);
+        }, 5000);
+      }
+
     } catch (error) {
       console.error('Error while listening:', error.message);
     }