Sign inSign up

d9media/openai-text-rewriter

By d9media

•Updated 6 months ago

Small Flask-based Docker service for rewriting text/HTML via the OpenAI API for use in Plesk

Image
0

1.7K

d9media/openai-text-rewriter repository overview

⁠OpenAI Rewriter Docker Service

This project provides a simple Docker-based service for rewriting content using the OpenAI API. It is designed to be multi-architecture compatible (x86_64, arm64, etc.), allowing it to run on various platforms, including Macs with M1/M2 chips. The service accepts content via a POST request and returns rewritten content.

⁠Features

  • Accepts OpenAI prompts and content via POST requests.
  • Requires authentication via a custom server API key.
  • Supports configurable OpenAI parameters (model, max_completion_tokens, max_tokens, temperature).
  • Compatible with both x86_64 (amd64) and arm64 architectures.
  • Responses are returned as base64-encoded HTML, maintaining original formatting.

⁠Environment Variables

  • OPENAI_API_KEY: Your OpenAI API key (required).
  • SERVER_API_KEY: Custom API key for authenticating server requests (required) - this can be any text string you define. Recommended length: 128.
  • OPENAI_MODEL: Default OpenAI model to use when a request does not include model (default: gpt-4o-mini).
  • SERVER_PORT: The port the server listens on (default: 5000).

⁠Usage

  1. Build the Docker Image

    docker buildx build --platform linux/amd64,linux/arm64 -t your_dockerhub_username/openai-rewriter-enhanced --push .
    
  2. Run the Docker Container

    Replace your_openai_api_key and your_server_api_key with your actual keys:

    docker run -d -p 5000:5000 \
    -e OPENAI_API_KEY=your_openai_api_key \
    -e SERVER_API_KEY=your_server_api_key \
    -e OPENAI_MODEL=gpt-4o-mini \
    -e SERVER_PORT=5000 \
    your_dockerhub_username/openai-rewriter-enhanced
    
  3. API Request

    The service listens on the root endpoint (/) and accepts POST requests with the following parameters:

    ⁠Request Parameters
    • api_key: The server's custom API key to authenticate requests. (Not your OpenAI API Key. OpenAI is set in your docker container.)
    • task_prompt: The task description for the OpenAI model (e.g., "Rewrite the following article").
    • content: Base64-encoded HTML content to be rewritten.
    • model (optional): Chat Completions-compatible OpenAI model ID to use for this request. Falls back to OPENAI_MODEL.
    • max_completion_tokens (optional): Maximum number of tokens to generate (default: 500).
    • max_tokens (optional): Backward-compatible alias for max_completion_tokens.
    • temperature (optional): The creativity level of the response. Omit it to use the selected model's default.
    ⁠Example PHP Usage

    You can use the following PHP code to make a request to the service and retrieve the rewritten content:

    function rewrite_content($api_key, $task_prompt, $content) {
        $url = 'http://localhost:5000';
        
        // Base64 encode the content
        $encoded_content = base64_encode($content);
    
        // Prepare the data for the POST request
        $data = json_encode([
            'api_key' => $api_key,
            'task_prompt' => $task_prompt,
            'content' => $encoded_content,
            'model' => 'gpt-4o-mini', // optional
            'max_completion_tokens' => 600, // optional
            'temperature' => 0.5  // optional
        ]);
    
        // Use cURL to make the request
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, [
            'Content-Type: application/json'
        ]);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    
        // Execute the request
        $response = curl_exec($ch);
        curl_close($ch);
    
        // Parse the JSON response
        $responseData = json_decode($response, true);
    
        // Decode the base64 content and return it
        if (isset($responseData['rewritten_content'])) {
            return base64_decode($responseData['rewritten_content']);
        } else {
            return 'Error rewriting content';
        }
    }
    

⁠Example Request

The following example shows the structure of a request to the service:

{
    "api_key": "your_server_api_key",
    "task_prompt": "Rewrite the following article:",
    "content": "base64_encoded_html_content",
    "model": "gpt-4o-mini",
    "max_completion_tokens": 600,
    "temperature": 0.5
}

⁠Source code

https://github.com/d9media/openai-text-rewriter⁠

⁠Pull

docker pull d9media/openai-text-rewriter:latest

⁠License

This project is licensed under the MIT License.

Tag summary

Content type

Image

Digest

sha256:4b4deb93f…

Size

52.2 MB

Last updated

6 months ago

docker pull d9media/openai-text-rewriter