File size: 13,658 Bytes
2c1976e
130d8b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2c1976e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import gradio as gr
import datetime
import os

# Ensure logs directory exists
os.makedirs("logs", exist_ok=True)

def log_user_data(inputs, outputs):
    # Create a log entry
    timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    log_entry = f"{timestamp} | Input: {inputs} | Output: {outputs}\n"
    
    # Write to a log file
    with open("logs/user_data.log", "a") as log_file:
        log_file.write(log_entry)
    
    return outputs

def process_input(user_input):
    response = f"Hello, {user_input}!"
    log_user_data(user_input, response)
    return response

demo = gr.Interface(fn=process_input, inputs="text", outputs="text")
demo.launch()

# import os
# import sys
# import json
# import argparse
# import time
# import io
# import uuid
# from PIL import Image
# from typing import List, Dict, Any, Iterator
# import gradio as gr

# # Add the project root to the Python path
# current_dir = os.path.dirname(os.path.abspath(__file__))
# project_root = os.path.dirname(os.path.dirname(os.path.dirname(current_dir)))
# sys.path.insert(0, project_root)

# from opentools.models.initializer import Initializer
# from opentools.models.planner import Planner
# from opentools.models.memory import Memory
# from opentools.models.executor import Executor
# from opentools.models.utlis import make_json_serializable

# solver = None

# class ChatMessage:
#     def __init__(self, role: str, content: str, metadata: dict = None):
#         self.role = role
#         self.content = content
#         self.metadata = metadata or {}

# class Solver:
#     def __init__(
#         self,
#         planner,
#         memory,
#         executor,
#         task: str,
#         task_description: str,
#         output_types: str = "base,final,direct",
#         index: int = 0,
#         verbose: bool = True,
#         max_steps: int = 10,
#         max_time: int = 60,
#         output_json_dir: str = "results",
#         root_cache_dir: str = "cache"
#     ):
#         self.planner = planner
#         self.memory = memory
#         self.executor = executor
#         self.task = task
#         self.task_description = task_description
#         self.index = index
#         self.verbose = verbose
#         self.max_steps = max_steps
#         self.max_time = max_time
#         self.output_json_dir = output_json_dir
#         self.root_cache_dir = root_cache_dir

#         self.output_types = output_types.lower().split(',')
#         assert all(output_type in ["base", "final", "direct"] for output_type in self.output_types), "Invalid output type. Supported types are 'base', 'final', 'direct'."

#         # self.benchmark_data = self.load_benchmark_data()



#     def stream_solve_user_problem(self, user_query: str, user_image: Image.Image, messages: List[ChatMessage]) -> Iterator[List[ChatMessage]]:
#         """
#         Streams intermediate thoughts and final responses for the problem-solving process based on user input.
        
#         Args:
#             user_query (str): The text query input from the user.
#             user_image (Image.Image): The uploaded image from the user (PIL Image object).
#             messages (list): A list of ChatMessage objects to store the streamed responses.
#         """

#         if user_image:
#             # # Convert PIL Image to bytes (for processing)
#             # img_bytes_io = io.BytesIO()
#             # user_image.save(img_bytes_io, format="PNG")  # Convert image to PNG bytes
#             # img_bytes = img_bytes_io.getvalue()  # Get bytes
            
#             # Use image paths instead of bytes,
#             os.makedirs(os.path.join(self.root_cache_dir, 'images'), exist_ok=True)
#             img_path = os.path.join(self.root_cache_dir, 'images', str(uuid.uuid4()) + '.jpg')
#             user_image.save(img_path)
#         else:
#             img_path = None

#         # Set query cache
#         _cache_dir = os.path.join(self.root_cache_dir)
#         self.executor.set_query_cache_dir(_cache_dir)
        
#         # Step 1: Display the received inputs
#         if user_image:
#             messages.append(ChatMessage(role="assistant", content=f"πŸ“ Received Query: {user_query}\nπŸ–ΌοΈ Image Uploaded"))
#         else:
#             messages.append(ChatMessage(role="assistant", content=f"πŸ“ Received Query: {user_query}"))
#         yield messages

#         # Step 2: Add "thinking" status while processing
#         messages.append(ChatMessage(
#             role="assistant",
#             content="",
#             metadata={"title": "⏳ Thinking: Processing input..."}
#         ))

#         # Step 3: Initialize problem-solving state
#         start_time = time.time()
#         step_count = 0
#         json_data = {"query": user_query, "image": "Image received as bytes"}

#         # Step 4: Query Analysis
#         import pdb; pdb.set_trace()
#         query_analysis = self.planner.analyze_query(user_query, img_path)
#         json_data["query_analysis"] = query_analysis
#         messages.append(ChatMessage(role="assistant", content=f"πŸ” Query Analysis:\n{query_analysis}"))
#         yield messages

#         # Step 5: Execution loop (similar to your step-by-step solver)
#         while step_count < self.max_steps and (time.time() - start_time) < self.max_time:
#             step_count += 1
#             messages.append(ChatMessage(role="assistant", content=f"πŸ”„ Step {step_count}: Generating next step..."))
#             yield messages

#             # Generate the next step
#             next_step = self.planner.generate_next_step(
#                 user_query, img_path, query_analysis, self.memory, step_count, self.max_steps
#             )
#             context, sub_goal, tool_name = self.planner.extract_context_subgoal_and_tool(next_step)

#             # Display the step information
#             messages.append(ChatMessage(
#                 role="assistant",
#                 content=f"πŸ“Œ Step {step_count} Details:\n- Context: {context}\n- Sub-goal: {sub_goal}\n- Tool: {tool_name}"
#             ))
#             yield messages

#             # Handle tool execution or errors
#             if tool_name not in self.planner.available_tools:
#                 messages.append(ChatMessage(role="assistant", content=f"⚠️ Error: Tool '{tool_name}' is not available."))
#                 yield messages
#                 continue

#             # Execute the tool command
#             tool_command = self.executor.generate_tool_command(
#                 user_query, img_path, context, sub_goal, tool_name, self.planner.toolbox_metadata[tool_name]
#             )
#             explanation, command = self.executor.extract_explanation_and_command(tool_command)
#             result = self.executor.execute_tool_command(tool_name, command)
#             result = make_json_serializable(result)

#             messages.append(ChatMessage(role="assistant", content=f"βœ… Step {step_count} Result:\n{json.dumps(result, indent=4)}"))
#             yield messages

#             # Step 6: Memory update and stopping condition
#             self.memory.add_action(step_count, tool_name, sub_goal, tool_command, result)
#             stop_verification = self.planner.verificate_memory(user_query, img_path, query_analysis, self.memory)
#             conclusion = self.planner.extract_conclusion(stop_verification)

#             messages.append(ChatMessage(role="assistant", content=f"πŸ›‘ Step {step_count} Conclusion: {conclusion}"))
#             yield messages

#             if conclusion == 'STOP':
#                 break

#         # Step 7: Generate Final Output (if needed)
#         if 'final' in self.output_types:
#             final_output = self.planner.generate_final_output(user_query, img_path, self.memory)
#             messages.append(ChatMessage(role="assistant", content=f"🎯 Final Output:\n{final_output}"))
#             yield messages

#         if 'direct' in self.output_types:
#             direct_output = self.planner.generate_direct_output(user_query, img_path, self.memory)
#             messages.append(ChatMessage(role="assistant", content=f"πŸ”Ή Direct Output:\n{direct_output}"))
#             yield messages

#         # Step 8: Completion Message
#         messages.append(ChatMessage(role="assistant", content="βœ… Problem-solving process complete."))
#         yield messages
            
# def parse_arguments():
#     parser = argparse.ArgumentParser(description="Run the OpenTools demo with specified parameters.")
#     parser.add_argument("--llm_engine_name", default="gpt-4o", help="LLM engine name.")
#     parser.add_argument("--max_tokens", type=int, default=2000, help="Maximum tokens for LLM generation.")
#     parser.add_argument("--run_baseline_only", type=bool, default=False, help="Run only the baseline (no toolbox).")
#     parser.add_argument("--task", default="minitoolbench", help="Task to run.")
#     parser.add_argument("--task_description", default="", help="Task description.")
#     parser.add_argument(
#         "--output_types",
#         default="base,final,direct",
#         help="Comma-separated list of required outputs (base,final,direct)"
#     )
#     parser.add_argument("--enabled_tools", default="Generalist_Solution_Generator_Tool", help="List of enabled tools.")
#     parser.add_argument("--root_cache_dir", default="demo_solver_cache", help="Path to solver cache directory.")
#     parser.add_argument("--output_json_dir", default="demo_results", help="Path to output JSON directory.")
#     parser.add_argument("--max_steps", type=int, default=10, help="Maximum number of steps to execute.")
#     parser.add_argument("--max_time", type=int, default=60, help="Maximum time allowed in seconds.")
#     parser.add_argument("--verbose", type=bool, default=True, help="Enable verbose output.")
#     return parser.parse_args()


# def solve_problem_gradio(user_query, user_image):
#     """
#     Wrapper function to connect the solver to Gradio.
#     Streams responses from `solver.stream_solve_user_problem` for real-time UI updates.
#     """
#     global solver  # Ensure we're using the globally defined solver

#     if solver is None:
#         return [["assistant", "⚠️ Error: Solver is not initialized. Please restart the application."]]

#     messages = []  # Initialize message list
#     for message_batch in solver.stream_solve_user_problem(user_query, user_image, messages):
#         yield [[msg.role, msg.content] for msg in message_batch]  # Ensure correct format for Gradio Chatbot



# def main(args):
#     global solver
#     # Initialize Tools
#     enabled_tools = args.enabled_tools.split(",") if args.enabled_tools else []


#     # Instantiate Initializer
#     initializer = Initializer(
#         enabled_tools=enabled_tools,
#         model_string=args.llm_engine_name
#     )

#     # Instantiate Planner
#     planner = Planner(
#         llm_engine_name=args.llm_engine_name,
#         toolbox_metadata=initializer.toolbox_metadata,
#         available_tools=initializer.available_tools
#     )

#     # Instantiate Memory
#     memory = Memory()

#     # Instantiate Executor
#     executor = Executor(
#         llm_engine_name=args.llm_engine_name,
#         root_cache_dir=args.root_cache_dir,
#         enable_signal=False
#     )

#     # Instantiate Solver
#     solver = Solver(
#         planner=planner,
#         memory=memory,
#         executor=executor,
#         task=args.task,
#         task_description=args.task_description,
#         output_types=args.output_types,  # Add new parameter
#         verbose=args.verbose,
#         max_steps=args.max_steps,
#         max_time=args.max_time,
#         output_json_dir=args.output_json_dir,
#         root_cache_dir=args.root_cache_dir
#     )

#     # Test Inputs
#     # user_query = "How many balls are there in the image?"
#     # user_image_path = "/home/sheng/toolbox-agent/mathvista_113.png"  # Replace with your actual image path

#     # # Load the image as a PIL object
#     # user_image = Image.open(user_image_path).convert("RGB")  # Ensure it's in RGB mode

#     # print("\n=== Starting Problem Solving ===\n")
#     # messages = []
#     # for message_batch in solver.stream_solve_user_problem(user_query, user_image, messages):
#     #     for message in message_batch:
#     #         print(f"{message.role}: {message.content}") 

#     # messages = []
#     # solver.stream_solve_user_problem(user_query, user_image, messages)


#     # def solve_problem_stream(user_query, user_image):
#     #     messages = []  # Ensure it's a list of [role, content] pairs

#     #     for message_batch in solver.stream_solve_user_problem(user_query, user_image, messages):
#     #         yield message_batch  # Stream messages correctly in tuple format

#     # solve_problem_stream(user_query, user_image)

#     # ========== Gradio Interface ==========
#     with gr.Blocks() as demo:
#         gr.Markdown("# 🧠 OctoTools AI Solver")  # Title

#         with gr.Row():
#             user_query = gr.Textbox(label="Enter your query", placeholder="Type your question here...")
#             user_image = gr.Image(type="pil", label="Upload an image")  # Accepts multiple formats

#         run_button = gr.Button("Run")  # Run button
#         chatbot_output = gr.Chatbot(label="Problem-Solving Output")

#         # Link button click to function
#         run_button.click(fn=solve_problem_gradio, inputs=[user_query, user_image], outputs=chatbot_output)

#     # Launch the Gradio app
#     demo.launch()



# if __name__ == "__main__":
#     args = parse_arguments()
#     main(args)