chenzerong commited on
Commit
a5a8e3e
·
1 Parent(s): 77b010c

add translate

Browse files
Files changed (2) hide show
  1. requirements.txt +1 -1
  2. tools/translate.py +8 -7
requirements.txt CHANGED
@@ -3,4 +3,4 @@ smolagents
3
  requests
4
  duckduckgo_search
5
  pandas
6
- transformers
 
3
  requests
4
  duckduckgo_search
5
  pandas
6
+ huggingface_hub
tools/translate.py CHANGED
@@ -1,19 +1,20 @@
1
- from transformers import AutoModelForCausalLM, AutoTokenizer
2
  from smolagents.tools import Tool
 
3
 
4
  class TranslateTool(Tool):
5
  name = "translate"
6
  description = "Translate text from Chinese to English"
7
  inputs = {"text": {"type": "string", "description": "The text to translate"}}
8
  output_type = "string"
 
 
9
 
10
  def forward(self, text: str) -> str:
11
- inputs = self.tokenizer(text, return_tensors="pt")
12
- outputs = self.model.generate(**inputs, max_new_tokens=512)
13
- return self.tokenizer.decode(outputs[0], skip_special_tokens=True)
 
 
14
 
15
  def __init__(self, *args, **kwargs):
16
  super().__init__(*args, **kwargs)
17
- self.model_id = "ModelSpace/GemmaX2-28-9B-v0.1"
18
- self.tokenizer = AutoTokenizer.from_pretrained(self.model_id)
19
- self.model = AutoModelForCausalLM.from_pretrained(self.model_id)
 
 
1
  from smolagents.tools import Tool
2
+ from huggingface_hub import InferenceClient
3
 
4
  class TranslateTool(Tool):
5
  name = "translate"
6
  description = "Translate text from Chinese to English"
7
  inputs = {"text": {"type": "string", "description": "The text to translate"}}
8
  output_type = "string"
9
+ model_id = "Helsinki-NLP/opus-mt-zh-en"
10
+ client = InferenceClient(model_id)
11
 
12
  def forward(self, text: str) -> str:
13
+ response = self.client.text_generation(
14
+ f"Translate from Chinese to English: {text}",
15
+ max_new_tokens=100
16
+ )
17
+ return response
18
 
19
  def __init__(self, *args, **kwargs):
20
  super().__init__(*args, **kwargs)