Spaces:
Sleeping
Sleeping
peterbonnesoeur
commited on
Commit
·
dd7049b
1
Parent(s):
e69816a
Added translator tool
Browse files- .gitignore +15 -0
- app.py +6 -0
- requirements.txt +2 -0
- tool.py +40 -0
.gitignore
ADDED
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Python-generated files
|
2 |
+
__pycache__/
|
3 |
+
*.py[oc]
|
4 |
+
build/
|
5 |
+
dist/
|
6 |
+
wheels/
|
7 |
+
*.egg-info
|
8 |
+
.python-version
|
9 |
+
uv.lock
|
10 |
+
pyproject.toml
|
11 |
+
|
12 |
+
# Virtual environments
|
13 |
+
.venv
|
14 |
+
|
15 |
+
.codegpt
|
app.py
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from smolagents import launch_gradio_demo
|
2 |
+
from tool import TranslateTool
|
3 |
+
|
4 |
+
tool = TranslateTool()
|
5 |
+
|
6 |
+
launch_gradio_demo(tool)
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
smolagents
|
2 |
+
googletrans
|
tool.py
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import asyncio
|
2 |
+
from smolagents import Tool
|
3 |
+
from googletrans import Translator
|
4 |
+
|
5 |
+
|
6 |
+
class TranslateTool(Tool):
|
7 |
+
name = "google_translate"
|
8 |
+
description = """
|
9 |
+
Translates text from one language to another using the Google Translate API.
|
10 |
+
Supports a wide range of languages using standard language codes (e.g., 'en' for English, 'fr' for French).
|
11 |
+
If source_language is not specified, it will be auto-detected. If target_language is not specified, it defaults to English ('en').
|
12 |
+
"""
|
13 |
+
inputs = {
|
14 |
+
"text": {
|
15 |
+
"type": "string",
|
16 |
+
"description": "The text to translate."
|
17 |
+
},
|
18 |
+
"source_language": {
|
19 |
+
"type": "string",
|
20 |
+
"description": "The source language code (e.g., 'en' for English, 'fr' for French).",
|
21 |
+
},
|
22 |
+
"target_language": {
|
23 |
+
"type": "string",
|
24 |
+
"description": "The target language code (e.g., 'es' for Spanish, 'de' for German).",
|
25 |
+
}
|
26 |
+
}
|
27 |
+
output_type = "string"
|
28 |
+
|
29 |
+
async def translate_text(text: str, src: str, dest: str):
|
30 |
+
async with Translator() as translator:
|
31 |
+
res = await translator.translate(text, src, dest)
|
32 |
+
return res
|
33 |
+
|
34 |
+
def forward(self, text: str, source_language: str, target_language: str):
|
35 |
+
try:
|
36 |
+
translator = Translator()
|
37 |
+
translation = asyncio.run(translator.translate(text, src=source_language, dest=target_language))
|
38 |
+
return translation.text
|
39 |
+
except Exception as e:
|
40 |
+
return f"Error: Unable to translate due to {e}"
|