Spaces:
Sleeping
Sleeping
Create agent.py
Browse files
agent.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from smolagents import LiteLLMModel
|
| 2 |
+
from smolagents import CodeAgent, ToolCallingAgent, GoogleSearchTool, HfApiModel, VisitWebpageTool, DuckDuckGoSearchTool, tool, PythonInterpreterTool, WikipediaSearchTool
|
| 3 |
+
import requests
|
| 4 |
+
from youtube_transcript_api import YouTubeTranscriptApi
|
| 5 |
+
|
| 6 |
+
@tool
|
| 7 |
+
def reverse_sentence_tool(reverse_sentence: str) -> str:
|
| 8 |
+
"""
|
| 9 |
+
This tool receives a sentence where both the word order and the characters in each word are reversed.
|
| 10 |
+
It returns the sentence with words and order corrected.
|
| 11 |
+
|
| 12 |
+
Args:
|
| 13 |
+
reverse_sentence: A sentence with reversed words and reversed word order.
|
| 14 |
+
|
| 15 |
+
Returns:
|
| 16 |
+
A sentence in natural reading order.
|
| 17 |
+
"""
|
| 18 |
+
inverted_words = reverse_sentence.split(" ")[::-1]
|
| 19 |
+
correct_words = [word[::-1] for word in inverted_words]
|
| 20 |
+
|
| 21 |
+
return " ".join(correct_words)
|
| 22 |
+
|
| 23 |
+
@tool
|
| 24 |
+
def get_youtube_transcript(video_url: str) -> str:
|
| 25 |
+
"""
|
| 26 |
+
Fetches the transcript from a YouTube video if available.
|
| 27 |
+
|
| 28 |
+
Args:
|
| 29 |
+
video_url: Full URL to the YouTube video.
|
| 30 |
+
|
| 31 |
+
Returns:
|
| 32 |
+
Transcript text.
|
| 33 |
+
"""
|
| 34 |
+
video_id = video_url.split("v=")[-1]
|
| 35 |
+
transcript = YouTubeTranscriptApi.get_transcript(video_id)
|
| 36 |
+
full_text = " ".join([entry['text'] for entry in transcript])
|
| 37 |
+
return full_text
|
| 38 |
+
|
| 39 |
+
@tool
|
| 40 |
+
def check_answer(answer: str) -> str:
|
| 41 |
+
"""
|
| 42 |
+
Reviews the answer to check that it meets the requirements specified by the user and modifies it if necessary.
|
| 43 |
+
|
| 44 |
+
Args:
|
| 45 |
+
answer (str): The answer fo the Agent.
|
| 46 |
+
|
| 47 |
+
Returns:
|
| 48 |
+
str: The final answer.
|
| 49 |
+
"""
|
| 50 |
+
|
| 51 |
+
if answer[-1] == '.':
|
| 52 |
+
return answer[:-1]
|
| 53 |
+
|
| 54 |
+
if "St." in answer:
|
| 55 |
+
return answer.replace("St.", "Saint")
|
| 56 |
+
|
| 57 |
+
return answer
|
| 58 |
+
|
| 59 |
+
class BasicAgent:
|
| 60 |
+
def __init__(self):
|
| 61 |
+
self.api_key = ""
|
| 62 |
+
self.model = LiteLLMModel(model_id="gemini/gemini-2.0-flash", api_key=self.api_key)
|
| 63 |
+
|
| 64 |
+
self.agent = CodeAgent(
|
| 65 |
+
tools=[
|
| 66 |
+
DuckDuckGoSearchTool(),
|
| 67 |
+
PythonInterpreterTool(),
|
| 68 |
+
VisitWebpageTool(),
|
| 69 |
+
reverse_sentence_tool,
|
| 70 |
+
check_answer
|
| 71 |
+
],
|
| 72 |
+
model=self.model
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
print("BasicAgent initialized.")
|
| 76 |
+
|
| 77 |
+
def __call__(self, question: str) -> str:
|
| 78 |
+
print(f"Agent received question")
|
| 79 |
+
answer = self.agent.run(question)
|
| 80 |
+
|
| 81 |
+
return answer
|