File size: 3,500 Bytes
7d6545e
9b5b26a
 
 
c19d193
7d6545e
6aae614
64770ed
8ecb4f6
 
5ccdde7
64770ed
9b5b26a
64770ed
 
f9d3938
6b6e0fb
f9d3938
 
 
 
6b6e0fb
64770ed
9b5b26a
7d6545e
0dd5ee2
 
 
303aa30
0dd5ee2
a73def9
0dd5ee2
a73def9
7d6545e
0dd5ee2
8ecb4f6
 
7d6545e
8ecb4f6
 
0dd5ee2
8ecb4f6
0dd5ee2
8ecb4f6
7d6545e
 
9b5b26a
64770ed
9b5b26a
 
64770ed
e286479
64770ed
e286479
9b5b26a
 
 
 
 
 
8c01ffb
64770ed
6aae614
ae7a494
e121372
7d6545e
 
 
 
64770ed
13d500a
8c01ffb
9b5b26a
8c01ffb
861422e
 
7d6545e
8c01ffb
8fe992b
64770ed
8c01ffb
 
 
 
 
 
86096d8
8fe992b
 
64770ed
7d6545e
 
 
 
5ccdde7
 
 
 
 
 
 
 
 
 
 
 
 
 
7d6545e
 
 
 
9b5b26a
7d6545e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
import datetime
import requests
import pytz
import yaml
import time
from tools.final_answer import FinalAnswerTool
from huggingface_hub import InferenceClient
from pydub.generators import WhiteNoise
from pydub import AudioSegment
import gradio as gr

from Gradio_UI import GradioUI
import os
from huggingface_hub import login

hf_token = os.getenv("HF_TOKEN")  # Fetch secret from Hugging Face Space
if hf_token:
    login(hf_token)
else:
    print("⚠️ Hugging Face API token is missing! Set HF_TOKEN in Space secrets.")

# Sound generation tool
@tool
def generate_sound(sound_type: str, duration: int) -> str:
    """Generates a simple sound based on the specified type and duration.
    
    Args:
        sound_type: Type of sound to generate (e.g., 'rain', 'car', 'white_noise').
        duration: Duration of the sound in seconds.
    Returns:
        Path to the generated audio file.
    """
    try:
        duration_ms = duration * 1000  # Convert to milliseconds
        if sound_type == "rain":
            sound = WhiteNoise().to_audio_segment(duration=duration_ms).low_pass_filter(5000)
        else:
            return f"Unsupported sound type: {sound_type}"

        output_path = f"/tmp/{sound_type}_{duration}s.wav"
        sound.export(output_path, format="wav")
        return output_path

    except Exception as e:
        return f"Error generating sound: {str(e)}"

# Time zone tool
@tool
def get_current_time_in_timezone(timezone: str) -> str:
    """A tool that fetches the current local time in a specified timezone.
    Args:
        timezone: A string representing a valid timezone (e.g., 'America/New_York').
    """
    try:
        tz = pytz.timezone(timezone)
        local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
        return f"The current local time in {timezone} is: {local_time}"
    except Exception as e:
        return f"Error fetching time for timezone '{timezone}': {str(e)}"


final_answer = FinalAnswerTool()

model = HfApiModel(
    max_tokens=2096,
    temperature=0.5,
    model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
    custom_role_conversions=None,
    token= hf_token
)

image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)

with open("prompts.yaml", 'r') as stream:
    prompt_templates = yaml.safe_load(stream)

agent = CodeAgent(
    model=model,
    tools=[final_answer, generate_sound],  # Add your tool
    max_steps=6,
    verbosity_level=1,
    grammar=None,
    planning_interval=None,
    name=None,
    description=None,
    prompt_templates=prompt_templates
)

# Start the UI with processing time display
def launch_with_processing_time():
    def wrapped_launch():
        start_time = time.time()
        print("Processing request...")
        
        # Define a simple Gradio UI
        def generate_and_return_file(sound_type, duration):
            file_path = generate_sound(sound_type, duration)
            return file_path  # Return the file for download

        interface = gr.Interface(
            fn=generate_and_return_file,
            inputs=[gr.Textbox(label="Sound Type"), gr.Number(label="Duration (seconds)")],
            outputs=gr.File(label="Download Sound File"),
        )

        interface.launch()

        end_time = time.time()
        print(f"Processing completed in {end_time - start_time:.2f} seconds")

    wrapped_launch()

launch_with_processing_time()