Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,154 +5,146 @@ import re
|
|
| 5 |
import pandas as pd
|
| 6 |
from transformers import pipeline, set_seed
|
| 7 |
|
| 8 |
-
#
|
| 9 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 10 |
|
| 11 |
-
#
|
| 12 |
class BasicAgent:
|
| 13 |
def __init__(self):
|
| 14 |
-
print("BasicAgent initialized
|
| 15 |
set_seed(42)
|
|
|
|
| 16 |
self.generator = pipeline(
|
| 17 |
"text2text-generation",
|
| 18 |
model="google/flan-t5-large",
|
| 19 |
-
max_new_tokens=32
|
|
|
|
|
|
|
| 20 |
)
|
| 21 |
|
| 22 |
def __call__(self, question: str) -> str:
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
"No explanations or extra words.\n"
|
| 31 |
-
f"Q: {question}\nA:"
|
| 32 |
-
)
|
| 33 |
-
# Multiple choice detection
|
| 34 |
-
elif any(x in question for x in ["A)", "B)", "C)", "D)", "choose", "options"]):
|
| 35 |
-
prompt = (
|
| 36 |
-
"Answer only with one option exactly: A, B, C, or D. "
|
| 37 |
-
"No explanations or extra words.\n"
|
| 38 |
-
f"Q: {question}\nA:"
|
| 39 |
-
)
|
| 40 |
-
# Numeric detection (digits in question or ask for a number)
|
| 41 |
-
elif re.search(r"number|digits|how many|count|what is \d", q_lower):
|
| 42 |
-
prompt = (
|
| 43 |
-
"Answer only with the number, no words or punctuation.\n"
|
| 44 |
-
f"Q: {question}\nA:"
|
| 45 |
-
)
|
| 46 |
-
# Default: let model generate
|
| 47 |
-
else:
|
| 48 |
-
prompt = (
|
| 49 |
-
"Give ONLY the exact answer. "
|
| 50 |
-
"No explanation or extra words.\n"
|
| 51 |
-
f"Q: {question}\nA:"
|
| 52 |
-
)
|
| 53 |
-
|
| 54 |
-
# --- Generate answer ---
|
| 55 |
result = self.generator(prompt)[0]["generated_text"]
|
| 56 |
|
| 57 |
-
#
|
| 58 |
answer = result.strip()
|
| 59 |
-
answer = re.sub(r"(?i)^(the answer is|answer:)\s*", "", answer)
|
| 60 |
-
answer = re.split(r"[
|
| 61 |
-
answer = answer.
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
| 68 |
return answer
|
| 69 |
|
| 70 |
|
| 71 |
-
#
|
| 72 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 73 |
space_id = os.getenv("SPACE_ID")
|
| 74 |
|
| 75 |
-
if profile:
|
| 76 |
-
|
| 77 |
-
else:
|
| 78 |
-
return "Please Login to Hugging Face with the button.", pd.DataFrame()
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
submit_url = f"{api_url}/submit"
|
| 83 |
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
# --- Fetch Questions ---
|
| 92 |
try:
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
if not questions_data:
|
| 97 |
-
return "Fetched questions list is empty or invalid format.", pd.DataFrame()
|
| 98 |
except Exception as e:
|
| 99 |
-
return f"
|
| 100 |
|
| 101 |
-
results_log = []
|
| 102 |
answers_payload = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
-
for item in questions_data:
|
| 105 |
-
task_id = item.get("task_id")
|
| 106 |
-
question_text = item.get("question")
|
| 107 |
-
if not task_id or question_text is None:
|
| 108 |
-
continue
|
| 109 |
try:
|
| 110 |
-
|
| 111 |
-
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 112 |
-
results_log.append({
|
| 113 |
-
"Task ID": task_id,
|
| 114 |
-
"Question": question_text,
|
| 115 |
-
"Submitted Answer": submitted_answer
|
| 116 |
-
})
|
| 117 |
except Exception as e:
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
try:
|
| 130 |
-
response = requests.post(
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
|
|
|
|
|
|
|
|
|
| 134 |
f"Submission Successful!\n"
|
| 135 |
-
f"User: {
|
| 136 |
-
f"Overall Score: {
|
| 137 |
-
f"({
|
| 138 |
-
f"
|
|
|
|
| 139 |
)
|
| 140 |
-
results_df = pd.DataFrame(results_log)
|
| 141 |
-
return final_status, results_df
|
| 142 |
except Exception as e:
|
| 143 |
-
|
|
|
|
|
|
|
| 144 |
|
| 145 |
|
| 146 |
-
#
|
| 147 |
with gr.Blocks() as demo:
|
| 148 |
-
gr.Markdown("# GAIA
|
|
|
|
| 149 |
gr.LoginButton()
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
|
| 154 |
-
|
|
|
|
|
|
|
|
|
|
| 155 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
|
| 157 |
if __name__ == "__main__":
|
| 158 |
-
demo.launch(debug=True
|
|
|
|
| 5 |
import pandas as pd
|
| 6 |
from transformers import pipeline, set_seed
|
| 7 |
|
| 8 |
+
# ---------------- CONSTANTS ----------------
|
| 9 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 10 |
|
| 11 |
+
# ---------------- BASIC AGENT ----------------
|
| 12 |
class BasicAgent:
|
| 13 |
def __init__(self):
|
| 14 |
+
print("BasicAgent initialized")
|
| 15 |
set_seed(42)
|
| 16 |
+
|
| 17 |
self.generator = pipeline(
|
| 18 |
"text2text-generation",
|
| 19 |
model="google/flan-t5-large",
|
| 20 |
+
max_new_tokens=32,
|
| 21 |
+
temperature=0.0, # VERY IMPORTANT: deterministic
|
| 22 |
+
do_sample=False
|
| 23 |
)
|
| 24 |
|
| 25 |
def __call__(self, question: str) -> str:
|
| 26 |
+
prompt = (
|
| 27 |
+
"Answer the question with ONLY the final answer.\n"
|
| 28 |
+
"No explanation. No extra words. No punctuation.\n\n"
|
| 29 |
+
f"Question: {question}\n"
|
| 30 |
+
"Answer:"
|
| 31 |
+
)
|
| 32 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
result = self.generator(prompt)[0]["generated_text"]
|
| 34 |
|
| 35 |
+
# ---------- STRONG CLEANUP ----------
|
| 36 |
answer = result.strip()
|
| 37 |
+
answer = re.sub(r"(?i)^(the answer is|answer:)\s*", "", answer)
|
| 38 |
+
answer = re.split(r"[\n\.]", answer)[0]
|
| 39 |
+
answer = answer.strip()
|
| 40 |
+
answer = answer.rstrip(".,;:")
|
| 41 |
+
|
| 42 |
+
# Normalize boolean answers
|
| 43 |
+
if answer.lower() == "true":
|
| 44 |
+
answer = "True"
|
| 45 |
+
elif answer.lower() == "false":
|
| 46 |
+
answer = "False"
|
| 47 |
+
|
| 48 |
+
print(f"\nQ: {question}\nA: {answer}\n")
|
| 49 |
return answer
|
| 50 |
|
| 51 |
|
| 52 |
+
# ---------------- RUN + SUBMIT ----------------
|
| 53 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 54 |
space_id = os.getenv("SPACE_ID")
|
| 55 |
|
| 56 |
+
if not profile:
|
| 57 |
+
return "Please login with Hugging Face.", pd.DataFrame()
|
|
|
|
|
|
|
| 58 |
|
| 59 |
+
username = profile.username
|
| 60 |
+
agent = BasicAgent()
|
|
|
|
| 61 |
|
| 62 |
+
agent_code = (
|
| 63 |
+
f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 64 |
+
if space_id else "N/A"
|
| 65 |
+
)
|
| 66 |
|
| 67 |
+
# Fetch questions
|
|
|
|
|
|
|
| 68 |
try:
|
| 69 |
+
questions = requests.get(
|
| 70 |
+
f"{DEFAULT_API_URL}/questions", timeout=15
|
| 71 |
+
).json()
|
|
|
|
|
|
|
| 72 |
except Exception as e:
|
| 73 |
+
return f"Failed to fetch questions: {e}", pd.DataFrame()
|
| 74 |
|
|
|
|
| 75 |
answers_payload = []
|
| 76 |
+
results_log = []
|
| 77 |
+
|
| 78 |
+
for q in questions:
|
| 79 |
+
task_id = q["task_id"]
|
| 80 |
+
question_text = q["question"]
|
| 81 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
try:
|
| 83 |
+
answer = agent(question_text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
except Exception as e:
|
| 85 |
+
answer = "ERROR"
|
| 86 |
+
|
| 87 |
+
answers_payload.append({
|
| 88 |
+
"task_id": task_id,
|
| 89 |
+
"submitted_answer": answer
|
| 90 |
+
})
|
| 91 |
+
|
| 92 |
+
results_log.append({
|
| 93 |
+
"Task ID": task_id,
|
| 94 |
+
"Question": question_text,
|
| 95 |
+
"Submitted Answer": answer
|
| 96 |
+
})
|
| 97 |
+
|
| 98 |
+
submission_data = {
|
| 99 |
+
"username": username,
|
| 100 |
+
"agent_code": agent_code,
|
| 101 |
+
"answers": answers_payload
|
| 102 |
+
}
|
| 103 |
+
|
| 104 |
+
# Submit answers
|
| 105 |
try:
|
| 106 |
+
response = requests.post(
|
| 107 |
+
f"{DEFAULT_API_URL}/submit",
|
| 108 |
+
json=submission_data,
|
| 109 |
+
timeout=60
|
| 110 |
+
).json()
|
| 111 |
+
|
| 112 |
+
status = (
|
| 113 |
f"Submission Successful!\n"
|
| 114 |
+
f"User: {response.get('username')}\n"
|
| 115 |
+
f"Overall Score: {response.get('score')}% "
|
| 116 |
+
f"({response.get('correct_count')}/"
|
| 117 |
+
f"{response.get('total_attempted')} correct)\n"
|
| 118 |
+
f"Message: {response.get('message')}"
|
| 119 |
)
|
|
|
|
|
|
|
| 120 |
except Exception as e:
|
| 121 |
+
status = f"Submission failed: {e}"
|
| 122 |
+
|
| 123 |
+
return status, pd.DataFrame(results_log)
|
| 124 |
|
| 125 |
|
| 126 |
+
# ---------------- GRADIO UI ----------------
|
| 127 |
with gr.Blocks() as demo:
|
| 128 |
+
gr.Markdown("# GAIA Unit 4 – Basic Agent Runner")
|
| 129 |
+
|
| 130 |
gr.LoginButton()
|
| 131 |
+
run_btn = gr.Button("Run Evaluation & Submit")
|
| 132 |
+
|
| 133 |
+
status_box = gr.Textbox(
|
| 134 |
+
label="Submission Result",
|
| 135 |
+
lines=5,
|
| 136 |
+
interactive=False
|
| 137 |
+
)
|
| 138 |
|
| 139 |
+
results_table = gr.DataFrame(
|
| 140 |
+
label="Questions and Answers",
|
| 141 |
+
wrap=True
|
| 142 |
+
)
|
| 143 |
|
| 144 |
+
run_btn.click(
|
| 145 |
+
fn=run_and_submit_all,
|
| 146 |
+
outputs=[status_box, results_table]
|
| 147 |
+
)
|
| 148 |
|
| 149 |
if __name__ == "__main__":
|
| 150 |
+
demo.launch(debug=True)
|