Spaces:
Runtime error
Runtime error
Mike Jay
commited on
Commit
·
02c9b56
1
Parent(s):
e222391
files with questions data
Browse files- .gitignore +7 -0
- agents/basic_agent.py +9 -4
- agents/manager_agent.py +23 -0
- app.py +12 -10
- questions.py +16 -1
- requirements.txt +4 -0
.gitignore
CHANGED
|
@@ -1,6 +1,13 @@
|
|
| 1 |
|
| 2 |
# Omit HF Questions Data
|
| 3 |
data/*.json
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
# omit IDE configs
|
| 6 |
.vscode/settings.json
|
|
|
|
| 1 |
|
| 2 |
# Omit HF Questions Data
|
| 3 |
data/*.json
|
| 4 |
+
data/1f975693-876d-457b-a649-393859e79bf3.mp3
|
| 5 |
+
data/7bd855d8-463d-4ed5-93ca-5fe35145f733.xlsx
|
| 6 |
+
data/99c9cc74-fdc8-46c6-8f8d-3ce2d3bfeea3.mp3
|
| 7 |
+
data/cca530fc-4052-43b2-b130-b30968d8aa44.png
|
| 8 |
+
data/f918266a-b3e0-4914-865d-4faa564f1aef.py
|
| 9 |
+
|
| 10 |
+
|
| 11 |
|
| 12 |
# omit IDE configs
|
| 13 |
.vscode/settings.json
|
agents/basic_agent.py
CHANGED
|
@@ -1,14 +1,19 @@
|
|
| 1 |
"""Base Agent"""
|
| 2 |
|
|
|
|
|
|
|
| 3 |
|
| 4 |
class BasicAgent: # pylint: disable=too-few-public-methods
|
| 5 |
"""Base Agent for Evaluation"""
|
| 6 |
|
| 7 |
def __init__(self):
|
|
|
|
| 8 |
print("BasicAgent initialized.")
|
| 9 |
|
| 10 |
def __call__(self, question: str) -> str:
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
| 1 |
"""Base Agent"""
|
| 2 |
|
| 3 |
+
from agents.manager_agent import manager_agent_factory
|
| 4 |
+
|
| 5 |
|
| 6 |
class BasicAgent: # pylint: disable=too-few-public-methods
|
| 7 |
"""Base Agent for Evaluation"""
|
| 8 |
|
| 9 |
def __init__(self):
|
| 10 |
+
self.manager_agent = manager_agent_factory()
|
| 11 |
print("BasicAgent initialized.")
|
| 12 |
|
| 13 |
def __call__(self, question: str) -> str:
|
| 14 |
+
if not self.manager_agent:
|
| 15 |
+
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 16 |
+
fixed_answer = "This is a default answer."
|
| 17 |
+
print(f"Agent returning fixed answer: {fixed_answer}")
|
| 18 |
+
return fixed_answer
|
| 19 |
+
return self.manager_agent.run(question)
|
agents/manager_agent.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
""" "Manger Agent"""
|
| 2 |
+
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
from smolagents import CodeAgent, OpenAIServerModel, DuckDuckGoSearchTool
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
MODEL_ID = os.getenv("MANAGER_MODEL_ID")
|
| 9 |
+
BASE_URL = os.getenv("MANGER_BASE_URL")
|
| 10 |
+
API_KEY = os.getenv("OPENAI_API_KEY")
|
| 11 |
+
API_BASE = "/".join([BASE_URL, "v1"])
|
| 12 |
+
|
| 13 |
+
model = OpenAIServerModel(model_id=MODEL_ID, api_base=API_BASE, api_key=API_KEY)
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def manager_agent_factory() -> CodeAgent:
|
| 17 |
+
"""Manager Agent Instance"""
|
| 18 |
+
return CodeAgent(
|
| 19 |
+
model=model,
|
| 20 |
+
tools=[DuckDuckGoSearchTool(verify=False)],
|
| 21 |
+
planning_interval=3,
|
| 22 |
+
max_steps=20,
|
| 23 |
+
)
|
app.py
CHANGED
|
@@ -12,8 +12,7 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 12 |
API_URL = DEFAULT_API_URL
|
| 13 |
QUESTIONS_URL = f"{API_URL}/questions"
|
| 14 |
SUBMIT_URL = f"{API_URL}/submit"
|
| 15 |
-
|
| 16 |
-
SAFETY_OFF = os.getenv("SAFETY_OFF")
|
| 17 |
|
| 18 |
SPACE_ID = os.getenv("SPACE_ID")
|
| 19 |
if SPACE_ID:
|
|
@@ -25,6 +24,13 @@ def _check_agent_dependent_environment_variables() -> str:
|
|
| 25 |
openai_api_key = os.getenv("OPENAI_API_KEY")
|
| 26 |
if not openai_api_key:
|
| 27 |
return "OPENAI_API_KEY environment variable must be set"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
return None
|
| 29 |
|
| 30 |
|
|
@@ -46,7 +52,7 @@ def run_and_submit_all(
|
|
| 46 |
if error_message:
|
| 47 |
return error_message, None
|
| 48 |
|
| 49 |
-
questions_data = get_questions_data(questions_url=QUESTIONS_URL)
|
| 50 |
if not questions_data:
|
| 51 |
print("Questions list is empty.")
|
| 52 |
return "Questions list is empty or invalid format.", None
|
|
@@ -67,13 +73,9 @@ def run_and_submit_all(
|
|
| 67 |
print(status_update)
|
| 68 |
print(f"Submitting {len(answers_payload)} answers to: {SUBMIT_URL}")
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
)
|
| 74 |
-
else:
|
| 75 |
-
final_status = "\n\tSAFETY_OFF bypass active\n"
|
| 76 |
-
error_message = None
|
| 77 |
|
| 78 |
if final_status:
|
| 79 |
return final_status, results_df
|
|
|
|
| 12 |
API_URL = DEFAULT_API_URL
|
| 13 |
QUESTIONS_URL = f"{API_URL}/questions"
|
| 14 |
SUBMIT_URL = f"{API_URL}/submit"
|
| 15 |
+
FILES_URL = f"{API_URL}/files"
|
|
|
|
| 16 |
|
| 17 |
SPACE_ID = os.getenv("SPACE_ID")
|
| 18 |
if SPACE_ID:
|
|
|
|
| 24 |
openai_api_key = os.getenv("OPENAI_API_KEY")
|
| 25 |
if not openai_api_key:
|
| 26 |
return "OPENAI_API_KEY environment variable must be set"
|
| 27 |
+
manger_model_id = os.getenv("MANAGER_MODEL_ID")
|
| 28 |
+
if not manger_model_id:
|
| 29 |
+
return "MANAGER_MODEL_ID environment variable must be set"
|
| 30 |
+
manager_base_url = os.getenv("MANGER_BASE_URL")
|
| 31 |
+
if not manager_base_url:
|
| 32 |
+
return "MANGER_BASE_URL environment variable must be set"
|
| 33 |
+
|
| 34 |
return None
|
| 35 |
|
| 36 |
|
|
|
|
| 52 |
if error_message:
|
| 53 |
return error_message, None
|
| 54 |
|
| 55 |
+
questions_data = get_questions_data(questions_url=QUESTIONS_URL, files_url=FILES_URL)
|
| 56 |
if not questions_data:
|
| 57 |
print("Questions list is empty.")
|
| 58 |
return "Questions list is empty or invalid format.", None
|
|
|
|
| 73 |
print(status_update)
|
| 74 |
print(f"Submitting {len(answers_payload)} answers to: {SUBMIT_URL}")
|
| 75 |
|
| 76 |
+
final_status, error_message = submit_answers(
|
| 77 |
+
submit_url=SUBMIT_URL, submission_data=submission_data
|
| 78 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
if final_status:
|
| 81 |
return final_status, results_df
|
questions.py
CHANGED
|
@@ -7,6 +7,20 @@ import requests
|
|
| 7 |
QUESTIONS_DATA_FILE = "data/questions_data.json"
|
| 8 |
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
def _is_exists_questions_data_file() -> bool:
|
| 11 |
if os.path.exists(QUESTIONS_DATA_FILE):
|
| 12 |
print(f"The file '{QUESTIONS_DATA_FILE}' exists.")
|
|
@@ -52,7 +66,7 @@ def _json_question_data_from_res(res: requests.Response) -> dict:
|
|
| 52 |
return None
|
| 53 |
|
| 54 |
|
| 55 |
-
def get_questions_data(questions_url: str) -> dict:
|
| 56 |
"""Get Questions Data"""
|
| 57 |
if _is_exists_questions_data_file():
|
| 58 |
questions_data = _read_question_data_from_file()
|
|
@@ -62,5 +76,6 @@ def get_questions_data(questions_url: str) -> dict:
|
|
| 62 |
questions_data = _json_question_data_from_res(res)
|
| 63 |
if questions_data:
|
| 64 |
_write_question_data_to_file(questions_data=questions_data)
|
|
|
|
| 65 |
return questions_data
|
| 66 |
return None
|
|
|
|
| 7 |
QUESTIONS_DATA_FILE = "data/questions_data.json"
|
| 8 |
|
| 9 |
|
| 10 |
+
def _get_task_files(files_url: str, questions_data: dict):
|
| 11 |
+
for item in questions_data:
|
| 12 |
+
task_id = item.get("task_id")
|
| 13 |
+
file_name = item.get("file_name")
|
| 14 |
+
local_file = "/".join(["data", file_name])
|
| 15 |
+
if task_id and file_name:
|
| 16 |
+
file_url = "/".join([files_url, task_id])
|
| 17 |
+
response = requests.get(file_url, stream=True)
|
| 18 |
+
response.raise_for_status()
|
| 19 |
+
with open(local_file, "wb") as f:
|
| 20 |
+
f.write(response.content)
|
| 21 |
+
print(f"File '{file_name}' downloaded successfully.")
|
| 22 |
+
|
| 23 |
+
|
| 24 |
def _is_exists_questions_data_file() -> bool:
|
| 25 |
if os.path.exists(QUESTIONS_DATA_FILE):
|
| 26 |
print(f"The file '{QUESTIONS_DATA_FILE}' exists.")
|
|
|
|
| 66 |
return None
|
| 67 |
|
| 68 |
|
| 69 |
+
def get_questions_data(questions_url: str, files_url: str) -> dict:
|
| 70 |
"""Get Questions Data"""
|
| 71 |
if _is_exists_questions_data_file():
|
| 72 |
questions_data = _read_question_data_from_file()
|
|
|
|
| 76 |
questions_data = _json_question_data_from_res(res)
|
| 77 |
if questions_data:
|
| 78 |
_write_question_data_to_file(questions_data=questions_data)
|
| 79 |
+
_get_task_files(files_url=files_url, questions_data=questions_data)
|
| 80 |
return questions_data
|
| 81 |
return None
|
requirements.txt
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
|
|
|
|
|
| 1 |
gradio[oauth]
|
| 2 |
requests
|
|
|
|
|
|
|
| 3 |
|
|
|
|
| 1 |
+
duckduckgo_search
|
| 2 |
+
gradio
|
| 3 |
gradio[oauth]
|
| 4 |
requests
|
| 5 |
+
smolagents
|
| 6 |
+
smolagents[openai]
|
| 7 |
|