An open API service indexing awesome lists of open source software.

https://github.com/zouari-oss/chunk-translator

FastAPI endpoint for chunked text translation
https://github.com/zouari-oss/chunk-translator

api fastapi json open-source python rest rest-api translator

Last synced: 1 day ago
JSON representation

FastAPI endpoint for chunked text translation

Awesome Lists containing this project

README

          

[![Contributors](https://img.shields.io/badge/CONTRIBUTORS-01-blue?style=plastic)](https://github.com/ZouariOmar/AgriGO/graphs/contributors)
[![Forks](https://img.shields.io/badge/FORKS-00-blue?style=plastic)](https://github.com/ZouariOmar/AgriGO/network/members)
[![Stargazers](https://img.shields.io/badge/STARS-01-blue?style=plastic)](https://github.com/ZouariOmar/AgriGO/stargazers)
[![Issues](https://img.shields.io/badge/ISSUES-00-blue?style=plastic)](https://github.com/ZouariOmar/AgriGO/issues)
[![GPL3.0 License](https://img.shields.io/badge/LICENSE-GPL3.0-blue?style=plastic)](LICENSE)
[![Linkedin](https://img.shields.io/badge/Linkedin-6.9k-blue?style=plastic)](https://www.linkedin.com/in/zouari-omar-143239283)




chunk-translator


chunk-translator


FastAPI endpoint for chunked text translation


Overview
Key Features
Usage
Emailware
License
Contact
Acknowledgments


Python
API
FastAPI
REST API
JSON

## Overview

**chunk-translator** is a lightweight API built with **FastAPI** that translates large text safely by splitting it into manageable chunks.

Many translation models or APIs fail when processing very large inputs. This project solves that by:

- Splitting large text into chunks
- Translating each chunk independently
- Reassembling the translated output in the correct order

> The result is reliable translation **without losing lines or breaking formatting**.

## Key Features

- **FastAPI powered REST API**
- **Automatic text chunking**
- **Supports large document translation**
- **Reassembles chunks in correct order**
- **Prevents dropped lines during translation**
- **Simple JSON API interface**
- **Easy to integrate into pipelines**

## Usage

### 1. Install dependencies

```bash
pip install -r requirements.txt
```

### 2. Run the API

```bash
fastapi run
```

### 3. Use `/translate`

curl

```bash
curl -s \
--header "Content-Type: application/json" \
--request "POST" \
--data '{
"text": "Hello world",
"source_lang": "en",
"target_lang": "fr"
}' \
http://127.0.0.1:8000/translate
```

JavaScript (js)

```js
const response = await fetch("http://127.0.0.1:8000/translate", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
text: "Hello world",
source_lang: "en",
target_lang: "fr",
}),
});

const data = await response.json();
console.log(data);
```

Java

```java
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

public class TranslateClient {
public static void main(String[] args) throws Exception {
HttpClient client = HttpClient.newHttpClient();
String json = """
{
"text": "Hello world",
"source_lang": "en",
"target_lang": "fr"
}
""";

HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("http://127.0.0.1:8000/translate"))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(json))
.build();

HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
}
}
```

C

```c
#include

int main(void) {
CURL *curl = curl_easy_init();
if (!curl) return 1;

const char *json =
"{\"text\":\"Hello world\",\"source_lang\":\"en\",\"target_lang\":\"fr\"}";

struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Content-Type: application/json");

curl_easy_setopt(curl, CURLOPT_URL, "http://127.0.0.1:8000/translate");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json);
curl_easy_setopt(curl, CURLOPT_POST, 1L);
curl_easy_perform(curl);

curl_slist_free_all(headers);
curl_easy_cleanup(curl);
return 0;
}
```

C++ (cpp)

```cpp
#include
#include

int main() {
CURL* curl = curl_easy_init();
if (!curl) return 1;

std::string json =
R"({"text":"Hello world","source_lang":"en","target_lang":"fr"})";

struct curl_slist* headers = nullptr;
headers = curl_slist_append(headers, "Content-Type: application/json");

curl_easy_setopt(curl, CURLOPT_URL, "http://127.0.0.1:8000/translate");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json.c_str());
curl_easy_setopt(curl, CURLOPT_POST, 1L);
curl_easy_perform(curl);

curl_slist_free_all(headers);
curl_easy_cleanup(curl);
return 0;
}
```

### 4. Example Response

```JSON
{
"status": 200,
"translated_text": "Translated output..."
}
```

## Emailware

chunk-translator is an [emailware](https://en.wiktionary.org/wiki/emailware). Meaning, if you liked using this app or it has helped you in any way, I'd like you send me an email at about anything you'd want to say about this software. I'd really appreciate it!

## License

This repository is licensed under the **GPL-3.0 License**. You are free to use, modify, and distribute the content. See the [LICENSE](LICENSE) file for details.

## Contact

For questions or suggestions, feel free to reach out:

- **GitHub**: [ZouariOmar](https://github.com/ZouariOmar)
- **Email**:
- **LinkedIn**: [Zouari Omar](https://www.linkedin.com/in/zouari-omar)

## Acknowledgments

Built with ❤️ for the open source community

> **Happy Coding!**