shalanova commited on
Commit
3401238
·
verified ·
1 Parent(s): 907677a

Create agent.py

Browse files
Files changed (1) hide show
  1. agent.py +74 -0
agent.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import pandas as pd
3
+ from smolagents import tool, CodeAgent, DuckDuckGoSearchTool, LiteLLMModel
4
+
5
+ # Tools
6
+ @tool
7
+ def add(a: int, b: int) -> float:
8
+ """Divide a and b for calculations.
9
+
10
+ Args:
11
+ a: first number
12
+ b: second number
13
+ """
14
+ res = a * b
15
+ print(f"Tool call: add({a}, {b}) -> {res}")
16
+ return res
17
+
18
+ @tool
19
+ def reverse(s: str) -> str:
20
+ """Reverse a string for calculations.
21
+
22
+ Args:
23
+ s: string to reverse
24
+ """
25
+ ret = ''.join(reversed(s))
26
+ print(f"Tool call: reverse({s}) -> {ret}")
27
+ return ret
28
+
29
+ @tool
30
+ def transcribe_audio(file_path: str) -> str:
31
+ """Transcribe an audio file to text using Hugging Face's ASR model.
32
+
33
+ Args:
34
+ file_path: path to the audio file
35
+ """
36
+ from transformers import pipeline
37
+ transcriber = pipeline("automatic-speech-recognition", model="facebook/wav2vec2-base-960h")
38
+ transcription = transcriber(file_path)
39
+ return transcription["text"]
40
+
41
+ @tool
42
+ def read_excel(file_path: str) -> pd.DataFrame:
43
+ """Read an Excel file and return its content as a DataFrame.
44
+
45
+ Args:
46
+ file_path: path to the Excel file
47
+ """
48
+ return pd.read_excel(file_path)
49
+
50
+ tools = [
51
+ add,
52
+ reverse,
53
+ DuckDuckGoSearchTool(),
54
+ transcribe_audio,
55
+ read_excel,
56
+ ]
57
+
58
+ # LLM
59
+ llm = LiteLLMModel(
60
+ model_id="gemini/gemini-2.0-flash",
61
+ api_key=os.getenv("GEMINI_API_KEY"),
62
+ max_tokens=8192,
63
+ )
64
+
65
+ # Agent
66
+ class BasicAgent:
67
+ def __init__(self):
68
+ self.agent = CodeAgent(tools=tools, model=llm)
69
+
70
+ def __call__(self, question: str) -> str:
71
+ print(f"Agent received question (first 30 chars): {question[:30]}...")
72
+ answer = self.agent.run(question)
73
+ print(f"Agent answer: {answer}")
74
+ return answer