Rudraprasad commited on
Commit
e756fff
·
verified ·
1 Parent(s): 5c0f112

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -200
app.py CHANGED
@@ -1,200 +1,6 @@
1
- import os
2
- import gradio as gr
3
- import requests
4
- import inspect
5
- import pandas as pd
6
- from bs4 import BeautifulSoup
7
- import re
8
-
9
- # Constants
10
- DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
11
-
12
- # --- Generalized Tools ---
13
- class KnowledgeExtractionTool:
14
- def __init__(self):
15
- self.api_url = DEFAULT_API_URL
16
-
17
- def fetch_data(self, url):
18
- """Fetches data from a URL (Wikipedia or other sources)"""
19
- response = requests.get(url)
20
- if response.status_code == 200:
21
- return response.text
22
- return None
23
-
24
- def extract_winners_from_wikipedia(self, url):
25
- """Extracts competition winners from a Wikipedia page"""
26
- page_content = self.fetch_data(url)
27
- if not page_content:
28
- return []
29
-
30
- soup = BeautifulSoup(page_content, 'html.parser')
31
- winners_data = []
32
-
33
- # Look for the table of winners
34
- winners_table = soup.find('table', {'class': 'wikitable'})
35
- if winners_table:
36
- for row in winners_table.find_all('tr')[1:]: # Skipping the header row
37
- cells = row.find_all('td')
38
- if len(cells) > 3:
39
- name = cells[0].text.strip() # Winner's name
40
- year = int(cells[1].text.strip()) # Year
41
- nationality = cells[3].text.strip() # Nationality
42
- winners_data.append({"name": name, "year": year, "nationality": nationality})
43
- return winners_data
44
-
45
- def filter_winners(self, winners_data, year_threshold=1977, countries_to_check=None):
46
- """Filters winners by year and nationality (if country no longer exists)"""
47
- if not countries_to_check:
48
- countries_to_check = ["Yugoslavia", "Soviet Union", "East Germany"] # Example list
49
-
50
- filtered_winners = []
51
- for winner in winners_data:
52
- if winner["year"] > year_threshold and any(country in winner["nationality"] for country in countries_to_check):
53
- filtered_winners.append(winner)
54
- return filtered_winners
55
-
56
- def extract_first_name(self, full_name):
57
- """Extract the first name from a full name string"""
58
- return full_name.split()[0] if full_name else None
59
-
60
- def process_question(self, question):
61
- """Generalized function to process a wide variety of questions"""
62
- if "Malko Competition" in question and "first name" in question:
63
- print("Processing Malko Competition query...")
64
-
65
- # Fetch data from Wikipedia (or other sources)
66
- winners_data = self.extract_winners_from_wikipedia("https://en.wikipedia.org/wiki/Malko_Competition")
67
-
68
- # Filter by year and nationality of winners
69
- filtered_winners = self.filter_winners(winners_data)
70
-
71
- # Extract and return the first name
72
- if filtered_winners:
73
- first_name = self.extract_first_name(filtered_winners[0]["name"])
74
- return f"The first name of the winner is: {first_name}"
75
- else:
76
- return "No winners found with the specified conditions."
77
-
78
- # Add other types of questions and their handling here as needed
79
- return "Question could not be processed."
80
-
81
- # --- Basic Agent Definition ---
82
- class BasicAgent:
83
- def __init__(self):
84
- self.knowledge_tool = KnowledgeExtractionTool()
85
-
86
- def __call__(self, question: str) -> str:
87
- print(f"Processing question: {question}")
88
- return self.knowledge_tool.process_question(question)
89
-
90
- # --- Run and Submit All Function ---
91
- def run_and_submit_all(profile: gr.OAuthProfile | None):
92
- """
93
- Fetches all questions, runs the BasicAgent on them, submits all answers,
94
- and displays the results.
95
- """
96
- space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
97
-
98
- if profile:
99
- username = f"{profile.username}"
100
- print(f"User logged in: {username}")
101
- else:
102
- print("User not logged in.")
103
- return "Please Login to Hugging Face with the button.", None
104
-
105
- api_url = DEFAULT_API_URL
106
- questions_url = f"{api_url}/questions"
107
- submit_url = f"{api_url}/submit"
108
-
109
- # Instantiate Agent
110
- try:
111
- agent = BasicAgent()
112
- except Exception as e:
113
- print(f"Error instantiating agent: {e}")
114
- return f"Error initializing agent: {e}", None
115
-
116
- # Fetch Questions
117
- print(f"Fetching questions from: {questions_url}")
118
- try:
119
- response = requests.get(questions_url, timeout=15)
120
- response.raise_for_status()
121
- questions_data = response.json()
122
- if not questions_data:
123
- print("Fetched questions list is empty.")
124
- return "Fetched questions list is empty or invalid format.", None
125
- print(f"Fetched {len(questions_data)} questions.")
126
- except requests.exceptions.RequestException as e:
127
- print(f"Error fetching questions: {e}")
128
- return f"Error fetching questions: {e}", None
129
-
130
- # Run agent on each question
131
- results_log = []
132
- answers_payload = []
133
- print(f"Running agent on {len(questions_data)} questions...")
134
- for item in questions_data:
135
- task_id = item.get("task_id")
136
- question_text = item.get("question")
137
- if not task_id or question_text is None:
138
- print(f"Skipping item with missing task_id or question: {item}")
139
- continue
140
- try:
141
- submitted_answer = agent(question_text)
142
- answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
143
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
144
- except Exception as e:
145
- print(f"Error running agent on task {task_id}: {e}")
146
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
147
-
148
- if not answers_payload:
149
- print("Agent did not produce any answers to submit.")
150
- return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
151
-
152
- # Prepare and submit answers
153
- submission_data = {"username": username.strip(), "agent_code": f"https://huggingface.co/spaces/{os.getenv('SPACE_ID')}/tree/main", "answers": answers_payload}
154
- print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
155
- try:
156
- response = requests.post(submit_url, json=submission_data, timeout=60)
157
- response.raise_for_status()
158
- result_data = response.json()
159
- final_status = (
160
- f"Submission Successful!\n"
161
- f"User: {result_data.get('username')}\n"
162
- f"Overall Score: {result_data.get('score', 'N/A')}% "
163
- f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
164
- f"Message: {result_data.get('message', 'No message received.')}"
165
- )
166
- print("Submission successful.")
167
- results_df = pd.DataFrame(results_log)
168
- return final_status, results_df
169
- except requests.exceptions.RequestException as e:
170
- status_message = f"Submission Failed: Network error - {e}"
171
- print(status_message)
172
- results_df = pd.DataFrame(results_log)
173
- return status_message, results_df
174
-
175
- # --- Build Gradio Interface using Blocks ---
176
- with gr.Blocks() as demo:
177
- gr.Markdown("# Generalized Agent Evaluation Runner")
178
- gr.Markdown(
179
- """
180
- **Instructions:**
181
-
182
- 1. Clone this space and modify the code to define your agent's logic, tools, and packages.
183
- 2. Log in to your Hugging Face account using the button below.
184
- 3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
185
- """
186
- )
187
-
188
- gr.LoginButton()
189
-
190
- run_button = gr.Button("Run Evaluation & Submit All Answers")
191
- status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
192
- results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
193
-
194
- run_button.click(
195
- fn=run_and_submit_all,
196
- outputs=[status_output, results_table]
197
- )
198
-
199
- if __name__ == "__main__":
200
- demo.launch(debug=True, share=False)
 
1
+ login_btn = gr.LoginButton()
2
+ run_btn.click(
3
+ fn=run_and_submit_all,
4
+ inputs=[login_btn], # pass the profile from LoginButton
5
+ outputs=[status_out, results_tbl]
6
+ )