Spaces:
Sleeping
Sleeping
Update agent.py
Browse files
agent.py
CHANGED
|
@@ -36,7 +36,7 @@ import whisper
|
|
| 36 |
# return f"Error performing Google search: {str(e)}"
|
| 37 |
|
| 38 |
@tool
|
| 39 |
-
def ImageAnalysisTool(question: str) -> str:
|
| 40 |
"""Tool for analyzing images mentioned in the question.
|
| 41 |
Args:
|
| 42 |
question (str): The question text which may contain an image URL.
|
|
@@ -60,16 +60,10 @@ def ImageAnalysisTool(question: str) -> str:
|
|
| 60 |
except Exception as e:
|
| 61 |
return f"Error fetching image: {e}"
|
| 62 |
|
| 63 |
-
model = LiteLLMModel(
|
| 64 |
-
model_id="gemini/gemini-2.5-pro",
|
| 65 |
-
api_key=os.environ.get("GEMINI_KEY"),
|
| 66 |
-
max_tokens=8192
|
| 67 |
-
)
|
| 68 |
-
|
| 69 |
agent = CodeAgent(
|
| 70 |
tools=[],
|
| 71 |
model=model,
|
| 72 |
-
max_steps=
|
| 73 |
verbosity_level=2
|
| 74 |
)
|
| 75 |
|
|
@@ -96,17 +90,17 @@ def SpeechToTextTool(audio_path: str) -> str:
|
|
| 96 |
return result.get("text", "")
|
| 97 |
|
| 98 |
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
|
| 111 |
#@tool
|
| 112 |
#class LocalFileAudioTool:
|
|
@@ -123,8 +117,9 @@ def SpeechToTextTool(audio_path: str) -> str:
|
|
| 123 |
# return f"Transcribed audio from '{file_path}' (simulated)."
|
| 124 |
|
| 125 |
class MagAgent:
|
| 126 |
-
def __init__(self):
|
| 127 |
"""Initialize the MagAgent with search tools."""
|
|
|
|
| 128 |
print("Initializing MagAgent with search tools...")
|
| 129 |
model = LiteLLMModel(
|
| 130 |
model_id="gemini/gemini-2.0-flash",
|
|
@@ -139,7 +134,7 @@ class MagAgent:
|
|
| 139 |
self.agent = CodeAgent(
|
| 140 |
model= model,
|
| 141 |
tools=[
|
| 142 |
-
|
| 143 |
# GoogleSearchTool,
|
| 144 |
DuckDuckGoSearchTool(),
|
| 145 |
WikipediaSearchTool(),
|
|
@@ -153,7 +148,11 @@ class MagAgent:
|
|
| 153 |
async def __call__(self, question: str) -> str:
|
| 154 |
"""Process a question asynchronously using the MagAgent."""
|
| 155 |
print(f"MagAgent received question (first 50 chars): {question[:50]}...")
|
|
|
|
| 156 |
try:
|
|
|
|
|
|
|
|
|
|
| 157 |
# Define a task with fallback search logic
|
| 158 |
task = (
|
| 159 |
f"Answer the following question accurately and concisely: {question}\n"
|
|
|
|
| 36 |
# return f"Error performing Google search: {str(e)}"
|
| 37 |
|
| 38 |
@tool
|
| 39 |
+
def ImageAnalysisTool(question: str, model: LiteLLMModel) -> str:
|
| 40 |
"""Tool for analyzing images mentioned in the question.
|
| 41 |
Args:
|
| 42 |
question (str): The question text which may contain an image URL.
|
|
|
|
| 60 |
except Exception as e:
|
| 61 |
return f"Error fetching image: {e}"
|
| 62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
agent = CodeAgent(
|
| 64 |
tools=[],
|
| 65 |
model=model,
|
| 66 |
+
max_steps=10,
|
| 67 |
verbosity_level=2
|
| 68 |
)
|
| 69 |
|
|
|
|
| 90 |
return result.get("text", "")
|
| 91 |
|
| 92 |
|
| 93 |
+
@tool
|
| 94 |
+
def youtube_transcript(url: str) -> str:
|
| 95 |
+
"""
|
| 96 |
+
Get transcript of YouTube video.
|
| 97 |
+
Args:
|
| 98 |
+
url: YouTube video url in ""
|
| 99 |
+
"""
|
| 100 |
+
video_id = url.partition("https://www.youtube.com/watch?v=")[2]
|
| 101 |
+
transcript = YouTubeTranscriptApi.get_transcript(video_id)
|
| 102 |
+
transcript_text = " ".join([item["text"] for item in transcript])
|
| 103 |
+
return {"youtube_transcript": transcript_text}
|
| 104 |
|
| 105 |
#@tool
|
| 106 |
#class LocalFileAudioTool:
|
|
|
|
| 117 |
# return f"Transcribed audio from '{file_path}' (simulated)."
|
| 118 |
|
| 119 |
class MagAgent:
|
| 120 |
+
def __init__(self, rate_limiter: Optional[Limiter] = None):
|
| 121 |
"""Initialize the MagAgent with search tools."""
|
| 122 |
+
self.rate_limiter = rate_limiter
|
| 123 |
print("Initializing MagAgent with search tools...")
|
| 124 |
model = LiteLLMModel(
|
| 125 |
model_id="gemini/gemini-2.0-flash",
|
|
|
|
| 134 |
self.agent = CodeAgent(
|
| 135 |
model= model,
|
| 136 |
tools=[
|
| 137 |
+
youtube_transcript,
|
| 138 |
# GoogleSearchTool,
|
| 139 |
DuckDuckGoSearchTool(),
|
| 140 |
WikipediaSearchTool(),
|
|
|
|
| 148 |
async def __call__(self, question: str) -> str:
|
| 149 |
"""Process a question asynchronously using the MagAgent."""
|
| 150 |
print(f"MagAgent received question (first 50 chars): {question[:50]}...")
|
| 151 |
+
|
| 152 |
try:
|
| 153 |
+
if self.rate_limiter:
|
| 154 |
+
while not self.rate_limiter.consume(1):
|
| 155 |
+
await asyncio.sleep(60 / RATE_LIMIT)
|
| 156 |
# Define a task with fallback search logic
|
| 157 |
task = (
|
| 158 |
f"Answer the following question accurately and concisely: {question}\n"
|