Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,147 +1,54 @@
|
|
| 1 |
-
import os
|
| 2 |
import gradio as gr
|
| 3 |
-
import
|
| 4 |
-
import
|
| 5 |
-
|
| 6 |
-
from transformers import pipeline, set_seed
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
def __init__(self):
|
| 14 |
-
print("BasicAgent initialized")
|
| 15 |
-
set_seed(42)
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
model="google/flan-t5-large",
|
| 20 |
-
max_new_tokens=64,
|
| 21 |
-
temperature=0.0,
|
| 22 |
-
do_sample=False
|
| 23 |
-
)
|
| 24 |
-
|
| 25 |
-
def __call__(self, question: str) -> str:
|
| 26 |
-
prompt = (
|
| 27 |
-
"Answer the question EXACTLY as expected.\n"
|
| 28 |
-
"Give the final answer only.\n"
|
| 29 |
-
"Do not explain.\n\n"
|
| 30 |
-
f"Question:\n{question}\n\n"
|
| 31 |
-
"Answer:"
|
| 32 |
-
)
|
| 33 |
-
|
| 34 |
-
result = self.generator(prompt)[0]["generated_text"]
|
| 35 |
-
|
| 36 |
-
# ✅ MINIMAL CLEANUP (CRITICAL)
|
| 37 |
-
answer = result.strip()
|
| 38 |
|
| 39 |
-
# Remove only obvious prefixes
|
| 40 |
-
answer = re.sub(r"(?i)^(answer:|the answer is)\s*", "", answer)
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
print(f"\nQ: {question}\nA: {answer}\n")
|
| 46 |
-
return answer
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
# ---------------- RUN + SUBMIT ----------------
|
| 50 |
-
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 51 |
-
space_id = os.getenv("SPACE_ID")
|
| 52 |
-
|
| 53 |
-
if not profile:
|
| 54 |
-
return "Please login with Hugging Face.", pd.DataFrame()
|
| 55 |
-
|
| 56 |
-
username = profile.username
|
| 57 |
-
agent = BasicAgent()
|
| 58 |
-
|
| 59 |
-
agent_code = (
|
| 60 |
-
f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 61 |
-
if space_id else "N/A"
|
| 62 |
-
)
|
| 63 |
|
| 64 |
-
|
| 65 |
-
try:
|
| 66 |
-
questions = requests.get(
|
| 67 |
-
f"{DEFAULT_API_URL}/questions", timeout=15
|
| 68 |
-
).json()
|
| 69 |
-
except Exception as e:
|
| 70 |
-
return f"Failed to fetch questions: {e}", pd.DataFrame()
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
try:
|
| 80 |
-
answer = agent(question_text)
|
| 81 |
-
except Exception as e:
|
| 82 |
-
answer = "ERROR"
|
| 83 |
-
|
| 84 |
-
answers_payload.append({
|
| 85 |
-
"task_id": task_id,
|
| 86 |
-
"submitted_answer": answer
|
| 87 |
-
})
|
| 88 |
-
|
| 89 |
-
results_log.append({
|
| 90 |
-
"Task ID": task_id,
|
| 91 |
-
"Question": question_text,
|
| 92 |
-
"Submitted Answer": answer
|
| 93 |
-
})
|
| 94 |
-
|
| 95 |
-
submission_data = {
|
| 96 |
-
"username": username,
|
| 97 |
-
"agent_code": agent_code,
|
| 98 |
-
"answers": answers_payload
|
| 99 |
-
}
|
| 100 |
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
response = requests.post(
|
| 104 |
-
f"{DEFAULT_API_URL}/submit",
|
| 105 |
-
json=submission_data,
|
| 106 |
-
timeout=60
|
| 107 |
-
).json()
|
| 108 |
|
| 109 |
-
status = (
|
| 110 |
-
f"Submission Successful!\n"
|
| 111 |
-
f"User: {response.get('username')}\n"
|
| 112 |
-
f"Overall Score: {response.get('score')}% "
|
| 113 |
-
f"({response.get('correct_count')}/"
|
| 114 |
-
f"{response.get('total_attempted')} correct)\n"
|
| 115 |
-
f"Message: {response.get('message')}"
|
| 116 |
-
)
|
| 117 |
-
except Exception as e:
|
| 118 |
-
status = f"Submission failed: {e}"
|
| 119 |
|
| 120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
|
| 122 |
|
| 123 |
-
# ---------------- GRADIO UI ----------------
|
| 124 |
with gr.Blocks() as demo:
|
| 125 |
gr.Markdown("# GAIA Unit 4 – Basic Agent Runner")
|
| 126 |
|
| 127 |
-
gr.LoginButton()
|
| 128 |
run_btn = gr.Button("Run Evaluation & Submit")
|
| 129 |
-
|
| 130 |
-
status_box = gr.Textbox(
|
| 131 |
-
label="Submission Result",
|
| 132 |
-
lines=5,
|
| 133 |
-
interactive=False
|
| 134 |
-
)
|
| 135 |
-
|
| 136 |
-
results_table = gr.DataFrame(
|
| 137 |
-
label="Questions and Answers",
|
| 138 |
-
wrap=True
|
| 139 |
-
)
|
| 140 |
|
| 141 |
run_btn.click(
|
| 142 |
-
fn=
|
| 143 |
-
|
|
|
|
| 144 |
)
|
| 145 |
|
| 146 |
-
|
| 147 |
-
demo.launch(debug=True)
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 4 |
+
from gaia import run_gaia_evaluation
|
|
|
|
| 5 |
|
| 6 |
+
MODEL_NAME = "google/flan-t5-base" # use base for stability
|
|
|
|
| 7 |
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 9 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME)
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 12 |
+
model.to(device)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
def answer_question(question: str) -> str:
|
| 16 |
+
prompt = f"Answer the following question concisely:\n{question}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
inputs = tokenizer(prompt, return_tensors="pt", truncation=True).to(device)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
with torch.no_grad():
|
| 21 |
+
outputs = model.generate(
|
| 22 |
+
**inputs,
|
| 23 |
+
max_new_tokens=64,
|
| 24 |
+
do_sample=False
|
| 25 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
answer = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 28 |
+
return answer.strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
def run_evaluation():
|
| 32 |
+
"""
|
| 33 |
+
IMPORTANT:
|
| 34 |
+
- This function MUST return
|
| 35 |
+
- Must NOT print
|
| 36 |
+
- Must NOT loop forever
|
| 37 |
+
"""
|
| 38 |
+
results = run_gaia_evaluation(answer_question)
|
| 39 |
+
return results
|
| 40 |
|
| 41 |
|
|
|
|
| 42 |
with gr.Blocks() as demo:
|
| 43 |
gr.Markdown("# GAIA Unit 4 – Basic Agent Runner")
|
| 44 |
|
|
|
|
| 45 |
run_btn = gr.Button("Run Evaluation & Submit")
|
| 46 |
+
output = gr.JSON(label="Submission Result")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
run_btn.click(
|
| 49 |
+
fn=run_evaluation,
|
| 50 |
+
inputs=[],
|
| 51 |
+
outputs=output
|
| 52 |
)
|
| 53 |
|
| 54 |
+
demo.launch()
|
|
|