Spaces:
Sleeping
Sleeping
File size: 4,351 Bytes
fb960b8 697c6cd fb960b8 7674639 5fb70e0 697c6cd fb960b8 d018b86 cdb8feb d018b86 5fb70e0 fb960b8 697c6cd fb960b8 88cb8ab fb960b8 697c6cd fb960b8 697c6cd fb960b8 697c6cd fb960b8 88cb8ab fb960b8 88cb8ab cdb8feb 439e39d fb960b8 6078833 fb960b8 d018b86 88cb8ab fb960b8 d018b86 5fb70e0 d018b86 5fb70e0 d018b86 fb960b8 697c6cd 6d8c36e 5fb70e0 6d8c36e 697c6cd 6078833 6d8c36e 697c6cd cdb8feb 697c6cd 6078833 5fb70e0 fb960b8 d018b86 fb960b8 697c6cd fb960b8 697c6cd 7674639 fb960b8 697c6cd cdb8feb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
import gradio as gr
import pandas as pd
import json
import os
import time
import re
from rich.console import Console
from rich.panel import Panel
from src.utils import (
fetch_questions,
submit_answers,
get_file,
load_in_vector_db,
)
from src.inference import Agent
def run_and_submit_all(profile: gr.OAuthProfile | None):
console = Console()
space_id = os.getenv("SPACE_ID")
if profile:
username = f"{profile.username}"
console.print(f"User logged in: {username}", style="bold green")
else:
console.print("User not logged in.", style="bold red")
return "Please Login to Hugging Face with the button.", None
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
console.print(agent_code)
questions_data = fetch_questions()
if not questions_data:
return "Failed to fetch questions.", None
results_log = []
answers_payload = []
#chosen_task_id = "f918266a-b3e0-4914-865d-4faa564f1aef"
#questions_data = [item for item in questions_data if item.get("task_id") == chosen_task_id]
for item in questions_data:
task_id = item.get("task_id")
question_text = item.get("question")
file_name = item.get("file_name")
if not task_id or question_text is None:
continue
if file_name != "":
file_path = get_file(task_id)
file_context = f" You can access to the file here: '{file_path}'."
else:
file_context = ""
try:
console.rule(f"\n[bold blue]Task ID: {task_id}")
console.print(Panel(f"[bold]Question[/bold]\n{question_text}{file_context}", expand=False))
with open('./metadata.jsonl', 'r') as file:
for line in file:
item = json.loads(line)
if item.get('task_id') == task_id:
final_answer = item.get('Final answer')
agent = Agent()
submitted_answer = agent.run(
input=question_text + file_context,
task_id=task_id,
truth=final_answer
)
if submitted_answer == final_answer:
try:
load_in_vector_db(
markdown_content=f"{question_text}{file_context}\n\nFINAL ANSWER:{submitted_answer}",
#metadatas={
# "task_id": task_id,
# "question": question_text,
# "file_name": file_name,
#},
)
console.print(f"Correct answer vectorized and stored")
except Exception as e:
console.print(f"Error loading in vector DB: {e}", style="bold red")
console.print(Panel(f"[bold green]Submitted Answer[/bold green]\n{submitted_answer}", expand=False))
console.print(Panel(f"The correct final answer is: [bold]{final_answer}[/bold]"))
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
except Exception as e:
console.print(f"Error: {e}", style="bold red")
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
time.sleep(1) # Rate limit for API calls
if not answers_payload:
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
result_data = submit_answers(submission_data)
if result_data:
final_status = (
f"Submission Successful!\n"
f"User: {result_data.get('username')}\n"
f"Overall Score: {result_data.get('score', 'N/A')}% "
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
f"Message: {result_data.get('message', 'No message received.')}"
)
results_df = pd.DataFrame(results_log)
return final_status, results_df
else:
return "Submission Failed.", pd.DataFrame(results_log)
|