File size: 10,230 Bytes
10e9b7d
 
eccf8e4
3c4371f
8d9e499
a38b536
3a1e7f5
 
a38b536
3a1e7f5
a38b536
3a1e7f5
10e9b7d
3a1e7f5
8d9e499
c0a6618
 
162e437
c0a6618
8d9e499
c0a6618
e80aab9
3db6293
31c5a1f
e80aab9
a38b536
 
3a1e7f5
a38b536
31243f4
8d9e499
 
 
 
a38b536
8d9e499
a38b536
 
 
 
8d9e499
3a1e7f5
a38b536
3a1e7f5
 
a38b536
 
 
 
3a1e7f5
a38b536
3a1e7f5
 
 
a38b536
 
 
 
7d65c66
a38b536
 
 
 
e80aab9
3a1e7f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e80aab9
8d9e499
 
3a1e7f5
9e76a0e
 
 
3a1e7f5
 
 
 
9e76a0e
 
 
 
 
3a1e7f5
a38b536
3a1e7f5
 
 
 
 
 
 
 
9e76a0e
 
 
 
 
 
 
 
 
a38b536
 
78cc7ba
162e437
9e76a0e
a38b536
 
9e76a0e
a38b536
9e76a0e
162e437
9e76a0e
 
a38b536
9e76a0e
 
 
 
3a1e7f5
 
 
a38b536
 
9e76a0e
 
 
 
 
a38b536
 
9e76a0e
 
a38b536
 
 
 
 
 
9e76a0e
 
 
 
3db234b
9e76a0e
 
 
 
 
 
 
a38b536
9e76a0e
162e437
9e76a0e
3a1e7f5
 
9e76a0e
 
 
 
 
 
 
 
3a1e7f5
9e76a0e
 
 
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import os
import gradio as gr
import requests
import pandas as pd
from io import BytesIO
import re
import subprocess
import base64

# --- Tool-specific Imports ---
from pytube import YouTube
from langchain_huggingface import HuggingFaceInferenceAPI

# --- LangChain & Groq Imports ---
from groq import Groq
from langchain_groq import ChatGroq
from langchain.agents import AgentExecutor, create_tool_calling_agent
from langchain_tavily import TavilySearchResults
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 ---
def transcribe_audio_file(task_id: str) -> str:
    # (This function is complete and correct from the previous version)
    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 via FFmpeg ---
def transcribe_youtube_video(video_url: str) -> str:
    # (This function is complete and correct from the previous version)
    print(f"Tool 'transcribe_youtube_video' (ffmpeg) called with URL: {video_url}")
    video_path, audio_path = None, None
    try:
        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")
        command = ["ffmpeg", "-i", video_path, "-y", "-q:a", "0", "-map", "a", audio_path]
        subprocess.run(command, check=True, capture_output=True, text=True)
        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)

# --- NEW TOOL Definition: Image Analysis ---
def analyze_image_from_task_id(task_id: str) -> str:
    """
    Downloads an image file for a given task_id and analyzes it using a Vision-Language Model.
    Use this tool ONLY when a question explicitly mentions an image.
    """
    print(f"Tool 'analyze_image_from_task_id' called with task_id: {task_id}")
    try:
        file_url = f"{DEFAULT_API_URL}/files/{task_id}"
        print(f"Downloading image from: {file_url}")
        response = requests.get(file_url)
        response.raise_for_status()
        
        # Initialize the VLM client
        vlm_client = HuggingFaceInferenceAPI(
            model_id="llava-hf/llava-1.5-7b-hf",
            token=os.getenv("HF_TOKEN")
        )
        
        print("Analyzing image with Llava...")
        # The prompt for the VLM needs to be specific.
        # We can just ask it to describe the image in detail.
        text_prompt = "Describe the image in detail."
        result = vlm_client.image_to_text(image=response.content, prompt=text_prompt)
        print(f"Image analysis successful. Result: {result}")
        return result
        
    except Exception as e:
        return f"Error during image analysis: {e}"

# --- Agent Definition ---
class LangChainAgent:
    def __init__(self, groq_api_key: str, tavily_api_key: str, hf_token: str):
        self.llm = ChatGroq(model_name="llama3-70b-8192", groq_api_key=groq_api_key, temperature=0.0)

        self.tools = [
            TavilySearchResults(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."),
            Tool(name="image_analyzer", func=analyze_image_from_task_id, description="Use this for questions mentioning an image. Input MUST be the task_id."),
        ]
        
        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, a YouTube video transcriber, and an image analyzer.\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 a specific file type (audio, video, image) or 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.\n"
                "    - For an image: use `image_analyzer` with the `task_id`.\n"
                "    - For math or simple logic: answer directly.\n"
                "3.  **Execute and Answer:** After using a tool, analyze the result and provide ONLY THE 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")
        hf_token = os.getenv("HF_TOKEN")
        if not all([groq_api_key, tavily_api_key, hf_token]): raise ValueError("An API key (GROQ, TAVILY, or HF) is missing.")
        agent = LangChainAgent(groq_api_key=groq_api_key, tavily_api_key=tavily_api_key, hf_token=hf_token)
    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, Vision)")
    gr.Markdown("This agent can search, transcribe audio files, transcribe YouTube videos, and analyze images.")
    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", "HF_TOKEN"]:
        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)