Liphos commited on
Commit
4cd12b3
·
verified ·
1 Parent(s): 906610f

test with pygame

Browse files
Files changed (1) hide show
  1. app.py +25 -6
app.py CHANGED
@@ -1,7 +1,7 @@
1
  from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
2
  import datetime
3
  import requests
4
- import vlc
5
  import time
6
  import pytz
7
  import yaml
@@ -34,7 +34,28 @@ def get_current_time_in_timezone(timezone: str) -> str:
34
  return f"The current local time in {timezone} is: {local_time}"
35
  except Exception as e:
36
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
37
 
 
 
 
 
 
 
 
 
 
 
38
  @tool
39
  def play_preview_deezer(track: str, artist:str) -> None:
40
  """Play a preview of a track from Deezer.
@@ -64,12 +85,10 @@ def play_preview_deezer(track: str, artist:str) -> None:
64
  print(f"Artist: {first_track['artist']['name']}")
65
  print(f"Album: {first_track['album']['title']}")
66
  print(f"link: {first_track['link']}")
67
- # Play the track using VLC
68
  print("Playing preview: ", first_track['preview'])
69
- player = vlc.MediaPlayer(first_track['preview'])
70
- player.play()
71
- # wait time for vlc to load
72
- time.sleep(30)
73
  else:
74
  print("No tracks found.")
75
  else:
 
1
  from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
2
  import datetime
3
  import requests
4
+ import pygame
5
  import time
6
  import pytz
7
  import yaml
 
34
  return f"The current local time in {timezone} is: {local_time}"
35
  except Exception as e:
36
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
37
+
38
+ def play_audio_from_url(url):
39
+ # Initialize the pygame mixer
40
+ pygame.mixer.init()
41
+
42
+ # Stream the audio content from the URL
43
+ response = requests.get(url, stream=True)
44
+ response.raise_for_status()
45
+
46
+ # Load the audio data into a BytesIO object
47
+ audio_data = BytesIO(response.content)
48
 
49
+ # Load the audio data into pygame
50
+ pygame.mixer.music.load(audio_data, 'mp3')
51
+
52
+ # Play the audio
53
+ pygame.mixer.music.play()
54
+
55
+ # Keep the script running while the audio is playing
56
+ while pygame.mixer.music.get_busy():
57
+ pygame.time.Clock().tick(10)
58
+
59
  @tool
60
  def play_preview_deezer(track: str, artist:str) -> None:
61
  """Play a preview of a track from Deezer.
 
85
  print(f"Artist: {first_track['artist']['name']}")
86
  print(f"Album: {first_track['album']['title']}")
87
  print(f"link: {first_track['link']}")
88
+ # Play the track
89
  print("Playing preview: ", first_track['preview'])
90
+ play_audio_from_url(first_track['preview'])
91
+ time.sleep(2)
 
 
92
  else:
93
  print("No tracks found.")
94
  else: