Spaces:
Sleeping
Sleeping
Vinay Kerai
commited on
Commit
·
2051cf0
1
Parent(s):
71b5dbb
more tools
Browse files- agent.py +154 -20
- app.py +3 -8
- requirements.txt +5 -1
agent.py
CHANGED
|
@@ -1,17 +1,25 @@
|
|
| 1 |
import re
|
| 2 |
from dotenv import load_dotenv
|
|
|
|
| 3 |
|
|
|
|
| 4 |
from langchain_core.tools import Tool, tool
|
| 5 |
-
from langchain_core.messages import
|
| 6 |
from langgraph.prebuilt import create_react_agent
|
| 7 |
#from langgraph.graph import START, StateGraph
|
| 8 |
-
#from langgraph.prebuilt import tools_condition
|
| 9 |
from langchain_community.tools import DuckDuckGoSearchRun
|
|
|
|
| 10 |
from langchain_experimental.utilities import PythonREPL
|
| 11 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 12 |
#from langchain_ollama import ChatOllama
|
| 13 |
from langfuse.langchain import CallbackHandler
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
load_dotenv()
|
| 16 |
langfuse_handler = CallbackHandler()
|
| 17 |
|
|
@@ -25,6 +33,65 @@ with open('system_prompt.txt', 'r', encoding='utf-8') as f:
|
|
| 25 |
|
| 26 |
# --- Tools ---
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
@tool
|
| 29 |
def search_web(query: str) -> str:
|
| 30 |
"""
|
|
@@ -38,38 +105,105 @@ def search_web(query: str) -> str:
|
|
| 38 |
"""
|
| 39 |
try:
|
| 40 |
search = DuckDuckGoSearchRun()
|
| 41 |
-
search.invoke(query)
|
| 42 |
except Exception as e:
|
| 43 |
return f"An error occured using search_web tool: {e}"
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
func=python_repl.run,
|
| 53 |
-
)
|
| 54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
@tool
|
| 56 |
-
def
|
| 57 |
"""
|
| 58 |
-
|
| 59 |
|
| 60 |
Args:
|
| 61 |
-
|
| 62 |
|
| 63 |
Returns:
|
| 64 |
-
str:
|
| 65 |
"""
|
| 66 |
try:
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
except Exception as e:
|
| 69 |
-
return f"An error occured using
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
# --- LangGraph ---
|
| 75 |
agent = create_react_agent(
|
|
@@ -102,7 +236,7 @@ class GAIAAgent:
|
|
| 102 |
if match:
|
| 103 |
final_answer = match.group(0)
|
| 104 |
else:
|
| 105 |
-
final_answer =
|
| 106 |
|
| 107 |
print(f"Agent returning answer: {final_answer}")
|
| 108 |
return final_answer
|
|
|
|
| 1 |
import re
|
| 2 |
from dotenv import load_dotenv
|
| 3 |
+
import requests
|
| 4 |
|
| 5 |
+
# langchain imports
|
| 6 |
from langchain_core.tools import Tool, tool
|
| 7 |
+
from langchain_core.messages import HumanMessage
|
| 8 |
from langgraph.prebuilt import create_react_agent
|
| 9 |
#from langgraph.graph import START, StateGraph
|
| 10 |
+
#from langgraph.prebuilt import tools_condition
|
| 11 |
from langchain_community.tools import DuckDuckGoSearchRun
|
| 12 |
+
from langchain_community.retrievers import WikipediaRetriever
|
| 13 |
from langchain_experimental.utilities import PythonREPL
|
| 14 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 15 |
#from langchain_ollama import ChatOllama
|
| 16 |
from langfuse.langchain import CallbackHandler
|
| 17 |
|
| 18 |
+
# tool imports
|
| 19 |
+
import pandas as pd
|
| 20 |
+
import whisper
|
| 21 |
+
from youtube_transcript_api import YouTubeTranscriptApi
|
| 22 |
+
|
| 23 |
load_dotenv()
|
| 24 |
langfuse_handler = CallbackHandler()
|
| 25 |
|
|
|
|
| 33 |
|
| 34 |
# --- Tools ---
|
| 35 |
|
| 36 |
+
# python REPL tool
|
| 37 |
+
python_repl = PythonREPL()
|
| 38 |
+
execute_python = Tool(
|
| 39 |
+
name="execute_python",
|
| 40 |
+
description="A Python shell. Use this tool to execute python commands. "
|
| 41 |
+
"Input should be valid python code. "
|
| 42 |
+
"If you want to see the output of a value, you should print it out with `print(...)`.",
|
| 43 |
+
func=python_repl.run,
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
@tool
|
| 47 |
+
def get_youtube_transcript(url: str) -> str:
|
| 48 |
+
"""
|
| 49 |
+
Retrieve the text transcript of a YouTube video
|
| 50 |
+
|
| 51 |
+
Args:
|
| 52 |
+
url (str): link to the YouTube video
|
| 53 |
+
|
| 54 |
+
Returns:
|
| 55 |
+
str: text transcript
|
| 56 |
+
"""
|
| 57 |
+
def extract_video_id(url: str) -> str:
|
| 58 |
+
# extracts video id from youtube url
|
| 59 |
+
patterns = [
|
| 60 |
+
r"v=([a-zA-Z0-9_-]{11})", # regular link
|
| 61 |
+
r"youtu\.be/([a-zA-Z0-9_-]{11})", # shortened link
|
| 62 |
+
r"youtube\.com/embed/([a-zA-Z0-9_-]{11})", # embed link
|
| 63 |
+
]
|
| 64 |
+
for pattern in patterns:
|
| 65 |
+
match = re.search(pattern, url)
|
| 66 |
+
if match:
|
| 67 |
+
return match.group(1)
|
| 68 |
+
raise ValueError("Invalid YouTube URL")
|
| 69 |
+
|
| 70 |
+
try:
|
| 71 |
+
video_id = extract_video_id(url)
|
| 72 |
+
api = YouTubeTranscriptApi()
|
| 73 |
+
transcript = api.fetch(video_id)
|
| 74 |
+
txt = '\n'.join([s.text for s in transcript.snippets])
|
| 75 |
+
return txt
|
| 76 |
+
except Exception as e:
|
| 77 |
+
return f"An error occured using get_youtube_transcript tool: {e}"
|
| 78 |
+
|
| 79 |
+
@tool
|
| 80 |
+
def reverse_string(text: str) -> str:
|
| 81 |
+
"""
|
| 82 |
+
A tool to reverse the order of characters in a text string
|
| 83 |
+
|
| 84 |
+
Args:
|
| 85 |
+
text (str): text string to reverse
|
| 86 |
+
|
| 87 |
+
Returns:
|
| 88 |
+
str: reversed text string
|
| 89 |
+
"""
|
| 90 |
+
try:
|
| 91 |
+
return text[::-1]
|
| 92 |
+
except Exception as e:
|
| 93 |
+
return f"An error occured using reverse_string tool: {e}"
|
| 94 |
+
|
| 95 |
@tool
|
| 96 |
def search_web(query: str) -> str:
|
| 97 |
"""
|
|
|
|
| 105 |
"""
|
| 106 |
try:
|
| 107 |
search = DuckDuckGoSearchRun()
|
| 108 |
+
return search.invoke(query)
|
| 109 |
except Exception as e:
|
| 110 |
return f"An error occured using search_web tool: {e}"
|
| 111 |
|
| 112 |
+
@tool
|
| 113 |
+
def search_wikipedia(query: str) -> str:
|
| 114 |
+
"""
|
| 115 |
+
A tool to perform a search for a query using Wikipedia
|
| 116 |
+
|
| 117 |
+
Args:
|
| 118 |
+
query (str): query to search on Wikipedia
|
|
|
|
|
|
|
| 119 |
|
| 120 |
+
Returns:
|
| 121 |
+
str: wikipedia search result
|
| 122 |
+
"""
|
| 123 |
+
try:
|
| 124 |
+
retriever = WikipediaRetriever()
|
| 125 |
+
return retriever.invoke(query)
|
| 126 |
+
except Exception as e:
|
| 127 |
+
return f"An error occured using search_wiki tool: {e}"
|
| 128 |
+
|
| 129 |
@tool
|
| 130 |
+
def transcribe_audio(url: str) -> str:
|
| 131 |
"""
|
| 132 |
+
A tool to transcribe an audio file (.mp3) using an automatic speech recognition model
|
| 133 |
|
| 134 |
Args:
|
| 135 |
+
url (str): link to audio file (.mp3)
|
| 136 |
|
| 137 |
Returns:
|
| 138 |
+
str: transcript of the audio file
|
| 139 |
"""
|
| 140 |
try:
|
| 141 |
+
# fetch audio file
|
| 142 |
+
response = requests.get(url)
|
| 143 |
+
response.raise_for_status()
|
| 144 |
+
|
| 145 |
+
tmp = 'tmp_audio.mp3'
|
| 146 |
+
with open(tmp, "wb") as f:
|
| 147 |
+
f.write(response.content)
|
| 148 |
+
|
| 149 |
+
# transcribe
|
| 150 |
+
model = whisper.load_model('tiny')
|
| 151 |
+
result = model.transcribe(tmp)
|
| 152 |
+
|
| 153 |
+
return result['text']
|
| 154 |
except Exception as e:
|
| 155 |
+
return f"An error occured using transcribe_audio tool: {e}"
|
| 156 |
+
|
| 157 |
+
def view_py_file(url: str) -> str:
|
| 158 |
+
"""
|
| 159 |
+
A tool to view the contents of a python file (.py)
|
| 160 |
|
| 161 |
+
Args:
|
| 162 |
+
url (str): link to python file (.py)
|
| 163 |
+
|
| 164 |
+
Returns:
|
| 165 |
+
str: contents of python file
|
| 166 |
+
"""
|
| 167 |
+
try:
|
| 168 |
+
# fetch python file
|
| 169 |
+
response = requests.get(url)
|
| 170 |
+
response.raise_for_status()
|
| 171 |
+
|
| 172 |
+
return response.text
|
| 173 |
+
except Exception as e:
|
| 174 |
+
return f"An error occured using view_py_file tool: {e}"
|
| 175 |
+
|
| 176 |
+
def view_xlsx_file(url: str) -> str:
|
| 177 |
+
"""
|
| 178 |
+
A tool to view the contents of an excel file (.xlsx)
|
| 179 |
+
|
| 180 |
+
Args:
|
| 181 |
+
url (str): link to excel file (.xlsx)
|
| 182 |
+
|
| 183 |
+
Returns:
|
| 184 |
+
str: contents of excel file
|
| 185 |
+
"""
|
| 186 |
+
try:
|
| 187 |
+
# fetch python file
|
| 188 |
+
response = requests.get(url)
|
| 189 |
+
response.raise_for_status()
|
| 190 |
+
|
| 191 |
+
tmp = 'tmp.xlsx'
|
| 192 |
+
with open(tmp, "wb") as f:
|
| 193 |
+
f.write(response.content)
|
| 194 |
+
|
| 195 |
+
data = pd.read_excel('tmp.xlsx')
|
| 196 |
+
|
| 197 |
+
return data.to_string()
|
| 198 |
+
except Exception as e:
|
| 199 |
+
return f"An error occured using view_xlsx_file tool: {e}"
|
| 200 |
|
| 201 |
+
# agent toolkit
|
| 202 |
+
tools = [
|
| 203 |
+
execute_python, get_youtube_transcript,
|
| 204 |
+
reverse_string, search_web, transcribe_audio,
|
| 205 |
+
view_py_file, view_xlsx_file
|
| 206 |
+
]
|
| 207 |
|
| 208 |
# --- LangGraph ---
|
| 209 |
agent = create_react_agent(
|
|
|
|
| 236 |
if match:
|
| 237 |
final_answer = match.group(0)
|
| 238 |
else:
|
| 239 |
+
final_answer = final_message
|
| 240 |
|
| 241 |
print(f"Agent returning answer: {final_answer}")
|
| 242 |
return final_answer
|
app.py
CHANGED
|
@@ -83,15 +83,10 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 83 |
continue
|
| 84 |
if file_name:
|
| 85 |
# if question has an attached file, add the URL to the question prompt
|
|
|
|
| 86 |
file_url = f"{api_url}/files/{task_id}"
|
| 87 |
-
question_text += f'\
|
| 88 |
-
|
| 89 |
-
# add file type to prompt also
|
| 90 |
-
try:
|
| 91 |
-
ext = file_name.split('.')[-1]
|
| 92 |
-
question_text += f" (.{ext} file)"
|
| 93 |
-
except Exception as e:
|
| 94 |
-
print(f"Error getting file extension on {task_id}: {e}")
|
| 95 |
try:
|
| 96 |
submitted_answer = agent(question_text)
|
| 97 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
|
|
|
| 83 |
continue
|
| 84 |
if file_name:
|
| 85 |
# if question has an attached file, add the URL to the question prompt
|
| 86 |
+
file_ext = file_name.split('.')[-1]
|
| 87 |
file_url = f"{api_url}/files/{task_id}"
|
| 88 |
+
question_text += f'\nFILE URL ({file_ext}): "{file_url}"'
|
| 89 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
try:
|
| 91 |
submitted_answer = agent(question_text)
|
| 92 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
requirements.txt
CHANGED
|
@@ -8,4 +8,8 @@ langchain_core
|
|
| 8 |
langchain_huggingface
|
| 9 |
langchain_google_genai
|
| 10 |
langchain_experimental
|
| 11 |
-
langfuse
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
langchain_huggingface
|
| 9 |
langchain_google_genai
|
| 10 |
langchain_experimental
|
| 11 |
+
langfuse
|
| 12 |
+
pandas
|
| 13 |
+
openpyxl
|
| 14 |
+
openai-whisper
|
| 15 |
+
youtube_transcript_api
|