Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import os
|
| 2 |
import json
|
| 3 |
import requests
|
|
|
|
| 4 |
from typing import Dict, List, Any, Optional, Union
|
| 5 |
from langchain_core.prompts import ChatPromptTemplate
|
| 6 |
from langchain_core.output_parsers import StrOutputParser, JsonOutputParser
|
|
@@ -13,20 +14,25 @@ from youtube_transcript_api import YouTubeTranscriptApi
|
|
| 13 |
from youtube_transcript_api.formatters import TextFormatter
|
| 14 |
import re
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
# Environment variables setup
|
| 17 |
-
OPENAI_API_KEY = os.environ.get("
|
| 18 |
-
SERPER_API_KEY = os.environ.get("SERPER_API_KEY", "
|
| 19 |
-
MODEL_NAME = "gpt-4o
|
| 20 |
|
| 21 |
# Initialize the LLM
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
| 27 |
|
| 28 |
# Tools for the agent
|
| 29 |
-
|
| 30 |
@tool
|
| 31 |
def search(query: str) -> str:
|
| 32 |
"""
|
|
@@ -136,6 +142,7 @@ def analyze_video_content(youtube_url: str) -> str:
|
|
| 136 |
return transcript
|
| 137 |
|
| 138 |
# Use the LLM to analyze the transcript
|
|
|
|
| 139 |
analysis_prompt = ChatPromptTemplate.from_messages([
|
| 140 |
("system", "You are an expert at analyzing video content based on transcripts. "
|
| 141 |
"Extract the key points, main arguments, and important information."),
|
|
@@ -174,17 +181,20 @@ agent_prompt = ChatPromptTemplate.from_messages([
|
|
| 174 |
])
|
| 175 |
|
| 176 |
# Create the agent
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
|
|
|
|
|
|
|
|
|
| 188 |
|
| 189 |
# Main function to handle input and generate responses
|
| 190 |
def process_input(user_input: str) -> str:
|
|
@@ -198,12 +208,40 @@ def process_input(user_input: str) -> str:
|
|
| 198 |
The agent's response
|
| 199 |
"""
|
| 200 |
try:
|
|
|
|
| 201 |
response = agent_executor.invoke({"input": user_input})
|
| 202 |
return response["output"]
|
| 203 |
except Exception as e:
|
| 204 |
return f"Error processing your request: {str(e)}"
|
| 205 |
|
| 206 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
def main():
|
| 208 |
"""
|
| 209 |
Main function to demonstrate the agent's capabilities.
|
|
@@ -220,5 +258,5 @@ def main():
|
|
| 220 |
response = process_input(user_input)
|
| 221 |
print(f"\nAgent: {response}")
|
| 222 |
|
| 223 |
-
if __name__ == "__main__":
|
| 224 |
main()
|
|
|
|
| 1 |
import os
|
| 2 |
import json
|
| 3 |
import requests
|
| 4 |
+
import streamlit as st
|
| 5 |
from typing import Dict, List, Any, Optional, Union
|
| 6 |
from langchain_core.prompts import ChatPromptTemplate
|
| 7 |
from langchain_core.output_parsers import StrOutputParser, JsonOutputParser
|
|
|
|
| 14 |
from youtube_transcript_api.formatters import TextFormatter
|
| 15 |
import re
|
| 16 |
|
| 17 |
+
# Streamlit app configuration
|
| 18 |
+
st.set_page_config(page_title="GAIA Agent", page_icon="🤖", layout="wide")
|
| 19 |
+
st.title("🤖 GAIA Agent")
|
| 20 |
+
|
| 21 |
# Environment variables setup
|
| 22 |
+
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY", st.secrets.get("OPENAI_API_KEY", ""))
|
| 23 |
+
SERPER_API_KEY = os.environ.get("SERPER_API_KEY", st.secrets.get("SERPER_API_KEY", ""))
|
| 24 |
+
MODEL_NAME = os.environ.get("MODEL_NAME", st.secrets.get("MODEL_NAME", "gpt-4o"))
|
| 25 |
|
| 26 |
# Initialize the LLM
|
| 27 |
+
@st.cache_resource
|
| 28 |
+
def get_llm():
|
| 29 |
+
return ChatOpenAI(
|
| 30 |
+
model=MODEL_NAME,
|
| 31 |
+
temperature=0.1,
|
| 32 |
+
api_key=OPENAI_API_KEY
|
| 33 |
+
)
|
| 34 |
|
| 35 |
# Tools for the agent
|
|
|
|
| 36 |
@tool
|
| 37 |
def search(query: str) -> str:
|
| 38 |
"""
|
|
|
|
| 142 |
return transcript
|
| 143 |
|
| 144 |
# Use the LLM to analyze the transcript
|
| 145 |
+
llm = get_llm()
|
| 146 |
analysis_prompt = ChatPromptTemplate.from_messages([
|
| 147 |
("system", "You are an expert at analyzing video content based on transcripts. "
|
| 148 |
"Extract the key points, main arguments, and important information."),
|
|
|
|
| 181 |
])
|
| 182 |
|
| 183 |
# Create the agent
|
| 184 |
+
@st.cache_resource
|
| 185 |
+
def get_agent_executor():
|
| 186 |
+
llm = get_llm()
|
| 187 |
+
agent = create_openai_tools_agent(llm, tools, agent_prompt)
|
| 188 |
+
|
| 189 |
+
# Create the agent executor
|
| 190 |
+
return AgentExecutor(
|
| 191 |
+
agent=agent,
|
| 192 |
+
tools=tools,
|
| 193 |
+
verbose=True,
|
| 194 |
+
handle_parsing_errors=True,
|
| 195 |
+
max_iterations=5,
|
| 196 |
+
early_stopping_method="force"
|
| 197 |
+
)
|
| 198 |
|
| 199 |
# Main function to handle input and generate responses
|
| 200 |
def process_input(user_input: str) -> str:
|
|
|
|
| 208 |
The agent's response
|
| 209 |
"""
|
| 210 |
try:
|
| 211 |
+
agent_executor = get_agent_executor()
|
| 212 |
response = agent_executor.invoke({"input": user_input})
|
| 213 |
return response["output"]
|
| 214 |
except Exception as e:
|
| 215 |
return f"Error processing your request: {str(e)}"
|
| 216 |
|
| 217 |
+
# Streamlit UI
|
| 218 |
+
st.write("""
|
| 219 |
+
This agent is designed to pass the GAIA benchmark with at least a 30% score.
|
| 220 |
+
It uses OpenAI's GPT-4o model, Google search via Serper API, and can parse YouTube video content.
|
| 221 |
+
""")
|
| 222 |
+
|
| 223 |
+
# Check if API keys are set
|
| 224 |
+
if not OPENAI_API_KEY:
|
| 225 |
+
st.warning("⚠️ OpenAI API key is not set. Please add it to your secrets.")
|
| 226 |
+
if not SERPER_API_KEY:
|
| 227 |
+
st.warning("⚠️ Serper API key is not set. Please add it to your secrets.")
|
| 228 |
+
|
| 229 |
+
# User input
|
| 230 |
+
user_query = st.text_area("Enter your question or task:", height=100)
|
| 231 |
+
|
| 232 |
+
if st.button("Submit"):
|
| 233 |
+
if user_query:
|
| 234 |
+
with st.spinner("Thinking..."):
|
| 235 |
+
# Process the query
|
| 236 |
+
response = process_input(user_query)
|
| 237 |
+
|
| 238 |
+
# Display response
|
| 239 |
+
st.markdown("### Response:")
|
| 240 |
+
st.write(response)
|
| 241 |
+
else:
|
| 242 |
+
st.warning("Please enter a question or task.")
|
| 243 |
+
|
| 244 |
+
# For testing purposes outside of Streamlit
|
| 245 |
def main():
|
| 246 |
"""
|
| 247 |
Main function to demonstrate the agent's capabilities.
|
|
|
|
| 258 |
response = process_input(user_input)
|
| 259 |
print(f"\nAgent: {response}")
|
| 260 |
|
| 261 |
+
if __name__ == "__main__" and not "streamlit" in os.path.basename(os.environ.get("_", "")):
|
| 262 |
main()
|