File size: 2,067 Bytes
38ca34e
 
 
 
9b5b26a
 
38ca34e
9b5b26a
38ca34e
1c5851f
38ca34e
1c5851f
38ca34e
1c5851f
38ca34e
 
 
9b5b26a
38ca34e
 
 
 
 
 
 
 
 
 
 
 
 
9b5b26a
38ca34e
8c01ffb
38ca34e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8fe992b
33a5079
1c5851f
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
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()