vindruid commited on
Commit
3f208e9
ยท
unverified ยท
1 Parent(s): 7c8b896
Files changed (1) hide show
  1. app.py +40 -47
app.py CHANGED
@@ -517,6 +517,8 @@ def load_example_dataset(name):
517
  gr.update(visible=False), # Hide titanic button
518
  gr.update(visible=False), # Hide superstore button
519
  gr.update(visible=False), # Hide upload button
 
 
520
  df.describe().reset_index(),
521
  get_info_df(df),
522
  df.head(),
@@ -559,6 +561,8 @@ def handle_upload(file):
559
  gr.update(visible=False), # Hide titanic button
560
  gr.update(visible=False), # Hide superstore button
561
  gr.update(visible=True), # Hide upload button
 
 
562
  df.describe().reset_index(),
563
  get_info_df(df),
564
  df.head(),
@@ -572,6 +576,33 @@ def refresh_graph():
572
  global config
573
  config = {"configurable": {"thread_id": str(uuid.uuid4()), "session": str(uuid.uuid4())}}
574
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
575
  # Layout
576
  with gr.Blocks(theme=my_theme) as demo:
577
  demo.load(refresh_graph, inputs=None, outputs=None)
@@ -632,20 +663,20 @@ with gr.Blocks(theme=my_theme) as demo:
632
  <div class="container">
633
  <h1>
634
  <span style="font-size: 30px;">๐ŸŽฏ</span>
635
- <span class="title-gradient">Terloka Data Insight Tool</span>
636
  </h1>
637
  <p class="subtitle">Your gateway to smarter decisions through travel data.</p>
638
  </div>
639
 
640
  """)
641
 
642
- gr.Markdown(
643
  "> Upload a file to get started. Supported formats: `.csv`, `.xls`, `.xlsx`",
644
  elem_id="instruction"
645
  )
646
  warning_box = gr.Markdown("โš ๏ธ **You can't proceed without uploading your files first**", visible=True)
647
  upload_btn = gr.File(file_types=[".csv", ".xls", ".xlsx"], label="๐Ÿ“ Upload File")
648
- gr.Markdown("### Or use an example dataset:")
649
  with gr.Row():
650
  iris_btn = gr.Button("๐ŸŒธ Load Iris")
651
  titanic_btn = gr.Button("๐Ÿšข Load Titanic")
@@ -658,48 +689,10 @@ with gr.Blocks(theme=my_theme) as demo:
658
  "๐Ÿ‘‰ Want to understand your data first? Go to the Data Exploration tab first!",
659
  elem_id="chatbot_hint"
660
  )
661
-
662
  chatbot = gr.Chatbot(type="messages", label="Data Chatbot", elem_id="chatbot")
 
 
663
 
664
- chat_input = gr.MultimodalTextbox(
665
- interactive=True,
666
- file_count="multiple",
667
- placeholder="Ask about your data or upload files...",
668
- show_label=False,
669
- sources=[],
670
- elem_id="chat_input"
671
- )
672
-
673
- def print_like_dislike(x: gr.LikeData):
674
- print("User liked message:", x.liked, "at index:", x.index)
675
-
676
- def add_message(history, message):
677
- # Add uploaded files to history as user messages
678
- for f in message.get("files", []):
679
- history.append({"role": "user", "content": {"path": f}})
680
- # Add text message if any
681
- if message.get("text"):
682
- history.append({"role": "user", "content": message["text"]})
683
- # Clear input box after submit
684
- return history, gr.MultimodalTextbox(value=None, interactive=True)
685
-
686
- def bot(history: list):
687
- last_user_msg = history[-1]["content"]
688
- if isinstance(last_user_msg, dict): # If user uploaded files, skip LLM
689
- return history
690
-
691
- _, updated_history = respond(last_user_msg, history[:-1])
692
- return updated_history
693
-
694
-
695
- chat_msg = chat_input.submit(
696
- add_message, inputs=[chatbot, chat_input], outputs=[chatbot, chat_input]
697
- )
698
- bot_msg = chat_msg.then(bot, chatbot, chatbot, api_name="bot_response")
699
- bot_msg.then(lambda: gr.MultimodalTextbox(interactive=True), None, [chat_input])
700
-
701
- chatbot.like(print_like_dislike, None, None, like_user_message=True)
702
-
703
  with gr.Tab("๐Ÿ“Š Data Exploration"):
704
  with gr.Column():
705
  with gr.Accordion("๐Ÿงฎ Data Description", open=True):
@@ -723,7 +716,7 @@ with gr.Blocks(theme=my_theme) as demo:
723
  fn=handle_upload,
724
  inputs=upload_btn,
725
  outputs=[
726
- main_tabs, warning_box, iris_btn, titanic_btn, superstore_btn,upload_btn,
727
  describe_output, info_output,
728
  head_output, null_output,
729
  dup_output
@@ -732,7 +725,7 @@ with gr.Blocks(theme=my_theme) as demo:
732
  iris_btn.click(
733
  fn=lambda: load_example_dataset("iris"),
734
  outputs=[
735
- main_tabs, warning_box, iris_btn, titanic_btn, superstore_btn,upload_btn,
736
  describe_output, info_output,
737
  head_output, null_output,
738
  dup_output
@@ -742,7 +735,7 @@ with gr.Blocks(theme=my_theme) as demo:
742
  titanic_btn.click(
743
  fn=lambda: load_example_dataset("titanic"),
744
  outputs=[
745
- main_tabs, warning_box, iris_btn, titanic_btn, superstore_btn,upload_btn,
746
  describe_output, info_output,
747
  head_output, null_output,
748
  dup_output
@@ -752,7 +745,7 @@ with gr.Blocks(theme=my_theme) as demo:
752
  superstore_btn.click(
753
  fn=lambda: load_example_dataset("superstore"),
754
  outputs=[
755
- main_tabs, warning_box, iris_btn, titanic_btn, superstore_btn,upload_btn,
756
  describe_output, info_output,
757
  head_output, null_output,
758
  dup_output
 
517
  gr.update(visible=False), # Hide titanic button
518
  gr.update(visible=False), # Hide superstore button
519
  gr.update(visible=False), # Hide upload button
520
+ gr.update(visible=False), # Hide instruction button
521
+ gr.update(visible=False), # Hide example button
522
  df.describe().reset_index(),
523
  get_info_df(df),
524
  df.head(),
 
561
  gr.update(visible=False), # Hide titanic button
562
  gr.update(visible=False), # Hide superstore button
563
  gr.update(visible=True), # Hide upload button
564
+ gr.update(visible=False), # Hide instruction button
565
+ gr.update(visible=False), # Hide example button
566
  df.describe().reset_index(),
567
  get_info_df(df),
568
  df.head(),
 
576
  global config
577
  config = {"configurable": {"thread_id": str(uuid.uuid4()), "session": str(uuid.uuid4())}}
578
 
579
+ # --- Gradio UI ---
580
+ def respond(message, chat_history):
581
+ chat_history = []
582
+ res = react_graph.invoke(
583
+ {"messages": [HumanMessage(content=message)]}
584
+ , config=config)
585
+ for msg in res["messages"]:
586
+ msg.pretty_print()
587
+ if isinstance(msg, HumanMessage):
588
+ chat_history.append({"role": "user", "content": msg.content})
589
+
590
+ if isinstance(msg, AIMessage):
591
+ ai_response = msg.content
592
+ chat_history.append({"role": "assistant", "content": ai_response})
593
+
594
+ if isinstance(msg, ToolMessage):
595
+ if msg.name == "generate_plot_code":
596
+ plot_result = generate_plot_from_code(msg.content)
597
+ chat_history.append({"role": "assistant", "content": gr.Plot(plot_result)})
598
+
599
+ if msg.name == "enhance_plot_code":
600
+ plot_result = generate_plot_from_code(msg.content)
601
+ chat_history.append({"role": "assistant", "content": gr.Plot(plot_result)})
602
+
603
+ time.sleep(1)
604
+ return "", chat_history
605
+
606
  # Layout
607
  with gr.Blocks(theme=my_theme) as demo:
608
  demo.load(refresh_graph, inputs=None, outputs=None)
 
663
  <div class="container">
664
  <h1>
665
  <span style="font-size: 30px;">๐ŸŽฏ</span>
666
+ <span class="title-gradient">AI Chat to Visual</span>
667
  </h1>
668
  <p class="subtitle">Your gateway to smarter decisions through travel data.</p>
669
  </div>
670
 
671
  """)
672
 
673
+ instruction_box = gr.Markdown(
674
  "> Upload a file to get started. Supported formats: `.csv`, `.xls`, `.xlsx`",
675
  elem_id="instruction"
676
  )
677
  warning_box = gr.Markdown("โš ๏ธ **You can't proceed without uploading your files first**", visible=True)
678
  upload_btn = gr.File(file_types=[".csv", ".xls", ".xlsx"], label="๐Ÿ“ Upload File")
679
+ example_box = gr.Markdown("### Or use an example dataset:")
680
  with gr.Row():
681
  iris_btn = gr.Button("๐ŸŒธ Load Iris")
682
  titanic_btn = gr.Button("๐Ÿšข Load Titanic")
 
689
  "๐Ÿ‘‰ Want to understand your data first? Go to the Data Exploration tab first!",
690
  elem_id="chatbot_hint"
691
  )
 
692
  chatbot = gr.Chatbot(type="messages", label="Data Chatbot", elem_id="chatbot")
693
+ msg = gr.Textbox(label="",elem_id="chat_input", container=False, placeholder="Ask me anything about your data...")
694
+ msg.submit(respond, [msg, chatbot], [msg, chatbot])
695
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
696
  with gr.Tab("๐Ÿ“Š Data Exploration"):
697
  with gr.Column():
698
  with gr.Accordion("๐Ÿงฎ Data Description", open=True):
 
716
  fn=handle_upload,
717
  inputs=upload_btn,
718
  outputs=[
719
+ main_tabs, warning_box, iris_btn, titanic_btn, superstore_btn,upload_btn,instruction_box,example_box,
720
  describe_output, info_output,
721
  head_output, null_output,
722
  dup_output
 
725
  iris_btn.click(
726
  fn=lambda: load_example_dataset("iris"),
727
  outputs=[
728
+ main_tabs, warning_box, iris_btn, titanic_btn, superstore_btn,upload_btn,instruction_box,example_box,
729
  describe_output, info_output,
730
  head_output, null_output,
731
  dup_output
 
735
  titanic_btn.click(
736
  fn=lambda: load_example_dataset("titanic"),
737
  outputs=[
738
+ main_tabs, warning_box, iris_btn, titanic_btn, superstore_btn,upload_btn,instruction_box,example_box,
739
  describe_output, info_output,
740
  head_output, null_output,
741
  dup_output
 
745
  superstore_btn.click(
746
  fn=lambda: load_example_dataset("superstore"),
747
  outputs=[
748
+ main_tabs, warning_box, iris_btn, titanic_btn, superstore_btn,upload_btn,instruction_box,example_box,
749
  describe_output, info_output,
750
  head_output, null_output,
751
  dup_output