lsobrie commited on
Commit
f7ea9ee
·
1 Parent(s): 63eadc6

app development tryout

Browse files
Files changed (1) hide show
  1. app.py +29 -26
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
- Model 1: Translate the customer request into a structured graph description.
15
- The output should be a JSON-like string with two keys: 'nodes' and 'edges'.
16
- Nodes is a list of node names and edges is a list of pairs representing directed edges.
17
  """
18
  prompt = (
19
  "Translate the following customer request into a structured graph. "
20
- "Output only valid JSON with exactly two keys: 'nodes' (a list of node names) "
21
- "and 'edges' (a list of pairs of node names for directed edges). "
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
- Model 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,14 +41,13 @@ def model2_generate_response(graph_description):
41
 
42
  def model3_response_to_graph(response_text):
43
  """
44
- Model 3: Convert the detailed response into a structured graph description.
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
- "and 'edges' (a list of pairs of node names representing relationships). "
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' keys,
74
- and then visualize it using Matplotlib.
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-Model Pipeline with Flan-T5 and Graph Visualization")
93
  st.write(
94
- "Enter a customer request and see it transformed into a structured graph (using Model 1), "
95
- "then used to generate a detailed response (Model 2), and finally converted into a response graph (Model 3) "
96
- "that is visualized using NetworkX and Matplotlib."
 
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
- st.subheader("Step 1: Customer Request Input Graph")
 
107
  model1_output = model1_translate_to_graph(customer_request)
108
  st.code(model1_output, language="json")
109
- graph_input = try_parse_json(model1_output)
110
- if graph_input:
111
- build_and_visualize_graph(graph_input, title="Input Graph (Customer Request)")
112
  else:
113
  st.warning("Could not parse a valid graph structure from Model 1 output.")
114
 
115
- st.subheader("Step 2: Input Graph → Detailed Response")
 
116
  response_text = model2_generate_response(model1_output)
117
  st.write(response_text)
118
 
119
- st.subheader("Step 3: Detailed Response → Output Graph")
 
120
  model3_output = model3_response_to_graph(response_text)
121
  st.code(model3_output, language="json")
122
- graph_output = try_parse_json(model3_output)
123
- if graph_output:
124
- build_and_visualize_graph(graph_output, title="Output Graph (Structured Response)")
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