File size: 680 Bytes
d8e4846
a5a8e3e
d8e4846
 
 
 
 
 
a5a8e3e
 
d8e4846
 
a5a8e3e
 
 
 
 
d8e4846
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
from smolagents.tools import Tool
from huggingface_hub import InferenceClient

class TranslateTool(Tool):
    name = "translate"
    description = "Translate text from Chinese to English"
    inputs = {"text": {"type": "string", "description": "The text to translate"}}
    output_type = "string"
    model_id = "Helsinki-NLP/opus-mt-zh-en"
    client = InferenceClient(model_id)

    def forward(self, text: str) -> str:
        response = self.client.text_generation(
            f"Translate from Chinese to English: {text}",
            max_new_tokens=100
        )
        return response

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)