Spaces:
Running
Running
| from typing import Optional, List | |
| from langchain.agents import Tool, AgentType, initialize_agent | |
| from langchain.llms.base import LLM | |
| from firedetection import detect_fire_in_video | |
| # Define a LangChain tool using a decorator or by manually creating a Tool object. | |
| def detect_fire_in_video_tool(video_path: str) -> str: | |
| """ | |
| Takes a video path, calls 'detect_fire_in_video' function, | |
| and returns a message indicating the output location. | |
| """ | |
| output_video_path = detect_fire_in_video( | |
| input_video_path=video_path, | |
| output_video_path="temp_outputs/result.avi", | |
| model_path="/Users/bahakizil/Desktop/firedetection/firedetectionyolo/last.pt", # Adjust if your model file is named differently | |
| device="cpu" # or 'cuda'/'mps' if available | |
| ) | |
| return f"Fire detection completed. Output saved to: {output_video_path}" | |
| class SimpleFakeLLM(LLM): | |
| """ | |
| A trivial/fake LLM for demonstration. | |
| It always decides to call 'FireDetectionTool' regardless of the prompt. | |
| This replaces the need for external APIs like OpenAI or HuggingFace, | |
| while still demonstrating the agent + tool usage in LangChain. | |
| """ | |
| def _llm_type(self) -> str: | |
| return "fake-llm" | |
| def _call(self, prompt: str, stop: Optional[List[str]] = None) -> str: | |
| # Always produce a response suggesting the use of FireDetectionTool: | |
| return "I should use FireDetectionTool to process the video." | |
| def create_fire_detection_agent(): | |
| """ | |
| Creates a LangChain agent with: | |
| - A single tool: FireDetectionTool | |
| - A trivial LLM (SimpleFakeLLM) | |
| """ | |
| llm = SimpleFakeLLM() | |
| tools = [detect_fire_in_video_tool] | |
| agent = initialize_agent( | |
| tools=tools, | |
| llm=llm, | |
| agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, | |
| verbose=True | |
| ) | |
| return agent | |
| if __name__ == "__main__": | |
| # Example usage: | |
| agent = create_fire_detection_agent() | |
| # Prompt the agent to detect fire in a given video (sample_video.mp4) | |
| prompt = "Please detect fire in 'sample_video.mp4'." | |
| response = agent.run(prompt) | |
| print("\nAgent response:", response) | |