dwb2023 commited on
Commit
2711826
·
verified ·
1 Parent(s): 6557388

deepseek point-of-view

Browse files
Files changed (1) hide show
  1. app.py +25 -47
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: Literal["degree", "betweenness", "closeness"]) -> str:
7
  """
8
- Analyzes the Florentine Families social network graph using centrality metrics.
9
-
10
  Args:
11
- metric: The centrality metric to calculate. Must be one of:
12
- - "degree": Measures direct connections
13
- - "betweenness": Measures bridge/broker positions
14
- - "closeness": Measures average distance to all other nodes
15
  """
16
  graph = nx.florentine_families_graph()
17
-
18
- centrality_funcs = {
19
- "degree": nx.degree_centrality,
20
- "betweenness": nx.betweenness_centrality,
21
- "closeness": nx.closeness_centrality
22
- }
23
-
24
- if metric not in centrality_funcs:
25
- return f"Invalid metric. Please choose from: {', '.join(centrality_funcs.keys())}"
26
-
27
- centrality = centrality_funcs[metric](graph)
28
-
29
- # Sort by centrality value for clearer output
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 model and tool
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
- if __name__ == "__main__":
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()