Update app.py
Browse files
app.py
CHANGED
@@ -5,15 +5,16 @@ import pandas as pd
|
|
5 |
from io import BytesIO
|
6 |
import re
|
7 |
import subprocess
|
|
|
8 |
|
9 |
# --- Tool-specific Imports ---
|
10 |
from pytube import YouTube
|
11 |
|
12 |
-
# --- LangChain &
|
13 |
from groq import Groq
|
14 |
from langchain_groq import ChatGroq
|
15 |
from langchain.agents import AgentExecutor, create_tool_calling_agent
|
16 |
-
from langchain_tavily import
|
17 |
from langchain_core.prompts import ChatPromptTemplate
|
18 |
from langchain.tools import Tool
|
19 |
|
@@ -40,13 +41,13 @@ def transcribe_audio_file(task_id: str) -> str:
|
|
40 |
except Exception as e:
|
41 |
return f"Error during audio file transcription: {e}"
|
42 |
|
43 |
-
# --- Tool Definition: Video Transcription (using
|
44 |
def transcribe_youtube_video(video_url: str) -> str:
|
45 |
"""
|
46 |
Downloads a YouTube video from a URL, extracts its audio using FFmpeg, and transcribes it.
|
47 |
Use this tool ONLY when a question provides a youtube.com URL.
|
48 |
"""
|
49 |
-
print(f"Tool 'transcribe_youtube_video' (ffmpeg) called with URL: {video_url}")
|
50 |
video_path, audio_path = None, None
|
51 |
try:
|
52 |
os.makedirs(TEMP_DIR, exist_ok=True)
|
@@ -54,8 +55,10 @@ def transcribe_youtube_video(video_url: str) -> str:
|
|
54 |
stream = yt.streams.filter(only_audio=True).first()
|
55 |
video_path = stream.download(output_path=TEMP_DIR)
|
56 |
audio_path = os.path.join(TEMP_DIR, "output.mp3")
|
57 |
-
|
58 |
-
|
|
|
|
|
59 |
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
60 |
with open(audio_path, "rb") as audio_file:
|
61 |
transcription = client.audio.transcriptions.create(file=audio_file, model="whisper-large-v3", response_format="text")
|
@@ -71,13 +74,14 @@ class LangChainAgent:
|
|
71 |
def __init__(self, groq_api_key: str, tavily_api_key: str):
|
72 |
self.llm = ChatGroq(model_name="llama3-70b-8192", groq_api_key=groq_api_key, temperature=0.0)
|
73 |
self.tools = [
|
74 |
-
|
75 |
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."),
|
76 |
Tool(name="youtube_video_transcriber", func=transcribe_youtube_video, description="Use this for questions with a youtube.com URL. Input MUST be the URL."),
|
77 |
]
|
78 |
prompt = ChatPromptTemplate.from_messages([
|
79 |
("system", (
|
80 |
-
"You are a powerful problem-solving agent.
|
|
|
81 |
"**REASONING PROCESS:**\n"
|
82 |
"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"
|
83 |
"2. **Select ONE tool based on the question:**\n"
|
@@ -157,7 +161,7 @@ with gr.Blocks() as demo:
|
|
157 |
|
158 |
if __name__ == "__main__":
|
159 |
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
160 |
-
for key in ["GROQ_API_KEY", "TAVILY_API_KEY"]:
|
161 |
print(f"✅ {key} secret is set." if os.getenv(key) else f"⚠️ WARNING: {key} secret is not set.")
|
162 |
print("-"*(60 + len(" App Starting ")) + "\n")
|
163 |
-
demo.launch(debug=True, share=False)
|
|
|
5 |
from io import BytesIO
|
6 |
import re
|
7 |
import subprocess
|
8 |
+
import ffmpeg
|
9 |
|
10 |
# --- Tool-specific Imports ---
|
11 |
from pytube import YouTube
|
12 |
|
13 |
+
# --- LangChain & Dependency Imports ---
|
14 |
from groq import Groq
|
15 |
from langchain_groq import ChatGroq
|
16 |
from langchain.agents import AgentExecutor, create_tool_calling_agent
|
17 |
+
from langchain_tavily import TavilySearch
|
18 |
from langchain_core.prompts import ChatPromptTemplate
|
19 |
from langchain.tools import Tool
|
20 |
|
|
|
41 |
except Exception as e:
|
42 |
return f"Error during audio file transcription: {e}"
|
43 |
|
44 |
+
# --- Tool Definition: Video Transcription (using ffmpeg-python) ---
|
45 |
def transcribe_youtube_video(video_url: str) -> str:
|
46 |
"""
|
47 |
Downloads a YouTube video from a URL, extracts its audio using FFmpeg, and transcribes it.
|
48 |
Use this tool ONLY when a question provides a youtube.com URL.
|
49 |
"""
|
50 |
+
print(f"Tool 'transcribe_youtube_video' (ffmpeg-python) called with URL: {video_url}")
|
51 |
video_path, audio_path = None, None
|
52 |
try:
|
53 |
os.makedirs(TEMP_DIR, exist_ok=True)
|
|
|
55 |
stream = yt.streams.filter(only_audio=True).first()
|
56 |
video_path = stream.download(output_path=TEMP_DIR)
|
57 |
audio_path = os.path.join(TEMP_DIR, "output.mp3")
|
58 |
+
# Use ffmpeg-python instead of subprocess
|
59 |
+
stream = ffmpeg.input(video_path)
|
60 |
+
stream = ffmpeg.output(stream, audio_path, q=0, map='a', y='y')
|
61 |
+
ffmpeg.run(stream)
|
62 |
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
63 |
with open(audio_path, "rb") as audio_file:
|
64 |
transcription = client.audio.transcriptions.create(file=audio_file, model="whisper-large-v3", response_format="text")
|
|
|
74 |
def __init__(self, groq_api_key: str, tavily_api_key: str):
|
75 |
self.llm = ChatGroq(model_name="llama3-70b-8192", groq_api_key=groq_api_key, temperature=0.0)
|
76 |
self.tools = [
|
77 |
+
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."),
|
78 |
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."),
|
79 |
Tool(name="youtube_video_transcriber", func=transcribe_youtube_video, description="Use this for questions with a youtube.com URL. Input MUST be the URL."),
|
80 |
]
|
81 |
prompt = ChatPromptTemplate.from_messages([
|
82 |
("system", (
|
83 |
+
"You are a powerful problem-solving agent. Your goal is to answer the user's question accurately. "
|
84 |
+
"You have access to a web search tool, an audio file transcriber, and a YouTube video transcriber.\n\n"
|
85 |
"**REASONING PROCESS:**\n"
|
86 |
"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"
|
87 |
"2. **Select ONE tool based on the question:**\n"
|
|
|
161 |
|
162 |
if __name__ == "__main__":
|
163 |
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
164 |
+
for key in ["GROQ_API_KEY", "TAVILY_API_KEY", "SPACE_ID"]:
|
165 |
print(f"✅ {key} secret is set." if os.getenv(key) else f"⚠️ WARNING: {key} secret is not set.")
|
166 |
print("-"*(60 + len(" App Starting ")) + "\n")
|
167 |
+
demo.launch(debug=True, share=False)
|