Spaces:
Runtime error
Runtime error
Implement final assignment agent
Browse files
app.py
CHANGED
|
@@ -1,23 +1,137 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
-
import inspect
|
| 5 |
import pandas as pd
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# (Keep Constants as is)
|
| 8 |
# --- Constants ---
|
| 9 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
# --- Basic Agent Definition ---
|
| 12 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 13 |
class BasicAgent:
|
|
|
|
| 14 |
def __init__(self):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
print("BasicAgent initialized.")
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 23 |
"""
|
|
@@ -76,11 +190,12 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 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 question_text is None:
|
| 80 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 81 |
continue
|
| 82 |
try:
|
| 83 |
-
submitted_answer = agent(question_text)
|
| 84 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 85 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 86 |
except Exception as e:
|
|
@@ -91,7 +206,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 91 |
print("Agent did not produce any answers to submit.")
|
| 92 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 93 |
|
| 94 |
-
# 4. Prepare Submission
|
| 95 |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
| 96 |
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
| 97 |
print(status_update)
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
|
|
|
| 4 |
import pandas as pd
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
+
from smolagents import CodeAgent, OpenAIServerModel, DuckDuckGoSearchTool, VisitWebpageTool, tool, \
|
| 7 |
+
FinalAnswerTool, PythonInterpreterTool, SpeechToTextTool
|
| 8 |
+
import yaml
|
| 9 |
+
import importlib
|
| 10 |
+
from io import BytesIO
|
| 11 |
+
import tempfile
|
| 12 |
+
import base64
|
| 13 |
+
from youtube_transcript_api import YouTubeTranscriptApi
|
| 14 |
+
from youtube_transcript_api._errors import TranscriptsDisabled, NoTranscriptFound, VideoUnavailable
|
| 15 |
+
from urllib.parse import urlparse, parse_qs
|
| 16 |
+
import json
|
| 17 |
+
|
| 18 |
+
load_dotenv()
|
| 19 |
|
| 20 |
# (Keep Constants as is)
|
| 21 |
# --- Constants ---
|
| 22 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 23 |
|
| 24 |
+
|
| 25 |
+
@tool
|
| 26 |
+
def get_youtube_transcript(video_url: str) -> str:
|
| 27 |
+
"""
|
| 28 |
+
Retrieves the transcript from a YouTube video URL, including timestamps.
|
| 29 |
+
|
| 30 |
+
This tool fetches the English transcript for a given YouTube video. Automatically generated subtitles
|
| 31 |
+
are also supported. The result includes each snippet's start time, duration, and text.
|
| 32 |
+
|
| 33 |
+
Args:
|
| 34 |
+
video_url: The full URL of the YouTube video (e.g., https://www.youtube.com/watch?v=12345)
|
| 35 |
+
|
| 36 |
+
Returns:
|
| 37 |
+
A JSON-formatted string containing either the transcript with timestamps or an error message.
|
| 38 |
+
{
|
| 39 |
+
"success": true,
|
| 40 |
+
"transcript": [
|
| 41 |
+
{"start": 0.0, "duration": 1.54, "text": "Hey there"},
|
| 42 |
+
{"start": 1.54, "duration": 4.16, "text": "how are you"},
|
| 43 |
+
...
|
| 44 |
+
]
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
OR
|
| 48 |
+
|
| 49 |
+
{
|
| 50 |
+
"success": false,
|
| 51 |
+
"error": "Reason why the transcript could not be retrieved"
|
| 52 |
+
}
|
| 53 |
+
"""
|
| 54 |
+
try:
|
| 55 |
+
# Extract video ID from URL
|
| 56 |
+
parsed_url = urlparse(video_url)
|
| 57 |
+
query_params = parse_qs(parsed_url.query)
|
| 58 |
+
video_id = query_params.get("v", [None])[0]
|
| 59 |
+
|
| 60 |
+
if not video_id:
|
| 61 |
+
return json.dumps({"success": False, "error": "Invalid YouTube URL. Could not extract video ID."})
|
| 62 |
+
|
| 63 |
+
fetched_transcript = YouTubeTranscriptApi().fetch(video_id)
|
| 64 |
+
transcript_data = [
|
| 65 |
+
{
|
| 66 |
+
"start": snippet.start,
|
| 67 |
+
"duration": snippet.duration,
|
| 68 |
+
"text": snippet.text
|
| 69 |
+
}
|
| 70 |
+
for snippet in fetched_transcript
|
| 71 |
+
]
|
| 72 |
+
|
| 73 |
+
return json.dumps({"success": True, "transcript": transcript_data})
|
| 74 |
+
|
| 75 |
+
except VideoUnavailable:
|
| 76 |
+
return json.dumps({"success": False, "error": "The video is unavailable."})
|
| 77 |
+
except TranscriptsDisabled:
|
| 78 |
+
return json.dumps({"success": False, "error": "Transcripts are disabled for this video."})
|
| 79 |
+
except NoTranscriptFound:
|
| 80 |
+
return json.dumps({"success": False, "error": "No transcript found for this video."})
|
| 81 |
+
except Exception as e:
|
| 82 |
+
return json.dumps({"success": False, "error": str(e)})
|
| 83 |
+
|
| 84 |
# --- Basic Agent Definition ---
|
| 85 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 86 |
class BasicAgent:
|
| 87 |
+
|
| 88 |
def __init__(self):
|
| 89 |
+
model = OpenAIServerModel(api_key=os.environ.get("OPENAI_API_KEY"), model_id="gpt-4o")
|
| 90 |
+
|
| 91 |
+
self.code_agent = CodeAgent(
|
| 92 |
+
tools=[PythonInterpreterTool(), DuckDuckGoSearchTool(), VisitWebpageTool(), SpeechToTextTool(),
|
| 93 |
+
get_youtube_transcript,
|
| 94 |
+
FinalAnswerTool()],
|
| 95 |
+
model=model,
|
| 96 |
+
max_steps=20,
|
| 97 |
+
name="hf_agent_course_final_assignment_solver",
|
| 98 |
+
prompt_templates=yaml.safe_load(
|
| 99 |
+
importlib.resources.files("prompts").joinpath("code_agent.yaml").read_text()
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
)
|
| 103 |
print("BasicAgent initialized.")
|
| 104 |
+
|
| 105 |
+
def __call__(self, task_id:str, question: str, file_name: str) -> str:
|
| 106 |
+
if file_name:
|
| 107 |
+
question = self.enrich_question_with_associated_file_details(task_id, question, file_name)
|
| 108 |
+
|
| 109 |
+
final_result = self.code_agent.run(question)
|
| 110 |
+
return str(final_result)
|
| 111 |
+
|
| 112 |
+
def enrich_question_with_associated_file_details(self, task_id:str, question: str, file_name: str) -> str:
|
| 113 |
+
api_url = DEFAULT_API_URL
|
| 114 |
+
get_associated_files_url = f"{api_url}/files/{task_id}"
|
| 115 |
+
response = requests.get(get_associated_files_url, timeout=15)
|
| 116 |
+
response.raise_for_status()
|
| 117 |
+
|
| 118 |
+
if file_name.endswith(".mp3"):
|
| 119 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
|
| 120 |
+
tmp_file.write(response.content)
|
| 121 |
+
file_path = tmp_file.name
|
| 122 |
+
return question + "\n\nMentioned .mp3 file local path is: " + file_path
|
| 123 |
+
elif file_name.endswith(".py"):
|
| 124 |
+
file_content = response.text
|
| 125 |
+
return question + "\n\nBelow is mentioned Python file:\n\n```python\n" + file_content + "\n```\n"
|
| 126 |
+
elif file_name.endswith(".xlsx"):
|
| 127 |
+
xlsx_io = BytesIO(response.content)
|
| 128 |
+
df = pd.read_excel(xlsx_io)
|
| 129 |
+
file_content = df.to_csv(index=False)
|
| 130 |
+
return question + "\n\nBelow is mentioned excel file in CSV format:\n\n```csv\n" + file_content + "\n```\n"
|
| 131 |
+
elif file_name.endswith(".png"):
|
| 132 |
+
base64_str = base64.b64encode(response.content).decode('utf-8')
|
| 133 |
+
return question + "\n\nBelow is the .png image in base64 format:\n\n```base64\n" + base64_str + "\n```\n"
|
| 134 |
+
|
| 135 |
|
| 136 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 137 |
"""
|
|
|
|
| 190 |
for item in questions_data:
|
| 191 |
task_id = item.get("task_id")
|
| 192 |
question_text = item.get("question")
|
| 193 |
+
file_name = item.get("file_name")
|
| 194 |
if not task_id or question_text is None:
|
| 195 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 196 |
continue
|
| 197 |
try:
|
| 198 |
+
submitted_answer = agent(task_id, question_text, file_name)
|
| 199 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 200 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 201 |
except Exception as e:
|
|
|
|
| 206 |
print("Agent did not produce any answers to submit.")
|
| 207 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 208 |
|
| 209 |
+
# 4. Prepare Submission
|
| 210 |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
| 211 |
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
| 212 |
print(status_update)
|