Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1251,176 +1251,177 @@ except Exception as e:
|
|
| 1251 |
|
| 1252 |
# ====================================================
|
| 1253 |
# --- (Original Template Code - Mock Questions Version) ---
|
| 1254 |
-
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 1255 |
"""
|
| 1256 |
-
Fetches
|
| 1257 |
-
and displays the results.
|
| 1258 |
"""
|
| 1259 |
-
|
| 1260 |
-
|
| 1261 |
-
print(f"User: {username}{'' if profile else ' (dummy)'}")
|
| 1262 |
-
|
| 1263 |
-
# Check if global agent initialized
|
| 1264 |
-
if not agent:
|
| 1265 |
-
return "FATAL ERROR: Global agent failed to initialize. Check logs.", None
|
| 1266 |
-
|
| 1267 |
-
print("Using globally instantiated agent.")
|
| 1268 |
-
agent_code = f"httpsS://huggingface.co/spaces/{space_id}/tree/main" if space_id else "local_run" # Corrected URL
|
| 1269 |
-
print(f"Agent code URL: {agent_code}")
|
| 1270 |
-
print("--- USING MOCK QUESTIONS ---")
|
| 1271 |
-
|
| 1272 |
-
# --- MOCK QUESTIONS ---
|
| 1273 |
-
#
|
| 1274 |
-
# vvv PASTE YOUR FULL LIST OF 20 MOCK QUESTIONS HERE vvv
|
| 1275 |
-
#
|
| 1276 |
-
mock_questions_data = [
|
| 1277 |
-
{
|
| 1278 |
-
"task_id": "mock_level1_001",
|
| 1279 |
-
"question": r"""Here's a fun riddle that I'd like you to try.\n\nAn adventurer exploring an ancient tomb came across a horde of gold coins, all neatly stacked in columns. As he reached to scoop them into his backpack, a mysterious voice filled the room. \"You have fallen for my trap adventurer,\" the voice began, and suddenly the doorway to the chamber was sealed by a heavy rolling disk of stone. The adventurer tried to move the stone disk but was unable to budge the heavy stone. Trapped, he was startled when the voice again spoke. \n\n\"If you solve my riddle, I will reward you with a portion of my riches, but if you are not clever, you will never leave this treasure chamber. Before you are 200 gold coins. I pose a challenge to you, adventurer. Within these stacks of coins, all but 30 are face-up. You must divide the coins into two piles, one is yours, and one is mine. You may place as many coins as you like in either pile. You may flip any coins over, but you may not balance any coins on their edges. For every face-down coin in your pile, you will be rewarded with two gold coins. But be warned, if both piles do not contain the same number of face-down coins, the door will remain sealed for all eternity!\"\n\nThe adventurer smiled, as this would be an easy task. All he had to do was flip over every coin so it was face down, and he would win the entire treasure! As he moved to the columns of coins, however, the light suddenly faded, and he was left in total darkness. The adventurer reached forward and picked up one of the coins, and was shocked when he realized that both sides felt almost the same. Without the light, he was unable to determine which side of the coin was heads and which side was tails. He carefully replaced the coin in its original orientation and tried to think of a way to solve the puzzle. Finally, out of desperation, the adventurer removed 30 coins to create his pile. He then carefully flipped over each coin in his pile, so its orientation was inverted from its original state.\n\n\"I've finished,\" he said, and the lights returned. Looking at the two piles, he noticed that the larger pile contained 14 face-down coins.\n\nWhat was the outcome for the adventurer? If he failed the challenge, please respond with \"The adventurer died.\" Otherwise, please provide the number of coins the adventurer won at the conclusion of the riddle. If the adventurer won any coins, provide your response as the number of coins, with no other text."""
|
| 1280 |
-
},
|
| 1281 |
-
{
|
| 1282 |
-
"task_id": "mock_level1_002",
|
| 1283 |
-
"question": r"""If you use some of the letters in the given Letter Bank to spell out the sentence "I am a penguin halfway to the moon", which of the remaining unused letters would have to be changed to spell out, "The moon is made of cheese"? Return a comma-separated alphabetized list.\nLetter Bank: {OAMFETIMPECRFSHTDNIWANEPNOFAAIYOOMGUTNAHHLNEHCME}"""
|
| 1284 |
-
},
|
| 1285 |
-
{
|
| 1286 |
-
"task_id": "mock_level1_003",
|
| 1287 |
-
"question": r"""A data annotator stayed up too late creating test questions to check that a system was working properly and submitted several questions with mathematical errors. On nights when they created 15 test questions, they made 1 error. On nights when they created fewer than 15 questions, they also corrected 3 errors. On nights they created 20 questions, they made 0 errors. On nights when they created 25 or more, they made 4 errors. Over the course of five nights, the worker produced a total of 6 errors. When asked how many nights they created 15 questions, they gave three possible numbers as responses. What are the three numbers, presented in the format x, y, z in ascending order?"""
|
| 1288 |
-
},
|
| 1289 |
-
{
|
| 1290 |
-
"task_id": "mock_level1_004",
|
| 1291 |
-
"question": r"""Please solve the following crossword:\n\n|1|2|3|4|5|\n|6| | | | |\n|7| | | | |\n|8| | | | |\n|X|9| | | |\n\nI have indicated by numbers where the hints start, so you should replace numbers and spaces by the answers.\nAnd X denotes a black square that isn\u2019t to fill.\n\nACROSS\n- 1 Wooden strips on a bed frame\n- 6 _ Minhaj, Peabody-winning comedian for "Patriot Act"\n- 7 Japanese city of 2.6+ million\n- 8 Stopwatch, e.g.\n- 9 Pain in the neck\n\nDOWN\n- 1 Quick drink of whiskey\n- 2 Eye procedure\n- 3 "Same here," in a three-word phrase\n- 4 Already occupied, as a seat\n- 5 Sarcastically critical commentary. Answer by concatenating the characters you choose to fill the crossword, in row-major order."""
|
| 1292 |
-
},
|
| 1293 |
-
{
|
| 1294 |
-
"task_id": "mock_level1_005",
|
| 1295 |
-
"question": r"""I wanted to make another batch of cherry melomel. I remember liking the last recipe I tried, but I can't remember it off the top of my head. It was from the Reddit, r/mead. I remember that the user who made it had a really distinct name, I think it was StormBeforeDawn. Could you please look up the recipe for me? I'm not sure if it has been changed, so please make sure that the recipe you review wasn't updated after July 14, 2022. That's the last time I tried the recipe.\n\nWhat I want to know is how many cherries I'm supposed to use. I'm making a 10-gallon batch in two 5-gallon carboys. Please just respond with the integer number of pounds of whole cherries with pits that are supposed to be used for a 10-gallon batch."""
|
| 1296 |
-
},
|
| 1297 |
-
{
|
| 1298 |
-
"task_id": "mock_level1_006",
|
| 1299 |
-
"question": r"""Verify each of the following ISBN 13 numbers:\n\n1. 9783518188156\n2. 9788476540746\n3. 9788415091004\n4. 9788256014590\n5. 9782046407331\n\nIf any are invalid, correct them by changing the final digit. Then, return the list, comma separated, in the same order as in the question."""
|
| 1300 |
-
},
|
| 1301 |
-
{
|
| 1302 |
-
"task_id": "mock_level1_007",
|
| 1303 |
-
"question": r"""A porterhouse by any other name is centered around a letter. What does Three Dog Night think about the first natural number that starts with that letter? Give the first line from the lyrics that references it."""
|
| 1304 |
-
},
|
| 1305 |
-
{
|
| 1306 |
-
"task_id": "mock_level1_008",
|
| 1307 |
-
"question": r"""Bob has genome type Aa, and Linda has genome type Aa. Assuming that a child of theirs also has a child with someone who also has genome type Aa, what is the probability that Bob and Linda's grandchild will have Genome type Aa? Write the answer as a percentage, rounding to the nearest integer if necessary."""
|
| 1308 |
-
},
|
| 1309 |
-
{
|
| 1310 |
-
"task_id": "mock_level1_009",
|
| 1311 |
-
"question": r"""An array of candy is set out to choose from including gumballs, candy corn, gumdrops, banana taffy, chocolate chips, and gummy bears. There is one bag of each type of candy. The gumballs come in red, orange, yellow, green, blue, and brown. The candy corn is yellow, white, and orange. The gumdrops are red, green, purple, yellow, and orange. The banana taffy is yellow. The chocolate chips are brown and white. The gummy bears are red, green, yellow, and orange. Five people pass through and each selects one bag. The first selects one with only primary colors. The second selects one with no primary colors. The third selects one with all the primary colors. The fourth selects one that has neither the most nor the least colors of the remaining bags. The fifth selects the one with their favorite color, green. A second bag of the candy the first person chose is added to the remaining bag of candy. Which two candies are in the remaining bag after the addition? Give me them in a comma separated list, in alphabetical order"""
|
| 1312 |
-
},
|
| 1313 |
-
{
|
| 1314 |
-
"task_id": "mock_level1_010",
|
| 1315 |
-
"question": r"""In the year 2020, where were koi fish found in the watershed with the id 02040203? Give only the name of the pond, lake, or stream where the fish were found, and not the name of the city or county."""
|
| 1316 |
-
},
|
| 1317 |
-
{
|
| 1318 |
-
"task_id": "mock_level1_011",
|
| 1319 |
-
"question": r"""In Sonia Sanchez\u2019s poem \u201cfather\u2019s voice\u201d, what primary colour is evoked by the imagery in the beginning of the tenth stanza? Answer with a capitalized word."""
|
| 1320 |
-
},
|
| 1321 |
-
{
|
| 1322 |
-
"task_id": "mock_level1_012",
|
| 1323 |
-
"question": r"""According to Papers with Code, what was the name of the first model to go beyond 70% of accuracy on ImageNet ?"""
|
| 1324 |
-
},
|
| 1325 |
-
{
|
| 1326 |
-
"task_id": "mock_level1_013",
|
| 1327 |
-
"question": r"""What is the dimension of the boundary of the tame twindragon rounded to two decimal places?"""
|
| 1328 |
-
},
|
| 1329 |
-
{
|
| 1330 |
-
"task_id": "mock_level1_014",
|
| 1331 |
-
"question": r"""In what year was the home village of the subject of British Museum item #Bb,11.118 founded?"""
|
| 1332 |
-
},
|
| 1333 |
-
{
|
| 1334 |
-
"task_id": "mock_level1_015",
|
| 1335 |
-
"question": r"""What is the ISSN of the journal that included G. Scott's potato article that mentioned both a fast food restaurant and a Chinese politician in the title in a 2012 issue?"""
|
| 1336 |
-
},
|
| 1337 |
-
{
|
| 1338 |
-
"task_id": "mock_level1_016",
|
| 1339 |
-
"question": r"""VNV Nation has a song that shares its title with the nickname of Louis XV. What album was it released with?"""
|
| 1340 |
-
},
|
| 1341 |
-
{
|
| 1342 |
-
"task_id": "mock_level1_017",
|
| 1343 |
-
"question": r"""If I combine a Beatle's first name and a type of beer, in what category and year of Nobel Prize do I have a winner? Answer using the format CATEGORY, YEAR."""
|
| 1344 |
-
},
|
| 1345 |
-
{
|
| 1346 |
-
"task_id": "mock_level1_018",
|
| 1347 |
-
"question": r"""In the version of NumPy where the numpy.msort function was deprecated, which attribute was added to the numpy.polynomial package's polynomial classes?"""
|
| 1348 |
-
},
|
| 1349 |
-
{
|
| 1350 |
-
"task_id": "mock_level1_019",
|
| 1351 |
-
"question": r"""A word meaning dramatic or theatrical forms a species of duck when appended with two letters and then duplicated. What is that word?"""
|
| 1352 |
-
},
|
| 1353 |
-
{
|
| 1354 |
-
"task_id": "mock_level1_020",
|
| 1355 |
-
"question": r"""As of August 2023, how many in-text citations on the West African Vodun Wikipedia page reference a source that was cited using Scopus?"""
|
| 1356 |
-
}
|
| 1357 |
-
]
|
| 1358 |
-
|
| 1359 |
-
questions_data = mock_questions_data
|
| 1360 |
-
print(f"Using {len(questions_data)} mock questions.")
|
| 1361 |
|
| 1362 |
-
|
| 1363 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1364 |
|
| 1365 |
-
|
| 1366 |
-
|
| 1367 |
-
|
| 1368 |
|
| 1369 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1370 |
try:
|
| 1371 |
-
|
| 1372 |
-
|
| 1373 |
-
|
| 1374 |
-
question_text_with_context = f"{question_text}\n\n[Attached File: {file_path}]"
|
| 1375 |
-
print(f"Q includes file: {file_path}")
|
| 1376 |
-
|
| 1377 |
-
submitted_answer = agent(question_text_with_context)
|
| 1378 |
-
submitted_answer_str = str(submitted_answer) if submitted_answer is not None else ""
|
| 1379 |
-
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer_str})
|
| 1380 |
-
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer_str})
|
| 1381 |
-
print(f"--- Mock Task {task_id} Complete ---")
|
| 1382 |
except Exception as e:
|
| 1383 |
-
print(f"
|
| 1384 |
-
|
| 1385 |
-
submitted_answer = f"AGENT CRASH: {e}"
|
| 1386 |
-
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 1387 |
-
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 1388 |
|
| 1389 |
-
if not answers_payload:
|
|
|
|
|
|
|
| 1390 |
|
| 1391 |
-
|
| 1392 |
-
|
| 1393 |
-
|
| 1394 |
-
|
| 1395 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1396 |
|
| 1397 |
|
| 1398 |
-
# --- Build Gradio Interface ---
|
| 1399 |
with gr.Blocks() as demo:
|
| 1400 |
-
gr.Markdown("#
|
| 1401 |
-
gr.Markdown(
|
| 1402 |
-
|
| 1403 |
-
**
|
| 1404 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1405 |
gr.LoginButton()
|
| 1406 |
-
|
| 1407 |
-
|
| 1408 |
-
|
| 1409 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1410 |
|
| 1411 |
if __name__ == "__main__":
|
| 1412 |
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
| 1413 |
-
|
| 1414 |
-
|
| 1415 |
-
|
| 1416 |
-
|
| 1417 |
-
|
| 1418 |
-
|
| 1419 |
-
|
| 1420 |
-
|
| 1421 |
-
|
| 1422 |
-
|
| 1423 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1424 |
print("-"*(60 + len(" App Starting ")) + "\n")
|
| 1425 |
-
|
| 1426 |
-
|
|
|
|
|
|
| 1251 |
|
| 1252 |
# ====================================================
|
| 1253 |
# --- (Original Template Code - Mock Questions Version) ---
|
| 1254 |
+
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 1255 |
"""
|
| 1256 |
+
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
| 1257 |
+
and displays the results.
|
| 1258 |
"""
|
| 1259 |
+
# --- Determine HF Space Runtime URL and Repo URL ---
|
| 1260 |
+
space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1261 |
|
| 1262 |
+
if profile:
|
| 1263 |
+
username= f"{profile.username}"
|
| 1264 |
+
print(f"User logged in: {username}")
|
| 1265 |
+
else:
|
| 1266 |
+
print("User not logged in.")
|
| 1267 |
+
return "Please Login to Hugging Face with the button.", None
|
| 1268 |
|
| 1269 |
+
api_url = DEFAULT_API_URL
|
| 1270 |
+
questions_url = f"{api_url}/questions"
|
| 1271 |
+
submit_url = f"{api_url}/submit"
|
| 1272 |
|
| 1273 |
+
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 1274 |
+
try:
|
| 1275 |
+
agent = BasicAgent()
|
| 1276 |
+
except Exception as e:
|
| 1277 |
+
print(f"Error instantiating agent: {e}")
|
| 1278 |
+
return f"Error initializing agent: {e}", None
|
| 1279 |
+
# In the case of an app running as a hugging Face space, this link points toward your codebase ( usefull for others so please keep it public)
|
| 1280 |
+
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 1281 |
+
print(agent_code)
|
| 1282 |
+
|
| 1283 |
+
# 2. Fetch Questions
|
| 1284 |
+
print(f"Fetching questions from: {questions_url}")
|
| 1285 |
+
try:
|
| 1286 |
+
response = requests.get(questions_url, timeout=15)
|
| 1287 |
+
response.raise_for_status()
|
| 1288 |
+
questions_data = response.json()
|
| 1289 |
+
if not questions_data:
|
| 1290 |
+
print("Fetched questions list is empty.")
|
| 1291 |
+
return "Fetched questions list is empty or invalid format.", None
|
| 1292 |
+
print(f"Fetched {len(questions_data)} questions.")
|
| 1293 |
+
except requests.exceptions.RequestException as e:
|
| 1294 |
+
print(f"Error fetching questions: {e}")
|
| 1295 |
+
return f"Error fetching questions: {e}", None
|
| 1296 |
+
except requests.exceptions.JSONDecodeError as e:
|
| 1297 |
+
print(f"Error decoding JSON response from questions endpoint: {e}")
|
| 1298 |
+
print(f"Response text: {response.text[:500]}")
|
| 1299 |
+
return f"Error decoding server response for questions: {e}", None
|
| 1300 |
+
except Exception as e:
|
| 1301 |
+
print(f"An unexpected error occurred fetching questions: {e}")
|
| 1302 |
+
return f"An unexpected error occurred fetching questions: {e}", None
|
| 1303 |
+
|
| 1304 |
+
# 3. Run your Agent
|
| 1305 |
+
results_log = []
|
| 1306 |
+
answers_payload = []
|
| 1307 |
+
print(f"Running agent on {len(questions_data)} questions...")
|
| 1308 |
+
for item in questions_data:
|
| 1309 |
+
task_id = item.get("task_id")
|
| 1310 |
+
question_text = item.get("question")
|
| 1311 |
+
if not task_id or question_text is None:
|
| 1312 |
+
print(f"Skipping item with missing task_id or question: {item}")
|
| 1313 |
+
continue
|
| 1314 |
try:
|
| 1315 |
+
submitted_answer = agent(question_text)
|
| 1316 |
+
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 1317 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1318 |
except Exception as e:
|
| 1319 |
+
print(f"Error running agent on task {task_id}: {e}")
|
| 1320 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
|
|
|
|
|
|
|
|
|
| 1321 |
|
| 1322 |
+
if not answers_payload:
|
| 1323 |
+
print("Agent did not produce any answers to submit.")
|
| 1324 |
+
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 1325 |
|
| 1326 |
+
# 4. Prepare Submission
|
| 1327 |
+
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
| 1328 |
+
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
| 1329 |
+
print(status_update)
|
| 1330 |
+
|
| 1331 |
+
# 5. Submit
|
| 1332 |
+
print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
|
| 1333 |
+
try:
|
| 1334 |
+
response = requests.post(submit_url, json=submission_data, timeout=60)
|
| 1335 |
+
response.raise_for_status()
|
| 1336 |
+
result_data = response.json()
|
| 1337 |
+
final_status = (
|
| 1338 |
+
f"Submission Successful!\n"
|
| 1339 |
+
f"User: {result_data.get('username')}\n"
|
| 1340 |
+
f"Overall Score: {result_data.get('score', 'N/A')}% "
|
| 1341 |
+
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
|
| 1342 |
+
f"Message: {result_data.get('message', 'No message received.')}"
|
| 1343 |
+
)
|
| 1344 |
+
print("Submission successful.")
|
| 1345 |
+
results_df = pd.DataFrame(results_log)
|
| 1346 |
+
return final_status, results_df
|
| 1347 |
+
except requests.exceptions.HTTPError as e:
|
| 1348 |
+
error_detail = f"Server responded with status {e.response.status_code}."
|
| 1349 |
+
try:
|
| 1350 |
+
error_json = e.response.json()
|
| 1351 |
+
error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
|
| 1352 |
+
except requests.exceptions.JSONDecodeError:
|
| 1353 |
+
error_detail += f" Response: {e.response.text[:500]}"
|
| 1354 |
+
status_message = f"Submission Failed: {error_detail}"
|
| 1355 |
+
print(status_message)
|
| 1356 |
+
results_df = pd.DataFrame(results_log)
|
| 1357 |
+
return status_message, results_df
|
| 1358 |
+
except requests.exceptions.Timeout:
|
| 1359 |
+
status_message = "Submission Failed: The request timed out."
|
| 1360 |
+
print(status_message)
|
| 1361 |
+
results_df = pd.DataFrame(results_log)
|
| 1362 |
+
return status_message, results_df
|
| 1363 |
+
except requests.exceptions.RequestException as e:
|
| 1364 |
+
status_message = f"Submission Failed: Network error - {e}"
|
| 1365 |
+
print(status_message)
|
| 1366 |
+
results_df = pd.DataFrame(results_log)
|
| 1367 |
+
return status_message, results_df
|
| 1368 |
+
except Exception as e:
|
| 1369 |
+
status_message = f"An unexpected error occurred during submission: {e}"
|
| 1370 |
+
print(status_message)
|
| 1371 |
+
results_df = pd.DataFrame(results_log)
|
| 1372 |
+
return status_message, results_df
|
| 1373 |
|
| 1374 |
|
| 1375 |
+
# --- Build Gradio Interface using Blocks ---
|
| 1376 |
with gr.Blocks() as demo:
|
| 1377 |
+
gr.Markdown("# Basic Agent Evaluation Runner")
|
| 1378 |
+
gr.Markdown(
|
| 1379 |
+
"""
|
| 1380 |
+
**Instructions:**
|
| 1381 |
+
1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
|
| 1382 |
+
2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
|
| 1383 |
+
3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
|
| 1384 |
+
---
|
| 1385 |
+
**Disclaimers:**
|
| 1386 |
+
Once clicking on the "submit button, it can take quite some time ( this is the time for the agent to go through all the questions).
|
| 1387 |
+
This space provides a basic setup and is intentionally sub-optimal to encourage you to develop your own, more robust solution. For instance for the delay process of the submit button, a solution could be to cache the answers and submit in a seperate action or even to answer the questions in async.
|
| 1388 |
+
Please note that this version requires an OpenAI Key to run.
|
| 1389 |
+
"""
|
| 1390 |
+
)
|
| 1391 |
+
|
| 1392 |
gr.LoginButton()
|
| 1393 |
+
|
| 1394 |
+
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 1395 |
+
|
| 1396 |
+
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
| 1397 |
+
# Removed max_rows=10 from DataFrame constructor
|
| 1398 |
+
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 1399 |
+
|
| 1400 |
+
run_button.click(
|
| 1401 |
+
fn=run_and_submit_all,
|
| 1402 |
+
outputs=[status_output, results_table]
|
| 1403 |
+
)
|
| 1404 |
|
| 1405 |
if __name__ == "__main__":
|
| 1406 |
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
| 1407 |
+
# Check for SPACE_HOST and SPACE_ID at startup for information
|
| 1408 |
+
space_host_startup = os.getenv("SPACE_HOST")
|
| 1409 |
+
space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
|
| 1410 |
+
|
| 1411 |
+
if space_host_startup:
|
| 1412 |
+
print(f"✅ SPACE_HOST found: {space_host_startup}")
|
| 1413 |
+
print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
|
| 1414 |
+
else:
|
| 1415 |
+
print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
|
| 1416 |
+
|
| 1417 |
+
if space_id_startup: # Print repo URLs if SPACE_ID is found
|
| 1418 |
+
print(f"✅ SPACE_ID found: {space_id_startup}")
|
| 1419 |
+
print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
| 1420 |
+
print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
|
| 1421 |
+
else:
|
| 1422 |
+
print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
|
| 1423 |
+
|
| 1424 |
print("-"*(60 + len(" App Starting ")) + "\n")
|
| 1425 |
+
|
| 1426 |
+
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
| 1427 |
+
demo.launch(debug=True, share=False)
|