Skip to content

Commit

Permalink
adds docker-compose and logging
Browse files Browse the repository at this point in the history
  • Loading branch information
SonOfLope committed Jul 17, 2024
1 parent 45582e7 commit 05af5b4
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ FROM python:3.12-slim

WORKDIR /app

COPY . /app
COPY . .

RUN pip install --no-cache-dir -r requirements.txt

Expand Down
30 changes: 30 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
services:
point-virgule:
build: .
container_name: point-virgule
env_file:
- .env
networks:
- point-virgule

point:
build: ../point
container_name: point
ports:
- "5000:5000"
networks:
- point-virgule

# virgule:
# build: ../virgule
# container_name: virgule
# ports:
# - "5001:5001"
# networks:
# - point-virgule
# volumes:
# - .:/app

networks:
point-virgule:
driver: bridge
62 changes: 51 additions & 11 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
import os
import requests
import logging
from datetime import datetime
from dotenv import load_dotenv
from interactions import slash_command, GuildVoice, OptionType, SlashContext, ChannelType, slash_option, Client

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

class RecorderBot(Client):
def __init__(self, recording_path: str, *args, **kwargs):
def __init__(self, recording_path: str, api_url: str, *args, **kwargs):
super().__init__(*args, **kwargs)
self.recording_path = recording_path
self.point_url = api_url
self.active_recordings = {}

@slash_command(name="start_meeting", description="Commencer l'enregistrement d'une réunion")
@slash_option(
name="channel",
description="Sélectionnez le canal vocal pour enregistrer",
description="Choisir le canal vocal dans lequel enregistrer la réunion",
required=True,
opt_type=OptionType.CHANNEL,
channel_types=[ChannelType.GUILD_VOICE]
)
async def start_meeting(self, ctx: SlashContext, channel: GuildVoice):
await ctx.send("Connexion au canal vocal...")
await channel.connect()
await ctx.voice_state.start_recording()
self.active_recordings[ctx.guild_id] = channel
await ctx.send("Enregistrement démarré. Utilisez `/stop_meeting` pour arrêter l'enregistrement et déconnecter.")
voice_state = await channel.connect()
await ctx.send("Enregistrement démarré.")
self.active_recordings[ctx.guild_id] = voice_state
await ctx.send("Utilisez `/stop_meeting` pour arrêter l'enregistrement et déconnecter.")
logger.info(f"Enregistrement démarré dans la guilde: {ctx.guild_id}")


@slash_command(name="stop_meeting", description="Arrêter l'enregistrement d'une réunion")
async def stop_meeting(self, ctx: SlashContext):
Expand All @@ -31,25 +39,57 @@ async def stop_meeting(self, ctx: SlashContext):
await ctx.send("Déconnexion du canal vocal...")
await ctx.voice_state.stop_recording()
await channel.disconnect()
self.save_audio(ctx)
file_path = self.save_audio(ctx)
del self.active_recordings[ctx.guild_id]
await ctx.send("Enregistrement arrêté et sauvegardé.")
await ctx.send("Enregistrement arrêté. Envoi du fichier audio pour transcription...")

logger.info(f"Enregistrement arrêté dans la guilde: {ctx.guild_id}")

transcript = self.get_transcript(file_path)
if transcript:
await ctx.send(f"Transcription:\n{transcript}")
logger.info(f"Transcription réussie: {transcript[:100]}")
else:
await ctx.send("Erreur lors de la transcription de l'audio.")
logger.error(f"Erreur lors de la transcription de l'audio pour la guilde {ctx.guild_id}")

self.delete_audio(file_path)
else:
await ctx.send("Je n'enregistre pas dans ce serveur Discord.")
logger.error(f"Enregistrement non trouvé pour la guilde {ctx.guild_id}")

def save_audio(self, ctx: SlashContext):
"""Enregistrer les données audio pour chaque utilisateur dans l'enregistreur d'état vocal."""
for user_id, audio_data in ctx.voice_state.recorder.output.items():
timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
file_name = f"{user_id}_{timestamp}.mp3"
file_path = os.path.join(self.recording_path, file_name)
os.makedirs(os.path.dirname(file_path), exist_ok=True)
with open(file_path, "wb") as file:
file.write(audio_data.read())
return file_path

def get_transcript(self, file_path):
try:
with open(file_path, 'rb') as audio_file:
response = requests.post(self.api_url, files={'file': audio_file}, headers={'Content-Type': 'audio/mp3'})
if response.status_code == 200:
logger.info(f"L'appel de l'API de transcription a réussi pour le fichier {file_path}")
return response.text
logger.error(f"L'appel de l'API de transcription a échoué pour le fichier {file_path} avec le code d'état {response.status_code}")
return None
except Exception as e:
logger.error(f"Une erreur s'est produite lors de l'appel de l'API de transcription pour le fichier {file_path}: {e}")
return None

def delete_audio(self, file_path):
if os.path.exists(file_path):
os.remove(file_path)

if __name__ == "__main__":
load_dotenv()
token = os.getenv("DISCORD_TOKEN")
recording_path = os.getenv("RECORDING_PATH", "./recordings/")
bot = RecorderBot(recording_path)
api_url = os.getenv("TRANSCRIPTION_API_URL", "http://127.0.0.1:5000/transcript")
bot = RecorderBot(recording_path, api_url)
logger.info(f"{bot.user.name} démarré.")
bot.start(token)
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
discord-py-interactions
discord-py-interactions~=5.13.1
python-dotenv
pydub
requests

0 comments on commit 05af5b4

Please sign in to comment.