Scikit-LLM Combines Translation and Zero-Shot Classification for Multilingual Text
- Cargo
- Machine Learning
- Shipped
- Shipper
- Iván Palomares Carrascosa
- Readers
- 840
A scikit-learn pipeline translates multilingual text into English, then classifies it with an LLM using category labels instead of a large training set.


Multilingual text classification can be built as a sequence of two language model tasks: translate each input into a shared language, then assign it to a category with a zero-shot classifier. Scikit-LLM wraps both steps in components that fit into a scikit-learn pipeline, so teams can compose translation and classification without building a separate model for every input language.
The example uses English as the common language and classifies documents into Energy, Finance, or Healthcare. It demonstrates the mechanics on six multilingual examples and three unseen texts. Those predictions illustrate the workflow, though they aren’t a benchmark of accuracy or evidence that the pipeline will handle every language and domain equally well.
Configure the API and define categories
The notebook runs in Google Colab and routes Scikit-LLM requests to Groq’s OpenAI-compatible API. It requires a Groq API key stored in Colab as GROQ_API_KEY, then installs Scikit-LLM:
!pip install scikit-llm -qScikit-LLM’s configuration points to Groq’s endpoint and supplies the key. The example describes this route as a way to use free models and avoid per-token costs; actual availability and pricing depend on the provider’s current terms and model access.
from google.colab import userdata
from skllm.config import SKLLMConfig
groq_key = userdata.get('GROQ_API_KEY')
SKLLMConfig.set_gpt_url("https://api.groq.com/openai/v1")
SKLLMConfig.set_openai_key(groq_key)The sample data contains French, German, and Spanish text, with two examples each for Energy, Finance, and Healthcare. These labeled examples show the intended categories, but the classifier is configured with the unique label names rather than being trained on a large set of examples:
X_multilingual_texts = [
"La technologie des panneaux solaires a vu son efficacité augmenter de 15%.",
"Neue Batteriespeicher für Windenergie werden immer wirtschaftlicher.",
"Los mercados financieros sufrieron una fuerte caída hoy tras la subida de tipos.",
"Les actions technologiques ont rebondi grâce à de solides rapports sur les bénéfices.",
"Die klinische Studie für das neue Medikament reduzierte die Schwellung um 40%.",
"Un ensayo reciente demostró que la vacuna es muy eficaz para prevenir infecciones.",
]
y_labels = ["Energy", "Energy", "Finance", "Finance", "Healthcare", "Healthcare"]Chain translation and classification
The pipeline has two steps. GPTTranslator translates input into English, and ZeroShotGPTClassifier predicts a category from the translated text. Both use the same model identifier, custom_url::openai/gpt-oss-20b; separate Scikit-LLM classes set the task for each model instance.
To satisfy the scikit-learn interface, the code calls fit() with an empty placeholder for each category. The labels passed to that call are the category names themselves. This setup avoids conventional classifier training, but it also means the example provides no labeled examples to calibrate classification behavior through fit().
from sklearn.pipeline import Pipeline
from skllm.models.gpt.text2text.translation import GPTTranslator
from skllm.models.gpt.classification.zero_shot import ZeroShotGPTClassifier
text_pipeline = Pipeline([
('translator', GPTTranslator(
model="custom_url::openai/gpt-oss-20b",
output_language="English"
)),
('classifier', ZeroShotGPTClassifier(
model="custom_url::openai/gpt-oss-20b"
))
])
y_labels_unique = ["Energy", "Finance", "Healthcare"]
X_placeholder = [""] * len(y_labels_unique)
text_pipeline.fit(X_placeholder, y_labels_unique)Before classification, the translator can be called on the six examples directly. The displayed outputs preserve the main claims in the input: solar panel efficiency increased by 15%, a clinical study reduced swelling by 40%, and the other texts cover battery storage, financial markets, technology stocks, and vaccine effectiveness. Inspecting translations is useful because a mistranslated detail can affect the downstream category.
Predictions on unseen languages
The final examples are in Italian and Portuguese, languages absent from the sample training texts. They describe an influenza vaccine, a sodium-ion battery that stores excess wind energy, and technology stocks recovering during a trading session. The pipeline translates each one into English and returns Healthcare, Energy, and Finance, respectively.
X_new_multilingual = [
"Un nuovo vaccino antinfluenzale ha mostrato un'efficacia dell'85% negli studi clinici, promettendo difese immunitarie più forti.",
"I ricercatori hanno sviluppato una batteria agli ioni di sodio che immagazzina l'energia eolica in eccesso in modo molto economico.",
"As ações de tecnologia se recuperaram no final do pregão, impulsionadas por fortes relatórios de lucros corporativos."
]
predictions = text_pipeline.predict(X_new_multilingual)
new_translations = text_pipeline.named_steps['translator'].transform(X_new_multilingual)
for text, translation, prediction in zip(X_new_multilingual, new_translations, predictions):
print(f"Original Document: '{text}'")
print(f"Intermediate Translation: '{translation}'")
print(f"Final Predicted Category: {prediction}")The reported translations render the vaccine’s 85% efficacy figure, the battery’s storage use, and the stock market recovery in English before assigning the corresponding labels. This is a compact demonstration of the pipeline’s intended data flow, not a measured comparison with keyword classifiers or other multilingual systems.
Practical limits of the pattern
The design reduces the need for a language-specific classifier and lets category names seed a zero-shot task. It may be useful when labeled data is scarce or when incoming text arrives in several languages. But translation adds another model call and another point where meaning can shift. Domain terminology, ambiguous phrasing, and details such as negation or quantities deserve review, especially when category errors carry operational consequences.
Deployment also depends on API access, credentials, model availability, and provider pricing. The notebook’s claim of a free route is tied to the service configuration used in the example, not a guarantee of ongoing cost-free use. Teams should validate translations and category predictions on representative data, measure error rates for each language, and account for inference latency and usage limits before relying on the pattern in production.
Scikit-LLM makes the two stages composable with familiar scikit-learn pipeline structure. The practical takeaway is to treat translation and zero-shot prediction as inspectable steps: check what the first model produces, then evaluate whether the second assigns useful labels across the languages and content types the application actually receives.