Vivek-tiwari commited on
Commit
4c44ecf
·
verified ·
1 Parent(s): eee2dfb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -17
app.py CHANGED
@@ -4,7 +4,6 @@ import gradio as gr
4
  from transformers import ReactCodeAgent, HfEngine
5
  import pandas as pd
6
 
7
- from gradio import Chatbot
8
  from transformers.agents import stream_to_gradio
9
  from huggingface_hub import login
10
 
@@ -45,7 +44,7 @@ def get_images_in_directory(directory):
45
 
46
  def interact_with_agent(file_input, prompt):
47
  if file_input is None:
48
- yield [gr.ChatMessage(role="assistant", content="Please upload a CSV file before running the analysis.")]
49
  return
50
 
51
  shutil.rmtree("./figures", ignore_errors=True)
@@ -54,7 +53,7 @@ def interact_with_agent(file_input, prompt):
54
  try:
55
  data_file = pd.read_csv(file_input.name)
56
  except Exception as e:
57
- yield [gr.ChatMessage(role="assistant", content=f"Error reading CSV file: {str(e)}")]
58
  return
59
 
60
  data_structure_notes = f"""- Description (output of .describe()):
@@ -66,25 +65,24 @@ def interact_with_agent(file_input, prompt):
66
  if prompt:
67
  full_prompt += f"\nAdditional notes: {prompt}"
68
 
69
- messages = [gr.ChatMessage(role="user", content=full_prompt)]
70
- yield messages + [
71
- gr.ChatMessage(role="assistant", content="⏳ Starting task...")
72
- ]
73
 
74
  plot_image_paths = {}
75
  for msg in stream_to_gradio(agent, full_prompt, data_file=data_file):
76
- messages.append(msg)
77
- for image_path in get_images_in_directory("./figures"):
 
 
 
78
  if image_path not in plot_image_paths:
79
- image_message = gr.ChatMessage(
80
- role="assistant",
81
- content=gr.Image.update(value=image_path),
82
- )
83
  plot_image_paths[image_path] = True
84
- messages.append(image_message)
85
- yield messages + [
86
- gr.ChatMessage(role="assistant", content="⏳ Still processing...")
87
- ]
 
88
  yield messages
89
 
90
  with gr.Blocks(
 
4
  from transformers import ReactCodeAgent, HfEngine
5
  import pandas as pd
6
 
 
7
  from transformers.agents import stream_to_gradio
8
  from huggingface_hub import login
9
 
 
44
 
45
  def interact_with_agent(file_input, prompt):
46
  if file_input is None:
47
+ yield [["assistant", "Please upload a CSV file before running the analysis."]]
48
  return
49
 
50
  shutil.rmtree("./figures", ignore_errors=True)
 
53
  try:
54
  data_file = pd.read_csv(file_input.name)
55
  except Exception as e:
56
+ yield [["assistant", f"Error reading CSV file: {str(e)}"]]
57
  return
58
 
59
  data_structure_notes = f"""- Description (output of .describe()):
 
65
  if prompt:
66
  full_prompt += f"\nAdditional notes: {prompt}"
67
 
68
+ messages = [["user", full_prompt]]
69
+ yield messages + [["assistant", "⏳ Starting task..."]]
 
 
70
 
71
  plot_image_paths = {}
72
  for msg in stream_to_gradio(agent, full_prompt, data_file=data_file):
73
+ if isinstance(msg.content, str):
74
+ messages.append(["assistant", msg.content])
75
+ elif isinstance(msg.content, dict) and 'path' in msg.content:
76
+ # Handle image messages
77
+ image_path = msg.content['path']
78
  if image_path not in plot_image_paths:
79
+ messages.append(["assistant", (image_path,)])
 
 
 
80
  plot_image_paths[image_path] = True
81
+
82
+ yield messages + [["assistant", "⏳ Still processing..."]]
83
+
84
+ # Remove the last "Still processing..." message
85
+ messages = messages[:-1]
86
  yield messages
87
 
88
  with gr.Blocks(