innalhy commited on
Commit
91e9e99
·
1 Parent(s): 8c5c24b

edited app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -10
app.py CHANGED
@@ -1,22 +1,53 @@
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
-
8
  from Gradio_UI import GradioUI
9
 
10
- # Below is an example of a tool that does nothing. Amaze us with your creativity !
11
  @tool
12
- def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
13
- #Keep this format for the description / args / args description but feel free to modify the tool
14
- """A tool that does nothing yet
 
15
  Args:
16
- arg1: the first argument
17
- arg2: the second argument
 
18
  """
19
- return "What magic will you build ?"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  @tool
22
  def get_current_time_in_timezone(timezone: str) -> str:
@@ -55,7 +86,7 @@ with open("prompts.yaml", 'r') as stream:
55
 
56
  agent = CodeAgent(
57
  model=model,
58
- tools=[final_answer], ## add your tools here (don't remove final answer)
59
  max_steps=6,
60
  verbosity_level=1,
61
  grammar=None,
 
1
+ from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel,load_tool,tool
2
+ import base64
3
  import datetime
4
+ import hashlib
5
  import requests
6
  import pytz
7
  import yaml
8
  from tools.final_answer import FinalAnswerTool
 
9
  from Gradio_UI import GradioUI
10
 
11
+ # Message encryption/decryption tool
12
  @tool
13
+ def my_custom_tool(message: str, action: str, key: str) -> str:
14
+ """
15
+ A tool that encrypts or decrypts messages using Base64 encoding for secure communication.
16
+
17
  Args:
18
+ message: The plaintext to encrypt, or the encrypted text to decrypt.
19
+ action: Either 'encrypt' or 'decrypt'.
20
+ key: The secret key used for both encryption and decryption.
21
  """
22
+ # Normalize action so "Encrypt" and " encrypt " both work
23
+ action = action.lower().strip()
24
+ if action not in ("encrypt", "decrypt"):
25
+ return "Error: action must be 'encrypt' or 'decrypt'."
26
+
27
+ if not key:
28
+ return "Error: key cannot be empty."
29
+
30
+ # Derive a fixed-length 32-byte key from the password via SHA-256
31
+ key_bytes = hashlib.sha256(key.encode()).digest()
32
+
33
+ if action == "encrypt":
34
+ # XOR each message byte with a repeating key byte, then encode as Base64
35
+ encrypted = bytes(
36
+ b ^ key_bytes[i % len(key_bytes)]
37
+ for i, b in enumerate(message.encode())
38
+ )
39
+ return base64.urlsafe_b64encode(encrypted).decode()
40
+
41
+ try:
42
+ # Reverse the process: decode Base64, XOR with the same key, recover plaintext
43
+ encrypted_bytes = base64.urlsafe_b64decode(message.encode())
44
+ decrypted = bytes(
45
+ b ^ key_bytes[i % len(key_bytes)]
46
+ for i, b in enumerate(encrypted_bytes)
47
+ )
48
+ return decrypted.decode()
49
+ except Exception as e:
50
+ return f"Error during decryption: {str(e)}"
51
 
52
  @tool
53
  def get_current_time_in_timezone(timezone: str) -> str:
 
86
 
87
  agent = CodeAgent(
88
  model=model,
89
+ tools=[final_answer, my_custom_tool, get_current_time_in_timezone], ## add your tools here (don't remove final answer)
90
  max_steps=6,
91
  verbosity_level=1,
92
  grammar=None,