patopla commited on
Commit
f73cbc5
·
verified ·
1 Parent(s): 81917a3

Upload 2 files

Browse files
Files changed (2) hide show
  1. assistant.py +31 -0
  2. tools.py +61 -0
assistant.py ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import random
3
+ from smolagents import GradioUI, CodeAgent, HfApiModel
4
+
5
+ # Import our custom tools from their modules
6
+ from tools import DuckDuckGoSearchTool, CurrencyConverterTool
7
+
8
+
9
+ # Initialize the Hugging Face model
10
+ model = HfApiModel()
11
+
12
+ # Initialize the web search tool
13
+ search_tool = DuckDuckGoSearchTool()
14
+
15
+ # Initialize the weather tool
16
+ currency_converter_tool = CurrencyConverterTool()
17
+
18
+
19
+ # Load the guest dataset and initialize the guest info tool
20
+ guest_info_tool = load_guest_dataset()
21
+
22
+ # Create Alfred with all the tools
23
+ alfred = CodeAgent(
24
+ tools=[currency_converter_tool, search_tool],
25
+ model=model,
26
+ add_base_tools=True, # Add any additional base tools
27
+ planning_interval=3 # Enable planning every 3 steps
28
+ )
29
+
30
+ if __name__ == "__main__":
31
+ GradioUI(alfred).launch()
tools.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from smolagents import DuckDuckGoSearchTool
2
+ from smolagents import Tool
3
+ import random
4
+
5
+ # Initialize the DuckDuckGo search tool
6
+ search_tool = DuckDuckGoSearchTool()
7
+
8
+
9
+
10
+
11
+ class CurrencyConverterTool(Tool):
12
+ name = "currency_converter"
13
+ description = "Converts amounts between different currencies using dummy exchange rates."
14
+ inputs = {
15
+ "amount": {
16
+ "type": "number", # Cambiado de "float" a "number"
17
+ "description": "The amount to convert."
18
+ },
19
+ "from_currency": {
20
+ "type": "string",
21
+ "description": "The source currency code (e.g., USD, EUR, CLP)."
22
+ },
23
+ "to_currency": {
24
+ "type": "string",
25
+ "description": "The target currency code (e.g., USD, EUR, CLP)."
26
+ }
27
+ }
28
+ output_type = "string"
29
+
30
+ def forward(self, amount: float, from_currency: str, to_currency: str):
31
+ # Dummy exchange rates (base: USD)
32
+ exchange_rates = {
33
+ "USD": 1.0,
34
+ "EUR": 0.85,
35
+ "CLP": 800.0,
36
+ "ARS": 350.0,
37
+ "BRL": 5.2,
38
+ "MXN": 18.5,
39
+ "GBP": 0.75,
40
+ "JPY": 110.0,
41
+ "CAD": 1.25
42
+ }
43
+
44
+ # Validate currencies
45
+ if from_currency not in exchange_rates or to_currency not in exchange_rates:
46
+ available_currencies = ", ".join(exchange_rates.keys())
47
+ return f"Error: Moneda no soportada. Divisas disponibles: {available_currencies}"
48
+
49
+ # Same currency conversion
50
+ if from_currency == to_currency:
51
+ return f"{amount:.2f} {from_currency} = {amount:.2f} {to_currency} (misma divisa)"
52
+
53
+ # Convert to USD first, then to target currency
54
+ usd_amount = amount / exchange_rates[from_currency]
55
+ converted_amount = usd_amount * exchange_rates[to_currency]
56
+
57
+ # Add small random variation to simulate real market fluctuations (±2%)
58
+ variation = random.uniform(-0.02, 0.02)
59
+ converted_amount *= (1 + variation)
60
+
61
+ return f"{amount:.2f} {from_currency} = {converted_amount:.2f} {to_currency} (tasa simulada)"