Tialo commited on
Commit
4e580b0
·
verified ·
1 Parent(s): d9d9338

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -13
app.py CHANGED
@@ -1,10 +1,8 @@
1
  import json
2
  import gradio as gr
3
  import plotly.graph_objects as go
4
- import plotly.express as px
5
  import networkx as nx
6
- from typing import List, Dict, Any
7
-
8
 
9
  from langchain_openai.chat_models import ChatOpenAI
10
  from dialog2graph.pipelines.model_storage import ModelStorage
@@ -21,8 +19,15 @@ def initialize_pipeline():
21
  )
22
  return D2GLLMPipeline("d2g_pipeline", model_storage=ms, filling_llm="my_filling_model")
23
 
24
- def load_dialog_data(json_file: str) -> List[Dict[str, str]]:
25
- """Load dialog data from JSON file"""
 
 
 
 
 
 
 
26
  file_path = f"{json_file}.json"
27
  try:
28
  with open(file_path, 'r') as f:
@@ -178,11 +183,11 @@ def create_chat_visualization(dialog_data: List[Dict[str, str]]) -> str:
178
  chat_html += "</div>"
179
  return chat_html
180
 
181
- def process_dialog_and_visualize(dialog_choice: str) -> tuple:
182
  """Process the selected dialog and create visualization"""
183
  try:
184
  # Load the selected dialog data
185
- dialog_data = load_dialog_data(dialog_choice)
186
 
187
  if not dialog_data:
188
  return None, "Failed to load dialog data", ""
@@ -228,10 +233,18 @@ def create_gradio_app():
228
  with gr.Row():
229
  with gr.Column(scale=1):
230
  dialog_selector = gr.Radio(
231
- choices=["dialog1", "dialog2", "dialog3"],
232
  label="Select Dialog Dataset",
233
  value="dialog1",
234
- info="Choose one of the available dialog datasets"
 
 
 
 
 
 
 
 
235
  )
236
 
237
  process_btn = gr.Button(
@@ -245,6 +258,7 @@ def create_gradio_app():
245
  - **dialog1**: Hotel booking conversation
246
  - **dialog2**: Food delivery conversation
247
  - **dialog3**: Technical support conversation
 
248
  """)
249
 
250
  with gr.Column(scale=3):
@@ -259,16 +273,31 @@ def create_gradio_app():
259
  chat_output = gr.HTML(label="Chat Visualization")
260
 
261
  # Event handlers
 
 
 
 
 
 
 
 
 
262
  process_btn.click(
263
  fn=process_dialog_and_visualize,
264
- inputs=[dialog_selector],
265
  outputs=[plot_output, summary_output, chat_output]
266
  )
267
 
268
- # Auto-process on selection change
 
 
 
 
 
 
269
  dialog_selector.change(
270
- fn=process_dialog_and_visualize,
271
- inputs=[dialog_selector],
272
  outputs=[plot_output, summary_output, chat_output]
273
  )
274
 
 
1
  import json
2
  import gradio as gr
3
  import plotly.graph_objects as go
 
4
  import networkx as nx
5
+ from typing import List, Dict, Optional
 
6
 
7
  from langchain_openai.chat_models import ChatOpenAI
8
  from dialog2graph.pipelines.model_storage import ModelStorage
 
19
  )
20
  return D2GLLMPipeline("d2g_pipeline", model_storage=ms, filling_llm="my_filling_model")
21
 
22
+ def load_dialog_data(json_file: str, custom_dialog_json: Optional[str] = None) -> List[Dict[str, str]]:
23
+ """Load dialog data from JSON file or custom JSON string"""
24
+ if json_file == "custom" and custom_dialog_json:
25
+ try:
26
+ return json.loads(custom_dialog_json)
27
+ except json.JSONDecodeError as e:
28
+ gr.Error(f"Invalid JSON format in custom dialog: {str(e)}")
29
+ return []
30
+
31
  file_path = f"{json_file}.json"
32
  try:
33
  with open(file_path, 'r') as f:
 
183
  chat_html += "</div>"
184
  return chat_html
185
 
186
+ def process_dialog_and_visualize(dialog_choice: str, custom_dialog: str = "") -> tuple:
187
  """Process the selected dialog and create visualization"""
188
  try:
189
  # Load the selected dialog data
190
+ dialog_data = load_dialog_data(dialog_choice, custom_dialog if dialog_choice == "custom" else None)
191
 
192
  if not dialog_data:
193
  return None, "Failed to load dialog data", ""
 
233
  with gr.Row():
234
  with gr.Column(scale=1):
235
  dialog_selector = gr.Radio(
236
+ choices=["dialog1", "dialog2", "dialog3", "custom"],
237
  label="Select Dialog Dataset",
238
  value="dialog1",
239
+ info="Choose one of the available dialog datasets or use custom JSON"
240
+ )
241
+
242
+ custom_dialog_input = gr.Textbox(
243
+ label="Custom Dialog JSON",
244
+ placeholder='[{"text": "Hello! How can I help?", "participant": "assistant"}, {"text": "I need assistance", "participant": "user"}]',
245
+ lines=8,
246
+ visible=False,
247
+ info="Enter dialog data as JSON array with 'text' and 'participant' fields"
248
  )
249
 
250
  process_btn = gr.Button(
 
258
  - **dialog1**: Hotel booking conversation
259
  - **dialog2**: Food delivery conversation
260
  - **dialog3**: Technical support conversation
261
+ - **custom**: Provide your own dialog as JSON
262
  """)
263
 
264
  with gr.Column(scale=3):
 
273
  chat_output = gr.HTML(label="Chat Visualization")
274
 
275
  # Event handlers
276
+ def toggle_custom_input(choice):
277
+ return gr.update(visible=(choice == "custom"))
278
+
279
+ dialog_selector.change(
280
+ fn=toggle_custom_input,
281
+ inputs=[dialog_selector],
282
+ outputs=[custom_dialog_input]
283
+ )
284
+
285
  process_btn.click(
286
  fn=process_dialog_and_visualize,
287
+ inputs=[dialog_selector, custom_dialog_input],
288
  outputs=[plot_output, summary_output, chat_output]
289
  )
290
 
291
+ # Auto-process on selection change (but not for custom to avoid premature processing)
292
+ def auto_process(choice, custom_text):
293
+ if choice != "custom":
294
+ return process_dialog_and_visualize(choice, custom_text)
295
+ else:
296
+ return None, "Select 'Process Dialog & Generate Graph' to process custom dialog", ""
297
+
298
  dialog_selector.change(
299
+ fn=auto_process,
300
+ inputs=[dialog_selector, custom_dialog_input],
301
  outputs=[plot_output, summary_output, chat_output]
302
  )
303