Ilyas KHIAT commited on
Commit
1e3f619
·
1 Parent(s): bd97124
app.py CHANGED
@@ -17,7 +17,7 @@ def main():
17
 
18
  pg = st.navigation(
19
  {
20
- "Audit de contenus": [audit_page],
21
  "Equipe d'agents IA": [recommended_agents],
22
  "Chatbot": [chatbot],
23
  "Documentation": [documentation]
 
17
 
18
  pg = st.navigation(
19
  {
20
+ "Audit de contenus": [audit_page, kg_page],
21
  "Equipe d'agents IA": [recommended_agents],
22
  "Chatbot": [chatbot],
23
  "Documentation": [documentation]
audit_page/knowledge_graph.py CHANGED
@@ -1,10 +1,112 @@
1
  import streamlit as st
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
 
4
  def kg_main():
5
  #st.set_page_config(page_title="Graphe de connaissance", page_icon="", layout="wide")
6
-
 
7
  st.title("Graphe de connaissance")
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
  kg_main()
 
1
  import streamlit as st
2
+ from utils.kg.construct_kg import get_graph
3
+ from utils.audit.rag import get_text_from_content_for_doc,get_text_from_content_for_audio
4
+ from streamlit_agraph import agraph, Node, Edge, Config
5
+
6
+ def if_node_exists(nodes, node_id):
7
+ """
8
+ Check if a node exists in the graph.
9
+
10
+ Args:
11
+ graph (dict): A dictionary representing the graph with keys 'nodes' and 'relationships'.
12
+ node_id (str): The id of the node to check.
13
+
14
+ Returns:
15
+ return_value: True if the node exists, False otherwise.
16
+ """
17
+ for node in nodes:
18
+ if node.id == node_id:
19
+ return True
20
+ return False
21
+
22
+ def convert_neo4j_to_agraph(neo4j_graph):
23
+ """
24
+ Converts a Neo4j graph into an Agraph format.
25
+
26
+ Args:
27
+ neo4j_graph (dict): A dictionary representing the Neo4j graph with keys 'nodes' and 'relationships'.
28
+ 'nodes' is a list of dicts with each dict having 'id' and 'type' keys.
29
+ 'relationships' is a list of dicts with 'source', 'target', and 'type' keys.
30
+
31
+ Returns:
32
+ return_value: The Agraph visualization object.
33
+ """
34
+ nodes = []
35
+ edges = []
36
+
37
+ # Creating Agraph nodes
38
+ for node in neo4j_graph.nodes:
39
+ # Use the node id as the Agraph node id
40
+ node_id = node.id.replace(" ", "_") # Replace spaces with underscores for ids
41
+ label = node.id
42
+ size = 25 # Default size, can be customized
43
+ shape = "circle" # Default shape, can be customized
44
+
45
+ # For example purposes, no images are added, but you can set 'image' if needed.
46
+ nodes.append(Node(id=node_id, label=label, size=size, shape=shape))
47
+
48
+ # Creating Agraph edges
49
+ for relationship in neo4j_graph.relationships:
50
+ size = 25 # Default size, can be customized
51
+ shape = "circle" # Default shape, can be customized
52
+
53
+ source = relationship.source
54
+ source_id = source.id.replace(" ", "_")
55
+ label_source = source.id
56
+
57
+ source_node = Node(id=source_id, label=label_source, size=size, shape=shape)
58
+ if not if_node_exists(nodes, source_node.id):
59
+ nodes.append(source_node)
60
+
61
+ target = relationship.target
62
+ target_id = target.id.replace(" ", "_")
63
+ label_target = target.id
64
+
65
+ target_node = Node(id=target_id, label=label_target, size=size, shape=shape)
66
+ if not if_node_exists(nodes, target_node.id):
67
+ nodes.append(target_node)
68
+
69
+ label = relationship.type
70
+
71
+ edges.append(Edge(source=source_id, label=label, target=target_id))
72
+
73
+ # Define the configuration for Agraph
74
+ config = Config(width=750, height=950, directed=True, physics=True, hierarchical=False)
75
+
76
+ # Create the Agraph visualization
77
+ return_value = agraph(nodes=nodes, edges=edges, config=config)
78
+ st.write(return_value)
79
+ return return_value
80
 
81
 
82
  def kg_main():
83
  #st.set_page_config(page_title="Graphe de connaissance", page_icon="", layout="wide")
84
+ if "graph" not in st.session_state:
85
+ st.session_state.graph = None
86
  st.title("Graphe de connaissance")
87
 
88
+ if "audit" not in st.session_state or st.session_state.audit == {}:
89
+ st.error("Veuillez d'abord effectuer un audit pour obtenir des recommandations d'agents.")
90
+ return
91
+
92
+ audit = st.session_state.audit_simplified
93
+ content = st.session_state.audit["content"]
94
+
95
+ if audit["type de fichier"] == "pdf":
96
+ text = get_text_from_content_for_doc(content)
97
+ elif audit["type de fichier"] == "audio":
98
+ text = get_text_from_content_for_audio(content)
99
+
100
+ if st.button("Générer le graphe"):
101
+ graph = get_graph(text)
102
+ st.session_state.graph = graph
103
+ else:
104
+ graph = st.session_state.graph
105
+ if graph is not None:
106
+ #st.write(graph)
107
+ agraph_obj = convert_neo4j_to_agraph(graph[0])
108
+
109
+
110
+
111
 
112
  kg_main()
requirements.txt CHANGED
@@ -20,3 +20,6 @@ langchain-mistralai
20
  faiss-cpu
21
  langchain-community
22
  python-dotenv
 
 
 
 
20
  faiss-cpu
21
  langchain-community
22
  python-dotenv
23
+ langchain-experimental
24
+ neo4j
25
+ streamlit-agraph
utils/audit/audit_audio.py CHANGED
@@ -1,4 +1,4 @@
1
- import streamlit as st
2
  import numpy as np
3
  import scipy.io.wavfile as wavfile
4
  from pydub import AudioSegment
 
1
+
2
  import numpy as np
3
  import scipy.io.wavfile as wavfile
4
  from pydub import AudioSegment
utils/kg/construct_kg.py ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain_community.graphs import Neo4jGraph
2
+ from langchain_experimental.graph_transformers import LLMGraphTransformer
3
+ from langchain_openai import ChatOpenAI
4
+ from langchain_core.documents import Document
5
+
6
+ def get_graph(text):
7
+
8
+ graph = Neo4jGraph()
9
+ llm = ChatOpenAI(temperature=0, model_name="gpt-4o")
10
+
11
+ llm_transformer = LLMGraphTransformer(llm=llm)
12
+ documents = [Document(page_content=text)]
13
+
14
+ graph_documents = llm_transformer.convert_to_graph_documents(documents)
15
+ return graph_documents
16
+
17
+