Spaces:
Build error
Build error
Update langgraph_agent.py
Browse files90 unmodified lines9192939495969717 unmodified lines11511611711811912012112236 unmodified lines1591601611621631645 unmodified lines1701711721731741751761776 unmodified lines18418518618718818919090 unmodified linesif HF_API_TOKEN:HF_INFERENCE_CLIENT = InferenceClient(token=HF_API_TOKEN)else:print("WARNING: HF_API_TOKEN not set. Image tools will not function.")@tooldef read_file_content(file_path: str) -> Dict[str, str]:17 unmodified lines# Indicate that it's an image and needs to be described by a specific toolreturn {"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":# Indicate that it's an audio file and the LLM should process it nativelyreturn {"file_type": "audio", "file_name": file_path, "file_content": f"Audio file '{file_path}' detected. The LLM should process this natively."}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:36 unmodified linesexcept Exception as e:return {"error": f"Error describing image {image_path}: {str(e)}"}API_KEY = os.getenv("GEMINI_API_KEY")HF_SPACE_TOKEN = os.getenv("HF_SPACE_TOKEN")5 unmodified lineswiki_search, web_search, arvix_search,read_file_content,python_interpreter,describe_image, # Added new tool# transcribe_audio, # Removed as per user request]6 unmodified lines"""Build the LangGraph agent with chosen LLM (default: Gemini)."""if provider == "gemini":llm = ChatGoogleGenerativeAI(model= "gemini-1.5-flash-preview-05-20", # This model is capable of native audio processingtemperature=1.0,max_retries=2,api_key=GEMINI_API_KEY,90 unmodified lines9192939495969717 unmodified lines11511611711811912012112236 unmodified lines1591601611621631641651661671681691701711721731741751761771781791801811825 unmodified lines1881891901911921931941956 unmodified lines20220320420520620720890 unmodified linesif HF_API_TOKEN:HF_INFERENCE_CLIENT = InferenceClient(token=HF_API_TOKEN)else:print("WARNING: HF_API_TOKEN not set. Image and Audio tools will not function.")@tooldef read_file_content(file_path: str) -> Dict[str, str]:17 unmodified lines# Indicate that it's an image and needs to be described by a specific toolreturn {"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":# Indicate that it's an audio file and needs to be transcribed by a specific toolreturn {"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:36 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 modelfrom 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)}"}API_KEY = os.getenv("GEMINI_API_KEY")HF_SPACE_TOKEN = os.getenv("HF_SPACE_TOKEN")5 unmodified lineswiki_search, web_search, arvix_search,read_file_content,python_interpreter,describe_image,transcribe_audio, # Re-added tool]6 unmodified lines"""Build the LangGraph agent with chosen LLM (default: Gemini)."""if provider == "gemini":llm = ChatGoogleGenerativeAI(model= "gemini-1.5-flash-preview-05-20",temperature=1.0,max_retries=2,api_key=GEMINI_API_KEY,
langgraph_agent.py+24-6