File size: 9,761 Bytes
10e9b7d
 
eccf8e4
3c4371f
8d9e499
a38b536
95b97f6
 
a38b536
3a1e7f5
a38b536
95b97f6
10e9b7d
6d169a1
8d9e499
c0a6618
 
6d169a1
c0a6618
8d9e499
c0a6618
e80aab9
3db6293
31c5a1f
e80aab9
a38b536
95b97f6
a38b536
80e8087
 
 
 
a38b536
31243f4
8d9e499
 
 
 
a38b536
8d9e499
a38b536
 
 
 
8d9e499
95b97f6
 
a38b536
80e8087
95b97f6
80e8087
 
95b97f6
a38b536
 
95b97f6
 
 
 
 
 
 
a38b536
 
3a1e7f5
a38b536
3a1e7f5
95b97f6
 
 
a38b536
 
 
 
7d65c66
a38b536
 
 
 
e80aab9
8d9e499
 
80e8087
9e76a0e
 
6d169a1
3a1e7f5
 
9e76a0e
 
 
6d169a1
 
a38b536
80e8087
3a1e7f5
 
 
95b97f6
80e8087
95b97f6
 
9e76a0e
 
 
 
 
 
 
 
a38b536
 
78cc7ba
162e437
9e76a0e
a38b536
 
9e76a0e
a38b536
9e76a0e
162e437
9e76a0e
 
a38b536
9e76a0e
 
 
 
80e8087
 
a38b536
 
9e76a0e
 
 
 
 
a38b536
 
9e76a0e
 
a38b536
 
 
 
 
 
9e76a0e
 
 
 
3db234b
9e76a0e
 
 
 
 
 
 
a38b536
9e76a0e
162e437
9e76a0e
80e8087
 
9e76a0e
 
 
 
 
 
 
 
95b97f6
9e76a0e
 
95b97f6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import os
import gradio as gr
import requests
import pandas as pd
from io import BytesIO
import re
import ffmpeg
from tenacity import retry, stop_after_attempt, wait_fixed

# --- Tool-specific Imports ---
from pytube import YouTube
from youtube_transcript_api import YouTubeTranscriptApi, NoTranscriptFound

# --- LangChain & Dependency Imports ---
from groq import Groq
from langchain_groq import ChatGroq
from langchain.agents import AgentExecutor, create_tool_calling_agent
from langchain_tavily import TavilySearch 
from langchain_core.prompts import ChatPromptTemplate
from langchain.tools import Tool

# --- Constants ---
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
TEMP_DIR = "/tmp"

# --- Tool Definition: Audio File Transcription ---
@retry(stop=stop_after_attempt(3), wait=wait_fixed(2))
def transcribe_audio_file(task_id: str) -> str:
    """
    Downloads an audio file (.mp3) for a given task_id, transcribes it, and returns the text.
    Use this tool ONLY when a question explicitly mentions an audio file, .mp3, recording, or voice memo.
    """
    print(f"Tool 'transcribe_audio_file' called with task_id: {task_id}")
    try:
        file_url = f"{DEFAULT_API_URL}/files/{task_id}"
        audio_response = requests.get(file_url)
        audio_response.raise_for_status()
        audio_bytes = BytesIO(audio_response.content)
        audio_bytes.name = f"{task_id}.mp3"
        client = Groq(api_key=os.getenv("GROQ_API_KEY"))
        transcription = client.audio.transcriptions.create(file=audio_bytes, model="whisper-large-v3", response_format="text")
        return str(transcription)
    except Exception as e:
        return f"Error during audio file transcription: {e}"

# --- Tool Definition: Video Transcription (prioritizing transcripts) ---
@retry(stop=stop_after_attempt(3), wait=wait_fixed(2))
def transcribe_youtube_video(video_url: str) -> str:
    """
    Fetches a transcript for a YouTube video from a URL, falling back to download and transcription if needed.
    Use this tool ONLY when a question provides a youtube.com URL.
    """
    print(f"Tool 'transcribe_youtube_video' called with URL: {video_url}")
    video_path, audio_path = None, None
    try:
        # Extract video ID and try to fetch official transcript
        video_id = re.search(r"(?:v=|\/)([0-9A-Za-z_-]{11}).*", video_url).group(1)
        transcript = YouTubeTranscriptApi.get_transcript(video_id)
        return ' '.join([entry['text'] for entry in transcript])
    except NoTranscriptFound:
        print("No transcript found; falling back to download and transcribe.")
        # Fallback to original download logic
        os.makedirs(TEMP_DIR, exist_ok=True)
        yt = YouTube(video_url)
        stream = yt.streams.filter(only_audio=True).first()
        video_path = stream.download(output_path=TEMP_DIR)
        audio_path = os.path.join(TEMP_DIR, "output.mp3")
        stream = ffmpeg.input(video_path)
        stream = ffmpeg.output(stream, audio_path, q=0, map='a', y='y')
        ffmpeg.run(stream)
        client = Groq(api_key=os.getenv("GROQ_API_KEY"))
        with open(audio_path, "rb") as audio_file:
            transcription = client.audio.transcriptions.create(file=audio_file, model="whisper-large-v3", response_format="text")
        return str(transcription)
    except Exception as e:
        return f"Error during YouTube transcription: {e}"
    finally:
        if video_path and os.path.exists(video_path): os.remove(video_path)
        if audio_path and os.path.exists(audio_path): os.remove(audio_path)

# --- Agent Definition ---
class LangChainAgent:
    def __init__(self, groq_api_key: str, tavily_api_key: str):
        self.llm = ChatGroq(model_name="llama3-70b-8192", groq_api_key=groq_api_key, temperature=0.0)
        self.tools = [
            TavilySearch(name="web_search", max_results=3, tavily_api_key=tavily_api_key, description="A search engine for finding up-to-date information on the internet."),
            Tool(name="audio_file_transcriber", func=transcribe_audio_file, description="Use this for questions mentioning an audio file (.mp3, recording). Input MUST be the task_id."),
            Tool(name="youtube_video_transcriber", func=transcribe_youtube_video, description="Use this for questions with a youtube.com URL. Input MUST be the URL."),
        ]
        prompt = ChatPromptTemplate.from_messages([
            ("system", (
                "You are a powerful problem-solving agent. Your goal is to answer the user's question accurately. "
                "You have access to a web search tool, an audio file transcriber, and a YouTube video transcriber.\n\n"
                "**REASONING PROCESS:**\n"
                "1.  **Analyze the question:** Determine if a tool is needed. Is it a general knowledge question, or does it mention an audio file or a YouTube URL?\n"
                "2.  **Select ONE tool based on the question:**\n"
                "    - For general knowledge, facts, or current events: use `web_search`.\n"
                "    - For an audio file, .mp3, or voice memo: use `audio_file_transcriber` with the `task_id`.\n"
                "    - For a youtube.com URL: use `youtube_video_transcriber` with the URL. If transcription fails, fall back to web_search for video transcripts or summaries.\n"
                "    - For anything else (like images, which you cannot see, or math), you must answer directly without using a tool.\n"
                "3.  **Handle Errors:** If a tool fails (e.g., download error), retry once or use web_search to find alternatives.\n"
                "4.  **Execute and Answer:** After using a tool, analyze the result and provide ONLY THE FINAL ANSWER without extra text like 'FINAL ANSWER'."
            )),
            ("human", "Question: {input}\nTask ID: {task_id}"),
            ("placeholder", "{agent_scratchpad}"),
        ])
        agent = create_tool_calling_agent(self.llm, self.tools, prompt)
        self.agent_executor = AgentExecutor(agent=agent, tools=self.tools, verbose=True, handle_parsing_errors=True)

    def __call__(self, question: str, task_id: str) -> str:
        urls = re.findall(r'https?://[^\s]+', question)
        input_for_agent = {"input": question, "task_id": task_id}
        if urls and "youtube.com" in urls[0]:
            input_for_agent['video_url'] = urls[0]
        try:
            response = self.agent_executor.invoke(input_for_agent)
            return response.get("output", "Agent failed to produce an answer.")
        except Exception as e:
            return f"Agent execution failed with an error: {e}"

# --- Main Application Logic ---
def run_and_submit_all(profile: gr.OAuthProfile | None):
    space_id = os.getenv("SPACE_ID")
    if not profile: return "Please Login to Hugging Face with the button.", None
    username = profile.username
    try:
        groq_api_key = os.getenv("GROQ_API_KEY")
        tavily_api_key = os.getenv("TAVILY_API_KEY")
        if not all([groq_api_key, tavily_api_key]): raise ValueError("GROQ or TAVILY API key is missing.")
        agent = LangChainAgent(groq_api_key=groq_api_key, tavily_api_key=tavily_api_key)
    except Exception as e: return f"Error initializing agent: {e}", None
    
    questions_url = f"{DEFAULT_API_URL}/questions"
    try:
        response = requests.get(questions_url, timeout=20)
        response.raise_for_status()
        questions_data = response.json()
    except Exception as e: return f"Error fetching questions: {e}", None
    
    results_log, answers_payload = [], []
    for item in questions_data:
        task_id, q_text = item.get("task_id"), item.get("question")
        if not task_id or not q_text: continue
        answer = agent(question=q_text, task_id=task_id)
        answers_payload.append({"task_id": task_id, "submitted_answer": answer})
        results_log.append({"Task ID": task_id, "Question": q_text, "Submitted Answer": answer})
    
    agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
    submission_data = {"username": username, "agent_code": agent_code, "answers": answers_payload}
    submit_url = f"{DEFAULT_API_URL}/submit"
    try:
        response = requests.post(submit_url, json=submission_data, timeout=300)
        response.raise_for_status()
        result_data = response.json()
        final_status = (f"Submission Successful!\nUser: {result_data.get('username')}\n"
                        f"Overall Score: {result_data.get('score', 'N/A')}% "
                        f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
                        f"Message: {result_data.get('message', 'No message received.')}")
        return final_status, pd.DataFrame(results_log)
    except Exception as e: return f"Submission Failed: {e}", pd.DataFrame(results_log)

# --- Gradio Interface ---
with gr.Blocks() as demo:
    gr.Markdown("# Ultimate Agent Runner (Search, Audio, Video)")
    gr.Markdown("This agent can search, transcribe audio files, and transcribe YouTube videos.")
    gr.LoginButton()
    run_button = gr.Button("Run Evaluation & Submit All Answers")
    status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
    results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
    run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])

if __name__ == "__main__":
    print("\n" + "-"*30 + " App Starting " + "-"*30)
    for key in ["GROQ_API_KEY", "TAVILY_API_KEY", "SPACE_ID"]:
        print(f"✅ {key} secret is set." if os.getenv(key) else f"⚠️ WARNING: {key} secret is not set.")
    print("-"*(60 + len(" App Starting ")) + "\n")
    demo.launch(debug=True, share=False)