setting up a 2-model strategy
Browse files
app.py
CHANGED
|
@@ -4,54 +4,50 @@ import networkx as nx
|
|
| 4 |
import matplotlib.pyplot as plt
|
| 5 |
from transformers import pipeline
|
| 6 |
|
| 7 |
-
# Initialize
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
model3_generator = pipeline('text2text-generation', model='google/flan-t5-large')
|
| 11 |
|
| 12 |
-
def
|
| 13 |
"""
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
| 17 |
"""
|
| 18 |
prompt = (
|
| 19 |
-
"
|
| 20 |
-
"
|
| 21 |
-
"'nodes' should be a list of unique node names
|
| 22 |
-
"
|
| 23 |
-
|
|
|
|
|
|
|
| 24 |
"Structured Graph JSON:"
|
| 25 |
)
|
| 26 |
-
output =
|
| 27 |
return output
|
| 28 |
|
| 29 |
-
def
|
| 30 |
"""
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
"""
|
| 33 |
prompt = (
|
| 34 |
-
"
|
| 35 |
-
"provide a
|
| 36 |
-
|
| 37 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
)
|
| 39 |
-
output =
|
| 40 |
-
return output
|
| 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 |
-
)
|
| 54 |
-
output = model3_generator(prompt, max_new_tokens=150, do_sample=True, temperature=0.7)[0]['generated_text']
|
| 55 |
return output
|
| 56 |
|
| 57 |
def try_parse_json(text):
|
|
@@ -88,12 +84,10 @@ def build_and_visualize_graph(graph_data, title="Graph Visualization"):
|
|
| 88 |
st.pyplot(plt)
|
| 89 |
|
| 90 |
def main():
|
| 91 |
-
st.title("
|
| 92 |
st.write(
|
| 93 |
-
"Enter a customer request to see it
|
| 94 |
-
"
|
| 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)
|
|
@@ -105,7 +99,7 @@ def main():
|
|
| 105 |
|
| 106 |
# --- Stage 1: Request to Graph ---
|
| 107 |
st.subheader("Stage 1: Customer Request → Input Graph")
|
| 108 |
-
model1_output =
|
| 109 |
st.code(model1_output, language="json")
|
| 110 |
input_graph_data = try_parse_json(model1_output)
|
| 111 |
if input_graph_data:
|
|
@@ -113,20 +107,16 @@ def main():
|
|
| 113 |
else:
|
| 114 |
st.warning("Could not parse a valid graph structure from Model 1 output.")
|
| 115 |
|
| 116 |
-
# --- Stage 2:
|
| 117 |
-
st.subheader("Stage 2:
|
| 118 |
-
|
| 119 |
-
st.write(
|
| 120 |
-
|
| 121 |
-
|
| 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
|
| 130 |
|
| 131 |
if __name__ == "__main__":
|
| 132 |
main()
|
|
|
|
| 4 |
import matplotlib.pyplot as plt
|
| 5 |
from transformers import pipeline
|
| 6 |
|
| 7 |
+
# Initialize two text2text-generation pipelines using Flan-T5-large.
|
| 8 |
+
model_request_to_graph = pipeline('text2text-generation', model='google/flan-t5-large')
|
| 9 |
+
model_reply_with_graph = pipeline('text2text-generation', model='google/flan-t5-large')
|
|
|
|
| 10 |
|
| 11 |
+
def generate_input_graph(request_text):
|
| 12 |
"""
|
| 13 |
+
Model 1: Convert the customer request into a structured graph.
|
| 14 |
+
The prompt instructs the model to output only valid JSON with exactly two keys:
|
| 15 |
+
- 'nodes': a list of unique node names (strings)
|
| 16 |
+
- 'edges': a list of pairs [source, target] representing directed edges.
|
| 17 |
+
The output JSON is intended to be directly used for constructing a NetworkX graph.
|
| 18 |
"""
|
| 19 |
prompt = (
|
| 20 |
+
"You are an assistant that converts a natural language customer request into a "
|
| 21 |
+
"structured graph. Please output only valid JSON with exactly two keys: 'nodes' and 'edges'. "
|
| 22 |
+
"'nodes' should be a list of unique strings representing node names. "
|
| 23 |
+
"'edges' should be a list of pairs [source, target] representing directed edges between nodes. "
|
| 24 |
+
"This structure is intended for use with the Python NetworkX library. "
|
| 25 |
+
"Do not include any extra text or commentary.\n\n"
|
| 26 |
+
f"Customer Request: \"{request_text}\"\n\n"
|
| 27 |
"Structured Graph JSON:"
|
| 28 |
)
|
| 29 |
+
output = model_request_to_graph(prompt, max_new_tokens=150, do_sample=True, temperature=0.7)[0]['generated_text']
|
| 30 |
return output
|
| 31 |
|
| 32 |
+
def generate_reply_and_graph(request_text):
|
| 33 |
"""
|
| 34 |
+
Model 2: Generate a detailed response in a structured way.
|
| 35 |
+
The prompt instructs the model to produce a reply that not only answers the customer request,
|
| 36 |
+
but also provides a corresponding structured graph (as valid JSON with keys 'nodes' and 'edges')
|
| 37 |
+
that reflects the underlying relationships in the response. This graph is designed to be used
|
| 38 |
+
with NetworkX for transparent input and output.
|
| 39 |
"""
|
| 40 |
prompt = (
|
| 41 |
+
"You are an assistant that responds to a customer request in a detailed and structured manner. "
|
| 42 |
+
"First, provide a helpful textual reply to the request. Then, convert your reply into a structured graph "
|
| 43 |
+
"that shows the main concepts and relationships. Output only valid JSON after your reply, with exactly two keys: "
|
| 44 |
+
"'nodes' (a list of unique strings representing node names) and 'edges' (a list of pairs [source, target] representing "
|
| 45 |
+
"directed edges between nodes). This graph will be used with the Python NetworkX library. "
|
| 46 |
+
"Do not include any extra text or commentary outside of the JSON.\n\n"
|
| 47 |
+
f"Customer Request: \"{request_text}\"\n\n"
|
| 48 |
+
"Detailed Response and Structured Graph JSON:"
|
| 49 |
)
|
| 50 |
+
output = model_reply_with_graph(prompt, max_new_tokens=200, do_sample=True, temperature=0.7)[0]['generated_text']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
return output
|
| 52 |
|
| 53 |
def try_parse_json(text):
|
|
|
|
| 84 |
st.pyplot(plt)
|
| 85 |
|
| 86 |
def main():
|
| 87 |
+
st.title("Two-LLM Pipeline: Request & Reply with Graph Visualization")
|
| 88 |
st.write(
|
| 89 |
+
"Enter a customer request to see it transformed into a structured graph (input graph) "
|
| 90 |
+
"and then receive a detailed reply with its own structured graph (output graph)."
|
|
|
|
|
|
|
| 91 |
)
|
| 92 |
|
| 93 |
customer_request = st.text_area("Enter Customer Request:", height=150)
|
|
|
|
| 99 |
|
| 100 |
# --- Stage 1: Request to Graph ---
|
| 101 |
st.subheader("Stage 1: Customer Request → Input Graph")
|
| 102 |
+
model1_output = generate_input_graph(customer_request)
|
| 103 |
st.code(model1_output, language="json")
|
| 104 |
input_graph_data = try_parse_json(model1_output)
|
| 105 |
if input_graph_data:
|
|
|
|
| 107 |
else:
|
| 108 |
st.warning("Could not parse a valid graph structure from Model 1 output.")
|
| 109 |
|
| 110 |
+
# --- Stage 2: Detailed Reply with Graph ---
|
| 111 |
+
st.subheader("Stage 2: Detailed Response with Structured Graph")
|
| 112 |
+
model2_output = generate_reply_and_graph(customer_request)
|
| 113 |
+
st.write("Detailed Response and Graph (raw output):")
|
| 114 |
+
st.code(model2_output, language="json")
|
| 115 |
+
output_graph_data = try_parse_json(model2_output)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
if output_graph_data:
|
| 117 |
build_and_visualize_graph(output_graph_data, title="Output Graph (Structured Response)")
|
| 118 |
else:
|
| 119 |
+
st.warning("Could not parse a valid graph structure from Model 2 output.")
|
| 120 |
|
| 121 |
if __name__ == "__main__":
|
| 122 |
main()
|