Spaces:
Sleeping
Sleeping
Update app.py sound generator
Browse filesHere's your updated app.py with the sound generation tool and processing time display:
Sound Generation Tool: Uses an API to generate sounds based on type and duration.
Processing Time Display: Captures the start and end time of processing and prints the duration.
app.py
CHANGED
@@ -1,23 +1,32 @@
|
|
1 |
-
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
|
2 |
import datetime
|
3 |
import requests
|
4 |
import pytz
|
5 |
import yaml
|
|
|
6 |
from tools.final_answer import FinalAnswerTool
|
7 |
|
8 |
from Gradio_UI import GradioUI
|
9 |
|
10 |
-
#
|
11 |
@tool
|
12 |
-
def
|
13 |
-
|
14 |
-
"""A tool that does nothing yet
|
15 |
Args:
|
16 |
-
|
17 |
-
|
18 |
"""
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
|
|
21 |
@tool
|
22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
23 |
"""A tool that fetches the current local time in a specified timezone.
|
@@ -25,9 +34,7 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
25 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
26 |
"""
|
27 |
try:
|
28 |
-
# Create timezone object
|
29 |
tz = pytz.timezone(timezone)
|
30 |
-
# Get current time in that timezone
|
31 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
32 |
return f"The current local time in {timezone} is: {local_time}"
|
33 |
except Exception as e:
|
@@ -36,26 +43,21 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
36 |
|
37 |
final_answer = FinalAnswerTool()
|
38 |
|
39 |
-
# If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
|
40 |
-
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
41 |
-
|
42 |
model = HfApiModel(
|
43 |
-
max_tokens=2096,
|
44 |
-
temperature=0.5,
|
45 |
-
model_id='Qwen/Qwen2.5-Coder-32B-Instruct'
|
46 |
-
custom_role_conversions=None,
|
47 |
)
|
48 |
|
49 |
-
|
50 |
-
# Import tool from Hub
|
51 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
52 |
|
53 |
with open("prompts.yaml", 'r') as stream:
|
54 |
prompt_templates = yaml.safe_load(stream)
|
55 |
-
|
56 |
agent = CodeAgent(
|
57 |
model=model,
|
58 |
-
tools=[final_answer],
|
59 |
max_steps=6,
|
60 |
verbosity_level=1,
|
61 |
grammar=None,
|
@@ -65,5 +67,15 @@ agent = CodeAgent(
|
|
65 |
prompt_templates=prompt_templates
|
66 |
)
|
67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
|
69 |
-
|
|
|
1 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
|
2 |
import datetime
|
3 |
import requests
|
4 |
import pytz
|
5 |
import yaml
|
6 |
+
import time
|
7 |
from tools.final_answer import FinalAnswerTool
|
8 |
|
9 |
from Gradio_UI import GradioUI
|
10 |
|
11 |
+
# Sound generation tool
|
12 |
@tool
|
13 |
+
def generate_sound(sound_type: str, duration: int) -> str:
|
14 |
+
"""A tool that generates a sound based on the given type and duration.
|
|
|
15 |
Args:
|
16 |
+
sound_type: A string specifying the type of sound (e.g., 'rain', 'ocean waves', 'fire crackling').
|
17 |
+
duration: An integer representing the duration of the sound in seconds.
|
18 |
"""
|
19 |
+
try:
|
20 |
+
# Simulate API call to generate sound
|
21 |
+
response = requests.get(f"https://api.example.com/generate_sound?sound={sound_type}&duration={duration}")
|
22 |
+
if response.status_code == 200:
|
23 |
+
return f"Generated {sound_type} sound for {duration} seconds. Link: {response.json().get('sound_url', 'N/A')}"
|
24 |
+
else:
|
25 |
+
return f"Failed to generate sound: {response.text}"
|
26 |
+
except Exception as e:
|
27 |
+
return f"Error generating sound: {str(e)}"
|
28 |
|
29 |
+
# Time zone tool
|
30 |
@tool
|
31 |
def get_current_time_in_timezone(timezone: str) -> str:
|
32 |
"""A tool that fetches the current local time in a specified timezone.
|
|
|
34 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
35 |
"""
|
36 |
try:
|
|
|
37 |
tz = pytz.timezone(timezone)
|
|
|
38 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
39 |
return f"The current local time in {timezone} is: {local_time}"
|
40 |
except Exception as e:
|
|
|
43 |
|
44 |
final_answer = FinalAnswerTool()
|
45 |
|
|
|
|
|
|
|
46 |
model = HfApiModel(
|
47 |
+
max_tokens=2096,
|
48 |
+
temperature=0.5,
|
49 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
50 |
+
custom_role_conversions=None,
|
51 |
)
|
52 |
|
|
|
|
|
53 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
54 |
|
55 |
with open("prompts.yaml", 'r') as stream:
|
56 |
prompt_templates = yaml.safe_load(stream)
|
57 |
+
|
58 |
agent = CodeAgent(
|
59 |
model=model,
|
60 |
+
tools=[final_answer, generate_sound], # Added generate_sound tool
|
61 |
max_steps=6,
|
62 |
verbosity_level=1,
|
63 |
grammar=None,
|
|
|
67 |
prompt_templates=prompt_templates
|
68 |
)
|
69 |
|
70 |
+
# Start the UI with processing time display
|
71 |
+
def launch_with_processing_time():
|
72 |
+
def wrapped_launch():
|
73 |
+
start_time = time.time()
|
74 |
+
print("Processing request...")
|
75 |
+
GradioUI(agent).launch()
|
76 |
+
end_time = time.time()
|
77 |
+
print(f"Processing completed in {end_time - start_time:.2f} seconds")
|
78 |
+
|
79 |
+
wrapped_launch()
|
80 |
|
81 |
+
launch_with_processing_time()
|