deepseek point-of-view
Browse files
app.py
CHANGED
@@ -1,64 +1,42 @@
|
|
1 |
import networkx as nx
|
2 |
from smolagents import CodeAgent, HfApiModel, tool, GradioUI
|
3 |
-
from typing import Literal
|
4 |
|
|
|
5 |
@tool
|
6 |
-
def analyze_florentine_graph(metric:
|
7 |
"""
|
8 |
-
Analyzes the Florentine Families
|
9 |
-
|
10 |
Args:
|
11 |
-
metric: The centrality metric to calculate.
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
"""
|
16 |
graph = nx.florentine_families_graph()
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
return
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
sorted_values = sorted(centrality.items(), key=lambda x: x[1], reverse=True)
|
31 |
-
|
32 |
-
analysis = f"\nTop families by {metric} centrality:\n"
|
33 |
-
for family, value in sorted_values:
|
34 |
-
analysis += f"- {family}: {value:.3f}\n"
|
35 |
-
|
36 |
return analysis
|
37 |
|
38 |
-
# Initialize agent with
|
39 |
model = HfApiModel("meta-llama/Llama-3.3-70B-Instruct")
|
40 |
agent = CodeAgent(
|
41 |
tools=[analyze_florentine_graph],
|
42 |
model=model,
|
43 |
additional_authorized_imports=["networkx"],
|
|
|
44 |
)
|
45 |
|
46 |
-
# Launch Gradio interface using smolagents' built-in UI
|
47 |
-
|
48 |
-
ui = GradioUI(
|
49 |
-
agent=agent,
|
50 |
-
title="Florentine Families Network Analysis",
|
51 |
-
description="""
|
52 |
-
Analyze the historical Florentine families social network using various centrality metrics.
|
53 |
-
Try asking questions like:
|
54 |
-
- "Who were the most central families by degree centrality?"
|
55 |
-
- "Which families acted as brokers (high betweenness)?"
|
56 |
-
- "Analyze the network using closeness centrality"
|
57 |
-
""",
|
58 |
-
examples=[
|
59 |
-
"Analyze the network using degree centrality",
|
60 |
-
"Who were the main broker families according to betweenness centrality?",
|
61 |
-
"Show me the closeness centrality analysis"
|
62 |
-
]
|
63 |
-
)
|
64 |
-
ui.launch()
|
|
|
1 |
import networkx as nx
|
2 |
from smolagents import CodeAgent, HfApiModel, tool, GradioUI
|
|
|
3 |
|
4 |
+
# Define a tool for analyzing the Florentine Families graph
|
5 |
@tool
|
6 |
+
def analyze_florentine_graph(metric: str) -> str:
|
7 |
"""
|
8 |
+
Analyzes the Florentine Families graph based on the chosen centrality metric.
|
9 |
+
|
10 |
Args:
|
11 |
+
metric: The centrality metric to calculate. Valid options: 'degree', 'betweenness', or 'closeness'.
|
12 |
+
|
13 |
+
Returns:
|
14 |
+
A textual analysis of the chosen metric for the graph with formatted numerical results.
|
15 |
"""
|
16 |
graph = nx.florentine_families_graph()
|
17 |
+
|
18 |
+
if metric == "degree":
|
19 |
+
centrality = nx.degree_centrality(graph)
|
20 |
+
elif metric == "betweenness":
|
21 |
+
centrality = nx.betweenness_centrality(graph)
|
22 |
+
elif metric == "closeness":
|
23 |
+
centrality = nx.closeness_centrality(graph)
|
24 |
+
else:
|
25 |
+
return "Invalid metric. Please choose 'degree', 'betweenness', or 'closeness'."
|
26 |
+
|
27 |
+
analysis = f"Analysis of {metric} centrality:\n"
|
28 |
+
for node, value in centrality.items():
|
29 |
+
analysis += f"- {node}: {value:.3f}\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
return analysis
|
31 |
|
32 |
+
# Initialize the agent with proper configuration
|
33 |
model = HfApiModel("meta-llama/Llama-3.3-70B-Instruct")
|
34 |
agent = CodeAgent(
|
35 |
tools=[analyze_florentine_graph],
|
36 |
model=model,
|
37 |
additional_authorized_imports=["networkx"],
|
38 |
+
add_base_tools=True
|
39 |
)
|
40 |
|
41 |
+
# Launch the Gradio interface using smolagents' built-in UI component
|
42 |
+
GradioUI(agent).launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|