uncleMehrzad commited on
Commit
86bbad4
·
verified ·
1 Parent(s): 1173459

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -38
app.py CHANGED
@@ -9,60 +9,39 @@ 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 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}"
@@ -104,7 +83,7 @@ with open("prompts.yaml", 'r') as stream:
104
 
105
  agent = CodeAgent(
106
  model=model,
107
- tools=[final_answer], ## add your tools here (don't remove final answer)
108
  max_steps=6,
109
  verbosity_level=1,
110
  grammar=None,
 
9
 
10
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
11
  @tool
12
+ def find_the_song(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 finds the song name and description based on a user's given verse.
15
  Args:
16
  arg1: the user verse
17
 
18
  """
19
  try:
20
+ search_term = f"song lyrics \"{arg1}\"" # Enclose the verse in quotes for exact match
 
21
  search_tool = DuckDuckGoSearchTool() # Initialize the search tool
22
 
23
  search_results = search_tool.run(search_term) # Perform the search
24
  # search_results = search_tool.use({"query": search_term}) #Old usage for smolagents, keep in case its needed.
25
 
26
+
27
  if search_results:
28
+ # Extract song information from the search results.
29
+ # This part requires careful extraction logic and may need improvement.
30
+ # Ideally, we want to find the song title, artist, and a brief description.
 
31
 
 
 
32
  try:
33
+ # Simple approach: Split by newline and assume the first few lines contain the song info.
34
+ lines = search_results.split("\n")
35
+ song_info = ""
36
+ for i in range(min(3, len(lines))): # Consider the first 3 lines
37
+ song_info += lines[i] + "\n"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
 
39
+ return f"Possible song information:\n{song_info}\nRaw search result: {search_results}"
40
 
41
+ except Exception as e:
42
+ return f"Error extracting song info: {e}\nRaw search result: {search_results}"
43
  else:
44
+ return "Could not find any songs matching the verse." # Handle empty search results
45
 
46
  except Exception as e:
47
  return f"An error occurred: {e}"
 
83
 
84
  agent = CodeAgent(
85
  model=model,
86
+ tools=[final_answer, find_the_song,get_current_time_in_timezone], ## add your tools here (don't remove final answer)
87
  max_steps=6,
88
  verbosity_level=1,
89
  grammar=None,