Spaces:
Build error
Build error
Update langgraph_agent.py
Browse files2 unmodified lines345678150 unmodified lines1591601611621631641651661671683 unmodified lines17217317417517617719 unmodified lines1971981992002012022032042052062072082092102112122131 unmodified line2152162172182 unmodified linesimport contextlibimport pandas as pdfrom typing import Dict, List, Unionfrom PIL import Image as PILImagefrom huggingface_hub import InferenceClient150 unmodified linesexcept Exception as e:return {"error": f"Error transcribing audio {audio_path}: {str(e)}"}API_KEY = os.getenv("GEMINI_API_KEY")HF_API_TOKEN = os.getenv("HF_SPACE_TOKEN")GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")tools = [multiply, add, subtract, divide, modulus,wiki_search,3 unmodified linespython_interpreter,describe_image,transcribe_audio,]with open("prompt.txt", "r", encoding="utf-8") as f:19 unmodified lineselse:raise ValueError("Invalid provider. Choose 'gemini' or 'huggingface'.")llm_with_tools = llm.bind_tools(tools)def assistant(state: MessagesState):messages_to_send = [sys_msg] + state["messages"]llm_response = llm_with_tools.invoke(messages_to_send)print(f"LLM Raw Response: {llm_response}")return {"messages": [llm_response]}builder = StateGraph(MessagesState)builder.add_node("assistant", assistant)builder.add_node("tools", ToolNode(tools))builder.add_edge(START, "assistant")builder.add_conditional_edges("assistant", tools_condition)builder.add_edge("tools", "assistant")1 unmodified linereturn builder.compile()if __name__ == "__main__":pass2 unmodified lines3456789150 unmodified lines1601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012023 unmodified lines20620720820921021121219 unmodified lines2322332342352362372382392402412422432442452462472482491 unmodified line2512522532542552562572582592602612622 unmodified linesimport contextlibimport pandas as pdfrom typing import Dict, List, Unionimport re # Import regex module to help identify YouTube URLsfrom PIL import Image as PILImagefrom huggingface_hub import InferenceClient150 unmodified linesexcept Exception as e:return {"error": f"Error transcribing audio {audio_path}: {str(e)}"}# --- NEW YOUTUBE TOOL ---@tooldef Youtube(url: str, question: str) -> Dict[str, str]:"""Tells about the YouTube video identified by the given URL, answering a question about it.Note: This is a simulated response. In a real application, this would interact with a YouTube APIor a video analysis service to get actual video information and transcripts."""print(f"Youtube called with URL: {url}, Question: {question}")# Placeholder for actual YouTube API call.# In a real scenario, you'd use a library like `google-api-python-client` for YouTube Data API# or a dedicated video transcription/analysis service.# Simulating the previous video content for demonstrationif "https://www.youtube.com/watch?v=1htKBjuUWec" in url or re.search(r'youtube\.com/watch\?v=|youtu\.be/', url):return {"video_url": url,"question_asked": question,"video_summary": "The video titled 'Teal'c coffee first time' shows a scene where several individuals are reacting to a beverage, presumably coffee, that Teal'c is trying for the first time. Key moments include: A person off-screen remarking, 'Wow this coffee's great'; another asking if it's 'cinnamon chicory tea oak'; and Teal'c reacting strongly to the taste or temperature, stating 'isn't that hot' indicating he finds it very warm.","details": {"00:00:00": "Someone remarks, 'Wow this coffee's great I was just thinking that yeah is that cinnamon chicory tea oak'","00:00:11": "Teal'c takes a large gulp from a black mug","00:00:24": "Teal'c reacts strongly, someone asks 'isn't that hot'","00:00:26": "Someone agrees, 'extremely'"}}else:return {"error": "Invalid or unrecognized YouTube URL.", "url": url}# --- END NEW YOUTUBE TOOL ---API_KEY = os.getenv("GEMINI_API_KEY")HF_API_TOKEN = os.getenv("HF_SPACE_TOKEN") # This seems to be a duplicate or slightly different HF token varGEMINI_API_KEY = os.getenv("GEMINI_API_KEY") # This is fine# Update the tools list to include the new YouTube tooltools = [multiply, add, subtract, divide, modulus,wiki_search,3 unmodified linespython_interpreter,describe_image,transcribe_audio,Youtube, # <-- ADDED THE NEW YOUTUBE TOOL HERE]with open("prompt.txt", "r", encoding="utf-8") as f:19 unmodified lineselse:raise ValueError("Invalid provider. Choose 'gemini' or 'huggingface'.")# This is the crucial line that binds your defined Python tools to the LLMllm_with_tools = llm.bind_tools(tools)def assistant(state: MessagesState):messages_to_send = [sys_msg] + state["messages"]llm_response = llm_with_tools.invoke(messages_to_send)print(f"LLM Raw Response: {llm_response}") # Good for debuggingreturn {"messages": [llm_response]}builder = StateGraph(MessagesState)builder.add_node("assistant", assistant)builder.add_node("tools", ToolNode(tools)) # Ensure ToolNode also has access to all toolsbuilder.add_edge(START, "assistant")builder.add_conditional_edges("assistant", tools_condition)builder.add_edge("tools", "assistant")1 unmodified linereturn builder.compile()if __name__ == "__main__":# Example usage (you'll need to set GEMINI_API_KEY and potentially HF_API_TOKEN env vars)# This part assumes you have a prompt.txt file with the system_prompt as discussed earlier.# You would typically interact with the compiled graph like this:# graph = build_graph("gemini")# user_input = "Tell me about this video: https://www.youtube.com/watch?v=1htKBjuUWec"# result = graph.invoke({"messages": [HumanMessage(content=user_input)]})# print(result)pass
langgraph_agent.py+48-4