Spaces:
Running
Running
feat(app): config agent
Browse files
app.py
CHANGED
|
@@ -1,10 +1,25 @@
|
|
| 1 |
import os
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
-
import requests
|
| 4 |
-
import inspect
|
| 5 |
import pandas as pd
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
# (Keep Constants as is)
|
| 8 |
# --- Constants ---
|
| 9 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 10 |
|
|
@@ -13,10 +28,49 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 13 |
class BasicAgent:
|
| 14 |
def __init__(self):
|
| 15 |
print("BasicAgent initialized.")
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
print(f"Agent returning fixed answer: {fixed_answer}")
|
|
|
|
|
|
|
| 20 |
return fixed_answer
|
| 21 |
|
| 22 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
@@ -80,7 +134,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 80 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 81 |
continue
|
| 82 |
try:
|
| 83 |
-
submitted_answer = agent(question_text)
|
| 84 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 85 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 86 |
except Exception as e:
|
|
@@ -91,7 +145,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 91 |
print("Agent did not produce any answers to submit.")
|
| 92 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 93 |
|
| 94 |
-
# 4. Prepare Submission
|
| 95 |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
| 96 |
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
| 97 |
print(status_update)
|
|
@@ -193,4 +247,4 @@ if __name__ == "__main__":
|
|
| 193 |
print("-"*(60 + len(" App Starting ")) + "\n")
|
| 194 |
|
| 195 |
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
| 196 |
-
demo.launch(debug=True, share=False)
|
|
|
|
| 1 |
import os
|
| 2 |
+
import time
|
| 3 |
+
|
| 4 |
import gradio as gr
|
|
|
|
|
|
|
| 5 |
import pandas as pd
|
| 6 |
+
import requests
|
| 7 |
+
from smolagents import (
|
| 8 |
+
CodeAgent,
|
| 9 |
+
DuckDuckGoSearchTool,
|
| 10 |
+
LiteLLMModel,
|
| 11 |
+
WikipediaSearchTool,
|
| 12 |
+
FinalAnswerTool
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
from tools import (
|
| 16 |
+
analyze_audio_file,
|
| 17 |
+
analyze_image_file,
|
| 18 |
+
analyze_xlsx_file,
|
| 19 |
+
analyze_youtube_video,
|
| 20 |
+
download_file_of_task_id,
|
| 21 |
+
)
|
| 22 |
|
|
|
|
| 23 |
# --- Constants ---
|
| 24 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 25 |
|
|
|
|
| 28 |
class BasicAgent:
|
| 29 |
def __init__(self):
|
| 30 |
print("BasicAgent initialized.")
|
| 31 |
+
|
| 32 |
+
# Initialize the model
|
| 33 |
+
model = LiteLLMModel(model_id=os.getenv("MODEL_ID"),
|
| 34 |
+
api_key=os.getenv("API_KEY"))
|
| 35 |
+
|
| 36 |
+
# Initialize the searchs tool
|
| 37 |
+
duck_duck_go_search_tool = DuckDuckGoSearchTool()
|
| 38 |
+
wikipedia_search_tool = WikipediaSearchTool()
|
| 39 |
+
|
| 40 |
+
# Initialize Agent
|
| 41 |
+
self.agent = CodeAgent(
|
| 42 |
+
model = model,
|
| 43 |
+
tools=[download_file_of_task_id, analyze_audio_file, analyze_image_file,
|
| 44 |
+
analyze_xlsx_file, duck_duck_go_search_tool, wikipedia_search_tool,
|
| 45 |
+
analyze_youtube_video, FinalAnswerTool()]
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
def __call__(self, question: str, task_id: str) -> str:
|
| 49 |
+
task = f"""
|
| 50 |
+
You are a general AI assistant.
|
| 51 |
+
I will ask you a question and you can use 8 steps to answer it.
|
| 52 |
+
You can use the tools I provided to you to answer the question.
|
| 53 |
+
Every time you use a tool, the number of steps will decrease by one.
|
| 54 |
+
If you have a list of possible pages to visit, prefer the wikipedia ones.
|
| 55 |
+
If a page does not allow visit, skip it.
|
| 56 |
+
Report your thoughts, and finish your answer with the following template:
|
| 57 |
+
FINAL ANSWER: [YOUR FINAL ANSWER].
|
| 58 |
+
|
| 59 |
+
YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings.
|
| 60 |
+
|
| 61 |
+
If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise.
|
| 62 |
+
If the answer is a number, represent it with digits.
|
| 63 |
+
If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise.
|
| 64 |
+
If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.
|
| 65 |
+
|
| 66 |
+
The taskid is {task_id} in case you need to get extra files, use taskid and not name of the file
|
| 67 |
+
and the question is {question}
|
| 68 |
+
"""
|
| 69 |
+
|
| 70 |
+
fixed_answer = self.agent.run(task)
|
| 71 |
print(f"Agent returning fixed answer: {fixed_answer}")
|
| 72 |
+
time.sleep(50)
|
| 73 |
+
|
| 74 |
return fixed_answer
|
| 75 |
|
| 76 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
|
|
| 134 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 135 |
continue
|
| 136 |
try:
|
| 137 |
+
submitted_answer = agent(question_text, task_id)
|
| 138 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 139 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 140 |
except Exception as e:
|
|
|
|
| 145 |
print("Agent did not produce any answers to submit.")
|
| 146 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 147 |
|
| 148 |
+
# 4. Prepare Submission
|
| 149 |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
| 150 |
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
| 151 |
print(status_update)
|
|
|
|
| 247 |
print("-"*(60 + len(" App Starting ")) + "\n")
|
| 248 |
|
| 249 |
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
| 250 |
+
demo.launch(debug=True, share=False)
|