app development tryout
Browse files
app.py
CHANGED
|
@@ -11,15 +11,15 @@ model3_generator = pipeline('text2text-generation', model='google/flan-t5-large'
|
|
| 11 |
|
| 12 |
def model1_translate_to_graph(request_text):
|
| 13 |
"""
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
Nodes is a list of node names and edges is a list of pairs
|
| 17 |
"""
|
| 18 |
prompt = (
|
| 19 |
"Translate the following customer request into a structured graph. "
|
| 20 |
-
"Output only valid JSON with exactly two keys: 'nodes'
|
| 21 |
-
"
|
| 22 |
-
"Do not include any extra text.\n"
|
| 23 |
f"Customer Request: \"{request_text}\"\n"
|
| 24 |
"Structured Graph JSON:"
|
| 25 |
)
|
|
@@ -28,7 +28,7 @@ def model1_translate_to_graph(request_text):
|
|
| 28 |
|
| 29 |
def model2_generate_response(graph_description):
|
| 30 |
"""
|
| 31 |
-
|
| 32 |
"""
|
| 33 |
prompt = (
|
| 34 |
"Based on the following structured customer request graph (formatted as JSON), "
|
|
@@ -41,14 +41,13 @@ def model2_generate_response(graph_description):
|
|
| 41 |
|
| 42 |
def model3_response_to_graph(response_text):
|
| 43 |
"""
|
| 44 |
-
|
| 45 |
Output only valid JSON with two keys: 'nodes' and 'edges'.
|
| 46 |
"""
|
| 47 |
prompt = (
|
| 48 |
"Translate the following response into a structured graph. "
|
| 49 |
-
"Output only valid JSON with two keys: 'nodes' (a list of concept names) "
|
| 50 |
-
"
|
| 51 |
-
"Do not include any extra text.\n"
|
| 52 |
f"Response: \"{response_text}\"\n"
|
| 53 |
"Structured Response Graph JSON:"
|
| 54 |
)
|
|
@@ -70,8 +69,8 @@ def try_parse_json(text):
|
|
| 70 |
|
| 71 |
def build_and_visualize_graph(graph_data, title="Graph Visualization"):
|
| 72 |
"""
|
| 73 |
-
Build a NetworkX graph from a dictionary with 'nodes' and 'edges'
|
| 74 |
-
and
|
| 75 |
"""
|
| 76 |
if not graph_data or 'nodes' not in graph_data or 'edges' not in graph_data:
|
| 77 |
st.error("Invalid graph data format.")
|
|
@@ -89,11 +88,12 @@ def build_and_visualize_graph(graph_data, title="Graph Visualization"):
|
|
| 89 |
st.pyplot(plt)
|
| 90 |
|
| 91 |
def main():
|
| 92 |
-
st.title("Three-
|
| 93 |
st.write(
|
| 94 |
-
"Enter a customer request
|
| 95 |
-
"
|
| 96 |
-
"
|
|
|
|
| 97 |
)
|
| 98 |
|
| 99 |
customer_request = st.text_area("Enter Customer Request:", height=150)
|
|
@@ -103,25 +103,28 @@ def main():
|
|
| 103 |
st.error("Please enter a valid customer request.")
|
| 104 |
return
|
| 105 |
|
| 106 |
-
|
|
|
|
| 107 |
model1_output = model1_translate_to_graph(customer_request)
|
| 108 |
st.code(model1_output, language="json")
|
| 109 |
-
|
| 110 |
-
if
|
| 111 |
-
build_and_visualize_graph(
|
| 112 |
else:
|
| 113 |
st.warning("Could not parse a valid graph structure from Model 1 output.")
|
| 114 |
|
| 115 |
-
|
|
|
|
| 116 |
response_text = model2_generate_response(model1_output)
|
| 117 |
st.write(response_text)
|
| 118 |
|
| 119 |
-
|
|
|
|
| 120 |
model3_output = model3_response_to_graph(response_text)
|
| 121 |
st.code(model3_output, language="json")
|
| 122 |
-
|
| 123 |
-
if
|
| 124 |
-
build_and_visualize_graph(
|
| 125 |
else:
|
| 126 |
st.warning("Could not parse a valid graph structure from Model 3 output.")
|
| 127 |
|
|
|
|
| 11 |
|
| 12 |
def model1_translate_to_graph(request_text):
|
| 13 |
"""
|
| 14 |
+
Stage 1: Translate the customer request into a structured graph.
|
| 15 |
+
Output valid JSON with two keys: 'nodes' and 'edges'.
|
| 16 |
+
Nodes is a list of unique node names, and edges is a list of pairs [source, target].
|
| 17 |
"""
|
| 18 |
prompt = (
|
| 19 |
"Translate the following customer request into a structured graph. "
|
| 20 |
+
"Output only valid JSON with exactly two keys: 'nodes' and 'edges'. "
|
| 21 |
+
"'nodes' should be a list of unique node names, and 'edges' should be a list of pairs "
|
| 22 |
+
"of node names representing directed edges. Do not include any extra text.\n"
|
| 23 |
f"Customer Request: \"{request_text}\"\n"
|
| 24 |
"Structured Graph JSON:"
|
| 25 |
)
|
|
|
|
| 28 |
|
| 29 |
def model2_generate_response(graph_description):
|
| 30 |
"""
|
| 31 |
+
Stage 2: Generate a detailed response based on the structured graph description.
|
| 32 |
"""
|
| 33 |
prompt = (
|
| 34 |
"Based on the following structured customer request graph (formatted as JSON), "
|
|
|
|
| 41 |
|
| 42 |
def model3_response_to_graph(response_text):
|
| 43 |
"""
|
| 44 |
+
Stage 3: Convert the detailed response into a structured graph.
|
| 45 |
Output only valid JSON with two keys: 'nodes' and 'edges'.
|
| 46 |
"""
|
| 47 |
prompt = (
|
| 48 |
"Translate the following response into a structured graph. "
|
| 49 |
+
"Output only valid JSON with two keys: 'nodes' (a list of concept names) and 'edges' "
|
| 50 |
+
"(a list of pairs representing relationships between these concepts). Do not include any extra text.\n"
|
|
|
|
| 51 |
f"Response: \"{response_text}\"\n"
|
| 52 |
"Structured Response Graph JSON:"
|
| 53 |
)
|
|
|
|
| 69 |
|
| 70 |
def build_and_visualize_graph(graph_data, title="Graph Visualization"):
|
| 71 |
"""
|
| 72 |
+
Build a NetworkX graph from a dictionary with 'nodes' and 'edges'
|
| 73 |
+
and visualize it using Matplotlib.
|
| 74 |
"""
|
| 75 |
if not graph_data or 'nodes' not in graph_data or 'edges' not in graph_data:
|
| 76 |
st.error("Invalid graph data format.")
|
|
|
|
| 88 |
st.pyplot(plt)
|
| 89 |
|
| 90 |
def main():
|
| 91 |
+
st.title("Integrated Three-Stage Pipeline with Flan-T5 and Graph Visualization")
|
| 92 |
st.write(
|
| 93 |
+
"Enter a customer request to see it processed through three stages:\n\n"
|
| 94 |
+
"1. **Stage 1:** The request is converted into a structured graph (input graph).\n"
|
| 95 |
+
"2. **Stage 2:** The graph is used to generate a detailed response.\n"
|
| 96 |
+
"3. **Stage 3:** The response is converted into another structured graph (output graph) and visualized."
|
| 97 |
)
|
| 98 |
|
| 99 |
customer_request = st.text_area("Enter Customer Request:", height=150)
|
|
|
|
| 103 |
st.error("Please enter a valid customer request.")
|
| 104 |
return
|
| 105 |
|
| 106 |
+
# --- Stage 1: Request to Graph ---
|
| 107 |
+
st.subheader("Stage 1: Customer Request → Input Graph")
|
| 108 |
model1_output = model1_translate_to_graph(customer_request)
|
| 109 |
st.code(model1_output, language="json")
|
| 110 |
+
input_graph_data = try_parse_json(model1_output)
|
| 111 |
+
if input_graph_data:
|
| 112 |
+
build_and_visualize_graph(input_graph_data, title="Input Graph (Customer Request)")
|
| 113 |
else:
|
| 114 |
st.warning("Could not parse a valid graph structure from Model 1 output.")
|
| 115 |
|
| 116 |
+
# --- Stage 2: Graph → Detailed Response ---
|
| 117 |
+
st.subheader("Stage 2: Input Graph → Detailed Response")
|
| 118 |
response_text = model2_generate_response(model1_output)
|
| 119 |
st.write(response_text)
|
| 120 |
|
| 121 |
+
# --- Stage 3: Detailed Response → Output Graph ---
|
| 122 |
+
st.subheader("Stage 3: Detailed Response → Output Graph")
|
| 123 |
model3_output = model3_response_to_graph(response_text)
|
| 124 |
st.code(model3_output, language="json")
|
| 125 |
+
output_graph_data = try_parse_json(model3_output)
|
| 126 |
+
if output_graph_data:
|
| 127 |
+
build_and_visualize_graph(output_graph_data, title="Output Graph (Structured Response)")
|
| 128 |
else:
|
| 129 |
st.warning("Could not parse a valid graph structure from Model 3 output.")
|
| 130 |
|