dwb2023 commited on
Commit
2189307
·
verified ·
1 Parent(s): 52220ed

initial commit

Browse files
Files changed (1) hide show
  1. app.py +76 -0
app.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import networkx as nx
2
+ from smolagents import CodeAgent, HfApiModel, tool
3
+ import gradio as gr
4
+
5
+ # Define a tool for analyzing the Florentine Families graph
6
+ @tool
7
+ def analyze_florentine_graph(metric: str) -> str:
8
+ """
9
+ Analyzes the Florentine Families graph based on the chosen metric.
10
+
11
+ Args:
12
+ metric (str): The centrality or analysis metric to calculate (e.g., 'degree', 'betweenness', 'closeness').
13
+
14
+ Returns:
15
+ str: A textual analysis of the chosen metric for the graph.
16
+ """
17
+ # Load the Florentine Families graph
18
+ graph = nx.florentine_families_graph()
19
+
20
+ if metric == "degree":
21
+ centrality = nx.degree_centrality(graph)
22
+ elif metric == "betweenness":
23
+ centrality = nx.betweenness_centrality(graph)
24
+ elif metric == "closeness":
25
+ centrality = nx.closeness_centrality(graph)
26
+ else:
27
+ return "Invalid metric. Please choose 'degree', 'betweenness', or 'closeness'."
28
+
29
+ # Format the results
30
+ analysis = f"Analysis of {metric} centrality:\n"
31
+ for node, value in centrality.items():
32
+ analysis += f"- {node}: {value:.3f}\n"
33
+
34
+ return analysis
35
+
36
+ # Create the CodeAgent
37
+ class FlorentineGraphAgentUI:
38
+ def __init__(self):
39
+ self.model = HfApiModel("meta-llama/Llama-3.3-70B-Instruct")
40
+ self.agent = CodeAgent(
41
+ tools=[analyze_florentine_graph],
42
+ model=self.model,
43
+ additional_authorized_imports=["networkx"]
44
+ )
45
+
46
+ def process_query(self, query: str) -> str:
47
+ """Process a natural language query to analyze the graph."""
48
+ return self.agent.run(query)
49
+
50
+ def launch(self):
51
+ """Launch the Gradio interface."""
52
+ interface = gr.Interface(
53
+ fn=self.process_query,
54
+ inputs=gr.Textbox(
55
+ label="What would you like to analyze about the Florentine Families graph?",
56
+ placeholder="e.g., 'Analyze degree centrality'"
57
+ ),
58
+ outputs=gr.Textbox(label="Graph Analysis Results"),
59
+ title="Florentine Families Graph Analysis",
60
+ description="""
61
+ This app analyzes the Florentine Families graph using various centrality metrics.
62
+ You can request analyses like degree centrality, betweenness centrality, or closeness centrality.
63
+ """,
64
+ examples=[
65
+ ["Analyze degree centrality"],
66
+ ["Analyze betweenness centrality"],
67
+ ["Analyze closeness centrality"]
68
+ ],
69
+ cache_examples=False
70
+ )
71
+ interface.launch()
72
+
73
+ if __name__ == "__main__":
74
+ # Initialize and launch the app
75
+ app = FlorentineGraphAgentUI()
76
+ app.launch()