Spaces:
Sleeping
Sleeping
aelin commited on
Commit ·
dbb07de
1
Parent(s): f9bd7be
Integrates shared LLM into all agents and improves logging
Browse filesCentralizes LLM instantiation for agents to ensure consistent model use across functionalities. Adds logging statements to tool functions for better traceability and debugging. Removes redundant LLM initialization in the app logic.
_tools.py
CHANGED
|
@@ -8,6 +8,7 @@ from PIL import Image
|
|
| 8 |
from llama_index.tools.duckduckgo import DuckDuckGoSearchToolSpec
|
| 9 |
from huggingface_hub import InferenceClient
|
| 10 |
from llama_index.core.agent.workflow import ReActAgent
|
|
|
|
| 11 |
|
| 12 |
client = InferenceClient(
|
| 13 |
provider="hf-inference",
|
|
@@ -20,13 +21,14 @@ search_tool_spec = DuckDuckGoSearchToolSpec()
|
|
| 20 |
|
| 21 |
def search_tool(query: str) -> str:
|
| 22 |
"""Browse the web using DuckDuckGo."""
|
| 23 |
-
print(f"
|
| 24 |
return search_tool_spec.duckduckgo_full_search(query=query)
|
| 25 |
|
| 26 |
def fetch_file_bytes(task_id: str) -> str | None:
|
| 27 |
"""
|
| 28 |
Fetch a file from the given task ID.
|
| 29 |
"""
|
|
|
|
| 30 |
try:
|
| 31 |
response = requests.get(f"{DEFAULT_API_URL}/files/{task_id}", timeout=15)
|
| 32 |
response.raise_for_status()
|
|
@@ -38,36 +40,43 @@ def fetch_file_bytes(task_id: str) -> str | None:
|
|
| 38 |
|
| 39 |
def bytes_to_image(image_bytes: bytes) -> Image:
|
| 40 |
"""Convert bytes to image URL."""
|
|
|
|
| 41 |
file = Image.open(io.BytesIO(image_bytes))
|
| 42 |
file.save("temp_image.png")
|
| 43 |
return file
|
| 44 |
|
| 45 |
def document_bytes_to_text(doc_bytes: bytes) -> str:
|
| 46 |
"""Convert document bytes to text."""
|
|
|
|
| 47 |
return doc_bytes.decode("utf-8")
|
| 48 |
|
| 49 |
def xlsx_to_text(file_bytes: bytes) -> str:
|
| 50 |
"""Convert XLSX file bytes to text using pandas."""
|
|
|
|
| 51 |
io_bytes = io.BytesIO(file_bytes)
|
| 52 |
df = pd.read_excel(io_bytes, engine='openpyxl')
|
| 53 |
return df.to_string(index=False)
|
| 54 |
|
| 55 |
def extract_text_from_image(image_url: bytes) -> str:
|
| 56 |
"""Extract text from an image using Tesseract."""
|
|
|
|
| 57 |
return client.image_to_text(image_url=image_url, task="image-to-text", model="Salesforce/blip-image-captioning-base").generated_text
|
| 58 |
|
| 59 |
def extract_text_from_csv(file_bytes: bytes) -> str:
|
| 60 |
"""Extract text from a CSV file."""
|
|
|
|
| 61 |
io_bytes = io.BytesIO(file_bytes)
|
| 62 |
df = pd.read_csv(io_bytes)
|
| 63 |
return df.to_string(index=False)
|
| 64 |
|
| 65 |
def extract_text_from_code_file(bytes: bytes) -> str:
|
| 66 |
"""Extract text from a code file."""
|
|
|
|
| 67 |
return bytes.decode("utf-8")
|
| 68 |
|
| 69 |
def extract_text_from_audio_file(file_bytes: bytes) -> str:
|
| 70 |
"""Extract text from an audio file."""
|
|
|
|
| 71 |
return client.automatic_speech_recognition(file_bytes, model="openai/whisper-large-v2").text
|
| 72 |
|
| 73 |
def webpage_to_markdown(url: str) -> str:
|
|
@@ -75,6 +84,7 @@ def webpage_to_markdown(url: str) -> str:
|
|
| 75 |
Access a web page and return its content as markdown.
|
| 76 |
Limits output to 10,000 characters to avoid excessive responses.
|
| 77 |
"""
|
|
|
|
| 78 |
try:
|
| 79 |
response = requests.get(url, timeout=20)
|
| 80 |
response.raise_for_status()
|
|
@@ -89,17 +99,16 @@ def webpage_to_markdown(url: str) -> str:
|
|
| 89 |
return f"Unexpected error: {str(e)}"
|
| 90 |
|
| 91 |
|
|
|
|
| 92 |
# Initialize tools
|
| 93 |
# --- ReActAgent and AgentWorkflow tool declaration ---
|
| 94 |
|
| 95 |
-
# Define agents for each tool (one agent per tool, with a clear description)
|
| 96 |
-
|
| 97 |
search_agent = ReActAgent(
|
| 98 |
name="search_agent",
|
| 99 |
description="Searches the web using DuckDuckGo.",
|
| 100 |
system_prompt="A helpful assistant that can search the web using DuckDuckGo.",
|
| 101 |
tools=[search_tool],
|
| 102 |
-
llm=
|
| 103 |
)
|
| 104 |
|
| 105 |
fetch_file_agent = ReActAgent(
|
|
@@ -107,7 +116,7 @@ fetch_file_agent = ReActAgent(
|
|
| 107 |
description="Fetches a file from a given task ID.",
|
| 108 |
system_prompt="A helpful assistant that can fetch files by task ID.",
|
| 109 |
tools=[fetch_file_bytes],
|
| 110 |
-
llm=
|
| 111 |
)
|
| 112 |
|
| 113 |
bytes_to_image_agent = ReActAgent(
|
|
@@ -115,7 +124,7 @@ bytes_to_image_agent = ReActAgent(
|
|
| 115 |
description="Converts bytes to an image.",
|
| 116 |
system_prompt="A helpful assistant that can convert bytes to an image.",
|
| 117 |
tools=[bytes_to_image],
|
| 118 |
-
llm=
|
| 119 |
)
|
| 120 |
|
| 121 |
document_bytes_to_text_agent = ReActAgent(
|
|
@@ -123,7 +132,7 @@ document_bytes_to_text_agent = ReActAgent(
|
|
| 123 |
description="Converts document bytes to text.",
|
| 124 |
system_prompt="A helpful assistant that can convert document bytes to text.",
|
| 125 |
tools=[document_bytes_to_text],
|
| 126 |
-
llm=
|
| 127 |
)
|
| 128 |
|
| 129 |
xlsx_to_text_agent = ReActAgent(
|
|
@@ -131,7 +140,7 @@ xlsx_to_text_agent = ReActAgent(
|
|
| 131 |
description="Converts XLSX file bytes to text.",
|
| 132 |
system_prompt="A helpful assistant that can convert XLSX file bytes to text.",
|
| 133 |
tools=[xlsx_to_text],
|
| 134 |
-
llm=
|
| 135 |
)
|
| 136 |
|
| 137 |
extract_text_from_image_agent = ReActAgent(
|
|
@@ -139,7 +148,7 @@ extract_text_from_image_agent = ReActAgent(
|
|
| 139 |
description="Extracts text from an image using Tesseract.",
|
| 140 |
system_prompt="A helpful assistant that can extract text from images.",
|
| 141 |
tools=[extract_text_from_image],
|
| 142 |
-
llm=
|
| 143 |
)
|
| 144 |
|
| 145 |
extract_text_from_csv_agent = ReActAgent(
|
|
@@ -147,7 +156,7 @@ extract_text_from_csv_agent = ReActAgent(
|
|
| 147 |
description="Extracts text from a CSV file.",
|
| 148 |
system_prompt="A helpful assistant that can extract text from CSV files.",
|
| 149 |
tools=[extract_text_from_csv],
|
| 150 |
-
llm=
|
| 151 |
)
|
| 152 |
|
| 153 |
extract_text_from_code_file_agent = ReActAgent(
|
|
@@ -155,7 +164,7 @@ extract_text_from_code_file_agent = ReActAgent(
|
|
| 155 |
description="Extracts text from a code file.",
|
| 156 |
system_prompt="A helpful assistant that can extract text from code files.",
|
| 157 |
tools=[extract_text_from_code_file],
|
| 158 |
-
llm=
|
| 159 |
)
|
| 160 |
|
| 161 |
extract_text_from_audio_file_agent = ReActAgent(
|
|
@@ -163,7 +172,7 @@ extract_text_from_audio_file_agent = ReActAgent(
|
|
| 163 |
description="Extracts text from an audio file.",
|
| 164 |
system_prompt="A helpful assistant that can extract text from audio files.",
|
| 165 |
tools=[extract_text_from_audio_file],
|
| 166 |
-
llm=
|
| 167 |
)
|
| 168 |
|
| 169 |
webpage_to_markdown_agent = ReActAgent(
|
|
@@ -171,6 +180,5 @@ webpage_to_markdown_agent = ReActAgent(
|
|
| 171 |
description="Accesses a web page by URL and returns the content as markdown.",
|
| 172 |
system_prompt="A helpful assistant that can access web pages and return markdown.",
|
| 173 |
tools=[webpage_to_markdown],
|
| 174 |
-
llm=
|
| 175 |
)
|
| 176 |
-
|
|
|
|
| 8 |
from llama_index.tools.duckduckgo import DuckDuckGoSearchToolSpec
|
| 9 |
from huggingface_hub import InferenceClient
|
| 10 |
from llama_index.core.agent.workflow import ReActAgent
|
| 11 |
+
from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI
|
| 12 |
|
| 13 |
client = InferenceClient(
|
| 14 |
provider="hf-inference",
|
|
|
|
| 21 |
|
| 22 |
def search_tool(query: str) -> str:
|
| 23 |
"""Browse the web using DuckDuckGo."""
|
| 24 |
+
print(f"Calling search_tool with query: {query}")
|
| 25 |
return search_tool_spec.duckduckgo_full_search(query=query)
|
| 26 |
|
| 27 |
def fetch_file_bytes(task_id: str) -> str | None:
|
| 28 |
"""
|
| 29 |
Fetch a file from the given task ID.
|
| 30 |
"""
|
| 31 |
+
print(f"Calling fetch_file_bytes with task_id: {task_id}")
|
| 32 |
try:
|
| 33 |
response = requests.get(f"{DEFAULT_API_URL}/files/{task_id}", timeout=15)
|
| 34 |
response.raise_for_status()
|
|
|
|
| 40 |
|
| 41 |
def bytes_to_image(image_bytes: bytes) -> Image:
|
| 42 |
"""Convert bytes to image URL."""
|
| 43 |
+
print("Calling bytes_to_image")
|
| 44 |
file = Image.open(io.BytesIO(image_bytes))
|
| 45 |
file.save("temp_image.png")
|
| 46 |
return file
|
| 47 |
|
| 48 |
def document_bytes_to_text(doc_bytes: bytes) -> str:
|
| 49 |
"""Convert document bytes to text."""
|
| 50 |
+
print("Calling document_bytes_to_text")
|
| 51 |
return doc_bytes.decode("utf-8")
|
| 52 |
|
| 53 |
def xlsx_to_text(file_bytes: bytes) -> str:
|
| 54 |
"""Convert XLSX file bytes to text using pandas."""
|
| 55 |
+
print("Calling xlsx_to_text")
|
| 56 |
io_bytes = io.BytesIO(file_bytes)
|
| 57 |
df = pd.read_excel(io_bytes, engine='openpyxl')
|
| 58 |
return df.to_string(index=False)
|
| 59 |
|
| 60 |
def extract_text_from_image(image_url: bytes) -> str:
|
| 61 |
"""Extract text from an image using Tesseract."""
|
| 62 |
+
print("Calling extract_text_from_image")
|
| 63 |
return client.image_to_text(image_url=image_url, task="image-to-text", model="Salesforce/blip-image-captioning-base").generated_text
|
| 64 |
|
| 65 |
def extract_text_from_csv(file_bytes: bytes) -> str:
|
| 66 |
"""Extract text from a CSV file."""
|
| 67 |
+
print("Calling extract_text_from_csv")
|
| 68 |
io_bytes = io.BytesIO(file_bytes)
|
| 69 |
df = pd.read_csv(io_bytes)
|
| 70 |
return df.to_string(index=False)
|
| 71 |
|
| 72 |
def extract_text_from_code_file(bytes: bytes) -> str:
|
| 73 |
"""Extract text from a code file."""
|
| 74 |
+
print("Calling extract_text_from_code_file")
|
| 75 |
return bytes.decode("utf-8")
|
| 76 |
|
| 77 |
def extract_text_from_audio_file(file_bytes: bytes) -> str:
|
| 78 |
"""Extract text from an audio file."""
|
| 79 |
+
print("Calling extract_text_from_audio_file")
|
| 80 |
return client.automatic_speech_recognition(file_bytes, model="openai/whisper-large-v2").text
|
| 81 |
|
| 82 |
def webpage_to_markdown(url: str) -> str:
|
|
|
|
| 84 |
Access a web page and return its content as markdown.
|
| 85 |
Limits output to 10,000 characters to avoid excessive responses.
|
| 86 |
"""
|
| 87 |
+
print(f"Calling webpage_to_markdown with url: {url}")
|
| 88 |
try:
|
| 89 |
response = requests.get(url, timeout=20)
|
| 90 |
response.raise_for_status()
|
|
|
|
| 99 |
return f"Unexpected error: {str(e)}"
|
| 100 |
|
| 101 |
|
| 102 |
+
llm = HuggingFaceInferenceAPI(model_name="Qwen/Qwen2.5-Coder-32B-Instruct")
|
| 103 |
# Initialize tools
|
| 104 |
# --- ReActAgent and AgentWorkflow tool declaration ---
|
| 105 |
|
|
|
|
|
|
|
| 106 |
search_agent = ReActAgent(
|
| 107 |
name="search_agent",
|
| 108 |
description="Searches the web using DuckDuckGo.",
|
| 109 |
system_prompt="A helpful assistant that can search the web using DuckDuckGo.",
|
| 110 |
tools=[search_tool],
|
| 111 |
+
llm=llm,
|
| 112 |
)
|
| 113 |
|
| 114 |
fetch_file_agent = ReActAgent(
|
|
|
|
| 116 |
description="Fetches a file from a given task ID.",
|
| 117 |
system_prompt="A helpful assistant that can fetch files by task ID.",
|
| 118 |
tools=[fetch_file_bytes],
|
| 119 |
+
llm=llm,
|
| 120 |
)
|
| 121 |
|
| 122 |
bytes_to_image_agent = ReActAgent(
|
|
|
|
| 124 |
description="Converts bytes to an image.",
|
| 125 |
system_prompt="A helpful assistant that can convert bytes to an image.",
|
| 126 |
tools=[bytes_to_image],
|
| 127 |
+
llm=llm,
|
| 128 |
)
|
| 129 |
|
| 130 |
document_bytes_to_text_agent = ReActAgent(
|
|
|
|
| 132 |
description="Converts document bytes to text.",
|
| 133 |
system_prompt="A helpful assistant that can convert document bytes to text.",
|
| 134 |
tools=[document_bytes_to_text],
|
| 135 |
+
llm=llm,
|
| 136 |
)
|
| 137 |
|
| 138 |
xlsx_to_text_agent = ReActAgent(
|
|
|
|
| 140 |
description="Converts XLSX file bytes to text.",
|
| 141 |
system_prompt="A helpful assistant that can convert XLSX file bytes to text.",
|
| 142 |
tools=[xlsx_to_text],
|
| 143 |
+
llm=llm,
|
| 144 |
)
|
| 145 |
|
| 146 |
extract_text_from_image_agent = ReActAgent(
|
|
|
|
| 148 |
description="Extracts text from an image using Tesseract.",
|
| 149 |
system_prompt="A helpful assistant that can extract text from images.",
|
| 150 |
tools=[extract_text_from_image],
|
| 151 |
+
llm=llm,
|
| 152 |
)
|
| 153 |
|
| 154 |
extract_text_from_csv_agent = ReActAgent(
|
|
|
|
| 156 |
description="Extracts text from a CSV file.",
|
| 157 |
system_prompt="A helpful assistant that can extract text from CSV files.",
|
| 158 |
tools=[extract_text_from_csv],
|
| 159 |
+
llm=llm,
|
| 160 |
)
|
| 161 |
|
| 162 |
extract_text_from_code_file_agent = ReActAgent(
|
|
|
|
| 164 |
description="Extracts text from a code file.",
|
| 165 |
system_prompt="A helpful assistant that can extract text from code files.",
|
| 166 |
tools=[extract_text_from_code_file],
|
| 167 |
+
llm=llm,
|
| 168 |
)
|
| 169 |
|
| 170 |
extract_text_from_audio_file_agent = ReActAgent(
|
|
|
|
| 172 |
description="Extracts text from an audio file.",
|
| 173 |
system_prompt="A helpful assistant that can extract text from audio files.",
|
| 174 |
tools=[extract_text_from_audio_file],
|
| 175 |
+
llm=llm,
|
| 176 |
)
|
| 177 |
|
| 178 |
webpage_to_markdown_agent = ReActAgent(
|
|
|
|
| 180 |
description="Accesses a web page by URL and returns the content as markdown.",
|
| 181 |
system_prompt="A helpful assistant that can access web pages and return markdown.",
|
| 182 |
tools=[webpage_to_markdown],
|
| 183 |
+
llm=llm,
|
| 184 |
)
|
|
|
app.py
CHANGED
|
@@ -4,10 +4,10 @@ import gradio as gr
|
|
| 4 |
import requests
|
| 5 |
import pandas as pd
|
| 6 |
from _types import Questions, Question, UserScore
|
| 7 |
-
from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI
|
| 8 |
from llama_index.core.agent.workflow import AgentWorkflow
|
| 9 |
from llama_index.core.workflow import Context
|
| 10 |
from _tools import (
|
|
|
|
| 11 |
search_agent,
|
| 12 |
fetch_file_agent,
|
| 13 |
bytes_to_image_agent,
|
|
@@ -33,9 +33,6 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 33 |
class BasicAgent:
|
| 34 |
def __init__(self):
|
| 35 |
print("BasicAgent initialized.")
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
llm = HuggingFaceInferenceAPI(model_name="Qwen/Qwen2.5-Coder-32B-Instruct")
|
| 39 |
|
| 40 |
agent = AgentWorkflow(
|
| 41 |
agents=[
|
|
@@ -51,7 +48,6 @@ class BasicAgent:
|
|
| 51 |
webpage_to_markdown_agent,
|
| 52 |
],
|
| 53 |
root_agent="search_agent",
|
| 54 |
-
llm=llm,
|
| 55 |
verbose=True,
|
| 56 |
system_prompt="""
|
| 57 |
You are a general AI assistant. I will ask you a question. Think carefully and give your answer straight away as asked in the question or
|
|
|
|
| 4 |
import requests
|
| 5 |
import pandas as pd
|
| 6 |
from _types import Questions, Question, UserScore
|
|
|
|
| 7 |
from llama_index.core.agent.workflow import AgentWorkflow
|
| 8 |
from llama_index.core.workflow import Context
|
| 9 |
from _tools import (
|
| 10 |
+
llm,
|
| 11 |
search_agent,
|
| 12 |
fetch_file_agent,
|
| 13 |
bytes_to_image_agent,
|
|
|
|
| 33 |
class BasicAgent:
|
| 34 |
def __init__(self):
|
| 35 |
print("BasicAgent initialized.")
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
agent = AgentWorkflow(
|
| 38 |
agents=[
|
|
|
|
| 48 |
webpage_to_markdown_agent,
|
| 49 |
],
|
| 50 |
root_agent="search_agent",
|
|
|
|
| 51 |
verbose=True,
|
| 52 |
system_prompt="""
|
| 53 |
You are a general AI assistant. I will ask you a question. Think carefully and give your answer straight away as asked in the question or
|