lorenzoscottb commited on
Commit
b77ef40
β€’
1 Parent(s): a0177e3

Create graph_utils.py

Browse files
Files changed (1) hide show
  1. graph_utils.py +94 -0
graph_utils.py ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pyvis.network import Network
2
+ import gradio as gr
3
+ from transformers import pipeline
4
+ import os
5
+
6
+ model_id = models/DReAMy-lib/t5-base-DreamBank-Generation-Act-Char
7
+
8
+ title = "RE Generation"
9
+ description_GNER = """
10
+ A T5 model tuned to perform text generation, and predict which characters are present in the report. Note that, in the Hall and Van de Castle, the character lists never includes the dreamer. Hence, if you (willingly or not) enter a report that does not contain another character reference, the model will/should (correctly) produce an empty string. Moreover, it is likely that the produced list of CHAR could be longer than the one produced by the SA model, as not all CHAR might be associated with emotions.
11
+ """
12
+
13
+ def get_graph_dict(graph_text):
14
+ edge_labels = {}
15
+ if graph_text == "":
16
+ edge_labels = {("No_Graphs", None):None}
17
+
18
+ else:
19
+ try:
20
+ for trpl in graph_text[1:-1].split(" | "):
21
+ h,r,t = trpl[1:-1].split(" # ")
22
+ edge_labels[(h,t)] = r
23
+ except:
24
+ edge_labels = {("Error", None):None}
25
+ return edge_labels
26
+
27
+ def text_to_graph(text):
28
+
29
+ # Use a pipeline as a high-level helper
30
+ pipe = pipeline(
31
+ "text2text-generation",
32
+ model=model_id,
33
+ token=usr_tkn,
34
+ max_length=300,
35
+ min_length=10,
36
+ )
37
+
38
+ # generate text graph
39
+ graph_text = pipe(text)
40
+
41
+ graph_text = graph_text[0]["generated_text"]
42
+
43
+ # get the nodes: label dict
44
+ edge_labels = get_graph_dict(graph_text)
45
+
46
+ # create network
47
+ net = Network(directed=True)
48
+
49
+ # nodes & edges
50
+ for (h, t), r in edge_labels.items():
51
+ if (h == "Error") or (h == "No_Graphs"):
52
+ net.add_node(h, shape="circle")
53
+ continue
54
+ else:
55
+ net.add_node(h, shape="circle")
56
+ net.add_node(t, shape="circle")
57
+ net.add_edge(h, t, title=r, label=r)
58
+
59
+ # set structure
60
+ net.repulsion(
61
+ node_distance=200,
62
+ central_gravity=0.2,
63
+ spring_length=200,
64
+ spring_strength=0.05,
65
+ damping=0.09
66
+ )
67
+
68
+ net.set_edge_smooth('dynamic')
69
+
70
+ # get html
71
+ html = net.generate_html()
72
+ html = html.replace("'", "\"")
73
+
74
+ html_s = f"""<iframe style="width: 100%; height: 600px;margin:0 auto" name="result" allow="midi; geolocation; microphone; camera;
75
+ display-capture; encrypted-media;" sandbox="allow-modals allow-forms
76
+ allow-scripts allow-same-origin allow-popups
77
+ allow-top-navigation-by-user-activation allow-downloads" allowfullscreen=""
78
+ allowpaymentrequest="" frameborder="0" srcdoc='{html}'></iframe>"""
79
+
80
+ return html_s, graph_text
81
+
82
+ # demo = gr.Interface(
83
+ # text_to_graph,
84
+ # inputs=gr.Textbox(label="Text", placeholder="Enter a text here."),
85
+ # outputs=[gr.HTML(label="Extracted graph"),gr.Textbox(label="Extracted text")]
86
+ # examples= [
87
+ # ["I was skating on the outdoor ice pond that used to be across the street from my house. I was not alone, but I did not recognize any of the other people who were skating around. I went through my whole repertoire of jumps, spires, and steps-some of which I can do and some of which I'm not yet sure of. They were all executed flawlessly-some I repeated, some I did only once. I seemed to know that if I went into competition, I would be sure of coming in third because there were only three contestants. Up to that time I hadn't considered it because I hadn't thought I was good enough, but now since everything was going so well, I decided to enter."],
88
+ # ["I was talking on the telephone to the father of an old friend of mine (boy, 21 years old). We were discussing the party the Saturday night before to which I had invited his son as a guest. I asked him if his son had a good time at the party. He told me not to tell his son that he had told me, but that he had had a good time, except he was a little surprised that I had acted the way I did."],
89
+ # ["I was walking alone with my dog in a forest."]
90
+ # ],
91
+ # title=title,
92
+ # description=description_GNER,
93
+ # cache_examples=True,
94
+ # )