Spaces:
Build error
Build error
Romain Lembo
Fix argument description and enhance file existence check in python code parser
1a5c922
| """Tool to parse Python code.""" | |
| import os | |
| from langchain_core.tools import tool | |
| from langchain_community.document_loaders.generic import GenericLoader | |
| from langchain_community.document_loaders.parsers import LanguageParser | |
| def python_code_parser(file_path: str) -> str: | |
| """ | |
| Parse Python code to extract function names and their docstrings. | |
| Args: | |
| file_path: The path to the Python file to parse. | |
| Returns: | |
| Interpreted Python code as a string. | |
| """ | |
| if not os.path.exists(file_path): | |
| return "0" | |
| loader = GenericLoader.from_filesystem( | |
| file_path, | |
| glob="**/*", | |
| suffixes=[".py"], | |
| parser=LanguageParser() | |
| ) | |
| search_docs = loader.load() | |
| formatted_search_docs = "\n\n---\n\n".join( | |
| [ | |
| f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content}\n</Document>' | |
| for doc in search_docs | |
| ]) | |
| return {"audio_results": formatted_search_docs} |