Update app.py
Browse files
app.py
CHANGED
@@ -2,14 +2,6 @@ import os
|
|
2 |
import gradio as gr
|
3 |
import requests
|
4 |
import pandas as pd
|
5 |
-
from io import BytesIO
|
6 |
-
import re
|
7 |
-
import ffmpeg
|
8 |
-
from tenacity import retry, stop_after_attempt, wait_fixed
|
9 |
-
|
10 |
-
# --- Tool-specific Imports ---
|
11 |
-
from pytube import YouTube
|
12 |
-
from youtube_transcript_api import YouTubeTranscriptApi, NoTranscriptFound
|
13 |
|
14 |
# --- LangChain & Dependency Imports ---
|
15 |
from groq import Groq
|
@@ -21,99 +13,37 @@ from langchain.tools import Tool
|
|
21 |
|
22 |
# --- Constants ---
|
23 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
24 |
-
TEMP_DIR = "/tmp"
|
25 |
-
|
26 |
-
# --- Tool Definition: Audio File Transcription ---
|
27 |
-
@retry(stop=stop_after_attempt(3), wait=wait_fixed(2))
|
28 |
-
def transcribe_audio_file(task_id: str) -> str:
|
29 |
-
"""
|
30 |
-
Downloads an audio file (.mp3) for a given task_id, transcribes it, and returns the text.
|
31 |
-
Use this tool ONLY when a question explicitly mentions an audio file, .mp3, recording, or voice memo.
|
32 |
-
"""
|
33 |
-
print(f"Tool 'transcribe_audio_file' called with task_id: {task_id}")
|
34 |
-
try:
|
35 |
-
file_url = f"{DEFAULT_API_URL}/files/{task_id}"
|
36 |
-
audio_response = requests.get(file_url)
|
37 |
-
audio_response.raise_for_status()
|
38 |
-
audio_bytes = BytesIO(audio_response.content)
|
39 |
-
audio_bytes.name = f"{task_id}.mp3"
|
40 |
-
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
41 |
-
transcription = client.audio.transcriptions.create(file=audio_bytes, model="whisper-large-v3", response_format="text")
|
42 |
-
return str(transcription)
|
43 |
-
except Exception as e:
|
44 |
-
return f"Error during audio file transcription: {e}"
|
45 |
-
|
46 |
-
# --- Tool Definition: Video Transcription (prioritizing transcripts) ---
|
47 |
-
@retry(stop=stop_after_attempt(3), wait=wait_fixed(2))
|
48 |
-
def transcribe_youtube_video(video_url: str) -> str:
|
49 |
-
"""
|
50 |
-
Fetches a transcript for a YouTube video from a URL, falling back to download and transcription if needed.
|
51 |
-
Use this tool ONLY when a question provides a youtube.com URL.
|
52 |
-
"""
|
53 |
-
print(f"Tool 'transcribe_youtube_video' called with URL: {video_url}")
|
54 |
-
video_path, audio_path = None, None
|
55 |
-
try:
|
56 |
-
# Extract video ID and try to fetch official transcript
|
57 |
-
video_id = re.search(r"(?:v=|\/)([0-9A-Za-z_-]{11}).*", video_url).group(1)
|
58 |
-
transcript = YouTubeTranscriptApi.get_transcript(video_id)
|
59 |
-
return ' '.join([entry['text'] for entry in transcript])
|
60 |
-
except NoTranscriptFound:
|
61 |
-
print("No transcript found; falling back to download and transcribe.")
|
62 |
-
# Fallback to original download logic
|
63 |
-
os.makedirs(TEMP_DIR, exist_ok=True)
|
64 |
-
yt = YouTube(video_url)
|
65 |
-
stream = yt.streams.filter(only_audio=True).first()
|
66 |
-
video_path = stream.download(output_path=TEMP_DIR)
|
67 |
-
audio_path = os.path.join(TEMP_DIR, "output.mp3")
|
68 |
-
stream = ffmpeg.input(video_path)
|
69 |
-
stream = ffmpeg.output(stream, audio_path, q=0, map='a', y='y')
|
70 |
-
ffmpeg.run(stream)
|
71 |
-
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
72 |
-
with open(audio_path, "rb") as audio_file:
|
73 |
-
transcription = client.audio.transcriptions.create(file=audio_file, model="whisper-large-v3", response_format="text")
|
74 |
-
return str(transcription)
|
75 |
-
except Exception as e:
|
76 |
-
return f"Error during YouTube transcription: {e}"
|
77 |
-
finally:
|
78 |
-
if video_path and os.path.exists(video_path): os.remove(video_path)
|
79 |
-
if audio_path and os.path.exists(audio_path): os.remove(audio_path)
|
80 |
|
81 |
# --- Agent Definition ---
|
82 |
-
class
|
83 |
def __init__(self, groq_api_key: str, tavily_api_key: str):
|
|
|
84 |
self.llm = ChatGroq(model_name="llama3-70b-8192", groq_api_key=groq_api_key, temperature=0.0)
|
|
|
|
|
85 |
self.tools = [
|
86 |
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."),
|
87 |
-
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."),
|
88 |
-
Tool(name="youtube_video_transcriber", func=transcribe_youtube_video, description="Use this for questions with a youtube.com URL. Input MUST be the URL."),
|
89 |
]
|
|
|
|
|
90 |
prompt = ChatPromptTemplate.from_messages([
|
91 |
("system", (
|
92 |
-
"You are a
|
93 |
-
"
|
94 |
-
"
|
95 |
-
"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"
|
96 |
-
"2. **Select ONE tool based on the question:**\n"
|
97 |
-
" - For general knowledge, facts, or current events: use `web_search`.\n"
|
98 |
-
" - For an audio file, .mp3, or voice memo: use `audio_file_transcriber` with the `task_id`.\n"
|
99 |
-
" - 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"
|
100 |
-
" - For anything else (like images, which you cannot see, or math), you must answer directly without using a tool.\n"
|
101 |
-
"3. **Handle Errors:** If a tool fails (e.g., download error), retry once or use web_search to find alternatives.\n"
|
102 |
-
"4. **Execute and Answer:** After using a tool, analyze the result and provide ONLY THE FINAL ANSWER without extra text like 'FINAL ANSWER'."
|
103 |
)),
|
104 |
-
("human", "
|
105 |
("placeholder", "{agent_scratchpad}"),
|
106 |
])
|
|
|
107 |
agent = create_tool_calling_agent(self.llm, self.tools, prompt)
|
108 |
self.agent_executor = AgentExecutor(agent=agent, tools=self.tools, verbose=True, handle_parsing_errors=True)
|
|
|
109 |
|
110 |
-
def __call__(self, question: str
|
111 |
-
|
112 |
-
input_for_agent = {"input": question, "task_id": task_id}
|
113 |
-
if urls and "youtube.com" in urls[0]:
|
114 |
-
input_for_agent['video_url'] = urls[0]
|
115 |
try:
|
116 |
-
response = self.agent_executor.invoke(
|
117 |
return response.get("output", "Agent failed to produce an answer.")
|
118 |
except Exception as e:
|
119 |
return f"Agent execution failed with an error: {e}"
|
@@ -123,11 +53,13 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
123 |
space_id = os.getenv("SPACE_ID")
|
124 |
if not profile: return "Please Login to Hugging Face with the button.", None
|
125 |
username = profile.username
|
|
|
|
|
126 |
try:
|
127 |
groq_api_key = os.getenv("GROQ_API_KEY")
|
128 |
tavily_api_key = os.getenv("TAVILY_API_KEY")
|
129 |
if not all([groq_api_key, tavily_api_key]): raise ValueError("GROQ or TAVILY API key is missing.")
|
130 |
-
agent =
|
131 |
except Exception as e: return f"Error initializing agent: {e}", None
|
132 |
|
133 |
questions_url = f"{DEFAULT_API_URL}/questions"
|
@@ -141,7 +73,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
141 |
for item in questions_data:
|
142 |
task_id, q_text = item.get("task_id"), item.get("question")
|
143 |
if not task_id or not q_text: continue
|
144 |
-
answer = agent(question=q_text
|
145 |
answers_payload.append({"task_id": task_id, "submitted_answer": answer})
|
146 |
results_log.append({"Task ID": task_id, "Question": q_text, "Submitted Answer": answer})
|
147 |
|
@@ -161,8 +93,8 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
161 |
|
162 |
# --- Gradio Interface ---
|
163 |
with gr.Blocks() as demo:
|
164 |
-
gr.Markdown("#
|
165 |
-
gr.Markdown("This agent can search
|
166 |
gr.LoginButton()
|
167 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
168 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
@@ -171,7 +103,7 @@ with gr.Blocks() as demo:
|
|
171 |
|
172 |
if __name__ == "__main__":
|
173 |
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
174 |
-
for key in ["GROQ_API_KEY", "TAVILY_API_KEY"
|
175 |
print(f"✅ {key} secret is set." if os.getenv(key) else f"⚠️ WARNING: {key} secret is not set.")
|
176 |
print("-"*(60 + len(" App Starting ")) + "\n")
|
177 |
-
demo.launch(debug=True, share=False)
|
|
|
2 |
import gradio as gr
|
3 |
import requests
|
4 |
import pandas as pd
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
# --- LangChain & Dependency Imports ---
|
7 |
from groq import Groq
|
|
|
13 |
|
14 |
# --- Constants ---
|
15 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
# --- Agent Definition ---
|
18 |
+
class SimpleAgent:
|
19 |
def __init__(self, groq_api_key: str, tavily_api_key: str):
|
20 |
+
print("Initializing SimpleAgent...")
|
21 |
self.llm = ChatGroq(model_name="llama3-70b-8192", groq_api_key=groq_api_key, temperature=0.0)
|
22 |
+
|
23 |
+
# The agent has ONLY ONE tool: web_search
|
24 |
self.tools = [
|
25 |
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."),
|
|
|
|
|
26 |
]
|
27 |
+
|
28 |
+
# A simple, direct prompt
|
29 |
prompt = ChatPromptTemplate.from_messages([
|
30 |
("system", (
|
31 |
+
"You are a helpful assistant. You have access to one tool: a web search engine. "
|
32 |
+
"Use it when you need to find current information or facts. "
|
33 |
+
"After using the tool, provide ONLY the final, concise answer."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
)),
|
35 |
+
("human", "{input}"),
|
36 |
("placeholder", "{agent_scratchpad}"),
|
37 |
])
|
38 |
+
|
39 |
agent = create_tool_calling_agent(self.llm, self.tools, prompt)
|
40 |
self.agent_executor = AgentExecutor(agent=agent, tools=self.tools, verbose=True, handle_parsing_errors=True)
|
41 |
+
print("SimpleAgent Initialized.")
|
42 |
|
43 |
+
def __call__(self, question: str) -> str:
|
44 |
+
print(f"Agent received question: {question[:50]}...")
|
|
|
|
|
|
|
45 |
try:
|
46 |
+
response = self.agent_executor.invoke({"input": question})
|
47 |
return response.get("output", "Agent failed to produce an answer.")
|
48 |
except Exception as e:
|
49 |
return f"Agent execution failed with an error: {e}"
|
|
|
53 |
space_id = os.getenv("SPACE_ID")
|
54 |
if not profile: return "Please Login to Hugging Face with the button.", None
|
55 |
username = profile.username
|
56 |
+
print(f"User logged in: {username}")
|
57 |
+
|
58 |
try:
|
59 |
groq_api_key = os.getenv("GROQ_API_KEY")
|
60 |
tavily_api_key = os.getenv("TAVILY_API_KEY")
|
61 |
if not all([groq_api_key, tavily_api_key]): raise ValueError("GROQ or TAVILY API key is missing.")
|
62 |
+
agent = SimpleAgent(groq_api_key=groq_api_key, tavily_api_key=tavily_api_key)
|
63 |
except Exception as e: return f"Error initializing agent: {e}", None
|
64 |
|
65 |
questions_url = f"{DEFAULT_API_URL}/questions"
|
|
|
73 |
for item in questions_data:
|
74 |
task_id, q_text = item.get("task_id"), item.get("question")
|
75 |
if not task_id or not q_text: continue
|
76 |
+
answer = agent(question=q_text)
|
77 |
answers_payload.append({"task_id": task_id, "submitted_answer": answer})
|
78 |
results_log.append({"Task ID": task_id, "Question": q_text, "Submitted Answer": answer})
|
79 |
|
|
|
93 |
|
94 |
# --- Gradio Interface ---
|
95 |
with gr.Blocks() as demo:
|
96 |
+
gr.Markdown("# Simple Agent Runner (Web Search Only)")
|
97 |
+
gr.Markdown("This agent can only search the web. Let's establish a stable baseline.")
|
98 |
gr.LoginButton()
|
99 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
100 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
|
|
103 |
|
104 |
if __name__ == "__main__":
|
105 |
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
106 |
+
for key in ["GROQ_API_KEY", "TAVILY_API_KEY"]:
|
107 |
print(f"✅ {key} secret is set." if os.getenv(key) else f"⚠️ WARNING: {key} secret is not set.")
|
108 |
print("-"*(60 + len(" App Starting ")) + "\n")
|
109 |
+
demo.launch(debug=True, share=False)
|