Spaces:
Build error
Build error
import os | |
import requests | |
import uuid | |
import mimetypes | |
import tempfile | |
from extensions.visual_qa import visualizer | |
from extensions.text_inspector_tool import TextInspectorTool | |
from agent import create_agent | |
from extensions.miscellaneous import ( | |
get_single_file_description, | |
) | |
def answer_single_question(example: object, model: str, visual_inspection_tool: TextInspectorTool) -> str: | |
document_inspection_tool = TextInspectorTool(model, 100000) | |
augmented_question = """You have one question to answer. It is paramount that you provide a correct answer. | |
Give it all you can: I know for a fact that you have access to all the relevant tools to solve it and find the correct answer (the answer does exist). | |
Failure or 'I cannot answer' or 'None found' will not be tolerated, success will be rewarded. | |
Run verification steps if that's needed, you must make sure you find the correct answer! Here is the task: | |
""" + example["question"] | |
if example["file_name"]: | |
prompt_use_files = "\n\nTo solve the task above, you will have to use this attached file:\n" | |
prompt_use_files += get_single_file_description( | |
example["file_name"], example["question"], visual_inspection_tool, document_inspection_tool | |
) | |
augmented_question += prompt_use_files | |
agent = create_agent(model_id=model) | |
return agent.run(augmented_question) | |
def ask_agent(question: object) -> str: | |
"""Run the agent with the provided question and file.""" | |
q_text = question.get("question", "") | |
print(f"Agent received question (first 50 chars): {q_text[:50]}...") | |
# Build the payload | |
question_object = { | |
"question": q_text, | |
"file_name": None | |
} | |
# If there’s a file name, build the full URL | |
file_name = question.get("file_name", "") | |
task_id = question.get("task_id", "") | |
if file_name: | |
user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0" | |
request_kwargs = { | |
"headers": {"User-Agent": user_agent}, | |
"stream": True, | |
} | |
# Send a HTTP request to the URL | |
file_path = f"https://agents-course-unit4-scoring.hf.space/files/{task_id}" | |
response = requests.get(file_path, **request_kwargs) | |
response.raise_for_status() | |
content_type = response.headers.get("content-type", "") | |
extension = mimetypes.guess_extension(content_type) | |
if extension is None: | |
extension = ".download" | |
fname = str(uuid.uuid4()) + extension | |
temp_dir = tempfile.gettempdir() | |
download_path = os.path.join(temp_dir, fname) | |
print(f"Downloading file from {file_path} to {download_path} with content type {extension}") | |
with open(download_path, "wb") as fh: | |
for chunk in response.iter_content(chunk_size=512): | |
fh.write(chunk) | |
print(f"File downloaded to: {download_path}") | |
question_object["file_name"] = download_path | |
print(f"Question object created: {question_object}") | |
answer = answer_single_question(question_object, model="gemini/gemini-2.5-flash", visual_inspection_tool=visualizer) | |
print(f"Agent returning answer: {answer}") | |
return answer |