Annessha18 commited on
Commit
7637e78
·
verified ·
1 Parent(s): 5d9be4a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +101 -109
app.py CHANGED
@@ -5,154 +5,146 @@ import re
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
- # --- Smarter Basic Agent ---
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
- # --- Detect question type ---
24
- q_lower = question.lower()
25
-
26
- # Boolean detection
27
- if any(x in q_lower for x in ["true or false", "yes or no", "is it", "does it", "can it"]):
28
- prompt = (
29
- "Answer only True or False exactly. "
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
- # --- Post-process answer ---
58
  answer = result.strip()
59
- answer = re.sub(r"(?i)^(the answer is|answer:)\s*", "", answer) # remove prefixes
60
- answer = re.split(r"[.\n]", answer)[0].strip() # first line or sentence
61
- answer = answer.rstrip(".,;:") # remove trailing punctuation
62
-
63
- # Enforce boolean formatting
64
- if answer.lower() in ["true", "false"]:
65
- answer = answer.capitalize()
66
-
67
- print(f"Question: {question}\nFinal answer: {answer}\n")
 
 
 
68
  return answer
69
 
70
 
71
- # --- Run and Submit Function ---
72
  def run_and_submit_all(profile: gr.OAuthProfile | None):
73
  space_id = os.getenv("SPACE_ID")
74
 
75
- if profile:
76
- username = profile.username
77
- else:
78
- return "Please Login to Hugging Face with the button.", pd.DataFrame()
79
 
80
- api_url = DEFAULT_API_URL
81
- questions_url = f"{api_url}/questions"
82
- submit_url = f"{api_url}/submit"
83
 
84
- try:
85
- agent = BasicAgent()
86
- except Exception as e:
87
- return f"Error initializing agent: {e}", pd.DataFrame()
88
 
89
- agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else "Agent code URL not available."
90
-
91
- # --- Fetch Questions ---
92
  try:
93
- response = requests.get(questions_url, timeout=15)
94
- response.raise_for_status()
95
- questions_data = response.json()
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"Error fetching questions: {e}", pd.DataFrame()
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
- submitted_answer = agent(question_text)
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
- results_log.append({
119
- "Task ID": task_id,
120
- "Question": question_text,
121
- "Submitted Answer": f"AGENT ERROR: {e}"
122
- })
123
-
124
- if not answers_payload:
125
- return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
126
-
127
- submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
128
-
 
 
 
 
 
 
 
 
 
129
  try:
130
- response = requests.post(submit_url, json=submission_data, timeout=60)
131
- response.raise_for_status()
132
- result_data = response.json()
133
- final_status = (
 
 
 
134
  f"Submission Successful!\n"
135
- f"User: {result_data.get('username')}\n"
136
- f"Overall Score: {result_data.get('score', 'N/A')}% "
137
- f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
138
- f"Message: {result_data.get('message', 'No message received.')}"
 
139
  )
140
- results_df = pd.DataFrame(results_log)
141
- return final_status, results_df
142
  except Exception as e:
143
- return f"Submission Failed: {e}", pd.DataFrame(results_log)
 
 
144
 
145
 
146
- # --- Gradio Interface ---
147
  with gr.Blocks() as demo:
148
- gr.Markdown("# GAIA Smarter Agent Runner")
 
149
  gr.LoginButton()
150
- run_button = gr.Button("Run Evaluation & Submit All Answers")
151
- status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
152
- results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
 
 
 
 
153
 
154
- run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
 
 
 
155
 
 
 
 
 
156
 
157
  if __name__ == "__main__":
158
- demo.launch(debug=True, share=False)
 
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)