RalphThings commited on
Commit
1ab3f7e
·
verified ·
1 Parent(s): db8755e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -4
app.py CHANGED
@@ -5,7 +5,9 @@ from langchain_community.tools import DuckDuckGoSearchRun
5
  from langchain.tools import tool
6
  from langchain_core.output_parsers import JsonOutputParser
7
  from langchain.agents import AgentExecutor, create_react_agent, initialize_agent, AgentType
8
- from youtube_transcript_api import YouTubeTranscriptApi
 
 
9
  import whisper
10
  import chess, chess.engine
11
  from bs4 import BeautifulSoup
@@ -271,7 +273,7 @@ def classify_vegetables(items: list) -> str:
271
 
272
  @tool
273
  # Optional: Add timeout to prevent runaway code execution
274
- # @timeout_decorator.timeout(10, timeout_exception=TimeoutError) # Limit execution to 10 seconds
275
  def execute_code(code: str) -> str:
276
  """
277
  Executes a given Python code snippet and returns the value of the 'output' variable.
@@ -302,8 +304,8 @@ def execute_code(code: str) -> str:
302
  if output_val is None:
303
  return "Result: Code executed, but no variable named 'output' was assigned."
304
  return str(output_val)
305
- # except TimeoutError:
306
- # return "Error: Code execution timed out."
307
  except Exception as e:
308
  # Capture and return execution errors
309
  error_details = traceback.format_exc()
 
5
  from langchain.tools import tool
6
  from langchain_core.output_parsers import JsonOutputParser
7
  from langchain.agents import AgentExecutor, create_react_agent, initialize_agent, AgentType
8
+ from youtube_transcript_api import YouTubeTranscriptApi, TranscriptsDisabled, NoTranscriptFound, TranscriptNotFoundError
9
+ import traceback # For detailed error logging
10
+ import timeout_decorator # Optional: for execute_code timeout (pip install timeout-decorator)
11
  import whisper
12
  import chess, chess.engine
13
  from bs4 import BeautifulSoup
 
273
 
274
  @tool
275
  # Optional: Add timeout to prevent runaway code execution
276
+ @timeout_decorator.timeout(10, timeout_exception=TimeoutError) # Limit execution to 10 seconds
277
  def execute_code(code: str) -> str:
278
  """
279
  Executes a given Python code snippet and returns the value of the 'output' variable.
 
304
  if output_val is None:
305
  return "Result: Code executed, but no variable named 'output' was assigned."
306
  return str(output_val)
307
+ except TimeoutError:
308
+ return "Error: Code execution timed out."
309
  except Exception as e:
310
  # Capture and return execution errors
311
  error_details = traceback.format_exc()