laverdes commited on
Commit
0df6a93
·
verified ·
1 Parent(s): e5017c2

feat: implement language detection tool

Browse files
Files changed (1) hide show
  1. app.py +38 -18
app.py CHANGED
@@ -1,21 +1,24 @@
1
- from smolagents import (
2
- CodeAgent,
3
- DuckDuckGoSearchTool,
4
- GoogleSearchTool,
5
- HfApiModel,
6
- load_tool,
7
- tool
8
- )
9
  import datetime
10
  import requests
11
  import pytz
12
  import yaml
 
13
  from tools.final_answer import FinalAnswerTool
14
  from tools.visit_webpage import VisitWebpageTool
15
  from tools.translation import TranslationTool
16
 
 
17
  from Gradio_UI import GradioUI
18
 
 
 
 
 
 
 
 
 
 
19
 
20
  @tool
21
  def get_current_time_in_timezone(timezone: str) -> str:
@@ -34,9 +37,9 @@ def get_current_time_in_timezone(timezone: str) -> str:
34
 
35
  @tool
36
  def conversational_utterance(user_content:str)-> str:
37
- """A tool that replies to a single casual query or message that does not require any other tool to be triggered
38
  Args:
39
- user_content: the message or query such as 'Hi!', 'How are you?', 'What are you?', 'tell me a joke'
40
  """
41
  messages = [
42
  {"role": "user", "content": [{"type": "text", "text": user_content}]}
@@ -44,14 +47,32 @@ def conversational_utterance(user_content:str)-> str:
44
  return model(messages).content
45
 
46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  final_answer = FinalAnswerTool()
48
- prefered_web_search = GoogleSearchTool()
49
- alternative_web_search = DuckDuckGoSearchTool()
50
  visit_webpage = VisitWebpageTool()
51
  translation_tool = TranslationTool()
52
 
53
- # If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
54
- # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
 
 
 
 
55
 
56
  model = HfApiModel(
57
  max_tokens=2096,
@@ -60,9 +81,8 @@ model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may
60
  custom_role_conversions=None,
61
  )
62
 
63
- # Import tool from Hub
64
- image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
65
- language_detection_tool = load_tool("team-language-detector/LanguageDetector", trust_remote_code=True)
66
 
67
  with open("prompts.yaml", 'r') as stream:
68
  prompt_templates = yaml.safe_load(stream)
@@ -77,7 +97,7 @@ agent = CodeAgent(
77
  get_current_time_in_timezone,
78
  conversational_utterance,
79
  image_generation_tool,
80
- language_detection_tool,
81
  translation_tool
82
  ],
83
  max_steps=6,
 
 
 
 
 
 
 
 
 
1
  import datetime
2
  import requests
3
  import pytz
4
  import yaml
5
+
6
  from tools.final_answer import FinalAnswerTool
7
  from tools.visit_webpage import VisitWebpageTool
8
  from tools.translation import TranslationTool
9
 
10
+ from transformers import pipeline
11
  from Gradio_UI import GradioUI
12
 
13
+ from smolagents import (
14
+ CodeAgent,
15
+ DuckDuckGoSearchTool,
16
+ GoogleSearchTool,
17
+ HfApiModel,
18
+ load_tool,
19
+ tool
20
+ )
21
+
22
 
23
  @tool
24
  def get_current_time_in_timezone(timezone: str) -> str:
 
37
 
38
  @tool
39
  def conversational_utterance(user_content:str)-> str:
40
+ """A tool that replies to a single casual query or message that does not require any other tool to be triggered.
41
  Args:
42
+ user_content: the message or query such as 'Hi!', 'How are you?', 'What are you?', 'tell me a joke'.
43
  """
44
  messages = [
45
  {"role": "user", "content": [{"type": "text", "text": user_content}]}
 
47
  return model(messages).content
48
 
49
 
50
+ @tool
51
+ def language_detection(text:str)-> str:
52
+ """Detects the language of the input text.
53
+ Args:
54
+ text: the input message or wording to detect language from.
55
+ """
56
+ model_ckpt = "papluca/xlm-roberta-base-language-detection"
57
+ pipe = pipeline("text-classification", model=model_ckpt)
58
+ preds = pipe(text, return_all_scores=True, truncation=True, max_length=128)
59
+ if preds:
60
+ pred = preds[0]
61
+ return str({p["label"]: float(p["score"]) for p in pred})
62
+ else:
63
+ return "None"
64
+
65
+ # tools from /tools/
66
  final_answer = FinalAnswerTool()
 
 
67
  visit_webpage = VisitWebpageTool()
68
  translation_tool = TranslationTool()
69
 
70
+ # tools from smoloagents library
71
+ prefered_web_search = GoogleSearchTool()
72
+ alternative_web_search = DuckDuckGoSearchTool()
73
+
74
+ # tools from Hub
75
+ image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
76
 
77
  model = HfApiModel(
78
  max_tokens=2096,
 
81
  custom_role_conversions=None,
82
  )
83
 
84
+ # If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
85
+ # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
 
86
 
87
  with open("prompts.yaml", 'r') as stream:
88
  prompt_templates = yaml.safe_load(stream)
 
97
  get_current_time_in_timezone,
98
  conversational_utterance,
99
  image_generation_tool,
100
+ language_detection,
101
  translation_tool
102
  ],
103
  max_steps=6,