PradeepBodhi commited on
Commit
be2ffde
·
verified ·
1 Parent(s): ff5f09a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -119
app.py CHANGED
@@ -1,160 +1,118 @@
1
  import os
2
  import gradio as gr
3
  import requests
 
4
  import pandas as pd
5
- from transformers import pipeline
 
 
 
6
 
7
  # --- Constants ---
8
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
 
 
9
 
10
- # --- Basic Agent Definition ---
11
  class BasicAgent:
12
  def __init__(self):
13
- print("Loading FLAN-T5 model...")
14
- self.model = pipeline("text2text-generation", model="google/flan-t5-base", max_length=256)
15
- print("Model loaded.")
16
-
17
  def __call__(self, question: str) -> str:
18
- prompt = f"Answer the following question accurately and concisely:\n{question.strip()}"
19
- result = self.model(prompt)
20
- answer = result[0]['generated_text'].strip()
21
- print(f"Answer generated: {answer}")
22
- return answer
23
-
24
- # --- Helper Functions ---
25
- def fetch_questions(api_url: str):
26
- """
27
- Fetches the questions from the provided API URL.
28
- Returns a list of questions or raises an error.
29
- """
30
- questions_url = f"{api_url}/questions"
31
- try:
32
- response = requests.get(questions_url, timeout=15)
33
- response.raise_for_status()
34
- questions_data = response.json()
35
- if not questions_data:
36
- print("Fetched questions list is empty.")
37
- return None
38
- print(f"Fetched {len(questions_data)} questions.")
39
- return questions_data
40
- except requests.exceptions.RequestException as e:
41
- print(f"Error fetching questions: {e}")
42
- return None
43
- except requests.exceptions.JSONDecodeError as e:
44
- print(f"Error decoding JSON response from questions endpoint: {e}")
45
- print(f"Response text: {response.text[:500]}")
46
- return None
47
-
48
- def submit_answers(api_url: str, submission_data: dict):
49
- """
50
- Submits the answers to the leaderboard API.
51
- """
52
- submit_url = f"{api_url}/submit"
53
- try:
54
- response = requests.post(submit_url, json=submission_data, timeout=60)
55
- response.raise_for_status()
56
- result_data = response.json()
57
- return result_data
58
- except requests.exceptions.RequestException as e:
59
- print(f"Submission failed: {e}")
60
- return None
61
-
62
- # --- Main Execution Function ---
63
  def run_and_submit_all(profile: gr.OAuthProfile | None):
64
- """
65
- Fetches all questions, runs the agent on them, and submits all answers.
66
- """
67
- # --- Check Hugging Face Profile ---
68
  if profile:
69
- username = profile.username
70
- print(f"User logged in: {username}")
71
  else:
72
- print("User not logged in.")
73
- return "Please log in to Hugging Face with the button.", None
 
 
 
74
 
75
- # --- Initialize Agent ---
76
  try:
77
- agent = BasicAgent()
 
 
78
  except Exception as e:
79
- print(f"Error initializing agent: {e}")
80
- return f"Error initializing agent: {e}", None
81
-
82
- # --- Fetch Questions ---
83
- questions_data = fetch_questions(DEFAULT_API_URL)
84
- if not questions_data:
85
- return "Failed to fetch questions.", None
86
 
87
- # --- Process Questions ---
88
  answers_payload = []
89
  results_log = []
90
  for item in questions_data:
91
  task_id = item.get("task_id")
92
  question_text = item.get("question")
93
- if not task_id or question_text is None:
94
- print(f"Skipping item with missing task_id or question: {item}")
95
  continue
96
  try:
97
- submitted_answer = agent.call(question_text)
98
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
99
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
100
  except Exception as e:
101
- print(f"Error processing task {task_id}: {e}")
102
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
103
-
104
- # --- Prepare Submission Data ---
105
- if not answers_payload:
106
- return "No answers to submit.", pd.DataFrame(results_log)
107
 
108
  submission_data = {
109
  "username": username.strip(),
110
- "agent_code": f"https://huggingface.co/spaces/{os.getenv('SPACE_ID')}/tree/main",
111
  "answers": answers_payload
112
  }
113
 
114
- # --- Submit Answers ---
115
- result_data = submit_answers(DEFAULT_API_URL, submission_data)
116
- if not result_data:
117
- return "Submission failed.", pd.DataFrame(results_log)
118
-
119
- final_status = (
120
- f"Submission Successful!\n"
121
- f"User: {result_data.get('username')}\n"
122
- f"Overall Score: {result_data.get('score', 'N/A')}% "
123
- f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
124
- f"Message: {result_data.get('message', 'No message received.')}"
125
- )
126
-
127
- results_df = pd.DataFrame(results_log)
128
- return final_status, results_df
129
 
130
  # --- Gradio Interface ---
131
  with gr.Blocks() as demo:
132
- gr.Markdown("# Basic Agent Evaluation Runner")
133
- gr.Markdown(
134
- """
135
  **Instructions:**
136
- 1. Clone this space and modify the code to define your agent's logic and tools.
137
- 2. Log in to your Hugging Face account using the button below.
138
- 3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
139
-
140
- ---
141
- **Disclaimers:**
142
- Clicking "submit" might take some time to process all questions and submit answers.
143
- """
144
- )
145
-
146
  gr.LoginButton()
 
 
 
 
147
 
148
- run_button = gr.Button("Run Evaluation & Submit All Answers")
149
- status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
150
- results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
151
-
152
- run_button.click(
153
- fn=run_and_submit_all,
154
- outputs=[status_output, results_table]
155
- )
156
-
157
- # --- Run the Interface ---
158
  if __name__ == "__main__":
159
- print("\n" + "-"*30 + " App Starting " + "-"*30)
160
- demo.launch(debug=True, share=False)
 
1
  import os
2
  import gradio as gr
3
  import requests
4
+ import time
5
  import pandas as pd
6
+ from dotenv import load_dotenv
7
+
8
+ # Load environment variables
9
+ load_dotenv()
10
 
11
  # --- Constants ---
12
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
13
+ HF_API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.2"
14
+ HUGGINGFACE_TOKEN = os.getenv("HUGGINGFACE_TOKEN")
15
 
16
+ # --- Agent Definition ---
17
  class BasicAgent:
18
  def __init__(self):
19
+ self.headers = {"Authorization": f"Bearer {HUGGINGFACE_TOKEN}"}
20
+
 
 
21
  def __call__(self, question: str) -> str:
22
+ prompt = f"""Answer the question concisely and factually. Provide only the answer in the exact format required by the question, with no additional text, explanations, or formatting. Do not use quotation marks, bullet points, or any other formatting. Ensure correct casing and pluralization as specified.
23
+
24
+ Question: {question}
25
+ Answer: """
26
+ payload = {
27
+ "inputs": prompt,
28
+ "parameters": {
29
+ "max_new_tokens": 100,
30
+ "return_full_text": False
31
+ }
32
+ }
33
+ max_retries = 3
34
+ for _ in range(max_retries):
35
+ try:
36
+ response = requests.post(HF_API_URL, headers=self.headers, json=payload, timeout=30)
37
+ response.raise_for_status()
38
+ answer = response.json()[0]['generated_text'].strip()
39
+ # Clean up answer
40
+ answer = answer.strip().rstrip('.').strip()
41
+ return answer
42
+ except requests.exceptions.HTTPError as e:
43
+ if e.response.status_code == 503:
44
+ time.sleep(10)
45
+ else:
46
+ print(f"HTTP Error: {e.response.text}")
47
+ break
48
+ except requests.exceptions.RequestException as e:
49
+ print(f"Request failed: {e}, retrying...")
50
+ time.sleep(5)
51
+ except Exception as e:
52
+ print(f"Error: {e}")
53
+ break
54
+ return "Error: Unable to generate answer."
55
+
56
+ # --- Submission Logic ---
 
 
 
 
 
 
 
 
 
 
57
  def run_and_submit_all(profile: gr.OAuthProfile | None):
 
 
 
 
58
  if profile:
59
+ username = f"{profile.username}"
 
60
  else:
61
+ return "Please Login to Hugging Face with the button.", None
62
+
63
+ agent = BasicAgent()
64
+ space_id = os.getenv("SPACE_ID")
65
+ agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else ""
66
 
 
67
  try:
68
+ response = requests.get(f"{DEFAULT_API_URL}/questions", timeout=15)
69
+ response.raise_for_status()
70
+ questions_data = response.json()
71
  except Exception as e:
72
+ return f"Error fetching questions: {e}", None
 
 
 
 
 
 
73
 
 
74
  answers_payload = []
75
  results_log = []
76
  for item in questions_data:
77
  task_id = item.get("task_id")
78
  question_text = item.get("question")
79
+ if not task_id or not question_text:
 
80
  continue
81
  try:
82
+ submitted_answer = agent(question_text)
83
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
84
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
85
  except Exception as e:
86
+ results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"Error: {e}"})
 
 
 
 
 
87
 
88
  submission_data = {
89
  "username": username.strip(),
90
+ "agent_code": agent_code,
91
  "answers": answers_payload
92
  }
93
 
94
+ try:
95
+ response = requests.post(f"{DEFAULT_API_URL}/submit", json=submission_data, timeout=60)
96
+ response.raise_for_status()
97
+ result_data = response.json()
98
+ final_status = f"Success! Score: {result_data.get('score', 'N/A')}%"
99
+ return final_status, pd.DataFrame(results_log)
100
+ except Exception as e:
101
+ return f"Submission failed: {e}", pd.DataFrame(results_log)
 
 
 
 
 
 
 
102
 
103
  # --- Gradio Interface ---
104
  with gr.Blocks() as demo:
105
+ gr.Markdown("# GAIA Evaluation Agent")
106
+ gr.Markdown("""
 
107
  **Instructions:**
108
+ 1. Log in to your Hugging Face account using the button below.
109
+ 2. Click 'Run Evaluation & Submit Answers' to fetch questions, run your agent, and submit answers.
110
+ """)
 
 
 
 
 
 
 
111
  gr.LoginButton()
112
+ run_button = gr.Button("Run Evaluation & Submit Answers")
113
+ status_output = gr.Textbox(label="Status")
114
+ results_table = gr.DataFrame(label="Results")
115
+ run_button.click(run_and_submit_all, outputs=[status_output, results_table])
116
 
 
 
 
 
 
 
 
 
 
 
117
  if __name__ == "__main__":
118
+ demo.launch()