File size: 1,430 Bytes
2189307
6557388
2189307
2711826
2189307
2711826
2189307
2711826
 
2189307
2711826
 
 
 
2189307
 
2711826
 
 
 
 
 
 
 
 
 
 
 
 
2189307
 
2711826
6557388
 
 
 
 
2711826
6557388
ff00b17
2711826
 
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
import networkx as nx
from smolagents import CodeAgent, HfApiModel, tool, GradioUI

# Define a tool for analyzing the Florentine Families graph
@tool
def analyze_florentine_graph(metric: str) -> str:
    """
    Analyzes the Florentine Families graph based on the chosen centrality metric.

    Args:
        metric: The centrality metric to calculate. Valid options: 'degree', 'betweenness', or 'closeness'.

    Returns:
        A textual analysis of the chosen metric for the graph with formatted numerical results.
    """
    graph = nx.florentine_families_graph()

    if metric == "degree":
        centrality = nx.degree_centrality(graph)
    elif metric == "betweenness":
        centrality = nx.betweenness_centrality(graph)
    elif metric == "closeness":
        centrality = nx.closeness_centrality(graph)
    else:
        return "Invalid metric. Please choose 'degree', 'betweenness', or 'closeness'."

    analysis = f"Analysis of {metric} centrality:\n"
    for node, value in centrality.items():
        analysis += f"- {node}: {value:.3f}\n"
    return analysis

# Initialize the agent with proper configuration
model = HfApiModel("meta-llama/Llama-3.3-70B-Instruct")
agent = CodeAgent(
    tools=[analyze_florentine_graph],
    model=model,
    additional_authorized_imports=["networkx"],
    add_base_tools=True
)

# Launch the Gradio interface using smolagents' built-in UI component
GradioUI(agent).launch()