bpavani5258 commited on
Commit
01ac10f
·
verified ·
1 Parent(s): 8c5c24b

Add text_to_graph_tool

Browse files
Files changed (1) hide show
  1. app.py +61 -15
app.py CHANGED
@@ -2,21 +2,68 @@ from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
2
  import datetime
3
  import requests
4
  import pytz
5
- import yaml
 
 
 
6
  from tools.final_answer import FinalAnswerTool
7
 
8
  from Gradio_UI import GradioUI
9
 
10
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
11
  @tool
12
- def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
13
- #Keep this format for the description / args / args description but feel free to modify the tool
14
- """A tool that does nothing yet
15
  Args:
16
- arg1: the first argument
17
- arg2: the second argument
18
  """
19
- return "What magic will you build ?"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  @tool
22
  def get_current_time_in_timezone(timezone: str) -> str:
@@ -55,14 +102,13 @@ with open("prompts.yaml", 'r') as stream:
55
 
56
  agent = CodeAgent(
57
  model=model,
58
- tools=[final_answer], ## add your tools here (don't remove final answer)
59
- max_steps=6,
60
- verbosity_level=1,
61
- grammar=None,
62
- planning_interval=None,
63
- name=None,
64
- description=None,
65
- prompt_templates=prompt_templates
66
  )
67
 
68
 
 
2
  import datetime
3
  import requests
4
  import pytz
5
+ import yaml
6
+ import os
7
+ import matplotlib.pyplot as plt
8
+ import networkx as nx
9
  from tools.final_answer import FinalAnswerTool
10
 
11
  from Gradio_UI import GradioUI
12
 
13
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
14
  @tool
15
+ def text_to_graph_tool(relationships: str) -> str:
16
+ """ Generates a directed network graph image from comma- or semicolon-separated relationship strings.
17
+
18
  Args:
19
+ relationships: A semicolon-separated list of edges formatted as 'Source -> Target' or 'Source -> Target: Label'.
20
+ Example: 'User -> WebServer: Requests; WebServer -> Database: Queries'
21
  """
22
+ try:
23
+ plt.figure(figsize=(9, 6))
24
+ G = nx.DiGraph()
25
+
26
+ lines = [r.strip() for r in relationships.replace("\n", ";").split(";") if r.strip()]
27
+ edge_labels = {}
28
+
29
+ for entry in lines:
30
+ if "->" in entry:
31
+ parts = entry.split("->")
32
+ source = parts[0].strip()
33
+ remainder = parts[1].strip()
34
+
35
+ if ":" in remainder:
36
+ target, label = remainder.split(":", 1)
37
+ target, label = target.strip(), label.strip()
38
+ else:
39
+ target, label = remainder, ""
40
+
41
+ G.add_edge(source, target)
42
+ if label:
43
+ edge_labels[(source, target)] = label
44
+
45
+ if len(G.nodes) == 0:
46
+ return "No valid relationships found. Use format: 'NodeA -> NodeB: Label'"
47
+
48
+ pos = nx.spring_layout(G, k=1.0, seed=42)
49
+ nx.draw_networkx_nodes(G, pos, node_size=2800, node_color="#4F46E5", alpha=0.9)
50
+ nx.draw_networkx_edges(G, pos, arrowstyle="->", arrowsize=18, edge_color="#64748B", width=2.0)
51
+ nx.draw_networkx_labels(G, pos, font_size=9, font_color="white", font_weight="bold")
52
+
53
+ if edge_labels:
54
+ nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels, font_size=8, font_color="#DC2626")
55
+
56
+ plt.axis("off")
57
+ plt.tight_layout()
58
+
59
+ os.makedirs("generated_graphs", exist_ok=True)
60
+ file_path = f"generated_graphs/graph_{int(datetime.datetime.now().timestamp())}.png"
61
+ plt.savefig(file_path, dpi=200, bbox_inches="tight")
62
+ plt.close()
63
+
64
+ return f"Graph successfully created and saved at: {file_path}"
65
+ except Exception as e:
66
+ return f"Error generating graph: {str(e)}"
67
 
68
  @tool
69
  def get_current_time_in_timezone(timezone: str) -> str:
 
102
 
103
  agent = CodeAgent(
104
  model=model,
105
+ tools=[
106
+ final_answer,
107
+ text_to_graph_tool,
108
+ image_generation_tool,
109
+ get_current_time_in_timezone
110
+ ],
111
+ ...
 
112
  )
113
 
114