Update app.py
Browse files
app.py
CHANGED
@@ -7,60 +7,35 @@ from tools.final_answer import FinalAnswerTool
|
|
7 |
|
8 |
from Gradio_UI import GradioUI
|
9 |
|
|
|
|
|
10 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
|
|
|
|
11 |
@tool
|
12 |
-
def
|
13 |
-
|
|
|
|
|
|
|
14 |
Args:
|
15 |
-
|
|
|
16 |
"""
|
17 |
-
# 天气图标映射库
|
18 |
-
weather_art = {
|
19 |
-
"晴": r'''
|
20 |
-
\ / ☀️
|
21 |
-
.-"""-.
|
22 |
-
_/_______\_
|
23 |
-
(___________)
|
24 |
-
''',
|
25 |
-
"雨": r'''
|
26 |
-
´¯`•.¸¸.•´¯`
|
27 |
-
(´•̥̥̥o•̥̥̥`).•´¯`•.☔️
|
28 |
-
`·.¸( •̥̥̥o•̥̥̥ )
|
29 |
-
''',
|
30 |
-
"云": r'''
|
31 |
-
.-~~~-.
|
32 |
-
/ ☁️ \
|
33 |
-
; ;
|
34 |
-
| |
|
35 |
-
\ /
|
36 |
-
`~~~~~`
|
37 |
-
'''
|
38 |
-
}
|
39 |
-
|
40 |
try:
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
data = response.json()
|
45 |
-
condition = data["current"]["weathercode"]
|
46 |
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
}
|
52 |
-
status = weather_map.get(condition, "未知")
|
53 |
-
|
54 |
-
# 生成艺术报告
|
55 |
-
art = weather_art.get(status, r'''
|
56 |
-
╱╲╱╲
|
57 |
-
◖_◗◖_◗
|
58 |
-
‾‾‾‾‾‾
|
59 |
-
''')
|
60 |
-
return f"{city}当前天气:{status}\n{art}"
|
61 |
|
|
|
|
|
62 |
except Exception as e:
|
63 |
-
return f"
|
64 |
|
65 |
@tool
|
66 |
def get_current_time_in_timezone(timezone: str) -> str:
|
@@ -99,7 +74,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
99 |
|
100 |
agent = CodeAgent(
|
101 |
model=model,
|
102 |
-
tools=[final_answer,
|
103 |
max_steps=6,
|
104 |
verbosity_level=1,
|
105 |
grammar=None,
|
|
|
7 |
|
8 |
from Gradio_UI import GradioUI
|
9 |
|
10 |
+
from transformers import pipeline
|
11 |
+
from typing import Optional
|
12 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
13 |
+
|
14 |
+
|
15 |
@tool
|
16 |
+
def text_translator(
|
17 |
+
text: str,
|
18 |
+
target_lang: Optional[str] = "auto"
|
19 |
+
) -> str:
|
20 |
+
"""智能中英互译工具
|
21 |
Args:
|
22 |
+
text: 待翻译文本(自动检测源语言)
|
23 |
+
target_lang: 目标语言代码(zh/en/ja等,默认自动判断)
|
24 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
try:
|
26 |
+
is_chinese = any('\u4e00' <= char <= '\u9fff' for char in text)
|
27 |
+
src_lang = "zh" if is_chinese else "en"
|
28 |
+
tgt_lang = "en" if is_chinese else "zh" if target_lang == "auto" else target_lang
|
|
|
|
|
29 |
|
30 |
+
model_name = "Helsinki-NLP/opus-mt-zh-en" if src_lang == "zh" else "Helsinki-NLP/opus-mt-en-zh"
|
31 |
+
translator = pipeline("translation", model=model_name)
|
32 |
+
|
33 |
+
result = translator(text, max_length=512)[0]['translation_text']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
+
return f"翻译结果 ({src_lang}→{tgt_lang}):\n{result}"
|
36 |
+
|
37 |
except Exception as e:
|
38 |
+
return f"翻译失败:{str(e)}"
|
39 |
|
40 |
@tool
|
41 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
|
74 |
|
75 |
agent = CodeAgent(
|
76 |
model=model,
|
77 |
+
tools=[final_answer, text_translator], ## add your tools here (don't remove final answer)
|
78 |
max_steps=6,
|
79 |
verbosity_level=1,
|
80 |
grammar=None,
|