Spaces:
Runtime error
Runtime error
| 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 | |
| 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 | |
| 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() |