Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files
agent.py
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
from dotenv import load_dotenv
|
| 4 |
+
load_dotenv() # Loads variables from .env into the environment
|
| 5 |
+
|
| 6 |
+
# os.environ["LANGFUSE_HOST"] = os.getenv("LANGFUSE_HOST")
|
| 7 |
+
# os.environ["LANGFUSE_PUBLIC_KEY"] = os.getenv("LANGFUSE_PUBLIC_KEY")
|
| 8 |
+
# os.environ["LANGFUSE_SECRET_KEY"] = os.getenv("LANGFUSE_SECRET_KEY")
|
| 9 |
+
|
| 10 |
+
# os.environ["HF_TOKEN"] = os.getenv("HF_token")
|
| 11 |
+
# os.environ["GEMINI_API_KEY"] = os.getenv("GEMINI_API_KEY")
|
| 12 |
+
#os.environ["TAVILY_API_KEY"] = os.getenv("TRAVILY_SECRET_KEY")
|
| 13 |
+
|
| 14 |
+
from langfuse import get_client
|
| 15 |
+
langfuse = get_client()
|
| 16 |
+
# Verify connection
|
| 17 |
+
if langfuse.auth_check():
|
| 18 |
+
print("Langfuse client is authenticated and ready!")
|
| 19 |
+
else:
|
| 20 |
+
print("Authentication failed. Please check your credentials and host.")
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
from smolagents import OpenAIServerModel
|
| 24 |
+
gemini_model = OpenAIServerModel(
|
| 25 |
+
model_id="gemini-2.5-flash",
|
| 26 |
+
api_base="https://generativelanguage.googleapis.com/v1beta/openai/",
|
| 27 |
+
api_key=os.getenv("GEMINI_API_KEY")
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
# from smolagents import TransformersModel
|
| 31 |
+
# VL_model = TransformersModel(
|
| 32 |
+
# model_id="Qwen/Qwen2.5-VL-32B-Instruct",
|
| 33 |
+
# device_map="auto",
|
| 34 |
+
# torch_dtype="auto",
|
| 35 |
+
# )
|
| 36 |
+
|
| 37 |
+
# coder_model = TransformersModel(
|
| 38 |
+
# model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
|
| 39 |
+
# device_map="auto",
|
| 40 |
+
# torch_dtype="auto",
|
| 41 |
+
# )
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
#from Tools.searchtool import WebSearchTool
|
| 45 |
+
from Tools.mywebpagevisit import CustomVisitWebpageTool
|
| 46 |
+
from smolagents import (
|
| 47 |
+
CodeAgent,
|
| 48 |
+
WebSearchTool,
|
| 49 |
+
#WikipediaSearchTool
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
web_search = WebSearchTool()
|
| 53 |
+
visit_webpage = CustomVisitWebpageTool()
|
| 54 |
+
#wikipedia_search = WikipediaSearchTool()
|
| 55 |
+
|
| 56 |
+
from openinference.instrumentation.smolagents import SmolagentsInstrumentor
|
| 57 |
+
SmolagentsInstrumentor().instrument()
|
| 58 |
+
|
| 59 |
+
web_agent = CodeAgent(
|
| 60 |
+
tools=[WebSearchTool,CustomVisitWebpageTool],
|
| 61 |
+
model= gemini_model,
|
| 62 |
+
additional_authorized_imports = ['requests', 'bs4','json'],
|
| 63 |
+
name="web_agent",
|
| 64 |
+
description="This is an agent that can do web search using 'WebSearchTool' and visit a webpage using 'CustomVisitWebpageTool'."
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
from Tools.ytdownload import youtube_audio_downloader,youtube_video_downloader
|
| 68 |
+
|
| 69 |
+
yt_agent = CodeAgent(
|
| 70 |
+
tools=[youtube_audio_downloader,youtube_video_downloader],
|
| 71 |
+
model= gemini_model,
|
| 72 |
+
name="youtube_downloader_agent",
|
| 73 |
+
description="This is an agent that can download youtube audio file using 'youtube_audio_downloader'and youtube video file using 'youtube_video_downloader'."
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
from Tools.transcriber import transcriber
|
| 77 |
+
|
| 78 |
+
transcriber_agent = CodeAgent(
|
| 79 |
+
tools=[transcriber],
|
| 80 |
+
model= gemini_model,
|
| 81 |
+
name="transcriber_agent",
|
| 82 |
+
description="This is an agent that can transcribe a local audio file using 'transcriber'.\n\nDo not remove adjectives or descriptors of ingredients if not specifically mentioned."
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
from Tools.visual_reasoner import video_reasoner, image_reasoner
|
| 86 |
+
VL_agent = CodeAgent(
|
| 87 |
+
tools=[video_reasoner,image_reasoner],
|
| 88 |
+
model= gemini_model,
|
| 89 |
+
name="VL_agent",
|
| 90 |
+
description="This is an agent that can answer questions on a video using 'video_reasoner' and on a image using 'image_reasoner'."
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
excel_agent = CodeAgent(
|
| 94 |
+
tools=[],
|
| 95 |
+
model= gemini_model,
|
| 96 |
+
name="excel_agent",
|
| 97 |
+
description="This is an agent that can work with excel.",
|
| 98 |
+
add_base_tools=False,
|
| 99 |
+
additional_authorized_imports = ['pandas', 'numpy', 'math']
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
from Tools.code_reader import code_reader
|
| 103 |
+
manager_agent = CodeAgent(
|
| 104 |
+
tools=[code_reader],
|
| 105 |
+
model=gemini_model,
|
| 106 |
+
managed_agents=[web_agent, yt_agent, transcriber_agent, VL_agent],
|
| 107 |
+
additional_authorized_imports=[
|
| 108 |
+
"numpy","os","pandas",'scipy','regex','math'
|
| 109 |
+
],
|
| 110 |
+
planning_interval=5,
|
| 111 |
+
verbosity_level=2,
|
| 112 |
+
max_steps=10,
|
| 113 |
+
description= "You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER]. YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string."
|
| 114 |
+
)
|
| 115 |
+
|
| 116 |
+
# manager_agent.run(
|
| 117 |
+
# "How many studio albums were published by Mercedes Sosa between 2000 and 2009 (included)? You can use the latest 2022 version of english wikipedia."
|
| 118 |
+
# )
|
| 119 |
+
|
| 120 |
+
class MyAgent:
|
| 121 |
+
def __init__(self):
|
| 122 |
+
print("BasicAgent initialized.")
|
| 123 |
+
|
| 124 |
+
def __call__(self, question: str) -> str:
|
| 125 |
+
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 126 |
+
try:
|
| 127 |
+
answer = manager_agent.run(question)
|
| 128 |
+
return answer
|
| 129 |
+
except Exception as e:
|
| 130 |
+
error = f"An error occurred while processing the question: {e}"
|
| 131 |
+
print(error)
|
| 132 |
+
return error
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
# if __name__ == "__main__":
|
| 136 |
+
# question = """
|
| 137 |
+
# What was the actual enrollment of the Malko competition in 2023?
|
| 138 |
+
# """
|
| 139 |
+
# agent = MyAgent()
|
| 140 |
+
# answer = agent(question)
|
| 141 |
+
# print(f"Answer: {answer}")
|
| 142 |
+
|
app.py
CHANGED
|
@@ -181,4 +181,4 @@ if __name__ == "__main__":
|
|
| 181 |
print("-"*(60 + len(" App Starting ")) + "\n")
|
| 182 |
|
| 183 |
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
| 184 |
-
demo.launch(debug=True, share=
|
|
|
|
| 181 |
print("-"*(60 + len(" App Starting ")) + "\n")
|
| 182 |
|
| 183 |
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
| 184 |
+
demo.launch(debug=True, share=True)
|