Hemasagar commited on
Commit
7829f59
·
1 Parent(s): 7b0f868

tree-convertion added

Browse files
Files changed (3) hide show
  1. Json_2_tree.py +22 -0
  2. app.py +151 -5
  3. requirements.txt +6 -2
Json_2_tree.py ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ # Function to recursively build the tree graph from the JSON
3
+ def json_to_dot(graph, node_id, parent_node, parent_label):
4
+ if isinstance(parent_node, dict):
5
+ for key, value in parent_node.items():
6
+ if key.startswith("Question"):
7
+ question_id = f"{node_id}_{key}"
8
+ label_text = "\n".join(value[i:i+30] for i in range(0, len(value), 30))
9
+ shape = 'diamond' if len(value) > 50 else 'box'
10
+ graph.node(question_id, label_text, shape=shape, style='filled', fillcolor='lightblue')
11
+ graph.edge(parent_label, question_id, color='black')
12
+ json_to_dot(graph, question_id, value, question_id)
13
+ elif key in ["Yes", "No"]:
14
+ option_label = f"{node_id}_{key}"
15
+ graph.node(option_label, key, shape='box', style='filled', fillcolor='lightgreen' if key == "Yes" else 'lightcoral')
16
+ graph.edge(parent_label, option_label, label=key, color='black')
17
+ json_to_dot(graph, option_label, value, option_label)
18
+ elif key == "Result":
19
+ result_label = f"{node_id}_{key}"
20
+ result_str = f"{key}: {value}\nCouncil regulations: {parent_node['Council regulations']}"
21
+ graph.node(result_label, result_str, shape='box', style='filled', fillcolor='lightgrey')
22
+ graph.edge(parent_label, result_label, color='black')
app.py CHANGED
@@ -1,8 +1,154 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
 
2
- from fastapi import FastAPI
 
 
3
 
4
- app = FastAPI()
5
 
6
- @app.get("/")
7
- def greet_json():
8
- return {"Hello": "World!"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from beyondllm import source,retrieve,embeddings,llms,generator
2
+ import os
3
+ from getpass import getpass
4
+ from beyondllm.vectordb import ChromaVectorDb
5
+ import json
6
+ from graphviz import Digraph
7
+ import graphviz
8
+ import streamlit as st
9
+ from beyondllm.llms import AzureOpenAIModel
10
+ from beyondllm.embeddings import AzureAIEmbeddings
11
+ from Scripts.Json_to_tree import create_decision_tree_image
12
+ import networkx as nx
13
+ import matplotlib.pyplot as plt
14
 
15
+ import os
16
+ os.environ["PATH"] += os.pathsep + 'Graphiviz/Graphviz-11.0.0-win64/bin/bin/'
17
+ os.environ["PATH"] += os.pathsep + "Graphviz2.38/bin/dot.exe"
18
 
19
+ st.title("Chat with document")
20
 
21
+ st.text("Enter API Key")
22
+
23
+ # api_key = st.text_input("API Key:", type="password")
24
+ # os.environ['OPENAI_API_KEY'] = api_key
25
+ st.success("API Key entered successfully!")
26
+
27
+ st.caption("Upload a PDF document to get information from the document.")
28
+ uploaded_file = st.file_uploader("Choose a PDF file", type='pdf')
29
+ submit=st.button("Get the data")
30
+ if submit:
31
+
32
+ question = "Give Decision taken in the document"
33
+ system_prompt = '''You are a business analyst with extensive knowledge of legal documents and regulatory documentation.
34
+ Your expertise is in helping people understand and extract key information from such documents.
35
+ Your task is to extract the rules and exceptions in a way that enables the creation of a decision tree, facilitating integration into the proper flow.
36
+ Legal Document Context: {context}
37
+
38
+ Create a decision tree in JSON format based on the following structure:
39
+
40
+ Write a question and question should be two response like yes or no. if yes it has fallowing answers or other question
41
+ - If Yes, the result should be: "Not restricted" additional -Council regulations: provide dates and articles if possible.
42
+ - If No, proceed to the next question2.( by giving some link to the next question not direct to next question)
43
+
44
+ 2. Next question based on the previous question outcome.
45
+ - If Yes, the result should be: "Not restricted" additional -Council regulations: provide dates and articles if possible.
46
+ - If No, proceed to the next question.
47
+ In simple terms - flow chat if conditons.
48
+ [Continue this structure for as many questions as needed, ensuring each question branches into Yes/No answers and provides appropriate results based on the Council regulations.]
49
+ Please continue this format for as many questions as needed, ensuring each question follows the same structure.
50
+ Output is the JSON response follow this pattern: Do not change everytime Json output
51
+ This is JSON output Example, add more questions in this formate only.
52
+ {
53
+ "Question1": ,
54
+ "Yes": {
55
+ "Result": ,
56
+ "Council regulations":
57
+ },
58
+ "No": {
59
+ "Question2": ,
60
+ "Yes": {
61
+ "Result":,
62
+ "Council regulations":
63
+ },
64
+ "No": {
65
+ "Question3": ,
66
+ "Yes": {
67
+ "Result": ,
68
+ "Council regulations":
69
+ },
70
+ "No": {
71
+ "Result": ,
72
+ "Council regulations":
73
+ }
74
+ }
75
+ }
76
+ }
77
+ Additional Instructions:
78
+
79
+ Analyze the entire document to identify all relevant rules and exceptions.
80
+ Ensure that the descriptions of rules and exceptions are clear and concise.
81
+ Include relevant dates, jurisdictions, and specific regulations where applicable.
82
+ Structure the questions and answers to facilitate the creation of a logical decision tree or workflow.
83
+ If the regulation mentions specific products, territories, or operations, include them in the appropriate sections.
84
+ Aim to simplify legal language while maintaining accuracy and intent.
85
+ [Provide your answer in JSON form. Reply with only the answer in JSON form and include no other commentary]:
86
+ Provide your answer in JSON form. Reply with only the answer in JSON form and include no other commentary
87
+
88
+ Return Valid Json to create Tree
89
+ '''
90
+
91
+
92
+
93
+ if uploaded_file is not None and question:
94
+
95
+ save_path = "./uploaded_files"
96
+ if not os.path.exists(save_path):
97
+ os.makedirs(save_path)
98
+ file_path = os.path.join(save_path, uploaded_file.name)
99
+ with open(file_path, "wb") as f:
100
+ f.write(uploaded_file.getbuffer())
101
+
102
+ data = source.fit(file_path, dtype="pdf", chunk_size=1024, chunk_overlap=0)
103
+ embed_model = AzureAIEmbeddings(
104
+ endpoint_url="https://marketplace.openai.azure.com/",
105
+ azure_key="d6d9522a01c74836907af2f3fd72ff85",
106
+ api_version="2024-02-01",
107
+ deployment_name="text-embed-marketplace")
108
+
109
+ retriever = retrieve.auto_retriever(data, embed_model, type="normal", top_k=4)
110
+ # vectordb = ChromaVectorDb(collection_name="my_persistent_collection", persist_directory="./db/chroma/")
111
+
112
+ # llm = llms.ChatOpenAIModel()
113
+ BASE_URL = "https://gpt-res.openai.azure.com/"
114
+ DEPLOYMENT_NAME= "gpt-4-32k"
115
+ API_KEY = "a20bc67dbd7c47ed8c978bbcfdacf930"
116
+ llm = AzureOpenAIModel(model="gpt4",azure_key = API_KEY,deployment_name=DEPLOYMENT_NAME ,endpoint_url=BASE_URL,model_kwargs={"max_tokens":512,"temperature":0.1})
117
+ pipeline = generator.Generate(question=question, system_prompt=system_prompt, retriever=retriever, llm=llm)
118
+ decision_tree_json = pipeline.call()
119
+ response = json.loads(decision_tree_json)
120
+ # Function to recursively create DOT format from JSON
121
+ def json_to_dot(graph, node_id, parent_node, parent_label):
122
+ if isinstance(parent_node, dict):
123
+ for key, value in parent_node.items():
124
+ if key.startswith("Question"):
125
+ question_id = f"{node_id}_{key}"
126
+ label_text = "\n".join(value[i:i+30] for i in range(0, len(value), 30))
127
+ shape = 'diamond' if len(value) > 50 else 'box'
128
+ graph.node(question_id, label_text, shape=shape, style='filled', fillcolor='lightblue')
129
+ graph.edge(parent_label, question_id, color='black')
130
+ json_to_dot(graph, question_id, value, question_id)
131
+ elif key in ["Yes", "No"]:
132
+ option_label = f"{node_id}_{key}"
133
+ graph.node(option_label, key, shape='box', style='filled', fillcolor='lightgreen' if key == "Yes" else 'lightcoral')
134
+ graph.edge(parent_label, option_label, label=key, color='black')
135
+ json_to_dot(graph, option_label, value, option_label)
136
+ elif key == "Result":
137
+ result_label = f"{node_id}_{key}"
138
+ result_str = f"{key}: {value}\nCouncil regulations: {parent_node['Council regulations']}"
139
+ graph.node(result_label, result_str, shape='box', style='filled', fillcolor='lightgrey')
140
+ graph.edge(parent_label, result_label, color='black')
141
+
142
+ # Create a new graph
143
+ dot = graphviz.Digraph(comment='Decision Tree')
144
+ # Add the root node
145
+ dot.node('Root', 'Start', shape='ellipse', style='filled', fillcolor='lightyellow')
146
+ # Build the DOT format
147
+ json_to_dot(dot, "Root", response, "Root")
148
+ # Render and display the graph using Graphviz engine
149
+ dot.format = 'png'
150
+ dot.render('decision_tree', view=True)
151
+ import streamlit as st
152
+ with st.chat_message(""):
153
+ st.write("")
154
+ st.image('decision_tree.png', caption='tree from json')
requirements.txt CHANGED
@@ -1,3 +1,7 @@
1
 
2
- fastapi
3
- uvicorn[standard]
 
 
 
 
 
1
 
2
+ beyondllm
3
+ streamlit
4
+ graphviz
5
+ llama_index.vector_stores.chroma
6
+ llama-index-embeddings-huggingface
7
+ llama-index-embeddings-azure_openai