mwill-AImission commited on
Commit
4469dae
·
verified ·
1 Parent(s): 28fc45e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -21
app.py CHANGED
@@ -1,27 +1,10 @@
1
  from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
2
  import datetime
3
  import requests
4
- import pytz
5
  import yaml
6
  from tools.final_answer import FinalAnswerTool
7
  from Gradio_UI import GradioUI
8
 
9
- # Tool for fetching current time in a given timezone
10
- @tool
11
- def get_current_time_in_timezone(timezone: str) -> str:
12
- """A tool that fetches the current local time in a specified timezone.
13
- Args:
14
- timezone: A string representing a valid timezone (e.g., 'America/New_York').
15
- """
16
- try:
17
- # Create timezone object
18
- tz = pytz.timezone(timezone)
19
- # Get current time in that timezone
20
- local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
21
- return f"The current local time in {timezone} is: {local_time}"
22
- except Exception as e:
23
- return f"Error fetching time for timezone '{timezone}': {str(e)}"
24
-
25
  # Tool for commenting code in simple language
26
  @tool
27
  def comment_code(code: str) -> str:
@@ -35,7 +18,6 @@ def comment_code(code: str) -> str:
35
  for line in code.split('\n'):
36
  stripped = line.strip()
37
  if stripped:
38
- # Example simplistic explanations
39
  if "def " in stripped:
40
  commented_code += "# This defines a function\n"
41
  elif "import " in stripped:
@@ -51,6 +33,35 @@ def comment_code(code: str) -> str:
51
  except Exception as e:
52
  return f"Error processing code: {str(e)}"
53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
 
55
  final_answer = FinalAnswerTool()
56
 
@@ -61,15 +72,15 @@ model = HfApiModel(
61
  custom_role_conversions=None,
62
  )
63
 
64
- # Import tool from Hub
65
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
66
 
67
  with open("prompts.yaml", 'r') as stream:
68
  prompt_templates = yaml.safe_load(stream)
69
-
70
  agent = CodeAgent(
71
  model=model,
72
- tools=[final_answer, comment_code], # Added our new tool
73
  max_steps=6,
74
  verbosity_level=1,
75
  grammar=None,
@@ -80,3 +91,4 @@ agent = CodeAgent(
80
  )
81
 
82
  GradioUI(agent).launch()
 
 
1
  from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
2
  import datetime
3
  import requests
 
4
  import yaml
5
  from tools.final_answer import FinalAnswerTool
6
  from Gradio_UI import GradioUI
7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  # Tool for commenting code in simple language
9
  @tool
10
  def comment_code(code: str) -> str:
 
18
  for line in code.split('\n'):
19
  stripped = line.strip()
20
  if stripped:
 
21
  if "def " in stripped:
22
  commented_code += "# This defines a function\n"
23
  elif "import " in stripped:
 
33
  except Exception as e:
34
  return f"Error processing code: {str(e)}"
35
 
36
+ # Tool for extracting code from an image (screenshot)
37
+ @tool
38
+ def extract_code_from_image(image_path: str) -> str:
39
+ """Extracts text from an image file (screenshot) that contains code.
40
+ Args:
41
+ image_path: The path to the image file.
42
+ """
43
+ try:
44
+ from PIL import Image
45
+ import pytesseract
46
+ image = Image.open(image_path)
47
+ code_text = pytesseract.image_to_string(image)
48
+ return code_text
49
+ except Exception as e:
50
+ return f"Error extracting code from image: {str(e)}"
51
+
52
+ # Tool for reading code from a file
53
+ @tool
54
+ def extract_code_from_file(file_path: str) -> str:
55
+ """Reads code text from a file.
56
+ Args:
57
+ file_path: The path to the file containing code.
58
+ """
59
+ try:
60
+ with open(file_path, 'r') as f:
61
+ code_text = f.read()
62
+ return code_text
63
+ except Exception as e:
64
+ return f"Error reading code from file: {str(e)}"
65
 
66
  final_answer = FinalAnswerTool()
67
 
 
72
  custom_role_conversions=None,
73
  )
74
 
75
+ # Import additional tool from Hub (if needed)
76
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
77
 
78
  with open("prompts.yaml", 'r') as stream:
79
  prompt_templates = yaml.safe_load(stream)
80
+
81
  agent = CodeAgent(
82
  model=model,
83
+ tools=[final_answer, comment_code, extract_code_from_image, extract_code_from_file],
84
  max_steps=6,
85
  verbosity_level=1,
86
  grammar=None,
 
91
  )
92
 
93
  GradioUI(agent).launch()
94
+