luigi12345 commited on
Commit
f87e8b4
1 Parent(s): fc82ea1
Files changed (2) hide show
  1. app.py +91 -2
  2. requirements.txt +1 -1
app.py CHANGED
@@ -1,13 +1,102 @@
1
  import gradio as gr
2
  import datetime, os, threading
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
- from multi_agent import run_multi_agent
5
 
6
  lock = threading.Lock()
7
 
8
  LLM = "gpt-4o-mini"
9
 
10
- def invoke(openai_api_key, task):
11
  if not openai_api_key:
12
  raise gr.Error("OpenAI API Key is required.")
13
 
 
1
  import gradio as gr
2
  import datetime, os, threading
3
+ import base64, json
4
+
5
+ from autogen import ConversableAgent, AssistantAgent
6
+ from autogen.coding import LocalCommandLineCodeExecutor
7
+
8
+ def read_file(file_path: str) -> str:
9
+ with open(file_path, "r", encoding="utf-8") as file:
10
+ return file.read()
11
+
12
+ def read_image_file(image_file_path: str) -> str:
13
+ with open(image_file_path, "rb") as image_file:
14
+ image_data = image_file.read()
15
+ return base64.b64encode(image_data).decode("utf-8")
16
+
17
+ def generate_markdown_image(image_data: str) -> str:
18
+ return f"![Image](data:image/png;base64,{image_data})"
19
+
20
+ def format_as_markdown(code: str) -> str:
21
+ markdown_code = '```\n'
22
+ markdown_code += code
23
+ markdown_code += '\n```'
24
+ return markdown_code
25
+
26
+ def get_latest_file(directory, file_extension):
27
+ latest_file = None
28
+ latest_date = datetime.datetime.min
29
+
30
+ for file in os.listdir(directory):
31
+ if file:
32
+ _, file_ext = os.path.splitext(file)
33
+
34
+ if file_ext == file_extension:
35
+ file_path = os.path.join(directory, file)
36
+ file_date = datetime.datetime.fromtimestamp(os.path.getmtime(file_path))
37
+
38
+ if file_date > latest_date:
39
+ latest_date = file_date
40
+ latest_file = file
41
+
42
+ return latest_file
43
+
44
+ def run_multi_agent(llm, task):
45
+ llm_config = {"model": llm}
46
+
47
+ executor = LocalCommandLineCodeExecutor(
48
+ timeout=60,
49
+ work_dir="coding",
50
+ )
51
+
52
+ code_executor_agent = ConversableAgent(
53
+ name="code_executor_agent",
54
+ llm_config=False,
55
+ code_execution_config={"executor": executor},
56
+ human_input_mode="NEVER",
57
+ default_auto_reply="TERMINATE",
58
+ )
59
+
60
+ code_writer_agent = AssistantAgent(
61
+ name="code_writer_agent",
62
+ llm_config=llm_config,
63
+ code_execution_config=False,
64
+ human_input_mode="NEVER",
65
+ )
66
+
67
+ chat_result = code_executor_agent.initiate_chat(
68
+ code_writer_agent,
69
+ message=task,
70
+ max_turns=10
71
+ )
72
+
73
+ chat = ""
74
+ first_message = True
75
+
76
+ for message in chat_result.chat_history:
77
+ if not first_message:
78
+ chat += f"**{message['role'].replace('assistant', 'Code Executor').replace('user', 'Code Writer')}**\n{message['content']}\n\n"
79
+ first_message = False
80
+
81
+ file_name_png = get_latest_file("coding", ".png")
82
+
83
+ image_data = read_image_file(f"/home/user/app/coding/{file_name_png}")
84
+ markdown_code_png = generate_markdown_image(image_data)
85
+
86
+ result = f"{markdown_code_png}\n\n{chat}"
87
+
88
+ print("===")
89
+ print(result)
90
+ print("===")
91
+
92
+ return result
93
 
 
94
 
95
  lock = threading.Lock()
96
 
97
  LLM = "gpt-4o-mini"
98
 
99
+ def invoke(openai_api_key, openai_api_base, task):
100
  if not openai_api_key:
101
  raise gr.Error("OpenAI API Key is required.")
102
 
requirements.txt CHANGED
@@ -1,3 +1,3 @@
1
  chess==1.10.0
2
  markdown==3.6
3
- pyautogen==0.2.25
 
1
  chess==1.10.0
2
  markdown==3.6
3
+ pyautogen==0.2.25