ucKaizen commited on
Commit
55972ed
·
verified ·
1 Parent(s): 225b614

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -7
app.py CHANGED
@@ -8,6 +8,7 @@ from autogen.coding import LocalCommandLineCodeExecutor
8
  WORK_DIR = "coding"
9
  CSV_PATH = os.path.join(WORK_DIR, "input.csv")
10
  PLOT_PATH = os.path.join(WORK_DIR, "plot.png")
 
11
  os.makedirs(WORK_DIR, exist_ok=True)
12
 
13
  # Agents
@@ -20,6 +21,7 @@ code_writer_agent = AssistantAgent(
20
  system_message=(
21
  "You are a coding assistant. Write Python code that reads 'input.csv' in the current directory, "
22
  "performs analysis based on the user prompt, and saves the resulting plot to 'plot.png'. "
 
23
  "Only generate the code, no explanations or comments."
24
  ),
25
  )
@@ -40,20 +42,37 @@ def process_csv_and_prompt(prompt, file):
40
  if file is not None:
41
  shutil.copyfile(file.name, CSV_PATH)
42
 
43
- # Clean up old plot
44
  if os.path.exists(PLOT_PATH):
45
  os.remove(PLOT_PATH)
 
 
 
 
 
46
 
47
  # Run agents
48
  code_executor_agent.reset()
49
  code_writer_agent.reset()
50
  full_prompt = f"{prompt}\n(Remember: the file is 'input.csv' in current directory)"
51
- code_executor_agent.initiate_chat(code_writer_agent, message=full_prompt)
52
 
53
- if os.path.exists(PLOT_PATH):
54
- return PLOT_PATH
 
 
 
55
  else:
56
- return None
 
 
 
 
 
 
 
 
 
57
 
58
  # Gradio Interface
59
  iface = gr.Interface(
@@ -62,9 +81,13 @@ iface = gr.Interface(
62
  gr.Textbox(label="Enter Analysis Prompt", placeholder="e.g. Generate 1000 Random numbers and plot histogram"),
63
  gr.File(label="Upload CSV", file_types=[".csv"]),
64
  ],
65
- outputs=gr.Image(label="Generated Plot"),
 
 
 
 
66
  title="Multi agent coding example",
67
- description="I am a multi agent system, I write code and run it for you, optionally upload CSV file data for analysis!",
68
  allow_flagging="never",
69
  )
70
 
 
8
  WORK_DIR = "coding"
9
  CSV_PATH = os.path.join(WORK_DIR, "input.csv")
10
  PLOT_PATH = os.path.join(WORK_DIR, "plot.png")
11
+ CODE_PATH = os.path.join(WORK_DIR, "generated_code.py")
12
  os.makedirs(WORK_DIR, exist_ok=True)
13
 
14
  # Agents
 
21
  system_message=(
22
  "You are a coding assistant. Write Python code that reads 'input.csv' in the current directory, "
23
  "performs analysis based on the user prompt, and saves the resulting plot to 'plot.png'. "
24
+ "If plot is not possible, output text or tabular results to 'results.txt'. "
25
  "Only generate the code, no explanations or comments."
26
  ),
27
  )
 
42
  if file is not None:
43
  shutil.copyfile(file.name, CSV_PATH)
44
 
45
+ # Clean up old outputs
46
  if os.path.exists(PLOT_PATH):
47
  os.remove(PLOT_PATH)
48
+ if os.path.exists(CODE_PATH):
49
+ os.remove(CODE_PATH)
50
+ results_path = os.path.join(WORK_DIR, "results.txt")
51
+ if os.path.exists(results_path):
52
+ os.remove(results_path)
53
 
54
  # Run agents
55
  code_executor_agent.reset()
56
  code_writer_agent.reset()
57
  full_prompt = f"{prompt}\n(Remember: the file is 'input.csv' in current directory)"
58
+ chat = code_executor_agent.initiate_chat(code_writer_agent, message=full_prompt)
59
 
60
+ # Save generated code
61
+ if chat and "content" in chat.messages[-1]:
62
+ generated_code = chat.messages[-1]["content"]
63
+ with open(CODE_PATH, "w") as f:
64
+ f.write(generated_code)
65
  else:
66
+ generated_code = "No code captured."
67
+
68
+ # Decide outputs
69
+ plot = PLOT_PATH if os.path.exists(PLOT_PATH) else None
70
+ table_text = None
71
+ if os.path.exists(results_path):
72
+ with open(results_path, "r") as f:
73
+ table_text = f.read()
74
+
75
+ return plot, generated_code, table_text or "No tabular/text results produced."
76
 
77
  # Gradio Interface
78
  iface = gr.Interface(
 
81
  gr.Textbox(label="Enter Analysis Prompt", placeholder="e.g. Generate 1000 Random numbers and plot histogram"),
82
  gr.File(label="Upload CSV", file_types=[".csv"]),
83
  ],
84
+ outputs=[
85
+ gr.Image(label="Generated Plot"),
86
+ gr.Code(label="Generated Code"),
87
+ gr.Textbox(label="Tabular/Text Results")
88
+ ],
89
  title="Multi agent coding example",
90
+ description="Upload CSV + analysis prompt. System generates Python code, executes it, and shows results as plot, code, and/or text.",
91
  allow_flagging="never",
92
  )
93