Update app.py
Browse files
app.py
CHANGED
|
@@ -1,251 +1,262 @@
|
|
| 1 |
import os
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
import pandas as pd
|
| 5 |
-
|
| 6 |
-
import
|
| 7 |
-
|
| 8 |
-
import
|
| 9 |
-
import
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
from smolagents import CodeAgent, tool, HfApiModel
|
| 15 |
-
print("Successfully imported CodeAgent, tool, HfApiModel from 'smolagents'")
|
| 16 |
-
except ImportError as e:
|
| 17 |
-
print(f"Error importing from smolagents: {e}")
|
| 18 |
-
print("Please ensure 'smolagents[huggingface]' is listed correctly in requirements.txt")
|
| 19 |
-
sys.exit(f"Fatal Error: Could not import smolagents components. Original error: {e}")
|
| 20 |
-
|
| 21 |
-
from huggingface_hub import HfApi
|
| 22 |
-
|
| 23 |
-
# --- Suchtool Imports ---
|
| 24 |
-
USE_TAVILY = False
|
| 25 |
-
USE_DUCKDUCKGO = True
|
| 26 |
-
if USE_TAVILY:
|
| 27 |
-
try:
|
| 28 |
-
from tavily import TavilyClient
|
| 29 |
-
except ImportError:
|
| 30 |
-
print("WARNUNG: TavilyClient nicht installiert.")
|
| 31 |
-
USE_TAVILY = False
|
| 32 |
-
USE_DUCKDUCKGO = True
|
| 33 |
-
if USE_DUCKDUCKGO:
|
| 34 |
-
try:
|
| 35 |
-
from duckduckgo_search import DDGS
|
| 36 |
-
except ImportError:
|
| 37 |
-
print("WARNUNG: duckduckgo-search nicht installiert.")
|
| 38 |
-
USE_DUCKDUCKGO = False
|
| 39 |
|
| 40 |
-
# ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
try:
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
for path in list(temp_files_to_clean):
|
| 58 |
-
try:
|
| 59 |
-
if os.path.exists(path): os.remove(path)
|
| 60 |
-
except OSError:
|
| 61 |
-
pass
|
| 62 |
-
temp_files_to_clean.discard(path)
|
| 63 |
-
atexit.register(cleanup_temp_files)
|
| 64 |
|
| 65 |
-
# --- Tool Definitions ---
|
| 66 |
@tool
|
| 67 |
-
def
|
| 68 |
"""
|
| 69 |
-
|
| 70 |
-
|
| 71 |
Args:
|
| 72 |
-
query (str): The search query
|
| 73 |
-
max_results (int): The maximum number of search results to retrieve.
|
| 74 |
-
|
| 75 |
Returns:
|
| 76 |
-
str: A
|
| 77 |
"""
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
if USE_TAVILY and isinstance(search_client, TavilyClient):
|
| 82 |
-
res = search_client.search(query=query, search_depth="basic", max_results=max_results)
|
| 83 |
-
items = res.get('results', [])
|
| 84 |
-
if not items:
|
| 85 |
-
return "No search results found."
|
| 86 |
-
return "\n".join([f"URL: {i['url']}\nContent: {i.get('content','')[:200]}..." for i in items])
|
| 87 |
-
elif USE_DUCKDUCKGO and isinstance(search_client, DDGS):
|
| 88 |
-
results = search_client.text(query, max_results=max_results)
|
| 89 |
-
if not results:
|
| 90 |
-
return "No search results found."
|
| 91 |
-
return "\n".join([f"Title: {r['title']}\nURL: {r['href']}\nSnippet: {r['body'][:200]}..." for r in results])
|
| 92 |
-
else:
|
| 93 |
-
return "Error: No compatible search client available."
|
| 94 |
-
except Exception as e:
|
| 95 |
-
return f"Error during search: {e}"
|
| 96 |
|
| 97 |
@tool
|
| 98 |
-
def
|
| 99 |
"""
|
| 100 |
-
|
| 101 |
-
|
| 102 |
Args:
|
| 103 |
-
|
| 104 |
-
|
| 105 |
Returns:
|
| 106 |
-
str:
|
| 107 |
"""
|
| 108 |
-
url = f"{DEFAULT_API_URL}/files/{task_id}"
|
| 109 |
try:
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
tmp_dir = tempfile.gettempdir()
|
| 115 |
-
filename = f"task_{task_id}_{datetime.now().strftime('%Y%m%d%H%M%S')}{suffix}"
|
| 116 |
-
path = os.path.join(tmp_dir, filename)
|
| 117 |
-
with open(path, 'wb') as f:
|
| 118 |
-
for chunk in response.iter_content(8192): f.write(chunk)
|
| 119 |
-
temp_files_to_clean.add(path)
|
| 120 |
-
return path
|
| 121 |
except Exception as e:
|
| 122 |
-
|
|
|
|
| 123 |
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
Read the text content of a previously downloaded file (PDF or plain text).
|
| 128 |
|
| 129 |
-
|
| 130 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
try:
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
for page in reader.pages:
|
| 147 |
-
text += page.extract_text() or ''
|
| 148 |
-
if len(text) > 7000:
|
| 149 |
-
text = text[:7000] + '\n... (truncated)'
|
| 150 |
-
break
|
| 151 |
-
return text
|
| 152 |
-
else:
|
| 153 |
-
with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
|
| 154 |
-
return f.read(7000)
|
| 155 |
except Exception as e:
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
agent_instance = CodeAgent(tools=tools, model=hf_model)
|
| 180 |
-
|
| 181 |
-
# --- Main Logic ---
|
| 182 |
-
def run_and_submit_all(profile, progress=gr.Progress(track_tqdm=True)):
|
| 183 |
-
# Parse profile if passed as JSON string
|
| 184 |
-
if isinstance(profile, str):
|
| 185 |
try:
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
try:
|
| 194 |
-
|
|
|
|
|
|
|
| 195 |
except Exception as e:
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
# Fetch questions
|
| 199 |
-
resp = requests.get(f"{DEFAULT_API_URL}/questions", timeout=30)
|
| 200 |
-
resp.raise_for_status()
|
| 201 |
-
questions = resp.json()
|
| 202 |
|
| 203 |
logs, payload = [], []
|
| 204 |
-
for item in
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
if not
|
| 208 |
continue
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
except Exception as e:
|
| 214 |
-
answer = f"ERROR: {e}"
|
| 215 |
-
logs.append({'Task ID': task_id, 'Question': question, 'Submitted Answer': answer})
|
| 216 |
-
payload.append({'task_id': task_id, 'submitted_answer': answer})
|
| 217 |
|
| 218 |
-
|
|
|
|
| 219 |
|
| 220 |
-
#
|
| 221 |
-
submission = {'username': username, 'agent_code': '...', 'answers': payload}
|
| 222 |
try:
|
| 223 |
-
post = requests.post(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 224 |
post.raise_for_status()
|
| 225 |
-
|
| 226 |
-
status =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 227 |
except Exception as e:
|
| 228 |
-
|
|
|
|
| 229 |
|
| 230 |
-
|
| 231 |
-
return status, df
|
| 232 |
|
| 233 |
-
# --- Gradio UI ---
|
| 234 |
with gr.Blocks() as demo:
|
| 235 |
-
gr.Markdown("#
|
| 236 |
-
gr.Markdown("
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
)
|
| 249 |
|
| 250 |
-
if __name__ ==
|
| 251 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import logging
|
| 3 |
+
|
| 4 |
import gradio as gr
|
| 5 |
import requests
|
| 6 |
import pandas as pd
|
| 7 |
+
# We still need the openai library, even if we change the endpoint
|
| 8 |
+
from openai import OpenAI
|
| 9 |
+
|
| 10 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, tool
|
| 11 |
+
from smolagents.models import OpenAIServerModel # Assuming this can handle base_url
|
| 12 |
+
|
| 13 |
+
# --- Logging ---
|
| 14 |
+
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")
|
| 15 |
+
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
# --- Constants ---
|
| 18 |
+
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space" # Keep this for submission
|
| 19 |
+
|
| 20 |
+
# --- GitHub Models Configuration ---
|
| 21 |
+
# Use GITHUB_TOKEN environment variable for authentication
|
| 22 |
+
GITHUB_TOKEN = os.getenv("GITHUB_TOKEN")
|
| 23 |
+
if not GITHUB_TOKEN:
|
| 24 |
+
# If running locally and GITHUB_TOKEN is not set, you might fall back
|
| 25 |
+
# to another mechanism or raise an error. For HF Spaces, secrets are needed.
|
| 26 |
+
raise RuntimeError("Please set GITHUB_TOKEN in your Space secrets.")
|
| 27 |
+
|
| 28 |
+
# GitHub Models endpoint
|
| 29 |
+
GITHUB_ENDPOINT = "https://models.github.ai/inference"
|
| 30 |
+
|
| 31 |
+
# Specify the model ID compatible with the GitHub endpoint
|
| 32 |
+
# Check GitHub Models documentation for available models. 'gpt-4.1' might not be the correct identifier.
|
| 33 |
+
# Let's assume a common format like 'openai/gpt-4o' or similar, adjust as needed.
|
| 34 |
+
# Using 'openai/gpt-4.1' as a placeholder based on your original code, VERIFY THIS with GitHub Models docs.
|
| 35 |
+
MODEL_ID = os.getenv("MODEL_ID", "openai/gpt-4.1") # Renamed for clarity, adjust if needed
|
| 36 |
+
|
| 37 |
+
# --- Configure OpenAI SDK (for tools if needed, now using GitHub endpoint) ---
|
| 38 |
+
# This client might be used by tools OR potentially by OpenAIServerModel internally
|
| 39 |
+
# depending on its implementation. Configuring it ensures consistency.
|
| 40 |
+
# Note: If OpenAIServerModel directly instantiates its own client using the parameters
|
| 41 |
+
# we provide later, this specific 'client' instance might not be used by the agent itself.
|
| 42 |
try:
|
| 43 |
+
client = OpenAI(
|
| 44 |
+
base_url=GITHUB_ENDPOINT,
|
| 45 |
+
api_key=GITHUB_TOKEN,
|
| 46 |
+
)
|
| 47 |
+
# Optional: Test connection or a simple call here if needed during setup
|
| 48 |
+
# client.models.list() # Example call, might need adjustment for GitHub's API structure
|
| 49 |
+
except Exception as e:
|
| 50 |
+
logger.error(f"Failed to initialize OpenAI client for GitHub Models: {e}")
|
| 51 |
+
# Decide how to handle this - raise error, log warning, etc.
|
| 52 |
+
raise RuntimeError(f"OpenAI client initialization failed for GitHub Models: {e}") from e
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
# --- Tools ---
|
| 56 |
+
# Tools remain the same, assuming they don't directly depend on the *specific* OpenAI API endpoint
|
| 57 |
+
# unless they internally use the globally configured 'client' (which we just updated).
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
|
|
|
| 59 |
@tool
|
| 60 |
+
def summarize_query(query: str) -> str:
|
| 61 |
"""
|
| 62 |
+
Reframes an unclear search query to improve relevance.
|
|
|
|
| 63 |
Args:
|
| 64 |
+
query (str): The original search query.
|
|
|
|
|
|
|
| 65 |
Returns:
|
| 66 |
+
str: A concise, improved version.
|
| 67 |
"""
|
| 68 |
+
# This tool currently doesn't use an LLM, so it's unaffected by the endpoint change.
|
| 69 |
+
# If it *did* use the 'client' instance, it would now point to GitHub Models.
|
| 70 |
+
return f"Summarize and reframe: {query}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
@tool
|
| 73 |
+
def wikipedia_search(page: str) -> str:
|
| 74 |
"""
|
| 75 |
+
Fetches the summary extract of an English Wikipedia page.
|
|
|
|
| 76 |
Args:
|
| 77 |
+
page (str): e.g. 'Mercedes_Sosa_discography'
|
|
|
|
| 78 |
Returns:
|
| 79 |
+
str: The page’s extract text.
|
| 80 |
"""
|
|
|
|
| 81 |
try:
|
| 82 |
+
url = f"https://en.wikipedia.org/api/rest_v1/page/summary/{page}"
|
| 83 |
+
r = requests.get(url, timeout=10)
|
| 84 |
+
r.raise_for_status()
|
| 85 |
+
return r.json().get("extract", "")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
except Exception as e:
|
| 87 |
+
logger.exception("Wikipedia lookup failed")
|
| 88 |
+
return f"Wikipedia error: {e}"
|
| 89 |
|
| 90 |
+
search_tool = DuckDuckGoSearchTool()
|
| 91 |
+
wiki_tool = wikipedia_search
|
| 92 |
+
summarize_tool = summarize_query
|
|
|
|
| 93 |
|
| 94 |
+
# --- ReACT Prompt ---
|
| 95 |
+
# The prompt itself doesn't need to change as it describes the agent's *behavior*
|
| 96 |
+
instruction_prompt = """
|
| 97 |
+
You are a ReACT agent with three tools:
|
| 98 |
+
• DuckDuckGoSearchTool(query: str)
|
| 99 |
+
• wikipedia_search(page: str)
|
| 100 |
+
• summarize_query(query: str)
|
| 101 |
+
Internally, for each question:
|
| 102 |
+
1. Thought: decide which tool to call.
|
| 103 |
+
2. Action: call the chosen tool.
|
| 104 |
+
3. Observation: record the result.
|
| 105 |
+
4. If empty/irrelevant:
|
| 106 |
+
Thought: retry with summarize_query + DuckDuckGoSearchTool.
|
| 107 |
+
Record new Observation.
|
| 108 |
+
5. Thought: integrate observations.
|
| 109 |
+
Finally, output your answer with the following template:
|
| 110 |
+
FINAL ANSWER: [YOUR FINAL ANSWER].
|
| 111 |
+
YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings.
|
| 112 |
+
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.
|
| 113 |
+
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.
|
| 114 |
+
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.
|
| 115 |
+
"""
|
| 116 |
|
| 117 |
+
# --- Build the Agent with OpenAIServerModel pointing to GitHub Models ---
|
| 118 |
+
|
| 119 |
+
# *** Key Change Here ***
|
| 120 |
+
# We configure OpenAIServerModel to use the GitHub endpoint and token.
|
| 121 |
+
# We assume OpenAIServerModel accepts 'api_base' or 'base_url' and passes it
|
| 122 |
+
# to the underlying OpenAI client it creates. 'base_url' is the modern parameter.
|
| 123 |
+
# If this doesn't work, you might need to check the smolagents documentation
|
| 124 |
+
# or source for how to specify a custom endpoint, or potentially subclass/modify it.
|
| 125 |
+
try:
|
| 126 |
+
model = OpenAIServerModel(
|
| 127 |
+
model_id=MODEL_ID, # Use the model ID for GitHub
|
| 128 |
+
api_key=GITHUB_TOKEN, # Use the GitHub token as the API key
|
| 129 |
+
api_base=GITHUB_ENDPOINT # Specify the GitHub endpoint *** Use api_base or base_url ***
|
| 130 |
+
# Try base_url if api_base doesn't work:
|
| 131 |
+
# base_url=GITHUB_ENDPOINT
|
| 132 |
+
# Add any other necessary parameters required by OpenAIServerModel or the underlying client
|
| 133 |
+
# e.g., model_kwargs if needed
|
| 134 |
+
)
|
| 135 |
+
logger.info(f"Configured OpenAIServerModel with GitHub endpoint: {GITHUB_ENDPOINT} and model: {MODEL_ID}")
|
| 136 |
+
except TypeError as te:
|
| 137 |
+
logger.error(f"TypeError configuring OpenAIServerModel: {te}. Trying with 'base_url' instead of 'api_base'.")
|
| 138 |
+
# Fallback attempt using base_url if api_base caused a TypeError
|
| 139 |
try:
|
| 140 |
+
model = OpenAIServerModel(
|
| 141 |
+
model_id=MODEL_ID,
|
| 142 |
+
api_key=GITHUB_TOKEN,
|
| 143 |
+
base_url=GITHUB_ENDPOINT # Use base_url
|
| 144 |
+
)
|
| 145 |
+
logger.info(f"Successfully configured OpenAIServerModel with GitHub endpoint using 'base_url'.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
except Exception as e:
|
| 147 |
+
logger.error(f"Failed to configure OpenAIServerModel with both 'api_base' and 'base_url': {e}")
|
| 148 |
+
raise RuntimeError(f"Could not configure SmolAgents model for GitHub endpoint: {e}") from e
|
| 149 |
+
except Exception as e:
|
| 150 |
+
logger.error(f"Failed to configure OpenAIServerModel: {e}")
|
| 151 |
+
raise RuntimeError(f"Could not configure SmolAgents model for GitHub endpoint: {e}") from e
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
smart_agent = CodeAgent(
|
| 155 |
+
tools=[search_tool, wiki_tool, summarize_tool],
|
| 156 |
+
model=model # Pass the configured model instance
|
| 157 |
+
)
|
| 158 |
+
|
| 159 |
+
# --- Gradio Wrapper ---
|
| 160 |
+
|
| 161 |
+
class BasicAgent:
|
| 162 |
+
def __init__(self):
|
| 163 |
+
# Updated log message
|
| 164 |
+
logger.info(f"Initialized SmolAgent with GitHub Model: {MODEL_ID} via {GITHUB_ENDPOINT}")
|
| 165 |
+
|
| 166 |
+
def __call__(self, question: str) -> str:
|
| 167 |
+
if not question.strip():
|
| 168 |
+
return "AGENT ERROR: empty question"
|
| 169 |
+
prompt = instruction_prompt.strip() + "\n\nQUESTION: " + question.strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
try:
|
| 171 |
+
# The agent uses the 'model' instance we configured above
|
| 172 |
+
return smart_agent.run(prompt)
|
| 173 |
+
except Exception as e:
|
| 174 |
+
logger.exception("Agent run error")
|
| 175 |
+
# Provide more specific error if possible, e.g., AuthenticationError from OpenAI client
|
| 176 |
+
return f"AGENT ERROR: {e}"
|
| 177 |
+
|
| 178 |
+
# --- Submission Logic ---
|
| 179 |
+
# This part remains largely the same, as it interacts with the external scoring service (DEFAULT_API_URL)
|
| 180 |
+
# It just uses the 'agent' which now internally calls GitHub Models.
|
| 181 |
+
|
| 182 |
+
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 183 |
+
if not profile:
|
| 184 |
+
return "Please log in to Hugging Face.", None
|
| 185 |
|
| 186 |
+
username = profile.username
|
| 187 |
+
space_id = os.getenv("SPACE_ID", "")
|
| 188 |
+
# Link to the code, unchanged
|
| 189 |
+
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 190 |
+
# Instantiate the agent wrapper, which now uses the GitHub-configured model
|
| 191 |
+
agent = BasicAgent()
|
| 192 |
+
|
| 193 |
+
# fetch questions (unchanged)
|
| 194 |
try:
|
| 195 |
+
resp = requests.get(f"{DEFAULT_API_URL}/questions", timeout=15)
|
| 196 |
+
resp.raise_for_status()
|
| 197 |
+
questions = resp.json() or []
|
| 198 |
except Exception as e:
|
| 199 |
+
logger.exception("Failed fetch")
|
| 200 |
+
return f"Error fetching questions: {e}", None
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
|
| 202 |
logs, payload = [], []
|
| 203 |
+
for item in questions:
|
| 204 |
+
tid = item.get("task_id")
|
| 205 |
+
q = item.get("question")
|
| 206 |
+
if not tid or not q:
|
| 207 |
continue
|
| 208 |
+
# Run the agent (now using GitHub Models)
|
| 209 |
+
ans = agent(q)
|
| 210 |
+
logs.append({"Task ID": tid, "Question": q, "Submitted Answer": ans})
|
| 211 |
+
payload.append({"task_id": tid, "submitted_answer": ans})
|
|
|
|
|
|
|
|
|
|
|
|
|
| 212 |
|
| 213 |
+
if not payload:
|
| 214 |
+
return "Agent did not produce any answers.", pd.DataFrame(logs)
|
| 215 |
|
| 216 |
+
# submit answers (unchanged)
|
|
|
|
| 217 |
try:
|
| 218 |
+
post = requests.post(
|
| 219 |
+
f"{DEFAULT_API_URL}/submit",
|
| 220 |
+
json={"username": username, "agent_code": agent_code, "answers": payload},
|
| 221 |
+
timeout=60
|
| 222 |
+
)
|
| 223 |
post.raise_for_status()
|
| 224 |
+
result = post.json()
|
| 225 |
+
status = (
|
| 226 |
+
f"Submission Successful!\n"
|
| 227 |
+
f"User: {result.get('username')}\n"
|
| 228 |
+
f"Score: {result.get('score','N/A')}%\n"
|
| 229 |
+
f"({result.get('correct_count','?')}/"
|
| 230 |
+
f"{result.get('total_attempted','?')})\n"
|
| 231 |
+
f"Message: {result.get('message','')}"
|
| 232 |
+
)
|
| 233 |
+
return status, pd.DataFrame(logs)
|
| 234 |
except Exception as e:
|
| 235 |
+
logger.exception("Submit failed")
|
| 236 |
+
return f"Submission Failed: {e}", pd.DataFrame(logs)
|
| 237 |
|
| 238 |
+
# --- Gradio App ---
|
|
|
|
| 239 |
|
|
|
|
| 240 |
with gr.Blocks() as demo:
|
| 241 |
+
gr.Markdown("# SmolAgent GAIA Runner (using GitHub Models) 🚀") # Updated title
|
| 242 |
+
gr.Markdown("""
|
| 243 |
+
**Instructions:**
|
| 244 |
+
1. Clone this space.
|
| 245 |
+
2. In Settings → Secrets, add `GITHUB_TOKEN` (your GitHub access token with appropriate permissions for GitHub Models).
|
| 246 |
+
3. Optionally, set `MODEL_ID` if you want to use a model other than the default (e.g., `openai/gpt-4o`). Verify the correct model identifier for GitHub Models.
|
| 247 |
+
4. Log in to Hugging Face.
|
| 248 |
+
5. Click **Run Evaluation & Submit All Answers**.
|
| 249 |
+
""") # Updated instructions
|
| 250 |
+
gr.LoginButton()
|
| 251 |
+
btn = gr.Button("Run Evaluation & Submit All Answers")
|
| 252 |
+
out_status = gr.Textbox(label="Status", lines=5, interactive=False)
|
| 253 |
+
out_table = gr.DataFrame(label="Questions & Answers", wrap=True)
|
| 254 |
+
btn.click(run_and_submit_all, outputs=[out_status, out_table])
|
| 255 |
|
| 256 |
+
if __name__ == "__main__":
|
| 257 |
+
# Check GITHUB_TOKEN presence before launching
|
| 258 |
+
if not GITHUB_TOKEN:
|
| 259 |
+
logger.error("GITHUB_TOKEN environment variable not set. Cannot start.")
|
| 260 |
+
else:
|
| 261 |
+
logger.info("Launching Gradio App...")
|
| 262 |
+
demo.launch(debug=True, share=False) # Set debug=False for production
|