Update app.py
Browse files
app.py
CHANGED
|
@@ -3,8 +3,16 @@ import gradio as gr
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
-
from tools import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
from smolagents import GradioUI, CodeAgent, HfApiModel, PythonInterpreterTool
|
|
|
|
| 8 |
|
| 9 |
# (Keep Constants as is)
|
| 10 |
# --- Constants ---
|
|
@@ -12,34 +20,69 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 12 |
|
| 13 |
# --- Basic Agent Definition ---
|
| 14 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
class BasicAgent:
|
| 18 |
def __init__(self):
|
| 19 |
print("BasicAgent initialized.")
|
| 20 |
-
|
| 21 |
-
web_search = Web_research()
|
| 22 |
-
multimodal_tool = multimodal_interpreter()
|
| 23 |
-
python_code_tool = PythonInterpreterTool()
|
| 24 |
-
wiki_tool = Wikipedia_reader()
|
| 25 |
-
translator=translate_everything()
|
| 26 |
-
wiki_url_tool=Find_wikipedia_URL()
|
| 27 |
-
audio_tool=audio_or_mp3__interpreter()
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
add_base_tools=True,
|
| 32 |
-
additional_authorized_imports=[
|
| 33 |
-
|
|
|
|
|
|
|
| 34 |
)
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
"Authorization": f"Bearer {HF_TOKEN}"
|
| 38 |
-
}
|
| 39 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 45 |
"""
|
|
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
+
from tools import (
|
| 7 |
+
Web_research,
|
| 8 |
+
multimodal_interpreter,
|
| 9 |
+
Wikipedia_reader,
|
| 10 |
+
translate_everything,
|
| 11 |
+
Find_wikipedia_URL,
|
| 12 |
+
audio_or_mp3__interpreter,
|
| 13 |
+
)
|
| 14 |
from smolagents import GradioUI, CodeAgent, HfApiModel, PythonInterpreterTool
|
| 15 |
+
from functools import lru_cache
|
| 16 |
|
| 17 |
# (Keep Constants as is)
|
| 18 |
# --- Constants ---
|
|
|
|
| 20 |
|
| 21 |
# --- Basic Agent Definition ---
|
| 22 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 23 |
+
|
| 24 |
+
@lru_cache()
|
| 25 |
+
def get_hf_model():
|
| 26 |
+
"""Load and cache the Hugging Face API model once."""
|
| 27 |
+
token = os.environ.get("HF_TOKEN")
|
| 28 |
+
if not token:
|
| 29 |
+
raise ValueError("HF_TOKEN is not set in environment variables.")
|
| 30 |
+
return HfApiModel(token=token)
|
| 31 |
+
|
| 32 |
class BasicAgent:
|
| 33 |
def __init__(self):
|
| 34 |
print("BasicAgent initialized.")
|
| 35 |
+
# Load tools only once
|
| 36 |
+
self.web_search = Web_research()
|
| 37 |
+
self.multimodal_tool = multimodal_interpreter()
|
| 38 |
+
self.python_code_tool = PythonInterpreterTool()
|
| 39 |
+
self.wiki_tool = Wikipedia_reader()
|
| 40 |
+
self.translator = translate_everything()
|
| 41 |
+
self.wiki_url_tool = Find_wikipedia_URL()
|
| 42 |
+
self.audio_tool = audio_or_mp3__interpreter()
|
| 43 |
+
self.model = get_hf_model()
|
| 44 |
+
|
| 45 |
+
# Initialize CodeAgent only once
|
| 46 |
+
self.alfred = CodeAgent(
|
| 47 |
+
tools=[
|
| 48 |
+
self.wiki_url_tool,
|
| 49 |
+
self.translator,
|
| 50 |
+
self.wiki_tool,
|
| 51 |
+
self.web_search,
|
| 52 |
+
self.multimodal_tool,
|
| 53 |
+
self.python_code_tool,
|
| 54 |
+
self.audio_tool,
|
| 55 |
+
],
|
| 56 |
+
model=self.model,
|
| 57 |
add_base_tools=True,
|
| 58 |
+
additional_authorized_imports=[
|
| 59 |
+
'urllib', 'chess', 'requests', 'bs4', 'pybaseball', 'numpy', 'pandas', 'accelerate'
|
| 60 |
+
],
|
| 61 |
+
max_print_outputs_length=100,
|
| 62 |
)
|
| 63 |
+
|
| 64 |
+
def __call__(self, question: str) -> str:
|
|
|
|
|
|
|
| 65 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 66 |
+
prompt = (
|
| 67 |
+
"You are a general AI assistant. I will ask you a question. "
|
| 68 |
+
"Please follow these steps in order to answer it: "
|
| 69 |
+
"(1) Check if the sentence is written in English. If not, use your custom translator tool. "
|
| 70 |
+
"(2) If the question is in English, do not use the translator. Determine whether it requires a web search, "
|
| 71 |
+
"and if so, use only the exact words from the question as keywords—no synonyms. "
|
| 72 |
+
"(3) If a web search is needed and the answer is likely on Wikipedia, try using the wiki_url_tool to find the relevant page; "
|
| 73 |
+
"if that fails, search manually; if you need a specific numerical fact (e.g., a year or a number), use the wiki_tool to extract data from Wikipedia tables. "
|
| 74 |
+
"(4) Never use synonyms not present in the question. Do not make assumptions. "
|
| 75 |
+
"If you do not know the answer, say so clearly. Always report your thoughts and finish your answer with the following template: "
|
| 76 |
+
"FINAL ANSWER: [YOUR FINAL ANSWER]. YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. "
|
| 77 |
+
"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. "
|
| 78 |
+
"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. "
|
| 79 |
+
"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. "
|
| 80 |
+
f"Here are the questions : {question}"
|
| 81 |
+
)
|
| 82 |
+
answer = self.alfred.run(prompt)
|
| 83 |
+
print(f"Agent returning fixed answer: {answer}")
|
| 84 |
+
return answer
|
| 85 |
+
|
| 86 |
|
| 87 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 88 |
"""
|