lushi-huggingface commited on
Commit
50feaac
·
verified ·
1 Parent(s): c593846

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -47
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 weather_check(city: str) -> str:
13
- """获取指定城市的实时天气并生成艺术化展示
 
 
 
14
  Args:
15
- city: 城市名称(支持中英文,如'北京'或'New York')
 
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
- response = requests.get(
42
- f"https://api.open-meteo.com/v1/forecast?city={city}&current_weather=true"
43
- )
44
- data = response.json()
45
- condition = data["current"]["weathercode"]
46
 
47
- # 转换天气代码为文字描述
48
- weather_map = {
49
- 0: "晴", 1: "晴", 2: "云", 3: "云",
50
- 45: "雾", 48: "雾", 51: "雨", 61: "雨"
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"天气查询失败:{str(e)}"
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, weather_check], ## add your tools here (don't remove 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,