Spaces:
Build error
Build error
Update langgraph_agent.py
Browse files2 unmodified lines34567893 unmodified lines1314151617181920212285 unmodified lines10810911011111211311430 unmodified lines14514614714814915015115215315415515615715815916016116216316416516623 unmodified lines1901911921931941951961971981992002012022 unmodified lines20520620720820921021121219 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 InferenceClient3 unmodified linesfrom langchain_openai import ChatOpenAIfrom langchain_huggingface import ChatHuggingFace, HuggingFaceEndpointfrom langchain_community.document_loaders import WikipediaLoader, ArxivLoaderfrom langchain_core.messages import SystemMessage, HumanMessagefrom langchain_google_genai import ChatGoogleGenerativeAIfrom langchain_core.tools import toolfrom langchain_google_community import GoogleSearchAPIWrapper@tool85 unmodified lineselif file_extension in (".jpeg", ".jpg", ".png"):return {"file_type": "image", "file_name": file_path, "file_content": f"Image file '{file_path}' detected. Use 'describe_image' tool to get a textual description."}elif file_extension == ".mp3":return {"file_type": "audio", "file_name": file_path, "file_content": f"Audio file '{file_path}' detected. Use 'transcribe_audio' tool to get the text transcription."}else:return {"file_type": "unsupported", "file_name": file_path, "file_content": f"Unsupported file type: {file_extension}. Only .txt, .py, .xlsx, .jpeg, .jpg, .png, .mp3 files are recognized."}except FileNotFoundError:30 unmodified linesexcept Exception as e:return {"error": f"Error describing image {image_path}: {str(e)}"}@tooldef transcribe_audio(audio_path: str) -> Dict[str, str]:"""Transcribes an audio file (e.g., MP3) to text using an automatic speech recognition model from the Hugging Face Inference API. Requires HF_API_TOKEN environment variable to be set."""if not HF_INFERENCE_CLIENT:return {"error": "Hugging Face API token not configured for audio transcription. Cannot use this tool."}try:with open(audio_path, "rb") as f:audio_bytes = f.read()transcription = HF_INFERENCE_CLIENT.automatic_speech_recognition(audio_bytes)return {"audio_transcription": transcription, "audio_path": audio_path}except FileNotFoundError:return {"error": f"Audio file not found: {audio_path}. Please ensure the file exists."}except 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]:"""23 unmodified lineselse: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,2 unmodified linesread_file_content,python_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)pass2 unmodified lines34567893 unmodified lines13141516171819202185 unmodified lines10710810911011111211311411511611730 unmodified lines14814915015115215315423 unmodified lines1781791801811821831841851861871881891902 unmodified lines19319419519619719819919 unmodified lines2192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682691 unmodified line2712722732742 unmodified linesimport contextlibimport pandas as pdfrom typing import Dict, List, Unionimport refrom PIL import Image as PILImagefrom huggingface_hub import InferenceClient3 unmodified linesfrom langchain_openai import ChatOpenAIfrom langchain_huggingface import ChatHuggingFace, HuggingFaceEndpointfrom langchain_community.document_loaders import WikipediaLoader, ArxivLoaderfrom langchain_core.messages import SystemMessage, HumanMessage, ToolMessagefrom langchain_google_genai import ChatGoogleGenerativeAIfrom langchain_core.tools import toolfrom langchain_google_community import GoogleSearchAPIWrapper@tool85 unmodified lineselif file_extension in (".jpeg", ".jpg", ".png"):return {"file_type": "image", "file_name": file_path, "file_content": f"Image file '{file_path}' detected. Use 'describe_image' tool to get a textual description."}elif file_extension == ".mp3":# For MP3, we indicate it's an audio file and expect the LLM to handle the blob directly.# In a real Langchain setup, you might actually read the bytes here and pass them# as a part of the message content to the LLM if it supports direct binary upload.# For now, this tool simply confirms its type for the agent.return {"file_type": "audio", "file_name": file_path, "file_content": f"Audio file '{file_path}' detected. The LLM (Gemini 2.5 Pro) can process this audio content directly."}else:return {"file_type": "unsupported", "file_name": file_path, "file_content": f"Unsupported file type: {file_extension}. Only .txt, .py, .xlsx, .jpeg, .jpg, .png, .mp3 files are recognized."}except FileNotFoundError:30 unmodified linesexcept Exception as e:return {"error": f"Error describing image {image_path}: {str(e)}"}# --- Youtube Tool (Remains the same) ---@tooldef Youtube(url: str, question: str) -> Dict[str, str]:"""23 unmodified lineselse:return {"error": "Invalid or unrecognized YouTube URL.", "url": url}# --- END YOUTUBE TOOL ---API_KEY = os.getenv("GEMINI_API_KEY")HF_API_TOKEN = os.getenv("HF_SPACE_TOKEN")GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")# Update the tools list (removed transcribe_audio)tools = [multiply, add, subtract, divide, modulus,wiki_search,2 unmodified linesread_file_content,python_interpreter,describe_image,Youtube, # <-- transcribe_audio has been removed]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"]# When sending messages to Gemini, if read_file_content identified an audio file,# you'll need to ensure the actual binary content of the audio file is included# in the message parts for the LLM to process it natively.# This part requires a bit more advanced handling than just text.# Langchain often handles this when you use `tool_code.File(...)` or similar constructs.# For simplicity in this prompt and code example, we're assuming the framework# will correctly pass the file content if `read_file_content` returns an audio type.# A more robust implementation would involve modifying the `assistant` node# to explicitly read the file bytes and add them to the message parts# if a file is detected in the input state.# Example of how you might include binary content (conceptual, depends on LangChain/API):# new_messages_to_send = []# for msg in messages_to_send:# if isinstance(msg, HumanMessage) and "audio file" in msg.content: # Simplified check# # Assume you can get the actual file path from the context# file_path_from_context = "Strawberry pie.mp3" # Or extract from msg.content# if os.path.exists(file_path_from_context):# with open(file_path_from_context, "rb") as f:# audio_bytes = f.read()# new_messages_to_send.append(# HumanMessage(# content=[# {"type": "text", "text": "Here is the audio file:"},# {"type": "media", "media_type": "audio/mp3", "data": audio_bytes}# ]# )# )# else:# new_messages_to_send.append(msg)# llm_response = llm_with_tools.invoke(new_messages_to_send)llm_response = llm_with_tools.invoke(messages_to_send) # For now, keep as is, rely on frameworkprint(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__":pass
langgraph_agent.py+50-38