lchumaceiro commited on
Commit
86096d8
·
verified ·
1 Parent(s): 99acb67
Files changed (1) hide show
  1. app.py +20 -29
app.py CHANGED
@@ -4,56 +4,44 @@ import requests
4
  import pytz
5
  import yaml
6
  import time
 
7
  from tools.final_answer import FinalAnswerTool
8
- from huggingface_hub import InferenceClient
9
  from pydub.generators import WhiteNoise
10
  from pydub import AudioSegment
11
- from gradio import Files
12
-
13
  from Gradio_UI import GradioUI
14
- import os
15
- from huggingface_hub import login
16
 
 
17
  hf_token = os.getenv("HF_TOKEN") # Fetch secret from Hugging Face Space
18
  if hf_token:
19
  login(hf_token)
20
  else:
21
  print("⚠️ Hugging Face API token is missing! Set HF_TOKEN in Space secrets.")
22
 
23
- # Sound generation tool
24
  @tool
25
  def generate_sound(sound_type: str, duration: int) -> str:
26
- """Generates a simple sound based on the specified type and duration.
27
-
28
- Args:
29
- sound_type: Type of sound to generate (e.g., 'rain', 'white_noise').
30
- duration: Duration of the sound in seconds.
31
-
32
- Returns:
33
- Download link
34
- """
35
  try:
36
  duration_ms = duration * 1000
 
 
37
  if sound_type == "rain":
38
  sound = WhiteNoise().to_audio_segment(duration=duration_ms).low_pass_filter(5000)
39
  else:
40
  return f"Unsupported sound type: {sound_type}"
41
 
42
- output_path = f"rain_{duration}s.wav"
43
  sound.export(output_path, format="wav")
44
-
45
- return Files(output_path) # This makes the file downloadable in Gradio
46
 
47
  except Exception as e:
48
  return f"Error generating sound: {str(e)}"
49
 
50
- # Time zone tool
51
  @tool
52
  def get_current_time_in_timezone(timezone: str) -> str:
53
- """A tool that fetches the current local time in a specified timezone.
54
- Args:
55
- timezone: A string representing a valid timezone (e.g., 'America/New_York').
56
- """
57
  try:
58
  tz = pytz.timezone(timezone)
59
  local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
@@ -61,36 +49,39 @@ def get_current_time_in_timezone(timezone: str) -> str:
61
  except Exception as e:
62
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
63
 
64
-
65
  final_answer = FinalAnswerTool()
66
 
 
67
  model = HfApiModel(
68
  max_tokens=2096,
69
  temperature=0.5,
70
  model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
71
  custom_role_conversions=None,
72
- token= hf_token
73
  )
74
 
 
75
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
76
 
 
77
  with open("prompts.yaml", 'r') as stream:
78
  prompt_templates = yaml.safe_load(stream)
79
 
 
80
  agent = CodeAgent(
81
  model=model,
82
- tools=[final_answer, generate_sound], # Add your tool
83
  max_steps=6,
84
  verbosity_level=1,
85
  grammar=None,
86
  planning_interval=None,
87
  name=None,
88
  description=None,
89
- prompt_templates=prompt_templates,
90
- output_modality="file"
91
  )
92
 
93
- # Start the UI with processing time display
94
  def launch_with_processing_time():
95
  def wrapped_launch():
96
  start_time = time.time()
 
4
  import pytz
5
  import yaml
6
  import time
7
+ import os
8
  from tools.final_answer import FinalAnswerTool
9
+ from huggingface_hub import InferenceClient, login
10
  from pydub.generators import WhiteNoise
11
  from pydub import AudioSegment
12
+ import gradio as gr
 
13
  from Gradio_UI import GradioUI
 
 
14
 
15
+ # Load Hugging Face token
16
  hf_token = os.getenv("HF_TOKEN") # Fetch secret from Hugging Face Space
17
  if hf_token:
18
  login(hf_token)
19
  else:
20
  print("⚠️ Hugging Face API token is missing! Set HF_TOKEN in Space secrets.")
21
 
22
+ # Sound generation tool
23
  @tool
24
  def generate_sound(sound_type: str, duration: int) -> str:
25
+ """Generates a simple sound file and returns its path for download."""
 
 
 
 
 
 
 
 
26
  try:
27
  duration_ms = duration * 1000
28
+ output_path = f"/home/user/app/{sound_type}_{duration}s.wav"
29
+
30
  if sound_type == "rain":
31
  sound = WhiteNoise().to_audio_segment(duration=duration_ms).low_pass_filter(5000)
32
  else:
33
  return f"Unsupported sound type: {sound_type}"
34
 
 
35
  sound.export(output_path, format="wav")
36
+ return output_path # ✅ Returns a file path for Gradio to display/download
 
37
 
38
  except Exception as e:
39
  return f"Error generating sound: {str(e)}"
40
 
41
+ # Time zone tool
42
  @tool
43
  def get_current_time_in_timezone(timezone: str) -> str:
44
+ """Fetches the current local time in a specified timezone."""
 
 
 
45
  try:
46
  tz = pytz.timezone(timezone)
47
  local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
 
49
  except Exception as e:
50
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
51
 
52
+ # ✅ Load the final answer tool
53
  final_answer = FinalAnswerTool()
54
 
55
+ # ✅ Model configuration
56
  model = HfApiModel(
57
  max_tokens=2096,
58
  temperature=0.5,
59
  model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
60
  custom_role_conversions=None,
61
+ token=hf_token
62
  )
63
 
64
+ # ✅ Load external tools
65
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
66
 
67
+ # ✅ Load prompt templates
68
  with open("prompts.yaml", 'r') as stream:
69
  prompt_templates = yaml.safe_load(stream)
70
 
71
+ # ✅ Define the agent (Removed `output_modality`)
72
  agent = CodeAgent(
73
  model=model,
74
+ tools=[final_answer, generate_sound, image_generation_tool], # Included image generation tool
75
  max_steps=6,
76
  verbosity_level=1,
77
  grammar=None,
78
  planning_interval=None,
79
  name=None,
80
  description=None,
81
+ prompt_templates=prompt_templates
 
82
  )
83
 
84
+ # Start the UI with processing time display
85
  def launch_with_processing_time():
86
  def wrapped_launch():
87
  start_time = time.time()