Spaces:
Build error
Build error
fabiochiusano
commited on
Commit
·
cc1a8be
1
Parent(s):
5ca37e8
max num chars and manage url errors
Browse files- app.py +13 -5
- networks/network.html +116 -0
app.py
CHANGED
|
@@ -82,7 +82,7 @@ if option == "Text":
|
|
| 82 |
if 'text' not in st.session_state:
|
| 83 |
st.session_state.text = ""
|
| 84 |
text = st.session_state.text
|
| 85 |
-
text = st.text_area('Text:', value=text, height=300, disabled=disabled)
|
| 86 |
elif option == "Article at URL":
|
| 87 |
url_options = [
|
| 88 |
"Crypto",
|
|
@@ -114,8 +114,6 @@ else:
|
|
| 114 |
st.session_state.news_option = news_options[0]
|
| 115 |
news_option = st.selectbox('Use articles about:', news_options, index=news_options.index(st.session_state.news_option))
|
| 116 |
|
| 117 |
-
placeholder = st.empty()
|
| 118 |
-
|
| 119 |
def generate_kb():
|
| 120 |
st.session_state.option = option
|
| 121 |
st.session_state.text_option = text_option
|
|
@@ -142,7 +140,12 @@ def generate_kb():
|
|
| 142 |
elif url_option == "Rome":
|
| 143 |
kb = utils.load_kb("networks/network_2_rome.p")
|
| 144 |
else:
|
| 145 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
else:
|
| 147 |
if news_option == "Google":
|
| 148 |
kb = utils.load_kb("networks/network_3_google.p")
|
|
@@ -157,6 +160,7 @@ def generate_kb():
|
|
| 157 |
utils.save_network_html(kb, filename="networks/network.html")
|
| 158 |
st.session_state.kb_chart = "networks/network.html"
|
| 159 |
st.session_state.kb_text = kb.get_textual_representation()
|
|
|
|
| 160 |
|
| 161 |
|
| 162 |
st.session_state.option = option
|
|
@@ -178,9 +182,13 @@ if 'kb_chart' not in st.session_state:
|
|
| 178 |
st.session_state.kb_chart = None
|
| 179 |
if 'kb_text' not in st.session_state:
|
| 180 |
st.session_state.kb_text = None
|
|
|
|
|
|
|
| 181 |
|
| 182 |
# show graph
|
| 183 |
-
if st.session_state.
|
|
|
|
|
|
|
| 184 |
with st.container():
|
| 185 |
st.subheader("Generated KB")
|
| 186 |
st.markdown("*You can interact with the graph and zoom.*")
|
|
|
|
| 82 |
if 'text' not in st.session_state:
|
| 83 |
st.session_state.text = ""
|
| 84 |
text = st.session_state.text
|
| 85 |
+
text = st.text_area('Text:', value=text, height=300, disabled=disabled, max_chars=1500)
|
| 86 |
elif option == "Article at URL":
|
| 87 |
url_options = [
|
| 88 |
"Crypto",
|
|
|
|
| 114 |
st.session_state.news_option = news_options[0]
|
| 115 |
news_option = st.selectbox('Use articles about:', news_options, index=news_options.index(st.session_state.news_option))
|
| 116 |
|
|
|
|
|
|
|
| 117 |
def generate_kb():
|
| 118 |
st.session_state.option = option
|
| 119 |
st.session_state.text_option = text_option
|
|
|
|
| 140 |
elif url_option == "Rome":
|
| 141 |
kb = utils.load_kb("networks/network_2_rome.p")
|
| 142 |
else:
|
| 143 |
+
try:
|
| 144 |
+
kb = utils.from_url_to_kb(url, model, tokenizer)
|
| 145 |
+
except Exception as e:
|
| 146 |
+
print("Couldn't extract article from URL")
|
| 147 |
+
st.session_state.error_url = "Couldn't extract article from URL"
|
| 148 |
+
return
|
| 149 |
else:
|
| 150 |
if news_option == "Google":
|
| 151 |
kb = utils.load_kb("networks/network_3_google.p")
|
|
|
|
| 160 |
utils.save_network_html(kb, filename="networks/network.html")
|
| 161 |
st.session_state.kb_chart = "networks/network.html"
|
| 162 |
st.session_state.kb_text = kb.get_textual_representation()
|
| 163 |
+
st.session_state.error_url = None
|
| 164 |
|
| 165 |
|
| 166 |
st.session_state.option = option
|
|
|
|
| 182 |
st.session_state.kb_chart = None
|
| 183 |
if 'kb_text' not in st.session_state:
|
| 184 |
st.session_state.kb_text = None
|
| 185 |
+
if 'error_url' not in st.session_state:
|
| 186 |
+
st.session_state.error_url = None
|
| 187 |
|
| 188 |
# show graph
|
| 189 |
+
if st.session_state.error_url:
|
| 190 |
+
st.markdown(st.session_state.error_url)
|
| 191 |
+
elif st.session_state.kb_chart:
|
| 192 |
with st.container():
|
| 193 |
st.subheader("Generated KB")
|
| 194 |
st.markdown("*You can interact with the graph and zoom.*")
|
networks/network.html
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<html>
|
| 2 |
+
<head>
|
| 3 |
+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/vis-network@latest/styles/vis-network.css" type="text/css" />
|
| 4 |
+
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/vis-network@latest/dist/vis-network.min.js"> </script>
|
| 5 |
+
<center>
|
| 6 |
+
<h1></h1>
|
| 7 |
+
</center>
|
| 8 |
+
|
| 9 |
+
<!-- <link rel="stylesheet" href="../node_modules/vis/dist/vis.min.css" type="text/css" />
|
| 10 |
+
<script type="text/javascript" src="../node_modules/vis/dist/vis.js"> </script>-->
|
| 11 |
+
|
| 12 |
+
<style type="text/css">
|
| 13 |
+
|
| 14 |
+
#mynetwork {
|
| 15 |
+
width: 700px;
|
| 16 |
+
height: 700px;
|
| 17 |
+
background-color: #ffffff;
|
| 18 |
+
border: 1px solid lightgray;
|
| 19 |
+
position: relative;
|
| 20 |
+
float: left;
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
</style>
|
| 29 |
+
|
| 30 |
+
</head>
|
| 31 |
+
|
| 32 |
+
<body>
|
| 33 |
+
<div id = "mynetwork"></div>
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
<script type="text/javascript">
|
| 37 |
+
|
| 38 |
+
// initialize global variables.
|
| 39 |
+
var edges;
|
| 40 |
+
var nodes;
|
| 41 |
+
var network;
|
| 42 |
+
var container;
|
| 43 |
+
var options, data;
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
// This method is responsible for drawing the graph, returns the drawn network
|
| 47 |
+
function drawGraph() {
|
| 48 |
+
var container = document.getElementById('mynetwork');
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
// parsing and collecting nodes and edges from the python
|
| 53 |
+
nodes = new vis.DataSet([{"color": "#00FF00", "id": "Napoleon", "label": "Napoleon", "shape": "circle"}, {"color": "#00FF00", "id": "French Revolution", "label": "French Revolution", "shape": "circle"}, {"color": "#00FF00", "id": "France", "label": "France", "shape": "circle"}]);
|
| 54 |
+
edges = new vis.DataSet([{"arrows": "to", "from": "Napoleon", "label": "participant in", "title": "participant in", "to": "French Revolution"}, {"arrows": "to", "from": "French Revolution", "label": "participant", "title": "participant", "to": "Napoleon"}, {"arrows": "to", "from": "French Revolution", "label": "country", "title": "country", "to": "France"}]);
|
| 55 |
+
|
| 56 |
+
// adding nodes and edges to the graph
|
| 57 |
+
data = {nodes: nodes, edges: edges};
|
| 58 |
+
|
| 59 |
+
var options = {
|
| 60 |
+
"configure": {
|
| 61 |
+
"enabled": false
|
| 62 |
+
},
|
| 63 |
+
"edges": {
|
| 64 |
+
"color": {
|
| 65 |
+
"inherit": true
|
| 66 |
+
},
|
| 67 |
+
"smooth": {
|
| 68 |
+
"enabled": true,
|
| 69 |
+
"type": "dynamic"
|
| 70 |
+
}
|
| 71 |
+
},
|
| 72 |
+
"interaction": {
|
| 73 |
+
"dragNodes": true,
|
| 74 |
+
"hideEdgesOnDrag": false,
|
| 75 |
+
"hideNodesOnDrag": false
|
| 76 |
+
},
|
| 77 |
+
"physics": {
|
| 78 |
+
"enabled": true,
|
| 79 |
+
"repulsion": {
|
| 80 |
+
"centralGravity": 0.2,
|
| 81 |
+
"damping": 0.09,
|
| 82 |
+
"nodeDistance": 200,
|
| 83 |
+
"springConstant": 0.05,
|
| 84 |
+
"springLength": 200
|
| 85 |
+
},
|
| 86 |
+
"solver": "repulsion",
|
| 87 |
+
"stabilization": {
|
| 88 |
+
"enabled": true,
|
| 89 |
+
"fit": true,
|
| 90 |
+
"iterations": 1000,
|
| 91 |
+
"onlyDynamicEdges": false,
|
| 92 |
+
"updateInterval": 50
|
| 93 |
+
}
|
| 94 |
+
}
|
| 95 |
+
};
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
network = new vis.Network(container, data, options);
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
return network;
|
| 109 |
+
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
+
drawGraph();
|
| 113 |
+
|
| 114 |
+
</script>
|
| 115 |
+
</body>
|
| 116 |
+
</html>
|