Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -9,14 +9,63 @@ 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 |
-
#Keep this format for the description / args / args description but feel free to modify the tool
|
14 |
-
"""A tool that
|
15 |
Args:
|
16 |
-
arg1: the
|
17 |
-
|
18 |
"""
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
@tool
|
22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
|
9 |
|
10 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
11 |
@tool
|
12 |
+
def moshaere(arg1: str) -> str: # it's import to specify the return type
|
13 |
+
# Keep this format for the description / args / args description but feel free to modify the tool
|
14 |
+
"""A tool that continues the poetry by adding another verse to it. the verse should start with the ending alphabet of user verse
|
15 |
Args:
|
16 |
+
arg1: the user verse
|
17 |
+
|
18 |
"""
|
19 |
+
try:
|
20 |
+
last_char = arg1[-1].lower() # Get the last character (lowercase for consistency)
|
21 |
+
search_term = f"Persian poetry verse starting with {last_char}" # Create a search query
|
22 |
+
search_tool = DuckDuckGoSearchTool() # Initialize the search tool
|
23 |
+
|
24 |
+
search_results = search_tool.run(search_term) # Perform the search
|
25 |
+
# search_results = search_tool.use({"query": search_term}) #Old usage for smolagents, keep in case its needed.
|
26 |
+
|
27 |
+
# Check if the search results are not empty
|
28 |
+
if search_results:
|
29 |
+
# Try to extract a relevant verse from the search results.
|
30 |
+
# This part needs careful handling to avoid hallucination.
|
31 |
+
# One approach is to look for text snippets containing " بیت " (verse) or other relevant keywords.
|
32 |
+
#Another way is just pick a random line from the results.
|
33 |
+
|
34 |
+
#Simple way of extracting verse (risky):
|
35 |
+
# verse = search_results.split('\n')[0] #get first line
|
36 |
+
try:
|
37 |
+
verse_list = search_results.split("\n")
|
38 |
+
verse = None
|
39 |
+
for line in verse_list:
|
40 |
+
if len(line)>2 and (line[0].isalpha()):
|
41 |
+
verse = line
|
42 |
+
break
|
43 |
+
if verse is None:
|
44 |
+
return "Could not find an appropriate verse."
|
45 |
+
|
46 |
+
|
47 |
+
#Refine verse a little bit more:
|
48 |
+
refined_verse = verse
|
49 |
+
if ":" in refined_verse:
|
50 |
+
refined_verse = refined_verse.split(":")[1]
|
51 |
+
if "-" in refined_verse:
|
52 |
+
refined_verse = refined_verse.split("-")[1]
|
53 |
+
|
54 |
+
# Ensure the extracted verse starts with the correct letter:
|
55 |
+
if refined_verse[0].lower() == last_char:
|
56 |
+
return refined_verse
|
57 |
+
else:
|
58 |
+
return f"Could not find an appropriate verse directly from the search result starting with '{last_char}'. Please verify manually if there is a fitting one. \n Raw result: {search_results}"
|
59 |
+
|
60 |
+
except Exception as e:
|
61 |
+
return f"Error while extracting verse: {e} \n Raw Results: {search_results}"
|
62 |
+
|
63 |
+
|
64 |
+
else:
|
65 |
+
return "Could not find any relevant verses." # Handle empty search results
|
66 |
+
|
67 |
+
except Exception as e:
|
68 |
+
return f"An error occurred: {e}"
|
69 |
|
70 |
@tool
|
71 |
def get_current_time_in_timezone(timezone: str) -> str:
|