Spaces:
Runtime error
Runtime error
added examples
Browse files- app.py +39 -19
- app_v3.py +380 -0
- examples/baseball.png +0 -0
app.py
CHANGED
|
@@ -282,6 +282,7 @@ def main(args):
|
|
| 282 |
""")
|
| 283 |
|
| 284 |
with gr.Row():
|
|
|
|
| 285 |
with gr.Column(scale=1):
|
| 286 |
with gr.Row():
|
| 287 |
api_key = gr.Textbox(
|
|
@@ -309,29 +310,48 @@ def main(args):
|
|
| 309 |
label="Enabled Tools",
|
| 310 |
)
|
| 311 |
|
| 312 |
-
with gr.Column(scale=
|
| 313 |
-
user_image = gr.Image(type="pil", label="Upload an image (optional)", height=500) # Accepts multiple formats
|
| 314 |
|
| 315 |
with gr.Row():
|
| 316 |
-
user_query = gr.Textbox( placeholder="Type your question here...", label="Query")
|
| 317 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 318 |
with gr.Row():
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
clear_btn = gr.Button(value="ποΈ Clear history", interactive=True)
|
| 330 |
-
|
| 331 |
-
with gr.Row():
|
| 332 |
-
comment_textbox = gr.Textbox(value="",
|
| 333 |
-
placeholder="Feel free to add any comments here. Thanks for using OctoTools!",
|
| 334 |
-
label="π¬ Comment", interactive=True)
|
| 335 |
|
| 336 |
# Link button click to function
|
| 337 |
run_button.click(
|
|
|
|
| 282 |
""")
|
| 283 |
|
| 284 |
with gr.Row():
|
| 285 |
+
# Left column for settings
|
| 286 |
with gr.Column(scale=1):
|
| 287 |
with gr.Row():
|
| 288 |
api_key = gr.Textbox(
|
|
|
|
| 310 |
label="Enabled Tools",
|
| 311 |
)
|
| 312 |
|
| 313 |
+
with gr.Column(scale=5):
|
|
|
|
| 314 |
|
| 315 |
with gr.Row():
|
|
|
|
| 316 |
|
| 317 |
+
# Middle column for the query
|
| 318 |
+
with gr.Column(scale=2):
|
| 319 |
+
user_image = gr.Image(type="pil", label="Upload an image (optional)", height=500) # Accepts multiple formats
|
| 320 |
+
|
| 321 |
+
with gr.Row():
|
| 322 |
+
user_query = gr.Textbox( placeholder="Type your question here...", label="Question")
|
| 323 |
+
|
| 324 |
+
with gr.Row():
|
| 325 |
+
run_button = gr.Button("Run") # Run button
|
| 326 |
+
|
| 327 |
+
# Right column for the output
|
| 328 |
+
with gr.Column(scale=3):
|
| 329 |
+
chatbot_output = gr.Chatbot(type="messages", label="Step-wise problem-solving output (Deep Thinking)", height=500)
|
| 330 |
+
# chatbot_output.like(lambda x: print(f"User liked: {x}"))
|
| 331 |
+
|
| 332 |
+
# TODO: Add actions to the buttons
|
| 333 |
+
with gr.Row(elem_id="buttons") as button_row:
|
| 334 |
+
upvote_btn = gr.Button(value="π Upvote", interactive=True)
|
| 335 |
+
downvote_btn = gr.Button(value="π Downvote", interactive=True)
|
| 336 |
+
clear_btn = gr.Button(value="ποΈ Clear history", interactive=True)
|
| 337 |
+
|
| 338 |
+
with gr.Row():
|
| 339 |
+
comment_textbox = gr.Textbox(value="",
|
| 340 |
+
placeholder="Feel free to add any comments here. Thanks for using OctoTools!",
|
| 341 |
+
label="π¬ Comment", interactive=True)
|
| 342 |
+
|
| 343 |
+
# Bottom row for examples
|
| 344 |
with gr.Row():
|
| 345 |
+
with gr.Column(scale=5):
|
| 346 |
+
gr.Examples(
|
| 347 |
+
examples=[
|
| 348 |
+
[ "examples/baseball.png", "How many baseballs are there?"],
|
| 349 |
+
[ "examples/baseball.png", "How many buckets are there?"],
|
| 350 |
+
[ None, "Who is the president of the United States?"]
|
| 351 |
+
],
|
| 352 |
+
inputs=[user_image, user_query],
|
| 353 |
+
label="Try these examples"
|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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import sys
|
| 3 |
+
import json
|
| 4 |
+
import argparse
|
| 5 |
+
import time
|
| 6 |
+
import io
|
| 7 |
+
import uuid
|
| 8 |
+
from PIL import Image
|
| 9 |
+
from typing import List, Dict, Any, Iterator
|
| 10 |
+
|
| 11 |
+
import gradio as gr
|
| 12 |
+
from gradio import ChatMessage
|
| 13 |
+
|
| 14 |
+
# Add the project root to the Python path
|
| 15 |
+
current_dir = os.path.dirname(os.path.abspath(__file__))
|
| 16 |
+
project_root = os.path.dirname(os.path.dirname(os.path.dirname(current_dir)))
|
| 17 |
+
sys.path.insert(0, project_root)
|
| 18 |
+
|
| 19 |
+
from octotools.models.initializer import Initializer
|
| 20 |
+
from octotools.models.planner import Planner
|
| 21 |
+
from octotools.models.memory import Memory
|
| 22 |
+
from octotools.models.executor import Executor
|
| 23 |
+
from octotools.models.utils import make_json_serializable
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
class Solver:
|
| 27 |
+
def __init__(
|
| 28 |
+
self,
|
| 29 |
+
planner,
|
| 30 |
+
memory,
|
| 31 |
+
executor,
|
| 32 |
+
task: str,
|
| 33 |
+
task_description: str,
|
| 34 |
+
output_types: str = "base,final,direct",
|
| 35 |
+
index: int = 0,
|
| 36 |
+
verbose: bool = True,
|
| 37 |
+
max_steps: int = 10,
|
| 38 |
+
max_time: int = 60,
|
| 39 |
+
output_json_dir: str = "results",
|
| 40 |
+
root_cache_dir: str = "cache"
|
| 41 |
+
):
|
| 42 |
+
self.planner = planner
|
| 43 |
+
self.memory = memory
|
| 44 |
+
self.executor = executor
|
| 45 |
+
self.task = task
|
| 46 |
+
self.task_description = task_description
|
| 47 |
+
self.index = index
|
| 48 |
+
self.verbose = verbose
|
| 49 |
+
self.max_steps = max_steps
|
| 50 |
+
self.max_time = max_time
|
| 51 |
+
self.output_json_dir = output_json_dir
|
| 52 |
+
self.root_cache_dir = root_cache_dir
|
| 53 |
+
|
| 54 |
+
self.output_types = output_types.lower().split(',')
|
| 55 |
+
assert all(output_type in ["base", "final", "direct"] for output_type in self.output_types), "Invalid output type. Supported types are 'base', 'final', 'direct'."
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def stream_solve_user_problem(self, user_query: str, user_image: Image.Image, api_key: str, messages: List[ChatMessage]) -> Iterator[List[ChatMessage]]:
|
| 59 |
+
"""
|
| 60 |
+
Streams intermediate thoughts and final responses for the problem-solving process based on user input.
|
| 61 |
+
|
| 62 |
+
Args:
|
| 63 |
+
user_query (str): The text query input from the user.
|
| 64 |
+
user_image (Image.Image): The uploaded image from the user (PIL Image object).
|
| 65 |
+
messages (list): A list of ChatMessage objects to store the streamed responses.
|
| 66 |
+
"""
|
| 67 |
+
|
| 68 |
+
if user_image:
|
| 69 |
+
# # Convert PIL Image to bytes (for processing)
|
| 70 |
+
# img_bytes_io = io.BytesIO()
|
| 71 |
+
# user_image.save(img_bytes_io, format="PNG") # Convert image to PNG bytes
|
| 72 |
+
# img_bytes = img_bytes_io.getvalue() # Get bytes
|
| 73 |
+
|
| 74 |
+
# Use image paths instead of bytes,
|
| 75 |
+
os.makedirs(os.path.join(self.root_cache_dir, 'images'), exist_ok=True)
|
| 76 |
+
img_path = os.path.join(self.root_cache_dir, 'images', str(uuid.uuid4()) + '.jpg')
|
| 77 |
+
user_image.save(img_path)
|
| 78 |
+
else:
|
| 79 |
+
img_path = None
|
| 80 |
+
|
| 81 |
+
# Set query cache
|
| 82 |
+
_cache_dir = os.path.join(self.root_cache_dir)
|
| 83 |
+
self.executor.set_query_cache_dir(_cache_dir)
|
| 84 |
+
|
| 85 |
+
# Step 1: Display the received inputs
|
| 86 |
+
if user_image:
|
| 87 |
+
messages.append(ChatMessage(role="assistant", content=f"π Received Query: {user_query}\nπΌοΈ Image Uploaded"))
|
| 88 |
+
else:
|
| 89 |
+
messages.append(ChatMessage(role="assistant", content=f"π Received Query: {user_query}"))
|
| 90 |
+
yield messages
|
| 91 |
+
|
| 92 |
+
# # Step 2: Add "thinking" status while processing
|
| 93 |
+
# messages.append(ChatMessage(
|
| 94 |
+
# role="assistant",
|
| 95 |
+
# content="",
|
| 96 |
+
# metadata={"title": "β³ Thinking: Processing input..."}
|
| 97 |
+
# ))
|
| 98 |
+
|
| 99 |
+
# Step 3: Initialize problem-solving state
|
| 100 |
+
start_time = time.time()
|
| 101 |
+
step_count = 0
|
| 102 |
+
json_data = {"query": user_query, "image": "Image received as bytes"}
|
| 103 |
+
|
| 104 |
+
# Step 4: Query Analysis
|
| 105 |
+
query_analysis = self.planner.analyze_query(user_query, img_path)
|
| 106 |
+
json_data["query_analysis"] = query_analysis
|
| 107 |
+
messages.append(ChatMessage(role="assistant",
|
| 108 |
+
content=f"{query_analysis}",
|
| 109 |
+
metadata={"title": "π Query Analysis"}))
|
| 110 |
+
yield messages
|
| 111 |
+
|
| 112 |
+
# Step 5: Execution loop (similar to your step-by-step solver)
|
| 113 |
+
while step_count < self.max_steps and (time.time() - start_time) < self.max_time:
|
| 114 |
+
step_count += 1
|
| 115 |
+
# messages.append(ChatMessage(role="assistant",
|
| 116 |
+
# content=f"Generating next step...",
|
| 117 |
+
# metadata={"title": f"π Step {step_count}"}))
|
| 118 |
+
yield messages
|
| 119 |
+
|
| 120 |
+
# Generate the next step
|
| 121 |
+
next_step = self.planner.generate_next_step(
|
| 122 |
+
user_query, img_path, query_analysis, self.memory, step_count, self.max_steps
|
| 123 |
+
)
|
| 124 |
+
context, sub_goal, tool_name = self.planner.extract_context_subgoal_and_tool(next_step)
|
| 125 |
+
|
| 126 |
+
# Display the step information
|
| 127 |
+
messages.append(ChatMessage(
|
| 128 |
+
role="assistant",
|
| 129 |
+
content=f"- Context: {context}\n- Sub-goal: {sub_goal}\n- Tool: {tool_name}",
|
| 130 |
+
metadata={"title": f"π Step {step_count}: {tool_name}"}
|
| 131 |
+
))
|
| 132 |
+
yield messages
|
| 133 |
+
|
| 134 |
+
# Handle tool execution or errors
|
| 135 |
+
if tool_name not in self.planner.available_tools:
|
| 136 |
+
messages.append(ChatMessage(
|
| 137 |
+
role="assistant",
|
| 138 |
+
content=f"β οΈ Error: Tool '{tool_name}' is not available."))
|
| 139 |
+
yield messages
|
| 140 |
+
continue
|
| 141 |
+
|
| 142 |
+
# Execute the tool command
|
| 143 |
+
tool_command = self.executor.generate_tool_command(
|
| 144 |
+
user_query, img_path, context, sub_goal, tool_name, self.planner.toolbox_metadata[tool_name]
|
| 145 |
+
)
|
| 146 |
+
explanation, command = self.executor.extract_explanation_and_command(tool_command)
|
| 147 |
+
result = self.executor.execute_tool_command(tool_name, command)
|
| 148 |
+
result = make_json_serializable(result)
|
| 149 |
+
|
| 150 |
+
messages.append(ChatMessage(
|
| 151 |
+
role="assistant",
|
| 152 |
+
content=f"{json.dumps(result, indent=4)}",
|
| 153 |
+
metadata={"title": f"β
Step {step_count} Result: {tool_name}"}))
|
| 154 |
+
yield messages
|
| 155 |
+
|
| 156 |
+
# Step 6: Memory update and stopping condition
|
| 157 |
+
self.memory.add_action(step_count, tool_name, sub_goal, tool_command, result)
|
| 158 |
+
stop_verification = self.planner.verificate_memory(user_query, img_path, query_analysis, self.memory)
|
| 159 |
+
conclusion = self.planner.extract_conclusion(stop_verification)
|
| 160 |
+
|
| 161 |
+
messages.append(ChatMessage(
|
| 162 |
+
role="assistant",
|
| 163 |
+
content=f"π Step {step_count} Conclusion: {conclusion}"))
|
| 164 |
+
yield messages
|
| 165 |
+
|
| 166 |
+
if conclusion == 'STOP':
|
| 167 |
+
break
|
| 168 |
+
|
| 169 |
+
# Step 7: Generate Final Output (if needed)
|
| 170 |
+
if 'final' in self.output_types:
|
| 171 |
+
final_output = self.planner.generate_final_output(user_query, img_path, self.memory)
|
| 172 |
+
messages.append(ChatMessage(role="assistant", content=f"π― Final Output:\n{final_output}"))
|
| 173 |
+
yield messages
|
| 174 |
+
|
| 175 |
+
if 'direct' in self.output_types:
|
| 176 |
+
direct_output = self.planner.generate_direct_output(user_query, img_path, self.memory)
|
| 177 |
+
messages.append(ChatMessage(role="assistant", content=f"πΉ Direct Output:\n{direct_output}"))
|
| 178 |
+
yield messages
|
| 179 |
+
|
| 180 |
+
# Step 8: Completion Message
|
| 181 |
+
messages.append(ChatMessage(role="assistant", content="β
Problem-solving process complete."))
|
| 182 |
+
yield messages
|
| 183 |
+
|
| 184 |
+
|
| 185 |
+
def parse_arguments():
|
| 186 |
+
parser = argparse.ArgumentParser(description="Run the OctoTools demo with specified parameters.")
|
| 187 |
+
parser.add_argument("--llm_engine_name", default="gpt-4o", help="LLM engine name.")
|
| 188 |
+
parser.add_argument("--max_tokens", type=int, default=2000, help="Maximum tokens for LLM generation.")
|
| 189 |
+
parser.add_argument("--run_baseline_only", type=bool, default=False, help="Run only the baseline (no toolbox).")
|
| 190 |
+
parser.add_argument("--task", default="minitoolbench", help="Task to run.")
|
| 191 |
+
parser.add_argument("--task_description", default="", help="Task description.")
|
| 192 |
+
parser.add_argument(
|
| 193 |
+
"--output_types",
|
| 194 |
+
default="base,final,direct",
|
| 195 |
+
help="Comma-separated list of required outputs (base,final,direct)"
|
| 196 |
+
)
|
| 197 |
+
parser.add_argument("--enabled_tools", default="Generalist_Solution_Generator_Tool", help="List of enabled tools.")
|
| 198 |
+
parser.add_argument("--root_cache_dir", default="demo_solver_cache", help="Path to solver cache directory.")
|
| 199 |
+
parser.add_argument("--output_json_dir", default="demo_results", help="Path to output JSON directory.")
|
| 200 |
+
parser.add_argument("--verbose", type=bool, default=True, help="Enable verbose output.")
|
| 201 |
+
return parser.parse_args()
|
| 202 |
+
|
| 203 |
+
|
| 204 |
+
def solve_problem_gradio(user_query, user_image, max_steps=10, max_time=60, api_key=None, llm_model_engine=None, enabled_tools=None):
|
| 205 |
+
"""
|
| 206 |
+
Wrapper function to connect the solver to Gradio.
|
| 207 |
+
Streams responses from `solver.stream_solve_user_problem` for real-time UI updates.
|
| 208 |
+
"""
|
| 209 |
+
|
| 210 |
+
if api_key is None:
|
| 211 |
+
return [["assistant", "β οΈ Error: OpenAI API Key is required."]]
|
| 212 |
+
|
| 213 |
+
# Initialize Tools
|
| 214 |
+
enabled_tools = args.enabled_tools.split(",") if args.enabled_tools else []
|
| 215 |
+
|
| 216 |
+
# Hack enabled_tools
|
| 217 |
+
enabled_tools = ["Generalist_Solution_Generator_Tool"]
|
| 218 |
+
# Instantiate Initializer
|
| 219 |
+
initializer = Initializer(
|
| 220 |
+
enabled_tools=enabled_tools,
|
| 221 |
+
model_string=llm_model_engine,
|
| 222 |
+
api_key=api_key
|
| 223 |
+
)
|
| 224 |
+
|
| 225 |
+
# Instantiate Planner
|
| 226 |
+
planner = Planner(
|
| 227 |
+
llm_engine_name=llm_model_engine,
|
| 228 |
+
toolbox_metadata=initializer.toolbox_metadata,
|
| 229 |
+
available_tools=initializer.available_tools,
|
| 230 |
+
api_key=api_key
|
| 231 |
+
)
|
| 232 |
+
|
| 233 |
+
# Instantiate Memory
|
| 234 |
+
memory = Memory()
|
| 235 |
+
|
| 236 |
+
# Instantiate Executor
|
| 237 |
+
executor = Executor(
|
| 238 |
+
llm_engine_name=llm_model_engine,
|
| 239 |
+
root_cache_dir=args.root_cache_dir,
|
| 240 |
+
enable_signal=False,
|
| 241 |
+
api_key=api_key
|
| 242 |
+
)
|
| 243 |
+
|
| 244 |
+
# Instantiate Solver
|
| 245 |
+
solver = Solver(
|
| 246 |
+
planner=planner,
|
| 247 |
+
memory=memory,
|
| 248 |
+
executor=executor,
|
| 249 |
+
task=args.task,
|
| 250 |
+
task_description=args.task_description,
|
| 251 |
+
output_types=args.output_types, # Add new parameter
|
| 252 |
+
verbose=args.verbose,
|
| 253 |
+
max_steps=max_steps,
|
| 254 |
+
max_time=max_time,
|
| 255 |
+
output_json_dir=args.output_json_dir,
|
| 256 |
+
root_cache_dir=args.root_cache_dir
|
| 257 |
+
)
|
| 258 |
+
|
| 259 |
+
if solver is None:
|
| 260 |
+
return [["assistant", "β οΈ Error: Solver is not initialized. Please restart the application."]]
|
| 261 |
+
|
| 262 |
+
messages = [] # Initialize message list
|
| 263 |
+
for message_batch in solver.stream_solve_user_problem(user_query, user_image, api_key, messages):
|
| 264 |
+
yield [msg for msg in message_batch] # Ensure correct format for Gradio Chatbot
|
| 265 |
+
|
| 266 |
+
|
| 267 |
+
def main(args):
|
| 268 |
+
#################### Gradio Interface ####################
|
| 269 |
+
with gr.Blocks() as demo:
|
| 270 |
+
gr.Markdown("# π Chat with OctoTools: An Agentic Framework for Complex Reasoning") # Title
|
| 271 |
+
# gr.Markdown("[](https://octotools.github.io/)") # Title
|
| 272 |
+
gr.Markdown("""
|
| 273 |
+
**OctoTools** is a training-free, user-friendly, and easily extensible open-source agentic framework designed to tackle complex reasoning across diverse domains.
|
| 274 |
+
It introduces standardized **tool cards** to encapsulate tool functionality, a **planner** for both high-level and low-level planning, and an **executor** to carry out tool usage.
|
| 275 |
+
|
| 276 |
+
[Website](https://octotools.github.io/) |
|
| 277 |
+
[Github](https://github.com/octotools/octotools) |
|
| 278 |
+
[arXiv](https://github.com/octotools/octotools/assets/paper.pdf) |
|
| 279 |
+
[Paper](https://github.com/octotools/octotools/assets/paper.pdf) |
|
| 280 |
+
[Tool Cards](https://octotools.github.io/#tool-cards) |
|
| 281 |
+
[Example Visualizations](https://octotools.github.io/#visualization)
|
| 282 |
+
""")
|
| 283 |
+
|
| 284 |
+
with gr.Row():
|
| 285 |
+
# Left column for settings
|
| 286 |
+
with gr.Column(scale=1):
|
| 287 |
+
with gr.Row():
|
| 288 |
+
api_key = gr.Textbox(
|
| 289 |
+
show_label=True,
|
| 290 |
+
placeholder="Your API key will not be stored in any way.",
|
| 291 |
+
type="password",
|
| 292 |
+
label="OpenAI API Key",
|
| 293 |
+
# container=False
|
| 294 |
+
)
|
| 295 |
+
|
| 296 |
+
llm_model_engine = gr.Dropdown(
|
| 297 |
+
choices=["gpt-4o", "gpt-4o-2024-11-20", "gpt-4o-2024-08-06", "gpt-4o-2024-05-13",
|
| 298 |
+
"gpt-4o-mini", "gpt-4o-mini-2024-07-18"],
|
| 299 |
+
value="gpt-4o",
|
| 300 |
+
label="LLM Model"
|
| 301 |
+
)
|
| 302 |
+
with gr.Row():
|
| 303 |
+
max_steps = gr.Slider(value=5, minimum=1, maximum=10, step=1, label="Max Steps")
|
| 304 |
+
max_time = gr.Slider(value=180, minimum=60, maximum=300, step=30, label="Max Time (seconds)")
|
| 305 |
+
|
| 306 |
+
with gr.Row():
|
| 307 |
+
enabled_tools = gr.CheckboxGroup(
|
| 308 |
+
choices=all_tools,
|
| 309 |
+
value=all_tools,
|
| 310 |
+
label="Enabled Tools",
|
| 311 |
+
)
|
| 312 |
+
|
| 313 |
+
|
| 314 |
+
|
| 315 |
+
# Middle column for the query
|
| 316 |
+
with gr.Column(scale=2):
|
| 317 |
+
user_image = gr.Image(type="pil", label="Upload an image (optional)", height=500) # Accepts multiple formats
|
| 318 |
+
|
| 319 |
+
with gr.Row():
|
| 320 |
+
user_query = gr.Textbox( placeholder="Type your question here...", label="Question")
|
| 321 |
+
|
| 322 |
+
with gr.Row():
|
| 323 |
+
run_button = gr.Button("Run") # Run button
|
| 324 |
+
|
| 325 |
+
# Right column for the output
|
| 326 |
+
with gr.Column(scale=3):
|
| 327 |
+
chatbot_output = gr.Chatbot(type="messages", label="Step-wise problem-solving output (Deep Thinking)", height=500)
|
| 328 |
+
# chatbot_output.like(lambda x: print(f"User liked: {x}"))
|
| 329 |
+
|
| 330 |
+
# TODO: Add actions to the buttons
|
| 331 |
+
with gr.Row(elem_id="buttons") as button_row:
|
| 332 |
+
upvote_btn = gr.Button(value="π Upvote", interactive=True)
|
| 333 |
+
downvote_btn = gr.Button(value="π Downvote", interactive=True)
|
| 334 |
+
clear_btn = gr.Button(value="ποΈ Clear history", interactive=True)
|
| 335 |
+
|
| 336 |
+
with gr.Row():
|
| 337 |
+
comment_textbox = gr.Textbox(value="",
|
| 338 |
+
placeholder="Feel free to add any comments here. Thanks for using OctoTools!",
|
| 339 |
+
label="π¬ Comment", interactive=True)
|
| 340 |
+
|
| 341 |
+
# Link button click to function
|
| 342 |
+
run_button.click(
|
| 343 |
+
fn=solve_problem_gradio,
|
| 344 |
+
inputs=[user_query, user_image, max_steps, max_time, api_key, llm_model_engine, enabled_tools],
|
| 345 |
+
outputs=chatbot_output
|
| 346 |
+
)
|
| 347 |
+
#################### Gradio Interface ####################
|
| 348 |
+
|
| 349 |
+
# Launch the Gradio app
|
| 350 |
+
demo.launch()
|
| 351 |
+
|
| 352 |
+
|
| 353 |
+
if __name__ == "__main__":
|
| 354 |
+
args = parse_arguments()
|
| 355 |
+
|
| 356 |
+
# Manually set enabled tools
|
| 357 |
+
# args.enabled_tools = "Generalist_Solution_Generator_Tool"
|
| 358 |
+
|
| 359 |
+
# All tools
|
| 360 |
+
all_tools = [
|
| 361 |
+
"Generalist_Solution_Generator_Tool",
|
| 362 |
+
|
| 363 |
+
"Image_Captioner_Tool",
|
| 364 |
+
"Object_Detector_Tool",
|
| 365 |
+
"Text_Detector_Tool",
|
| 366 |
+
"Relevant_Patch_Zoomer_Tool",
|
| 367 |
+
|
| 368 |
+
"Python_Code_Generator_Tool",
|
| 369 |
+
|
| 370 |
+
"ArXiv_Paper_Searcher_Tool",
|
| 371 |
+
"Google_Search_Tool",
|
| 372 |
+
"Nature_News_Fetcher_Tool",
|
| 373 |
+
"Pubmed_Search_Tool",
|
| 374 |
+
"URL_Text_Extractor_Tool",
|
| 375 |
+
"Wikipedia_Knowledge_Searcher_Tool"
|
| 376 |
+
]
|
| 377 |
+
args.enabled_tools = ",".join(all_tools)
|
| 378 |
+
|
| 379 |
+
main(args)
|
| 380 |
+
|
examples/baseball.png
ADDED
|