import requests import time import gradio as gr from smolagents import tool @tool def suggest_similar_artists(band_name: str) -> str: """ Fetch similar artists for a given band name using the Last.fm API. Args: band_name: The name of the band or artist Returns: A string listing similar artists or an error message """ API_KEY = "9a0ab136b40bffccbf454dfdd5335b1a" URL = f"http://ws.audioscrobbler.com/2.0/?method=artist.getsimilar&artist={band_name}&api_key={API_KEY}&format=json" try: response = requests.get(URL) response.raise_for_status() data = response.json() if data.get("similarartists") and data["similarartists"].get("artist"): artists = [artist["name"] for artist in data["similarartists"]["artist"][:5]] return f"Similar artists to {band_name}: {', '.join(artists)}" else: return f"No similar artists found for {band_name}" except requests.exceptions.RequestException as req_err: return f"Network error while fetching similar artists: {str(req_err)}" except KeyError: return "Unexpected response format from API. Please try again." except Exception as e: return f"Unexpected error: {str(e)}" def gradio_interface(): """Wraps your suggest_similar_artists tool in a Gradio interface.""" def run_tool(band_name): return suggest_similar_artists(band_name) with gr.Blocks() as demo: gr.Markdown("## Similar Artists Finder") band_input = gr.Textbox(label="Band Name") output_text = gr.Textbox(label="Result") run_button = gr.Button("Suggest Similar Artists") run_button.click(fn=run_tool, inputs=band_input, outputs=output_text) return demo def main(): # Start the Gradio interface on port 7860 (common default) # 0.0.0.0 ensures it is accessible from outside the container app = gradio_interface() app.launch(server_name="0.0.0.0", server_port=7860) if __name__ == "__main__": main()