Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -6,12 +6,33 @@ import yaml
|
|
6 |
from tools.final_answer import FinalAnswerTool
|
7 |
|
8 |
from Gradio_UI import GradioUI
|
9 |
-
from langdetect import detect
|
10 |
from googletrans import Translator
|
11 |
|
12 |
-
translator = Translator()
|
13 |
|
14 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
@tool
|
16 |
def greet_in_local_language(timezone: str) -> str:
|
17 |
"""A tool that greets the user in the local timezone's language.
|
@@ -24,14 +45,11 @@ def greet_in_local_language(timezone: str) -> str:
|
|
24 |
tz = pytz.timezone(timezone)
|
25 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
26 |
|
27 |
-
# Detect language based on timezone
|
28 |
-
lang_code =
|
29 |
-
|
30 |
-
# Translate greeting into detected language
|
31 |
-
greeting = "Hello! Have a great day!"
|
32 |
-
translated_greeting = translator.translate(greeting, dest=lang_code).text
|
33 |
|
34 |
-
return f"{
|
35 |
except Exception as e:
|
36 |
return f"Error generating greeting for timezone '{timezone}': {str(e)}"
|
37 |
|
|
|
6 |
from tools.final_answer import FinalAnswerTool
|
7 |
|
8 |
from Gradio_UI import GradioUI
|
|
|
9 |
from googletrans import Translator
|
10 |
|
|
|
11 |
|
12 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
13 |
+
# Predefined greetings in different languages
|
14 |
+
GREETINGS = {
|
15 |
+
"en": "Hello! Have a great day!",
|
16 |
+
"es": "¡Hola! ¡Que tengas un gran día!",
|
17 |
+
"fr": "Bonjour ! Passez une excellente journée !",
|
18 |
+
"de": "Hallo! Einen schönen Tag noch!",
|
19 |
+
"it": "Ciao! Buona giornata!",
|
20 |
+
"zh": "你好!祝你有美好的一天!",
|
21 |
+
"ja": "こんにちは!良い一日を!"
|
22 |
+
}
|
23 |
+
|
24 |
+
def detect_language_from_timezone(timezone: str) -> str:
|
25 |
+
"""A simple mapping of timezones to languages."""
|
26 |
+
mapping = {
|
27 |
+
"America": "en",
|
28 |
+
"Europe": "fr",
|
29 |
+
"Asia": "zh",
|
30 |
+
"Africa": "en",
|
31 |
+
"Australia": "en"
|
32 |
+
}
|
33 |
+
region = timezone.split('/')[0]
|
34 |
+
return mapping.get(region, "en") # Default to English
|
35 |
+
|
36 |
@tool
|
37 |
def greet_in_local_language(timezone: str) -> str:
|
38 |
"""A tool that greets the user in the local timezone's language.
|
|
|
45 |
tz = pytz.timezone(timezone)
|
46 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
47 |
|
48 |
+
# Detect language based on timezone
|
49 |
+
lang_code = detect_language_from_timezone(timezone)
|
50 |
+
greeting = GREETINGS.get(lang_code, GREETINGS["en"]) # Default to English if unknown
|
|
|
|
|
|
|
51 |
|
52 |
+
return f"{greeting} The current local time in {timezone} is: {local_time}"
|
53 |
except Exception as e:
|
54 |
return f"Error generating greeting for timezone '{timezone}': {str(e)}"
|
55 |
|