ucKaizen commited on
Commit
cdf2dcf
·
verified ·
1 Parent(s): f819e8e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -0
app.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import shutil
3
+ import gradio as gr
4
+ from autogen import ConversableAgent, AssistantAgent
5
+ from autogen.coding import LocalCommandLineCodeExecutor
6
+
7
+ # Setup
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
14
+ llm_config = {"model": "gpt-4-turbo"}
15
+
16
+ code_writer_agent = AssistantAgent(
17
+ name="code_writer_agent",
18
+ llm_config=llm_config,
19
+ human_input_mode="NEVER",
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
+ )
26
+
27
+ executor = LocalCommandLineCodeExecutor(timeout=60, work_dir=WORK_DIR)
28
+
29
+ code_executor_agent = ConversableAgent(
30
+ name="code_executor_agent",
31
+ llm_config=False,
32
+ code_execution_config={"executor": executor},
33
+ human_input_mode="NEVER",
34
+ default_auto_reply="TERMINATE",
35
+ )
36
+
37
+ # Main function
38
+ def process_csv_and_prompt(prompt, file):
39
+ # Save uploaded 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(
60
+ fn=process_csv_and_prompt,
61
+ inputs=[
62
+ gr.Textbox(label="Enter Analysis Prompt", placeholder="e.g. Plot average of column B by category"),
63
+ gr.File(label="Upload CSV", file_types=[".csv"]),
64
+ ],
65
+ outputs=gr.Image(label="Generated Plot"),
66
+ title="CSV Plotter & Code Runner",
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
+
71
+ if __name__ == "__main__":
72
+ iface.launch()