diff --git a/babblebox/babblebox/api/clients/consumer.py b/babblebox/babblebox/api/clients/consumer.py deleted file mode 100644 index 33d7577..0000000 --- a/babblebox/babblebox/api/clients/consumer.py +++ /dev/null @@ -1,32 +0,0 @@ -import pulsar -import io -from avro.io import DatumReader, BinaryDecoder -from avro.schema import Parse, Schema - -topic = 'persistent://babblebox/audio_processing/test' - -client = pulsar.Client('pulsar://10.2.115.98:6650') -print("Pulsar Python client version:", pulsar.__version__) - -consumer = client.subscribe(topic, subscription_name='my-sub') - -# Define your Avro schema -schema_path = '../schemas/chat_message_schema.avsc' -with open(schema_path, 'r') as file: - schema = Parse(file.read()) - - -def deserialize_from_avro(data, avro_schema): - bytes_reader = io.BytesIO(data) - decoder = BinaryDecoder(bytes_reader) - reader = DatumReader(avro_schema) - return reader.read(decoder) - - -while True: - msg = consumer.receive() - #print(deserialize_from_avro(msg.data(), schema)) - print("Received message: '%s'" % msg.data()) - consumer.acknowledge(msg) - -client.close() diff --git a/babblebox/babblebox/api/clients/pulsar_client_avro.py b/babblebox/babblebox/api/clients/pulsar_client_avro.py index fc3f2a3..417d9b7 100644 --- a/babblebox/babblebox/api/clients/pulsar_client_avro.py +++ b/babblebox/babblebox/api/clients/pulsar_client_avro.py @@ -1,25 +1,25 @@ +import logging import pulsar from avro.io import DatumWriter, BinaryEncoder import io from django.conf import settings - from pulsar.schema import BytesSchema topic = settings.NEW_MESSAGE_TOPIC client = None producer = None -if settings.DISABLE_PULSAR_CLIENT is True: - print("Pulsar is enabled") +if settings.DISABLE_PULSAR_CLIENT is False: + logging.info("Pulsar is enabled") try: client = pulsar.Client(settings.PULSAR_URL) - print("No client created") producer = client.create_producer(topic, schema=BytesSchema()) except Exception as e: - print("Exception raised" + str(e)) + logging.error("Exception raised" + str(e)) +else: + logging.info("Pulsar is disabled") class PulsarClient: @classmethod - # Function to serialize Django model to Avro def serialize_to_avro(cls, data, avro_schema): writer = DatumWriter(avro_schema) bytes_writer = io.BytesIO() @@ -33,6 +33,8 @@ def send_message(cls, data, schema): try: serialized_data = PulsarClient.serialize_to_avro(data, schema) producer.send(serialized_data) - print("Message sent to pulsar for new transcription") + logging.info(f"Message sent to pulsar for new transcription: {data}") except Exception as e: - print("Exception raised" + str(e)) + # convert below line to better string formatting + logging.error("Failed sending pulsar message with data: {}".format(data)) + logging.error("Exception raised: " + str(e)) diff --git a/babblebox/babblebox/api/schemas/chat_message_schema.avsc b/babblebox/babblebox/api/clients/schemas/chat_message_schema.avsc similarity index 74% rename from babblebox/babblebox/api/schemas/chat_message_schema.avsc rename to babblebox/babblebox/api/clients/schemas/chat_message_schema.avsc index 5e1e59d..c9d9d0e 100644 --- a/babblebox/babblebox/api/schemas/chat_message_schema.avsc +++ b/babblebox/babblebox/api/clients/schemas/chat_message_schema.avsc @@ -21,6 +21,14 @@ { "name": "timestamp", "type": "string" + }, + { + "name": "owner", + "type": "int" + }, + { + "name": "owner_username", + "type": "string" } ] -} \ No newline at end of file +} diff --git a/babblebox/babblebox/api/models.py b/babblebox/babblebox/api/models.py index 22c2c29..b1ddc8a 100644 --- a/babblebox/babblebox/api/models.py +++ b/babblebox/babblebox/api/models.py @@ -8,7 +8,6 @@ from django.conf import settings from config.settings.base import BASE_DIR -from babblebox.api.whisper.whisper import get_transcription class AudioFile(models.Model): @@ -24,7 +23,7 @@ def save(self, *args, **kwargs): # Generate a unique ID self.id = str(uuid.uuid4()) super(AudioFile, self).save(*args, **kwargs) - print(self.audio) + logging.info(f"Uploading audio file: ${self.audio}") # Generate transcription after the file is saved # Update the transcription fields self.file_location = settings.MEDIA_ROOT + "/" + str(self.audio) @@ -121,12 +120,10 @@ def get_messages_for_chat(cls, chat_id): @classmethod def get_avro_schema(cls): - schema_file = os.path.join(BASE_DIR, 'babblebox/api/schemas/chat_message_schema.avsc') - print(BASE_DIR) - print(schema_file) + schema_file = os.path.join(BASE_DIR, 'babblebox/api/clients/schemas/chat_message_schema.avsc') + logging.info(f"Schema file path: {schema_file}") with open(schema_file, 'r') as file: schema = parse(file.read()) - print(schema) return schema diff --git a/babblebox/babblebox/api/views.py b/babblebox/babblebox/api/views.py index 62bd328..64f3f9a 100644 --- a/babblebox/babblebox/api/views.py +++ b/babblebox/babblebox/api/views.py @@ -93,15 +93,14 @@ class ChatViewSet(viewsets.ModelViewSet): def perform_create(self, serializer): with transaction.atomic(): - print("Creating chat") # Save the Chat instance created by the serializer # Assuming the request includes the owner information. # You may need to adjust this based on how your owner is determined (e.g., from the request user) owner = self.request.user chat = serializer.save(owner=owner) - # Create a Participant instance for the owner with the necessary flags ChatParticipant.objects.create(chat=chat, user=owner, has_read_access=True, has_write_access=True) + logger.info(f"Chat created with id: {chat.id}") def get_permissions(self): diff --git a/babblebox/babblebox/api/whisper/__init__.py b/babblebox/babblebox/api/whisper/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/babblebox/babblebox/api/whisper/whisper.py b/babblebox/babblebox/api/whisper/whisper.py deleted file mode 100644 index 4ed9d80..0000000 --- a/babblebox/babblebox/api/whisper/whisper.py +++ /dev/null @@ -1,18 +0,0 @@ -from faster_whisper import WhisperModel - - -def get_transcription(file): - return 'test' - model_size = "small.en" - - model = WhisperModel(model_size, device="cpu", compute_type="int8") - segments, info = model.transcribe(file, beam_size=5, language="en", task='translate') - - print("Detected language '%s' with probability %f" % (info.language, info.language_probability)) - - text = "" - for segment in segments: - print("[%.2fs -> %.2fs] %s" % (segment.start, segment.end, segment.text)) - text += segment.text - - return text diff --git a/babblebox/config/settings/base.py b/babblebox/config/settings/base.py index ceddd53..c1f365c 100644 --- a/babblebox/config/settings/base.py +++ b/babblebox/config/settings/base.py @@ -4,6 +4,7 @@ from pathlib import Path import os import environ +import logging BASE_DIR = Path(__file__).resolve(strict=True).parent.parent.parent @@ -13,17 +14,17 @@ env = environ.Env() if env.ENVIRON.get("DJANGO_READ_LOCAL_ENV_FILE", default=False): - print(f"Using local env file. BASE_DIR: {BASE_DIR}") + logging.info(f"Using local env file. BASE_DIR: {BASE_DIR}") environ.Env.read_env(os.path.join(BASE_DIR, ".envs/.local/.django")) environ.Env.read_env(os.path.join(BASE_DIR, ".envs/.local/.postgres")) - print(f"Using Database: {env.db('DATABASE_URL')['HOST']}") + logging.info(f"Using Database: {env.db('DATABASE_URL')['HOST']}") # GENERAL # ------------------------------------------------------------------------------ # https://docs.djangoproject.com/en/dev/ref/settings/#debug DEBUG = True -DISABLE_PULSAR_CLIENT = env("DISABLE_PULSAR_CLIENT", default=False) +DISABLE_PULSAR_CLIENT = env("DISABLE_PULSAR_CLIENT", default=True) # Local time zone. Choices are # http://en.wikipedia.org/wiki/List_of_tz_zones_by_name diff --git a/babblebox/manage.py b/babblebox/manage.py index 0bcf03a..88d4384 100755 --- a/babblebox/manage.py +++ b/babblebox/manage.py @@ -2,10 +2,11 @@ import os import sys from pathlib import Path +import logging if __name__ == "__main__": os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.local") - print(f"Starting local server from path: {os.getcwd()}") + logging.info(f"Starting local server from path: {os.getcwd()}") try: from django.core.management import execute_from_command_line except ImportError: