File size: 3,180 Bytes
7d6545e
9b5b26a
 
 
c19d193
7d6545e
86096d8
6aae614
86096d8
8ecb4f6
 
86096d8
9b5b26a
f9d3938
86096d8
6b6e0fb
f9d3938
 
 
 
6b6e0fb
349fb7b
9b5b26a
7d6545e
0dd5ee2
 
 
 
 
a73def9
 
0dd5ee2
a73def9
7d6545e
0dd5ee2
8ecb4f6
 
0dd5ee2
7d6545e
8ecb4f6
 
0dd5ee2
8ecb4f6
0dd5ee2
8ecb4f6
7d6545e
 
9b5b26a
6446aa5
1560c31
349fb7b
86096d8
9b5b26a
 
86096d8
9b5b26a
 
 
 
 
 
8c01ffb
86096d8
6aae614
ae7a494
86096d8
e121372
7d6545e
 
 
 
86096d8
13d500a
8c01ffb
86096d8
9b5b26a
8c01ffb
86096d8
861422e
 
7d6545e
86096d8
8c01ffb
8fe992b
86096d8
8c01ffb
 
 
 
 
 
86096d8
8fe992b
 
86096d8
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
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
import datetime
import requests
import pytz
import yaml
import time
import os
from tools.final_answer import FinalAnswerTool
from huggingface_hub import InferenceClient, login
from pydub.generators import WhiteNoise
from pydub import AudioSegment
import gradio as gr
from Gradio_UI import GradioUI

# Load Hugging Face token
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 with structured docstring for smolagents
@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', '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:
    """Fetches the current local time in a specified timezone."""
    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)}"

# ✅ Load the final answer tool
final_answer = FinalAnswerTool()

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

# ✅ Load external tools
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)

# ✅ Load prompt templates
with open("prompts.yaml", 'r') as stream:
    prompt_templates = yaml.safe_load(stream)

# ✅ Define the agent (Removed `output_modality`)
agent = CodeAgent(
    model=model,
    tools=[final_answer, generate_sound, image_generation_tool],  # Included image generation 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...")
        GradioUI(agent).launch()
        end_time = time.time()
        print(f"Processing completed in {end_time - start_time:.2f} seconds")

    wrapped_launch()

launch_with_processing_time()