Spaces:
Sleeping
Sleeping
add agent
#1
by thivy - opened
Files changed (17)
- .DS_StoreDeleted
- .gitattributes-1
- .gitignoreDeleted-2
- __pycache__/agents.cpython-312.pycDeleted
- __pycache__/tools.cpython-312.pycDeleted
- agents.pyDeleted-138
- app.py+9-18
- files/1f975693-876d-457b-a649-393859e79bf3.mp3Deleted-3
- files/7bd855d8-463d-4ed5-93ca-5fe35145f733.xlsxDeleted
- files/99c9cc74-fdc8-46c6-8f8d-3ce2d3bfeea3.mp3Deleted-3
- files/cca530fc-4052-43b2-b130-b30968d8aa44.pngDeleted
- files/f918266a-b3e0-4914-865d-4faa564f1aef.pyDeleted-35
- qa_graph.pyDeleted-225
- requirements.txt+1-14
- system_prompt.txtDeleted-7
- tools.pyDeleted-391
- yolo11n.ptDeleted-3
32 unmodified lines3334353632 unmodified lines*.zip filter=lfs diff=lfs merge=lfs -text*.zst filter=lfs diff=lfs merge=lfs -text*tfevents* filter=lfs diff=lfs merge=lfs -text*.mp3 filter=lfs diff=lfs merge=lfs -text32 unmodified lines33343532 unmodified lines*.zip filter=lfs diff=lfs merge=lfs -text*.zst filter=lfs diff=lfs merge=lfs -text*tfevents* filter=lfs diff=lfs merge=lfs -text
12.env.venv123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138from tools import ( general_tools, file_agent_tools, data_agent_tools, math_agent_tools, analyze_video_tools, youtube_transcript_tools, google_search, wiki_search, arxiv_search)from langgraph.prebuilt import create_react_agentfrom langgraph.checkpoint.memory import MemorySaverfrom langchain_openai import ChatOpenAIfrom langgraph_supervisor import create_supervisor
llm = ChatOpenAI(model="o4-mini")
memory = MemorySaver()
with open("system_prompt.txt", "r") as f: prompt = f.read() general_agent = create_react_agent( model=llm, tools=general_tools(), checkpointer=memory, prompt=prompt)
# Create agentsfile_agent = create_react_agent( model=llm, tools=file_agent_tools(), name="file_reader", prompt="You read files. Use tools to read files.")
math_agent = create_react_agent( model=llm, tools=math_agent_tools(), name="calculator", prompt="You do math. Use tools for all calculations.")
data_agent = create_react_agent( model=llm, tools=data_agent_tools(), name="data_processor", prompt="You process data. Use tools to filter and extract data.")
# Create video analysis agentsvideo_agent = create_react_agent( model=llm, tools=analyze_video_tools(), name="video_analyzer", prompt="""You analyze visual content in videos. Use tools to detect and track objects. The object_detection tool is a general object detection model. Use this for general cases. The analyze_video_content uses both the object detection model and a vision llm to analyze frames with content given a question. Use this for more difficult questions.""")
transcript_agent = create_react_agent( model=llm, tools=youtube_transcript_tools(), name="transcript_analyzer", prompt="You analyze audio/speech content in videos. Use tools to get transcripts.")
wiki_agent = create_react_agent( model=llm, tools=[wiki_search], name="wiki_analyst", prompt="You search information from wikipedia.")
google_agent = create_react_agent( model=llm, tools=[google_search], name="google_search_analyst", prompt="You search information from google search.")
arxiv_agent = create_react_agent( model=llm, tools=[arxiv_search], name="arxiv_analyst", prompt="You search information from arxiv.")
excel_prompt = """You are a supervisor. You coordinate file_reader, calculator, and data_processor to solve problems step by step. Do not do calculations or file reading yourself, use the tools.Report your thoughts, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER]. YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string."""
video_analyzer_prompt = """You coordinate video_analyzer and transcript_analyzer to answer questions about YouTube videos. Use video_analyzer for visual questions (objects, people, actions). Use transcript_analyzer for audio questions (what people say).Report your thoughts, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER]. YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string."""
search_analyzer_prompt = """You coordinate different search agents to answer questions. Report your thoughts, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER]. YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string."""
# Supervisorexcel_supervisor = create_supervisor( [file_agent, math_agent, data_agent], model=llm, prompt=excel_prompt).compile()
# Video supervisorvideo_supervisor = create_supervisor( [video_agent, transcript_agent], model=llm, prompt=video_analyzer_prompt).compile()
# search supervisorsearch_supervisor = create_supervisor( [wiki_agent, google_agent, arxiv_agent], model=llm, prompt=search_analyzer_prompt).compile()2 unmodified lines34567892 unmodified lines121314151617181920212223242526272829303149 unmodified lines8182838485868788899091929394952 unmodified linesimport requestsimport inspectimport pandas as pdfrom qa_graph import build_graph, Question, extract_final_answer# (Keep Constants as is)# --- Constants ---2 unmodified lines# --- Basic Agent Definition ---# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------class BasicAgent:"""A langgraph agent."""def __init__(self):print("BasicAgent initialized.")self.graph = build_graph()def __call__(self, question: Question) -> str:print(f"Agent received question (first 50 chars): {question.question[:50]}...")# Wrap the question in a HumanMessage from langchain_coremessages = self.graph.invoke({"question": question, "decision": "","answer": ""})answer = messages['answer']answer = extract_final_answer(answer)[1]print(answer)return answerdef run_and_submit_all( profile: gr.OAuthProfile | None):"""49 unmodified linesresults_log = []answers_payload = []print(f"Running agent on {len(questions_data)} questions...")questions = [Question(**item) for item in questions_data]for item in questions:task_id = item.task_idquestion_text = item.questionif not task_id or question_text is None:print(f"Skipping item with missing task_id or question: {item}")continuetry:submitted_answer = agent(item)answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})except Exception as e:2 unmodified lines3456782 unmodified lines1112131415161718192021222349 unmodified lines73747576777879808182838485862 unmodified linesimport requestsimport inspectimport pandas as pd# (Keep Constants as is)# --- Constants ---2 unmodified lines# --- Basic Agent Definition ---# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------class BasicAgent:def __init__(self):print("BasicAgent initialized.")def __call__(self, question: str) -> str:print(f"Agent received question (first 50 chars): {question[:50]}...")fixed_answer = "This is a default answer."print(f"Agent returning fixed answer: {fixed_answer}")return fixed_answerdef run_and_submit_all( profile: gr.OAuthProfile | None):"""49 unmodified linesresults_log = []answers_payload = []print(f"Running agent on {len(questions_data)} questions...")for item in questions_data:task_id = item.get("task_id")question_text = item.get("question")if not task_id or question_text is None:print(f"Skipping item with missing task_id or question: {item}")continuetry:submitted_answer = agent(question_text)answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})except Exception as e:
123version https://git-lfs.github.com/spec/v1oid sha256:200f767e732b49efef5c05d128903ee4d2c34e66fdce7f5593ac123b2e637673size 280868123version https://git-lfs.github.com/spec/v1oid sha256:b218c951c1f888f0bbe6f46c080f57afc7c9348fffc7ba4da35749ff1e2ac40fsize 1793041234567891011121314151617181920212223242526272829303132333435from random import randintimport time
class UhOh(Exception): pass
class Hmm: def __init__(self): self.value = randint(-100, 100)
def Yeah(self): if self.value == 0: return True else: raise UhOh()
def Okay(): while True: yield Hmm()
def keep_trying(go, first_try=True): maybe = next(go) try: if maybe.Yeah(): return maybe.value except UhOh: if first_try: print("Working...") print("Please wait patiently...") time.sleep(0.1) return keep_trying(go, first_try=False)
if __name__ == "__main__": go = Okay() print(f"{keep_trying(go)}")123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225from dataclasses import dataclassfrom langgraph.graph import START, StateGraph, ENDfrom typing import TypedDictfrom agents import general_agent, excel_supervisor, video_supervisorimport osfrom typing import ListOPENAI_API_KEY = os.getenv("OPENAI_API_KEY")os.environ["OPENAI_API_KEY"] = str(OPENAI_API_KEY)
@dataclassclass Question: task_id: str question: str Level: str file_name: str local_file_path: str|None = None
def get_file_type(file_path: str) -> str: """Determine file type from extension.""" if not file_path: return "none" file_path = file_path.lower() if file_path.endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp')): return "image" elif file_path.endswith(('.xlsx', '.xls', '.csv')): return "excel" elif file_path.endswith('.py'): return "python" elif file_path.endswith(('.mp3', '.wav', '.m4a', '.ogg')): return "audio" else: return "unknown"
def answer_qery(question: str, thread_id: str = "default") -> str: """Ask the agent a question.""" config = {"configurable": {"thread_id": thread_id}, "recursion_limit": 50} try: result = video_supervisor.invoke({ "messages": [ {"role": "user", "content": question} ] }) return result["messages"][-1].content except Exception as e: return f"Error: {str(e)}" def ask_question(question: str, thread_id: str = "default") -> str: """Ask the agent a question.""" config = {"configurable": {"thread_id": thread_id}, "recursion_limit": 100} try: response = general_agent.invoke( {"messages": [{"role": "user", "content": question}]}, config=config ) return response["messages"][-1].content except Exception as e: return f"Error: {str(e)}"
def ask_question_with_file(question: Question, thread_id: str = "default") -> str: """Ask the agent a question, with optional file analysis.""" q = question.question root_file = "./files" file_path = root_file + "/" + question.file_name if not question.file_name: return ask_question(q, thread_id) file_type = get_file_type(file_path) # Create enhanced question with file guidance if file_type == "image": enhanced_question = f"{q}\n\nThere is an image file at '{file_path}'. Use the analyze_image tool to examine it." elif file_type == "excel": enhanced_question = f"{q}\n\nFile path: {file_path}" result = excel_supervisor.invoke({ "messages": [ {"role": "user", "content": enhanced_question} ] }) return result["messages"][-1].content elif file_type == "python": enhanced_question = f"{q}\n\nThere is a Python file at '{file_path}'. Use the read_python_file tool to examine it." elif file_type == "audio": enhanced_question = f"{q}\n\nThere is an audio file at '{file_path}'. Use the transcribe_audio tool to process it." else: enhanced_question = f"{q}\n\nThere is a file at '{file_path}' but I'm not sure what type it is." return ask_question(enhanced_question, thread_id)
def ask_question_youtube(question: Question) -> str: """Ask the agent a question, with optional file analysis.""" q = question.question result = video_supervisor.invoke({ "messages": [ {"role": "user", "content": q} ] }) return result["messages"][-1].content
# Stateclass State(TypedDict): question: Question decision: str answer: str # NODE FUNCTIONS - These are the ones that work with LangGraphdef ask_question_node(state: State) -> dict: """Node function for questions without files.""" question_obj = state["question"] thread_id = f"test_{question_obj.task_id}" # Call your existing function answer = answer_qery(question_obj.question, thread_id) # Return dict to update state return {"answer": answer}
def ask_question_with_file_node(state: State) -> dict: """Node function for questions with files.""" question_obj = state["question"] thread_id = f"test_{question_obj.task_id}" # Call your existing function answer = ask_question_with_file(question_obj, thread_id) # Return dict to update state return {"answer": answer}
def ask_question_youtube_node(state: State) -> dict: """Node function for questions with files.""" question_obj = state["question"] # Call your existing function answer = ask_question_youtube(question_obj) # Return dict to update state return {"answer": answer}
def router_node(state: State): """Router node - returns dict to update state""" if state["question"].file_name: decision = "query_with_file" elif "youtube.com" in state["question"].question or "youtu.be" in state["question"].question: decision = "youtube" else: decision = "query" return {"decision": decision}
def router_function(state: State): """Routing function - returns string to choose path""" return state["decision"]
def build_graph(): # Graph builder = StateGraph(State)
# Use the NODE functions (not the original functions) builder.add_node("query_with_file", ask_question_with_file_node) builder.add_node("query", ask_question_node) builder.add_node("youtube", ask_question_youtube_node) builder.add_node("router", router_node)
# Define edges builder.add_edge(START, "router") builder.add_conditional_edges( "router", router_function, { "query_with_file": "query_with_file", "query": "query", "youtube": "youtube", }, ) builder.add_edge("query_with_file", END) builder.add_edge("query", END) builder.add_edge("youtube", END)
react_graph = builder.compile() return react_graphdef extract_final_answer(text: str) -> str|List[str]: """Extract the final answer from a string containing 'FINAL ANSWER: answer'""" # Method 1: Simple string split (most common case) if "FINAL ANSWER:" in text: # Split on "FINAL ANSWER:" and take the part after it parts = text.split("FINAL ANSWER:", 1) # Split only on first occurrence return parts else: return "FINAL ANSWER: unknown" if __name__ == "__main__": test = [ { "task_id": "8e867cd7-cff9-4e6c-867a-ff5ddc2550be", "question": "How many studio albums were published by Mercedes Sosa between 2000 and 2009 (included)? You can use the latest 2022 version of english wikipedia.", "Level": "1", "file_name": "" }, { "task_id": "99c9cc74-fdc8-46c6-8f8d-3ce2d3bfeea3", "question": "Hi, I'm making a pie but I could use some help with my shopping list. I have everything I need for the crust, but I'm not sure about the filling. I got the recipe from my friend Aditi, but she left it as a voice memo and the speaker on my phone is buzzing so I can't quite make out what she's saying. Could you please listen to the recipe and list all of the ingredients that my friend described? I only want the ingredients for the filling, as I have everything I need to make my favorite pie crust. I've attached the recipe as Strawberry pie.mp3.\n\nIn your response, please only list the ingredients, not any measurements. So if the recipe calls for \"a pinch of salt\" or \"two cups of ripe strawberries\" the ingredients on the list would be \"salt\" and \"ripe strawberries\".\n\nPlease format your response as a comma separated list of ingredients. Also, please alphabetize the ingredients.", "Level": "1", "file_name": "99c9cc74-fdc8-46c6-8f8d-3ce2d3bfeea3.mp3" } ]
questions = [Question(**item) for item in test] for i, question in enumerate(questions): print(f"\n{i}. {question.question}") react_graph = build_graph() # Invoke the graph and capture the result result = react_graph.invoke({ "question": question, "decision": "", "answer": "" }) answer = result['answer'] print(answer) answer = extract_final_answer(answer)[1] print(answer)123456789101112131415gradiorequestslanggraphlanggraph-supervisorlangchainlangchain_communitylangchain_openaiduckduckgo-searchwikipediaarxivopenpyxlultralyticsyoutube-transcript-apigoogle-api-python-clientlangchain-google-community12gradiorequests
1234567You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER]. YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391from langchain_core.messages import HumanMessagefrom langchain_core.tools import toolfrom langchain_community.tools import ( DuckDuckGoSearchRun, WikipediaQueryRun, ArxivQueryRun)from langchain_google_community.search import ( GoogleSearchAPIWrapper, GoogleSearchRun)from langchain_community.utilities import WikipediaAPIWrapper, ArxivAPIWrapperfrom langchain_openai import ChatOpenAI
import base64import pandas as pdimport os
import osfrom huggingface_hub import InferenceClientimport jsonimport requestsfrom youtube_transcript_api import YouTubeTranscriptApifrom ultralytics import YOLOimport cv2
import re
from dotenv import load_dotenvload_dotenv()HF_TOKEN = os.getenv("HF_TOKEN")GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")GOOGLE_CSE_ID = os.getenv("GOOGLE_CSE_ID")client = InferenceClient( provider="hf-inference", api_key=HF_TOKEN,)
llm = ChatOpenAI(model="o4-mini")vision_llm = ChatOpenAI(model="gpt-4o")
@tooldef analyze_image(img_path: str, question: str) -> str: """Analyze an image and answer a question about it.""" try: with open(img_path, "rb") as image_file: image_bytes = image_file.read() image_base64 = base64.b64encode(image_bytes).decode("utf-8") message = [ HumanMessage( content=[ {"type": "text", "text": question}, { "type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{image_base64}"} } ] ) ] response = vision_llm.invoke(message) return response.content except Exception as e: return f"Error analyzing image: {str(e)}"
@tooldef read_excel_file(file_path: str, question: str) -> str: """Read and analyze an Excel file to answer a question.""" try: # Read Excel file df = pd.read_excel(file_path) df_dict = df.to_dict(orient='records') info = json.dumps(df_dict) return info except Exception as e: return f"Error reading Excel file: {str(e)}"
@tooldef read_python_file(file_path: str, question: str) -> str: """Read and analyze a Python file to answer a question.""" try: with open(file_path, 'r', encoding='utf-8') as f: code_content = f.read() prompt = f"""Here is Python code from a file:
```python {code_content} ```
Question: {question}
Please analyze the code and answer the question.""" response = llm.invoke([HumanMessage(content=prompt)]) return response.content except Exception as e: return f"Error reading Python file: {str(e)}"
@tooldef transcribe_audio(file_path: str, question: str) -> str: """Transcribe audio file.""" try: headers = { "Authorization": f"Bearer {HF_TOKEN}", "Content-Type": "audio/mpeg" # Add this line for MP3 files } API_URL = "https://router.huggingface.co/hf-inference/models/openai/whisper-large-v3"
def query(filename): with open(filename, "rb") as f: data = f.read() response = requests.request("POST", API_URL, headers=headers, data=data) return json.loads(response.content.decode("utf-8"))
data = query(file_path) return data except Exception as e: return f"Error transcribing audio: {str(e)}"
# Simple math tools@tooldef add(a: float, b: float) -> float: """Add two numbers.""" return a + b
@tooldef sum_list(numbers: list) -> float: """Sum a list of numbers.""" return sum(numbers)
# Simple data tools@tooldef extract_values(data: str, column: str) -> list: """Extract all values from a column in JSON data.""" parsed = json.loads(data) values = [] for row in parsed: for key, value in row.items(): if column.lower() in key.lower(): try: values.append(float(value)) except: pass return values
@tooldef filter_rows(data: str, exclude_words: list) -> str: """Remove rows containing any of the exclude words.""" parsed = json.loads(data) filtered = [] for row in parsed: row_text = " ".join(str(v).lower() for v in row.values()) if not any(word.lower() in row_text for word in exclude_words): filtered.append(row) return json.dumps(filtered)
@tooldef read_excel(file_path: str) -> str: """Read any Excel file and return as JSON.""" df = pd.read_excel(file_path) return json.dumps(df.to_dict(orient='records'))
@tooldef object_detection(video_url: str) -> str: """Analyze objects and visual content in a YouTube video.""" try: model = YOLO("yolo11n.pt") # Load an official Detect model results = model.track(video_url) # Track objects across frames frame_objects = [] for i, result in enumerate(results): if result.boxes is not None: objects_in_frame = [] for j in range(len(result.boxes)): class_name = result.names[int(result.boxes.cls[j].item())] confidence = float(result.boxes.conf[j].item()) if confidence > 0.5: # Only high confidence detections objects_in_frame.append(class_name) frame_objects.append({ "frame": i, "objects": objects_in_frame, "unique_objects": list(set(objects_in_frame)) }) return json.dumps(frame_objects, indent=2) except Exception as e: return f"Error analyzing video: {str(e)}"
@tooldef get_youtube_transcript(video_url: str) -> str: """Get transcript from a YouTube video.""" try: # Extract video ID video_id_match = re.search(r'(?:v=|\/)([0-9A-Za-z_-]{11}).*', video_url) if not video_id_match: return "Error: Could not extract video ID" video_id = video_id_match.group(1) transcript = YouTubeTranscriptApi.get_transcript(video_id) # Format with timestamps formatted_transcript = [] for entry in transcript: formatted_transcript.append({ "start": entry['start'], "duration": entry['duration'], "text": entry['text'] }) return json.dumps(formatted_transcript, indent=2) except Exception as e: return f"Error getting transcript: {str(e)}"
# @tooldef analyze_video_content(video_url: str, question: str = "", max_vision_frames: int = 1) -> str: """Analyze video content using YOLO for object detection and vision LLM for detailed analysis.""" try: model = YOLO("yolo11n.pt") results = model.track(video_url) # Step 1: YOLO analysis for all frames frame_objects = [] frames_with_content = [] for i, result in enumerate(results): frame_data = { "frame": i, "objects": [], "unique_objects": [], "object_counts": {} } if result.boxes is not None: objects_in_frame = [] for j in range(len(result.boxes)): class_name = result.names[int(result.boxes.cls[j].item())] confidence = float(result.boxes.conf[j].item()) if confidence > 0.5: objects_in_frame.append(class_name) # Count objects for obj in objects_in_frame: frame_data["object_counts"][obj] = frame_data["object_counts"].get(obj, 0) + 1 frame_data["objects"] = objects_in_frame frame_data["unique_objects"] = list(set(objects_in_frame)) # Store frame for potential vision analysis if objects_in_frame: # Only store frames with detected objects frames_with_content.append({ "frame_index": i, "objects": objects_in_frame, "object_counts": frame_data["object_counts"], "total_objects": len(objects_in_frame), "image": result.orig_img }) frame_objects.append(frame_data) # Step 2: If there's a specific question, use vision LLM on selected frames detailed_analyses = [] if question.strip(): # Sort frames by total objects and select top frames frames_with_content.sort(key=lambda x: x["total_objects"], reverse=True) selected_frames = frames_with_content[:max_vision_frames] for frame_data in selected_frames: try: # Encode frame directly to base64 _, buffer = cv2.imencode('.jpg', frame_data["image"]) image_bytes = buffer.tobytes() image_base64 = base64.b64encode(image_bytes).decode("utf-8") message = [ HumanMessage( content=[ {"type": "text", "text": question}, { "type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{image_base64}"} } ] ) ] vision_response = vision_llm.invoke(message) detailed_analyses.append({ "frame_index": frame_data["frame_index"], "yolo_objects": frame_data["objects"], "yolo_counts": frame_data["object_counts"], "vision_analysis": vision_response.content }) except Exception as vision_error: detailed_analyses.append({ "frame_index": frame_data["frame_index"], "yolo_objects": frame_data["objects"], "yolo_counts": frame_data["object_counts"], "vision_analysis": f"Vision analysis failed: {str(vision_error)}" }) # Combine results result_data = { "video_url": video_url, "question": question, "total_frames": len(frame_objects), "yolo_analysis": frame_objects, "frames_with_objects": len(frames_with_content) } if detailed_analyses: result_data["detailed_vision_analysis"] = detailed_analyses result_data["vision_frames_analyzed"] = len(detailed_analyses) return json.dumps(result_data, indent=2) except Exception as e: return f"Error analyzing video content: {str(e)}"@tooldef google_search(): """Google search tool""" api_wrapper = GoogleSearchAPIWrapper( google_api_key=GOOGLE_API_KEY, google_cse_id=GOOGLE_CSE_ID, k=10, # Number of results siterestrict=False # Site restrictions) google_search = GoogleSearchRun(api_wrapper=api_wrapper) return google_search
@tooldef wiki_search(): """Google search tool""" api_wrapper = WikipediaAPIWrapper() search = WikipediaQueryRun(api_wrapper=api_wrapper) return search
@tooldef arxiv_search(): """Google search tool""" api_wrapper = ArxivAPIWrapper() search = ArxivQueryRun(api_wrapper=api_wrapper) return searchdef general_tools(): tools = [ analyze_image, read_python_file, transcribe_audio, ] return tools
def analyze_video_tools(): tools = [object_detection, analyze_video_content] return tools
def youtube_transcript_tools(): tools = [get_youtube_transcript] return tools
def file_agent_tools(): tools = [read_excel] return tools
def math_agent_tools(): tools = [add, sum_list] return tools
def data_agent_tools(): tools = [extract_values, filter_rows] return tools
def search_agen_tools(): tools = [ google_search, ArxivQueryRun(api_wrapper=ArxivAPIWrapper()), WikipediaQueryRun(api_wrapper=WikipediaAPIWrapper()) ] return tools123version https://git-lfs.github.com/spec/v1oid sha256:0ebbc80d4a7680d14987a577cd21342b65ecfd94632bd9a8da63ae6417644ee1size 5613764.DS_StoreDeleted
.gitattributes-1
.gitignoreDeleted-2
__pycache__/agents.cpython-312.pycDeleted
__pycache__/tools.cpython-312.pycDeleted
agents.pyDeleted-138
app.py+9-18
files/1f975693-876d-457b-a649-393859e79bf3.mp3Deleted-3
files/7bd855d8-463d-4ed5-93ca-5fe35145f733.xlsxDeleted
files/99c9cc74-fdc8-46c6-8f8d-3ce2d3bfeea3.mp3Deleted-3
files/cca530fc-4052-43b2-b130-b30968d8aa44.pngDeleted
files/f918266a-b3e0-4914-865d-4faa564f1aef.pyDeleted-35
qa_graph.pyDeleted-225
requirements.txt+1-14
system_prompt.txtDeleted-7
tools.pyDeleted-391
yolo11n.ptDeleted-3