morpheus65535 / bazarr

Bazarr is a companion application to Sonarr and Radarr. It manages and downloads subtitles based on your requirements. You define your preferences by TV show or movie and Bazarr takes care of everything for you.
https://www.bazarr.media
GNU General Public License v3.0
2.91k stars 225 forks source link

API of some kind ? (for a GPTsubtrans adaptation) #2450

Closed Maitresinh closed 7 months ago

Maitresinh commented 7 months ago

Someone (IlmariKu) on the GPT subtrans Github is willing to adapt it to bazarr for unraid, and he's asking if there is an API to to so (or any other way). GPT subrans would be a great improvemnt for translation...

VampiricAlien commented 7 months ago

If using Whisper the app requires asr-webservice to hook into, https://wiki.bazarr.media/Additional-Configuration/Whisper-Provider/ or you might be able to use web hooks.

morpheus65535 commented 7 months ago

And here's the provider's code that communicate with Whisper: https://github.com/morpheus65535/bazarr/blob/development/custom_libs/subliminal_patch/providers/whisperai.py

morpheus65535 commented 7 months ago

No news, good news!

Maitresinh commented 2 months ago

i've tried to build it with the help of ChatGPT. Nothing working until now

Here is a detailed step-by-step guide for installing GPT-Subtrans with Bazarr, including configuration for automatic subtitle translation and manual subtitle selection using a GUI or frontend.

Step 1: Setting up the Environment Ensure Docker is installed on your unRAID server if it’s not already set up. Install Bazarr on unRAID: Configure Bazarr to manage your media library's subtitles by linking it to your downloaders (Transmission, etc.) and media managers (Sonarr, Radarr). Step 2: Create the Docker Container for GPT-Subtrans

  1. Create a Dockerfile for GPT-Subtrans Create a directory for GPT-Subtrans:

bash Copier le code mkdir -p /mnt/user/appdata/gpt-subtrans-gui cd /mnt/user/appdata/gpt-subtrans-gui Create a Dockerfile in this directory:

bash Copier le code nano Dockerfile Add the following content to the Dockerfile:

dockerfile Copier le code FROM python:3.10-slim

Install git, fluxbox, xvfb, and other dependencies for GUI

RUN apt-get update && apt-get install -y git fluxbox x11vnc xvfb

Clone the GPT-Subtrans repository

RUN git clone https://github.com/machinewrapped/gpt-subtrans.git /app/gpt-subtrans

Install GPT-Subtrans dependencies

WORKDIR /app/gpt-subtrans RUN bash install.sh

Install Flask for frontend (optional)

RUN pip install flask

Install necessary translation SDKs

RUN pip install openai google.generativeai anthropic

Start a VNC server to allow GUI access

CMD ["bash", "-c", "xvfb-run -s '-screen 0 1024x768x16' python3 gui-subtrans.py"] Save and exit (CTRL + X, then Y, then Enter).

  1. Build the Docker Image Navigate to the directory where the Dockerfile is located:

bash Copier le code cd /mnt/user/appdata/gpt-subtrans-gui Build the Docker image from the Dockerfile:

bash Copier le code docker build -t gpt-subtrans-gui:latest .

  1. Configure the OpenAI API Key You need to include your OpenAI API key for GPT-Subtrans to function. You can do this either by using a .env file or as an environment variable in Docker.

Option 1: Use a .env File for the API Key Create a .env file in the GPT-Subtrans directory:

bash Copier le code nano /mnt/user/appdata/gpt-subtrans-gui/.env Add your OpenAI API key to the file:

bash Copier le code OPENAI_API_KEY=your_openai_api_key Save and exit (CTRL + X, then Y, then Enter).

Option 2: Pass the API Key as an Environment Variable If you prefer not to use a .env file, you can pass the API key as a Docker environment variable during container creation.

  1. Create and Start the Docker Container Create a Docker container from the image:

If you used the .env file for the API key:

bash Copier le code docker create \ --name=gpt-subtrans \ -v /mnt/user/data/media:/media \ -p 5900:5900 \ gpt-subtrans-gui:latest OR, if you pass the API key as an environment variable:

bash Copier le code docker create \ --name=gpt-subtrans \ -v /mnt/user/data/media:/media \ -e OPENAI_API_KEY="your_openai_api_key" \ -p 5900:5900 \ gpt-subtrans-gui:latest -p 5900:5900: Exposes the port for accessing the GUI via VNC. OPENAI_API_KEY="your_openai_api_key": Replace with your actual OpenAI API key if passing it directly. Start the container:

bash Copier le code docker start gpt-subtrans Access the GUI (Optional):

Use a VNC client (e.g., RealVNC or TightVNC) to connect to your unRAID server at :5900. The GUI will allow you to manually select and translate subtitles using GPT-Subtrans. Step 3: Automate Subtitle Translation with Bazarr

  1. Create a Post-Processing Script for Bazarr Create a post-processing script for Bazarr to automatically invoke GPT-Subtrans after subtitles are downloaded:

bash Copier le code nano /mnt/user/appdata/bazarr/gpt_subtrans_postprocess.sh Add the following code to the script:

bash Copier le code

!/bin/bash

Environment variables passed by Bazarr

SUBTITLE_PATH="${SUBTITLE_PATH}" SUBTITLE_LANGUAGE_CODE3="${SUBTITLE_LANGUAGE_CODE3}"

echo "Subtitle file path: $SUBTITLE_PATH" echo "Subtitle language: $SUBTITLE_LANGUAGE_CODE3"

if [[ "$SUBTITLE_LANGUAGE_CODE3" == "fre" ]]; then echo "Subtitles are already in French. No translation needed." else echo "Translating subtitles $SUBTITLE_PATH to French." docker exec gpt-subtrans python3 /app/gpt-subtrans/batch_process.py "$SUBTITLE_PATH" --target-lang fr fi Make the script executable:

bash Copier le code chmod +x /mnt/user/appdata/bazarr/gpt_subtrans_postprocess.sh

  1. Configure Bazarr to Use the Script Open Bazarr's web interface.

Go to Settings > Processing > Post-Processing Scripts.

Add the path to the post-processing script:

bash Copier le code /mnt/user/appdata/bazarr/gpt_subtrans_postprocess.sh Bazarr will now trigger GPT-Subtrans to translate the subtitles after downloading them.

Optional: Set Up a Frontend for Subtitle Selection If you want a more visual way to choose which subtitles to translate, you can set up a Flask-based frontend.

  1. Create a Flask Frontend for Subtitle Selection Create an application directory:

bash Copier le code mkdir /mnt/user/appdata/gpt-subtrans-gui/app cd /mnt/user/appdata/gpt-subtrans-gui/app Create a Python file for the Flask app:

bash Copier le code nano app.py Add the following code:

python Copier le code from flask import Flask, render_template, request import os import subprocess

app = Flask(name)

@app.route('/') def index():

List all .srt files in the /media directory

subtitles = []
for root, dirs, files in os.walk("/media"):
    for file in files:
        if file.endswith(".srt"):
            subtitles.append(os.path.join(root, file))
return render_template('index.html', subtitles=subtitles)

@app.route('/translate', methods=['POST']) def translate(): subtitle_file = request.form['subtitle']

Call GPT-Subtrans to translate the subtitles

subprocess.call(['docker', 'exec', 'gpt-subtrans', 'python3', '/app/gpt-subtrans/batch_process.py', subtitle_file, '--target-lang', 'fr'])
return f"Translating {subtitle_file} to French"

if name == 'main': app.run(debug=True, host='0.0.0.0', port=5000) Create the HTML template in the templates/ directory:

bash Copier le code mkdir templates nano templates/index.html Add the following HTML:

html Copier le code <!DOCTYPE html>

GPT-Subtrans Frontend

Available Subtitles

Rebuild the Docker image with Flask:

bash Copier le code docker build -t gpt-subtrans-gui:latest . Start the Flask frontend:

The Flask app will be available at http://:5000, allowing you to select subtitles manually for translation.