Spaces:
Sleeping
Sleeping
revert
Browse files
app.py
CHANGED
@@ -4,22 +4,22 @@ import requests
|
|
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
|
10 |
from pydub.generators import WhiteNoise
|
11 |
from pydub import AudioSegment
|
12 |
-
|
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 |
-
#
|
23 |
@tool
|
24 |
def generate_sound(sound_type: str, duration: int) -> str:
|
25 |
"""Generates a simple sound based on the specified type and duration.
|
@@ -27,7 +27,6 @@ def generate_sound(sound_type: str, duration: int) -> str:
|
|
27 |
Args:
|
28 |
sound_type: Type of sound to generate (e.g., 'rain', 'white_noise').
|
29 |
duration: Duration of the sound in seconds.
|
30 |
-
|
31 |
Returns:
|
32 |
Path to the generated audio file.
|
33 |
"""
|
@@ -35,7 +34,6 @@ def generate_sound(sound_type: str, duration: int) -> str:
|
|
35 |
duration_ms = duration * 1000 # Convert to milliseconds
|
36 |
if sound_type == "rain":
|
37 |
sound = WhiteNoise().to_audio_segment(duration=duration_ms).low_pass_filter(5000)
|
38 |
-
|
39 |
else:
|
40 |
return f"Unsupported sound type: {sound_type}"
|
41 |
|
@@ -46,19 +44,12 @@ def generate_sound(sound_type: str, duration: int) -> str:
|
|
46 |
except Exception as e:
|
47 |
return f"Error generating sound: {str(e)}"
|
48 |
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
# ✅ Time zone tool
|
53 |
@tool
|
54 |
def get_current_time_in_timezone(timezone: str) -> str:
|
55 |
-
"""
|
56 |
-
|
57 |
Args:
|
58 |
-
timezone
|
59 |
-
|
60 |
-
Returns:
|
61 |
-
str: The current local time in the specified timezone.
|
62 |
"""
|
63 |
try:
|
64 |
tz = pytz.timezone(timezone)
|
@@ -67,29 +58,25 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
67 |
except Exception as e:
|
68 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
69 |
|
70 |
-
|
71 |
final_answer = FinalAnswerTool()
|
72 |
|
73 |
-
# ✅ Model configuration
|
74 |
model = HfApiModel(
|
75 |
max_tokens=2096,
|
76 |
temperature=0.5,
|
77 |
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
78 |
custom_role_conversions=None,
|
79 |
-
token=hf_token
|
80 |
)
|
81 |
|
82 |
-
# ✅ Load external tools
|
83 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
84 |
|
85 |
-
# ✅ Load prompt templates
|
86 |
with open("prompts.yaml", 'r') as stream:
|
87 |
prompt_templates = yaml.safe_load(stream)
|
88 |
|
89 |
-
# ✅ Define the agent (Removed `output_modality`)
|
90 |
agent = CodeAgent(
|
91 |
model=model,
|
92 |
-
tools=[final_answer, generate_sound
|
93 |
max_steps=6,
|
94 |
verbosity_level=1,
|
95 |
grammar=None,
|
@@ -99,7 +86,7 @@ agent = CodeAgent(
|
|
99 |
prompt_templates=prompt_templates
|
100 |
)
|
101 |
|
102 |
-
#
|
103 |
def launch_with_processing_time():
|
104 |
def wrapped_launch():
|
105 |
start_time = time.time()
|
|
|
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 |
+
|
12 |
from Gradio_UI import GradioUI
|
13 |
+
import os
|
14 |
+
from huggingface_hub import login
|
15 |
|
|
|
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 based on the specified type and duration.
|
|
|
27 |
Args:
|
28 |
sound_type: Type of sound to generate (e.g., 'rain', 'white_noise').
|
29 |
duration: Duration of the sound in seconds.
|
|
|
30 |
Returns:
|
31 |
Path to the generated audio file.
|
32 |
"""
|
|
|
34 |
duration_ms = duration * 1000 # Convert to milliseconds
|
35 |
if sound_type == "rain":
|
36 |
sound = WhiteNoise().to_audio_segment(duration=duration_ms).low_pass_filter(5000)
|
|
|
37 |
else:
|
38 |
return f"Unsupported sound type: {sound_type}"
|
39 |
|
|
|
44 |
except Exception as e:
|
45 |
return f"Error generating sound: {str(e)}"
|
46 |
|
47 |
+
# Time zone tool
|
|
|
|
|
|
|
48 |
@tool
|
49 |
def get_current_time_in_timezone(timezone: str) -> str:
|
50 |
+
"""A tool that fetches the current local time in a specified timezone.
|
|
|
51 |
Args:
|
52 |
+
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
|
|
|
|
|
|
53 |
"""
|
54 |
try:
|
55 |
tz = pytz.timezone(timezone)
|
|
|
58 |
except Exception as e:
|
59 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
60 |
|
61 |
+
|
62 |
final_answer = FinalAnswerTool()
|
63 |
|
|
|
64 |
model = HfApiModel(
|
65 |
max_tokens=2096,
|
66 |
temperature=0.5,
|
67 |
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
68 |
custom_role_conversions=None,
|
69 |
+
token= hf_token
|
70 |
)
|
71 |
|
|
|
72 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
73 |
|
|
|
74 |
with open("prompts.yaml", 'r') as stream:
|
75 |
prompt_templates = yaml.safe_load(stream)
|
76 |
|
|
|
77 |
agent = CodeAgent(
|
78 |
model=model,
|
79 |
+
tools=[final_answer, generate_sound], # Add your tool
|
80 |
max_steps=6,
|
81 |
verbosity_level=1,
|
82 |
grammar=None,
|
|
|
86 |
prompt_templates=prompt_templates
|
87 |
)
|
88 |
|
89 |
+
# Start the UI with processing time display
|
90 |
def launch_with_processing_time():
|
91 |
def wrapped_launch():
|
92 |
start_time = time.time()
|