Spaces:
Sleeping
Sleeping
Add tools for file reading and audio transcription; update .gitignore and requirements
Browse files- .gitignore +1 -0
- app.py +6 -3
- requirements.txt +4 -0
- tools.py +109 -0
.gitignore
CHANGED
|
@@ -1 +1,2 @@
|
|
| 1 |
.DS_STORE
|
|
|
|
|
|
| 1 |
.DS_STORE
|
| 2 |
+
*.pyc
|
app.py
CHANGED
|
@@ -10,6 +10,7 @@ from llama_index.tools.wikipedia import WikipediaToolSpec
|
|
| 10 |
from llama_index.core.tools import FunctionTool
|
| 11 |
from llama_index.tools.duckduckgo import DuckDuckGoSearchToolSpec
|
| 12 |
|
|
|
|
| 13 |
|
| 14 |
# (Keep Constants as is)
|
| 15 |
# --- Constants ---
|
|
@@ -34,9 +35,8 @@ Here is the question:
|
|
| 34 |
llm = HuggingFaceInferenceAPI(model_name='Qwen/Qwen3-32B', num_output=4096, temperature=0.01)
|
| 35 |
wikipedia_tools = WikipediaToolSpec().to_tool_list()
|
| 36 |
search_tool = FunctionTool.from_defaults(DuckDuckGoSearchToolSpec().duckduckgo_full_search)
|
| 37 |
-
|
| 38 |
agent = AgentWorkflow.from_tools_or_functions(
|
| 39 |
-
tools_or_functions=[search_tool],
|
| 40 |
llm=llm,
|
| 41 |
#system_prompt=system_prompt,
|
| 42 |
verbose=False,
|
|
@@ -102,7 +102,10 @@ async def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 102 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 103 |
continue
|
| 104 |
try:
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
| 106 |
submitted_answer = r.response.blocks[0].text
|
| 107 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 108 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
|
|
|
| 10 |
from llama_index.core.tools import FunctionTool
|
| 11 |
from llama_index.tools.duckduckgo import DuckDuckGoSearchToolSpec
|
| 12 |
|
| 13 |
+
from tools import read_python_file_tool, audio_to_text_tool, read_xlsx_file_tool, read_png_file_tool
|
| 14 |
|
| 15 |
# (Keep Constants as is)
|
| 16 |
# --- Constants ---
|
|
|
|
| 35 |
llm = HuggingFaceInferenceAPI(model_name='Qwen/Qwen3-32B', num_output=4096, temperature=0.01)
|
| 36 |
wikipedia_tools = WikipediaToolSpec().to_tool_list()
|
| 37 |
search_tool = FunctionTool.from_defaults(DuckDuckGoSearchToolSpec().duckduckgo_full_search)
|
|
|
|
| 38 |
agent = AgentWorkflow.from_tools_or_functions(
|
| 39 |
+
tools_or_functions=[search_tool, read_python_file_tool, audio_to_text_tool, read_xlsx_file_tool, read_png_file_tool] + wikipedia_tools,
|
| 40 |
llm=llm,
|
| 41 |
#system_prompt=system_prompt,
|
| 42 |
verbose=False,
|
|
|
|
| 102 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 103 |
continue
|
| 104 |
try:
|
| 105 |
+
prompt = system_prompt + question_text
|
| 106 |
+
if item.get("file_name", "") != "":
|
| 107 |
+
prompt += f'\nHere is the attached file you might need to answer the question: files/{item.get("file_name", "")}'
|
| 108 |
+
r = await agent.run(prompt)
|
| 109 |
submitted_answer = r.response.blocks[0].text
|
| 110 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 111 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
requirements.txt
CHANGED
|
@@ -8,3 +8,7 @@ llama-index-llms-huggingface-api
|
|
| 8 |
llama-index-tools-duckduckgo
|
| 9 |
llama-index-retrievers-bm25
|
| 10 |
llama-index-tools-wikipedia
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
llama-index-tools-duckduckgo
|
| 9 |
llama-index-retrievers-bm25
|
| 10 |
llama-index-tools-wikipedia
|
| 11 |
+
llama-index-tools-code-interpreter
|
| 12 |
+
openai-whisper
|
| 13 |
+
pandas
|
| 14 |
+
openpyxl
|
tools.py
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from llama_index.core.tools import FunctionTool
|
| 2 |
+
import whisper
|
| 3 |
+
import pandas as pd
|
| 4 |
+
from PIL import Image
|
| 5 |
+
|
| 6 |
+
def read_python_file(file_path: str) -> bytes:
|
| 7 |
+
"""
|
| 8 |
+
Reads the content of a .py file given its file path.
|
| 9 |
+
|
| 10 |
+
Args:
|
| 11 |
+
file_path (str): Path to the file.
|
| 12 |
+
|
| 13 |
+
Returns:
|
| 14 |
+
bytes: Content of the file as string.
|
| 15 |
+
"""
|
| 16 |
+
try:
|
| 17 |
+
with open(file_path, 'r') as file:
|
| 18 |
+
return file.read()
|
| 19 |
+
except Exception as e:
|
| 20 |
+
raise ValueError(f"Error reading file at {file_path}: {e}")
|
| 21 |
+
|
| 22 |
+
read_python_file_tool = FunctionTool.from_defaults(
|
| 23 |
+
fn=read_python_file,
|
| 24 |
+
name="read_file_content",
|
| 25 |
+
description="Reads the content of a .py file given its file path."
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def audio_to_text(file_path: str) -> str:
|
| 30 |
+
"""
|
| 31 |
+
Converts an audio file to text using OpenAI Whisper.
|
| 32 |
+
|
| 33 |
+
Args:
|
| 34 |
+
file_path (str): Path to the audio file.
|
| 35 |
+
|
| 36 |
+
Returns:
|
| 37 |
+
str: Transcribed text from the audio file.
|
| 38 |
+
"""
|
| 39 |
+
try:
|
| 40 |
+
# Load the Whisper model
|
| 41 |
+
model = whisper.load_model("base")
|
| 42 |
+
|
| 43 |
+
# Transcribe the audio file
|
| 44 |
+
result = model.transcribe(file_path)
|
| 45 |
+
|
| 46 |
+
# Return the transcribed text
|
| 47 |
+
return result['text']
|
| 48 |
+
except Exception as e:
|
| 49 |
+
raise ValueError(f"Error processing audio file at {file_path}: {e}")
|
| 50 |
+
|
| 51 |
+
audio_to_text_tool = FunctionTool.from_defaults(
|
| 52 |
+
fn=audio_to_text,
|
| 53 |
+
name="audio_to_text",
|
| 54 |
+
description="Converts an audio file to text using OpenAI Whisper."
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def read_xlsx_file(file_path: str) -> str:
|
| 59 |
+
"""
|
| 60 |
+
Reads the content of an .xlsx file and returns it as a string.
|
| 61 |
+
|
| 62 |
+
Args:
|
| 63 |
+
file_path (str): Path to the .xlsx file.
|
| 64 |
+
|
| 65 |
+
Returns:
|
| 66 |
+
str: Content of the .xlsx file as a string.
|
| 67 |
+
"""
|
| 68 |
+
try:
|
| 69 |
+
# Read the Excel file into a DataFrame
|
| 70 |
+
df = pd.read_excel(file_path)
|
| 71 |
+
|
| 72 |
+
# Convert the DataFrame to a string
|
| 73 |
+
return df.to_string(index=False)
|
| 74 |
+
except Exception as e:
|
| 75 |
+
raise ValueError(f"Error reading .xlsx file at {file_path}: {e}")
|
| 76 |
+
|
| 77 |
+
read_xlsx_file_tool = FunctionTool.from_defaults(
|
| 78 |
+
fn=read_xlsx_file,
|
| 79 |
+
name="read_xlsx_file",
|
| 80 |
+
description="Reads the content of an .xlsx file and returns it as a string."
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
def read_png_file(file_path: str) -> list:
|
| 85 |
+
"""
|
| 86 |
+
Reads the content of a .png file and returns its RGB pixel representation.
|
| 87 |
+
|
| 88 |
+
Args:
|
| 89 |
+
file_path (str): Path to the .png file.
|
| 90 |
+
|
| 91 |
+
Returns:
|
| 92 |
+
list: A 2D list representing the RGB pixel values of the image.
|
| 93 |
+
"""
|
| 94 |
+
try:
|
| 95 |
+
# Open the image file
|
| 96 |
+
image = Image.open(file_path).convert("RGB")
|
| 97 |
+
|
| 98 |
+
# Convert the image to a 2D list of RGB tuples
|
| 99 |
+
rgb_pixels = list(image.getdata())
|
| 100 |
+
width, height = image.size
|
| 101 |
+
return [rgb_pixels[i * width:(i + 1) * width] for i in range(height)]
|
| 102 |
+
except Exception as e:
|
| 103 |
+
raise ValueError(f"Error reading .png file at {file_path}: {e}")
|
| 104 |
+
|
| 105 |
+
read_png_file_tool = FunctionTool.from_defaults(
|
| 106 |
+
fn=read_png_file,
|
| 107 |
+
name="read_png_file",
|
| 108 |
+
description="Reads the content of a .png file and returns its RGB pixel representation."
|
| 109 |
+
)
|