asmaab commited on
Commit
1fcde86
·
verified ·
1 Parent(s): ae7a494

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -2
app.py CHANGED
@@ -17,7 +17,57 @@ def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return
17
  arg2: the second argument
18
  """
19
  return "What magic will you build ?"
20
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  @tool
22
  def get_current_time_in_timezone(timezone: str) -> str:
23
  """A tool that fetches the current local time in a specified timezone.
@@ -55,7 +105,7 @@ with open("prompts.yaml", 'r') as stream:
55
 
56
  agent = CodeAgent(
57
  model=model,
58
- tools=[final_answer], ## add your tools here (don't remove final answer)
59
  max_steps=6,
60
  verbosity_level=1,
61
  grammar=None,
 
17
  arg2: the second argument
18
  """
19
  return "What magic will you build ?"
20
+ @tool
21
+ def enhanced_translate(
22
+ text: str,
23
+ target_language: str,
24
+ source_language: str = "auto",
25
+ domain: str = "general",
26
+ preserve_formatting: bool = True
27
+ ) -> str:
28
+ """Translate text with advanced options.
29
+ Args:
30
+ text: Text to translate
31
+ target_language: Target language code (e.g., 'fr' for French)
32
+ source_language: Source language or 'auto' for detection
33
+ domain: Specialized domain ('general', 'medical', 'legal', 'technical')
34
+ preserve_formatting: Whether to maintain text formatting
35
+ """
36
+ try:
37
+ # Could use different backends based on domain
38
+ # This example uses deep_translator but could be extended
39
+ from deep_translator import GoogleTranslator, DeepL
40
+
41
+ # Select appropriate service based on domain and languages
42
+ if domain == "general":
43
+ translator = GoogleTranslator(source=source_language, target=target_language)
44
+ elif domain in ["legal", "medical"] and target_language in ["en", "de", "fr"]:
45
+ # DeepL might be better for certain domain/language combinations
46
+ translator = DeepL(source=source_language, target=target_language, domain=domain)
47
+ else:
48
+ translator = GoogleTranslator(source=source_language, target=target_language)
49
+
50
+ # Handle formatting preservation
51
+ if preserve_formatting and "<" in text and ">" in text:
52
+ # Simple HTML handling (a more robust solution would use proper HTML parsing)
53
+ import re
54
+ # Extract tags and replace with placeholders
55
+ tags = re.findall(r'<[^>]+>', text)
56
+ for i, tag in enumerate(tags):
57
+ text = text.replace(tag, f"__TAG_{i}__")
58
+
59
+ # Translate the text without tags
60
+ translated = translator.translate(text)
61
+
62
+ # Restore tags
63
+ for i, tag in enumerate(tags):
64
+ translated = translated.replace(f"__TAG_{i}__", tag)
65
+ else:
66
+ translated = translator.translate(text)
67
+
68
+ return f"Translation ({domain} domain): {translated}"
69
+ except Exception as e:
70
+ return f"Translation failed: {str(e)}"
71
  @tool
72
  def get_current_time_in_timezone(timezone: str) -> str:
73
  """A tool that fetches the current local time in a specified timezone.
 
105
 
106
  agent = CodeAgent(
107
  model=model,
108
+ tools=[final_answer,enhanced_translate,image_generation_tool, DuckDuckGoSearchTool()], ## add your tools here (don't remove final answer)
109
  max_steps=6,
110
  verbosity_level=1,
111
  grammar=None,