Skip to content
TheChatArchiveThe Chat Archive
Mistralmistral-medium-2505· Mistral AIHugging Face LMArena Human Preference 140K

craft a bash or c99 program using llama.cpp to natcase .srt subtitle files . should be…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with DeepSeekdeepseek-v3-0324’s answer.
  1. User

    craft a bash or c99 program using llama.cpp to natcase .srt subtitle files . should be context aware conversion. i have model gemma-3-1b, I could try others if required yet the machine is slow.

  2. Mistral Assistant

    Context-Aware SRT Subtitle Case Conversion with llama.cpp

    Here's a solution that uses llama.cpp with the Gemma-3-1b model to perform context-aware case conversion on SRT subtitle files. This approach will analyze the surrounding text to make intelligent decisions about capitalization.

    Bash Script Solution

    #!/bin/bash
    
    # Check if input file is provided
    if [ "$#" -ne 1 ]; then
        echo "Usage: $0 input.srt"
        exit 1
    fi
    
    INPUT_FILE="$1"
    OUTPUT_FILE="${INPUT_FILE%.srt}_natcase.srt"
    TEMP_FILE=$(mktemp)
    
    # Check if llama.cpp is available
    if ! command -v main &> /dev/null; then
        echo "Error: llama.cpp 'main' executable not found in PATH"
        exit 1
    fi
    
    # Process each subtitle block
    awk -v input="$INPUT_FILE" -v output="$OUTPUT_FILE" -v temp="$TEMP_FILE" '
    BEGIN {
        block = 0;
        print "Processing subtitle file..."
    }
    {
        if ($0 ~ /^[0-9]+$/) {
            # New block - print the number as-is
            block = 1;
            print $0 > temp;
        }
        else if ($0 ~ /^[0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]{3} --> [0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]{3}$/) {
            # Timing line - print as-is
            print $0 > temp;
        }
        else if ($0 == "") {
            # Empty line - end of block
            if (block) {
                # Process the text lines
                system("awk \047NR>1{print}\047 " temp " | main -m gemma-3-1b -p \"Convert the following subtitle text to natural case, preserving proper nouns and context-appropriate capitalization. Only output the converted text without any additional commentary:\" --temp 0.7 --repeat-penalty 1.1 | sed \0471d\047 >> " output);
                print "" >> output;
                block = 0;
                system("rm -f " temp);
                temp = "'"$(mktemp)"'";
            }
        }
        else {
            # Text line - collect for processing
            print $0 > temp;
        }
    }
    END {
        print "Conversion complete. Output saved to " output;
    }
    ' "$INPUT_FILE"
    

    C99 Program Solution

    For a more robust solution, here's a C program that interfaces with llama.cpp:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <sys/wait.h>
    
    #define MAX_LINE_LENGTH 1024
    #define MAX_BLOCK_LINES 20
    #define MAX_BLOCK_LENGTH 4096
    
    void process_block_with_llama(const char *block_text, FILE *output_file) {
        int pipefd[2];
        pid_t pid;
    
        if (pipe(pipefd) == -1) {
            perror("pipe");
            exit(EXIT_FAILURE);
        }
    
        pid = fork();
        if (pid == -1) {
            perror("fork");
            exit(EXIT_FAILURE);
        }
    
        if (pid == 0) { // Child process
            close(pipefd[1]); // Close unused write end
    
            // Redirect stdin from the pipe
            dup2(pipefd[0], STDIN_FILENO);
            close(pipefd[0]);
    
            // Execute llama.cpp command
            execlp("main", "main",
                   "-m", "gemma-3-1b",
                   "-p", "Convert the following subtitle text to natural case, preserving proper nouns and context-appropriate capitalization. Only output the converted text without any additional commentary:",
                   "--temp", "0.7",
                   "--repeat-penalty", "1.1",
                   NULL);
    
            perror("execlp");
            exit(EXIT_FAILURE);
        } else { // Parent process
            close(pipefd[0]); // Close unused read end
    
            // Write the block text to the pipe
            write(pipefd[1], block_text, strlen(block_text));
            close(pipefd[1]);
    
            // Wait for child to complete
            wait(NULL);
    
            // Read the output from the child process
            FILE *llama_output = popen("main -m gemma-3-1b -p \"Convert the following subtitle text to natural case, preserving proper nouns and context-appropriate capitalization. Only output the converted text without any additional commentary:\" --temp 0.7 --repeat-penalty 1.1", "r");
            if (llama_output) {
                char buffer[MAX_LINE_LENGTH];
                int first_line = 1;
    
                while (fgets(buffer, sizeof(buffer), llama_output) != NULL) {
                    if (first_line) {
                        first_line = 0;
                        continue; // Skip the first line which might be the prompt echo
                    }
                    fputs(buffer, output_file);
                }
                pclose(llama_output);
            }
        }
    }
    
    void process_srt_file(const char *input_filename, const char *output_filename) {
        FILE *input_file = fopen(input_filename, "r");
        if (!input_file) {
            perror("fopen input");
            exit(EXIT_FAILURE);
        }
    
        FILE *output_file = fopen(output_filename, "w");
        if (!output_file) {
            perror("fopen output");
            fclose(input_file);
            exit(EXIT_FAILURE);
        }
    
        char line[MAX_LINE_LENGTH];
        char block_text[MAX_BLOCK_LENGTH] = "";
        int in_block = 0;
        int block_line_count = 0;
    
        while (fgets(line, sizeof(line), input_file)) {
            // Check for block number
            if (strspn(line, "0123456789\n") == strlen(line) && strlen(line) > 1) {
                in_block = 1;
                block_line_count = 0;
                strncpy(block_text, line, sizeof(block_text));
                fputs(line, output_file);
            }
            // Check for timing line
            else if (strstr(line, "-->") && strchr(line, ':') && strchr(line, ',')) {
                strncat(block_text, line, sizeof(block_text) - strlen(block_text) - 1);
                fputs(line, output_file);
            }
            // Check for empty line (end of block)
            else if (strlen(line) <= 1) {
                if (in_block && block_line_count > 0) {
                    // Process the collected text lines
                    process_block_with_llama(block_text, output_file);
                    fputs("\n", output_file);
                    in_block = 0;
                }
                fputs("\n", output_file);
            }
            // Text line
            else if (in_block) {
                strncat(block_text, line, sizeof(block_text) - strlen(block_text) - 1);
                block_line_count++;
            }
        }
    
        fclose(input_file);
        fclose(output_file);
    }
    
    int main(int argc, char *argv[]) {
        if (argc != 2) {
            fprintf(stderr, "Usage: %s input.srt\n", argv[0]);
            return EXIT_FAILURE;
        }
    
        char output_filename[256];
        snprintf(output_filename, sizeof(output_filename), "%s_natcase.srt", argv[1]);
    
        process_srt_file(argv[1], output_filename);
    
        printf("Conversion complete. Output saved to %s\n", output_filename);
        return EXIT_SUCCESS;
    }
    

    Important Notes:

    1. Model Considerations: Gemma-3-1b is a good choice for this task, but you might want to try smaller models if performance is an issue. The gemma-2b or even tinyllama models might work better on slower machines.

    2. Prompt Engineering: The prompt is crucial for good results. You may need to adjust it based on your specific needs.

    3. Performance: On a slow machine, this will take significant time. Consider:

      • Processing in batches
      • Using a smaller model
      • Reducing the number of tokens processed at once
    4. Dependencies: You'll need to have llama.cpp properly installed with the model available.

    5. Alternative Approach: For better performance, you could pre-process the file to identify proper nouns and other elements that should remain capitalized, then use simpler rules for the rest.

    To compile the C program:

    gcc -o srt_natcase srt_natcase.c
    

    To run either solution:

    ./script_or_program input.srt
    

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.