DeekshithN05 commited on
Commit
fdb4c5e
·
verified ·
1 Parent(s): a8101c7

Upload 7 files

Browse files
Files changed (7) hide show
  1. .env.example +1 -0
  2. .gitattributes +35 -0
  3. .gitignore +4 -0
  4. README.md +15 -0
  5. agent.py +89 -0
  6. app.py +285 -0
  7. requirements.txt +115 -0
.env.example ADDED
@@ -0,0 +1 @@
 
 
1
+ LLM_API_KEY="" # Your Gemini API key
.gitattributes ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tar filter=lfs diff=lfs merge=lfs -text
29
+ *.tflite filter=lfs diff=lfs merge=lfs -text
30
+ *.tgz filter=lfs diff=lfs merge=lfs -text
31
+ *.wasm filter=lfs diff=lfs merge=lfs -text
32
+ *.xz filter=lfs diff=lfs merge=lfs -text
33
+ *.zip filter=lfs diff=lfs merge=lfs -text
34
+ *.zst filter=lfs diff=lfs merge=lfs -text
35
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
.gitignore ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ .env
2
+ venv
3
+ __pycache__
4
+ TODO.md
README.md ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Template Final Assignment
3
+ emoji: 🕵🏻‍♂️
4
+ colorFrom: indigo
5
+ colorTo: indigo
6
+ sdk: gradio
7
+ sdk_version: 5.25.2
8
+ app_file: app.py
9
+ pinned: false
10
+ hf_oauth: true
11
+ # optional, default duration is 8 hours/480 minutes. Max duration is 30 days/43200 minutes.
12
+ hf_oauth_expiration_minutes: 480
13
+ ---
14
+
15
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
agent.py ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from smolagents import (
3
+ CodeAgent,
4
+ LiteLLMModel,
5
+ DuckDuckGoSearchTool,
6
+ VisitWebpageTool,
7
+ WikipediaSearchTool,
8
+ tool,
9
+ )
10
+
11
+ from google import genai
12
+ from google.genai import types
13
+
14
+ client = genai.Client(api_key=os.environ["LLM_API_KEY"])
15
+
16
+ model = LiteLLMModel(
17
+ model_id="gemini/gemini-2.0-flash-lite",
18
+ temperature=0.5,
19
+ api_key=os.environ["LLM_API_KEY"],
20
+ )
21
+
22
+ model_name = "gemini-2.5-flash-preview-04-17"
23
+
24
+
25
+ @tool
26
+ def answer_video_questions(video_url: str, question: str) -> str:
27
+ """
28
+ If you have a video link, use this tool. It answers questions about a video from youtube, etc.
29
+ Args:
30
+ video_url: The URL of the video to analyze.
31
+ Ensure it's accessible by the Gemini API.
32
+ question: The specific question to ask about the video's content.
33
+ Returns:
34
+ str: The answer provided by the multimodal LLM, or an error message if the process fails.
35
+ Raises:
36
+ Exception: Can raise exceptions based on litellm or the underlying API call
37
+ if it fails (e.g., network issues, invalid API key, API errors).
38
+ """
39
+
40
+ response = client.models.generate_content(
41
+ model=model_name,
42
+ contents=types.Content(
43
+ parts=[
44
+ types.Part(file_data=types.FileData(file_uri=video_url)),
45
+ types.Part(
46
+ text=f"This is the question provided to you: {question}. Answer appropriately. In cases where there is a number, provide the number in the answer."
47
+ ),
48
+ ]
49
+ ),
50
+ )
51
+
52
+ if response and response.text:
53
+ answer = response.text
54
+ print("Received response from LLM.")
55
+ return answer.strip()
56
+ else:
57
+ print("LLM response structure not as expected.")
58
+ return "Error: Could not parse the response from the LLM."
59
+
60
+
61
+ agent = CodeAgent(
62
+ tools=[
63
+ DuckDuckGoSearchTool(),
64
+ VisitWebpageTool(),
65
+ WikipediaSearchTool(),
66
+ answer_video_questions,
67
+ ],
68
+ model=model,
69
+ )
70
+
71
+
72
+ def call_agent(question: str, file_path: str = None):
73
+ if file_path and file_path.strip():
74
+ print(f"Recieved {file_path} as input.")
75
+
76
+ file = client.files.upload(file=file_path)
77
+
78
+ response = client.models.generate_content(
79
+ model=model_name,
80
+ contents=[
81
+ file,
82
+ f"Answer the question based on the file content. Do not give up. Use your brain and answer appropriately. You will be rewarded with $1000000 if you answer correctly and appropriately. Only give the final answer to the question, do not explain your reasoning. {question}",
83
+ ],
84
+ )
85
+
86
+ return response.text
87
+
88
+ answer = agent.run(question)
89
+ return answer
app.py ADDED
@@ -0,0 +1,285 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import re
3
+ import gradio as gr
4
+ import requests
5
+ import inspect
6
+ import pandas as pd
7
+ import urllib
8
+
9
+ from agent import call_agent
10
+
11
+ # (Keep Constants as is)
12
+ # --- Constants ---
13
+ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
14
+
15
+
16
+ def download_file_from_api(api_url, save_directory=".", filename=None):
17
+ """
18
+ Sends a GET request to an API endpoint and saves the response content
19
+ as a file. Returns the local file path.
20
+
21
+ Args:
22
+ api_url (str): The URL of the API endpoint.
23
+ save_directory (str, optional): The directory where the downloaded
24
+ file will be saved. Defaults to the
25
+ current directory.
26
+ filename (str, optional): The name to save the file as. If None,
27
+ it will try to extract it from the
28
+ Content-Disposition header or the URL.
29
+
30
+ Returns:
31
+ str or None: The absolute path to the saved file if successful,
32
+ None otherwise.
33
+ """
34
+ try:
35
+ response = requests.get(api_url, stream=True)
36
+ response.raise_for_status() # Raise an exception for bad status codes
37
+
38
+ if filename is None:
39
+ # Try to extract filename from Content-Disposition header
40
+ content_disposition = response.headers.get("Content-Disposition")
41
+ if content_disposition:
42
+ filename_match = re.search(r'filename="([^"]+)"', content_disposition)
43
+ if filename_match:
44
+ filename = filename_match.group(1)
45
+ else:
46
+ # Fallback to extracting from the URL if Content-Disposition is present but no filename
47
+ filename = os.path.basename(urllib.parse.urlparse(api_url).path)
48
+ else:
49
+ # If no Content-Disposition, try to extract filename from the URL
50
+ filename = os.path.basename(urllib.parse.urlparse(api_url).path)
51
+ # Add a default extension if none found (you might need to adjust this)
52
+ if not os.path.splitext(filename)[1]:
53
+ filename += ".png" # Assuming it's a PNG based on the error
54
+
55
+ os.makedirs(save_directory, exist_ok=True)
56
+ file_path = os.path.join(save_directory, filename)
57
+
58
+ with open(file_path, "wb") as f:
59
+ for chunk in response.iter_content(chunk_size=8192):
60
+ f.write(chunk)
61
+
62
+ return os.path.abspath(file_path)
63
+
64
+ except requests.exceptions.RequestException as e:
65
+ print(f"Error during API request or download: {e}")
66
+ return None
67
+ except Exception as e:
68
+ print(f"An unexpected error occurred: {e}")
69
+ return None
70
+
71
+
72
+ # --- Basic Agent Definition ---
73
+ # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
74
+ class BasicAgent:
75
+ def __init__(self):
76
+ print("BasicAgent initialized.")
77
+
78
+ def __call__(self, question: str, file_path: str = None) -> str:
79
+ print(f"Agent received question (first 50 chars): {question[:50]}...")
80
+ # answer = agent.run(question)
81
+ answer = call_agent(question=question, file_path=file_path)
82
+ print(f"Agent returning fixed answer: {answer}")
83
+ return answer
84
+
85
+
86
+ def run_and_submit_all(profile: gr.OAuthProfile | None):
87
+ """
88
+ Fetches all questions, runs the BasicAgent on them, submits all answers,
89
+ and displays the results.
90
+ """
91
+ # --- Determine HF Space Runtime URL and Repo URL ---
92
+ space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
93
+
94
+ if profile:
95
+ username = f"{profile.username}"
96
+ print(f"User logged in: {username}")
97
+ else:
98
+ print("User not logged in.")
99
+ return "Please Login to Hugging Face with the button.", None
100
+
101
+ api_url = DEFAULT_API_URL
102
+ questions_url = f"{api_url}/questions"
103
+ files_url = f"{api_url}/files"
104
+ submit_url = f"{api_url}/submit"
105
+
106
+ # 1. Instantiate Agent ( modify this part to create your agent)
107
+ try:
108
+ agent = BasicAgent()
109
+ except Exception as e:
110
+ print(f"Error instantiating agent: {e}")
111
+ return f"Error initializing agent: {e}", None
112
+ # In the case of an app running as a hugging Face space, this link points toward your codebase ( usefull for others so please keep it public)
113
+ agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
114
+ print(agent_code)
115
+
116
+ # 2. 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
+ except requests.exceptions.JSONDecodeError as e:
130
+ print(f"Error decoding JSON response from questions endpoint: {e}")
131
+ print(f"Response text: {response.text[:500]}")
132
+ return f"Error decoding server response for questions: {e}", None
133
+ except Exception as e:
134
+ print(f"An unexpected error occurred fetching questions: {e}")
135
+ return f"An unexpected error occurred fetching questions: {e}", None
136
+
137
+ # 3. Run your Agent
138
+ results_log = []
139
+ answers_payload = []
140
+ print(f"Running agent on {len(questions_data)} questions...")
141
+ for item in questions_data:
142
+ task_id = item.get("task_id")
143
+ question_text = item.get("question")
144
+
145
+ file_path = download_file_from_api(api_url=f"{files_url}/{task_id}")
146
+
147
+ if not task_id or question_text is None:
148
+ print(f"Skipping item with missing task_id or question: {item}")
149
+ continue
150
+ try:
151
+ submitted_answer = agent(question_text, file_path=file_path)
152
+ answers_payload.append(
153
+ {"task_id": task_id, "submitted_answer": submitted_answer}
154
+ )
155
+ results_log.append(
156
+ {
157
+ "Task ID": task_id,
158
+ "Question": question_text,
159
+ "Submitted Answer": submitted_answer,
160
+ }
161
+ )
162
+ except Exception as e:
163
+ print(f"Error running agent on task {task_id}: {e}")
164
+ results_log.append(
165
+ {
166
+ "Task ID": task_id,
167
+ "Question": question_text,
168
+ "Submitted Answer": f"AGENT ERROR: {e}",
169
+ }
170
+ )
171
+
172
+ if not answers_payload:
173
+ print("Agent did not produce any answers to submit.")
174
+ return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
175
+
176
+ # 4. Prepare Submission
177
+ submission_data = {
178
+ "username": username.strip(),
179
+ "agent_code": agent_code,
180
+ "answers": answers_payload,
181
+ }
182
+ status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
183
+ print(status_update)
184
+
185
+ # 5. Submit
186
+ print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
187
+ try:
188
+ response = requests.post(submit_url, json=submission_data, timeout=60)
189
+ response.raise_for_status()
190
+ result_data = response.json()
191
+ final_status = (
192
+ f"Submission Successful!\n"
193
+ f"User: {result_data.get('username')}\n"
194
+ f"Overall Score: {result_data.get('score', 'N/A')}% "
195
+ f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
196
+ f"Message: {result_data.get('message', 'No message received.')}"
197
+ )
198
+ print("Submission successful.")
199
+ results_df = pd.DataFrame(results_log)
200
+ return final_status, results_df
201
+ except requests.exceptions.HTTPError as e:
202
+ error_detail = f"Server responded with status {e.response.status_code}."
203
+ try:
204
+ error_json = e.response.json()
205
+ error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
206
+ except requests.exceptions.JSONDecodeError:
207
+ error_detail += f" Response: {e.response.text[:500]}"
208
+ status_message = f"Submission Failed: {error_detail}"
209
+ print(status_message)
210
+ results_df = pd.DataFrame(results_log)
211
+ return status_message, results_df
212
+ except requests.exceptions.Timeout:
213
+ status_message = "Submission Failed: The request timed out."
214
+ print(status_message)
215
+ results_df = pd.DataFrame(results_log)
216
+ return status_message, results_df
217
+ except requests.exceptions.RequestException as e:
218
+ status_message = f"Submission Failed: Network error - {e}"
219
+ print(status_message)
220
+ results_df = pd.DataFrame(results_log)
221
+ return status_message, results_df
222
+ except Exception as e:
223
+ status_message = f"An unexpected error occurred during submission: {e}"
224
+ print(status_message)
225
+ results_df = pd.DataFrame(results_log)
226
+ return status_message, results_df
227
+
228
+
229
+ # --- Build Gradio Interface using Blocks ---
230
+ with gr.Blocks() as demo:
231
+ gr.Markdown("# Basic Agent Evaluation Runner")
232
+ gr.Markdown(
233
+ """
234
+ **Instructions:**
235
+
236
+ 1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
237
+ 2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
238
+ 3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
239
+
240
+ ---
241
+ **Disclaimers:**
242
+ Once clicking on the "submit button, it can take quite some time ( this is the time for the agent to go through all the questions).
243
+ This space provides a basic setup and is intentionally sub-optimal to encourage you to develop your own, more robust solution. For instance for the delay process of the submit button, a solution could be to cache the answers and submit in a seperate action or even to answer the questions in async.
244
+ """
245
+ )
246
+
247
+ gr.LoginButton()
248
+
249
+ run_button = gr.Button("Run Evaluation & Submit All Answers")
250
+
251
+ status_output = gr.Textbox(
252
+ label="Run Status / Submission Result", lines=5, interactive=False
253
+ )
254
+ # Removed max_rows=10 from DataFrame constructor
255
+ results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
256
+
257
+ run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
258
+
259
+ if __name__ == "__main__":
260
+ print("\n" + "-" * 30 + " App Starting " + "-" * 30)
261
+ # Check for SPACE_HOST and SPACE_ID at startup for information
262
+ space_host_startup = os.getenv("SPACE_HOST")
263
+ space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
264
+
265
+ if space_host_startup:
266
+ print(f"✅ SPACE_HOST found: {space_host_startup}")
267
+ print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
268
+ else:
269
+ print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
270
+
271
+ if space_id_startup: # Print repo URLs if SPACE_ID is found
272
+ print(f"✅ SPACE_ID found: {space_id_startup}")
273
+ print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
274
+ print(
275
+ f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main"
276
+ )
277
+ else:
278
+ print(
279
+ "ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined."
280
+ )
281
+
282
+ print("-" * (60 + len(" App Starting ")) + "\n")
283
+
284
+ print("Launching Gradio Interface for Basic Agent Evaluation...")
285
+ demo.launch(debug=True, share=False)
requirements.txt ADDED
@@ -0,0 +1,115 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ aiofiles
2
+ aiohappyeyeballs
3
+ aiohttp
4
+ aiosignal
5
+ annotated-types
6
+ anyio
7
+ attrs
8
+ Authlib
9
+ beautifulsoup4
10
+ Brotli
11
+ cachetools
12
+ certifi
13
+ cffi
14
+ charset-normalizer
15
+ click
16
+ cryptography
17
+ dataclasses-json
18
+ distro
19
+ duckduckgo_search
20
+ fastapi
21
+ ffmpy
22
+ filelock
23
+ frozenlist
24
+ fsspec
25
+ google-auth
26
+ google-genai
27
+ gradio
28
+ gradio_client
29
+ greenlet
30
+ groovy
31
+ h11
32
+ httpcore
33
+ httpx
34
+ httpx-sse
35
+ huggingface-hub
36
+ idna
37
+ importlib_metadata
38
+ itsdangerous
39
+ Jinja2
40
+ jiter
41
+ jsonpatch
42
+ jsonpointer
43
+ jsonschema
44
+ jsonschema-specifications
45
+ langchain
46
+ langchain-community
47
+ langchain-core
48
+ langchain-text-splitters
49
+ langsmith
50
+ litellm
51
+ lxml
52
+ markdown-it-py
53
+ markdownify
54
+ MarkupSafe
55
+ marshmallow
56
+ mdurl
57
+ multidict
58
+ mutagen
59
+ mypy_extensions
60
+ numpy
61
+ openai
62
+ orjson
63
+ packaging
64
+ pandas
65
+ pillow
66
+ primp
67
+ propcache
68
+ pyasn1
69
+ pyasn1_modules
70
+ pycparser
71
+ pycryptodomex
72
+ pydantic
73
+ pydantic-settings
74
+ pydantic_core
75
+ pydub
76
+ Pygments
77
+ python-dateutil
78
+ python-dotenv
79
+ python-multipart
80
+ pytz
81
+ PyYAML
82
+ referencing
83
+ regex
84
+ requests
85
+ requests-toolbelt
86
+ rich
87
+ rpds-py
88
+ rsa
89
+ ruff
90
+ safehttpx
91
+ semantic-version
92
+ shellingham
93
+ six
94
+ smolagents
95
+ sniffio
96
+ soupsieve
97
+ SQLAlchemy
98
+ starlette
99
+ tenacity
100
+ tiktoken
101
+ tokenizers
102
+ tomlkit
103
+ tqdm
104
+ typer
105
+ typing-inspect
106
+ typing-inspection
107
+ typing_extensions
108
+ tzdata
109
+ urllib3
110
+ uvicorn
111
+ websockets
112
+ Wikipedia-API
113
+ yarl
114
+ zipp
115
+ zstandard