You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_mdconvert.py 6.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #!/usr/bin/env python3 -m pytest
  2. import pytest
  3. import os
  4. import shutil
  5. import requests
  6. import io
  7. try:
  8. from autogen.browser_utils import MarkdownConverter, UnsupportedFormatException, FileConversionException
  9. except ImportError:
  10. skip_all = True
  11. else:
  12. skip_all = False
  13. skip_exiftool = shutil.which("exiftool") is None
  14. TEST_FILES_DIR = os.path.join(os.path.dirname(__file__), "test_files")
  15. JPG_TEST_EXIFTOOL = {
  16. "Author": "AutoGen Authors",
  17. "Title": "AutoGen: Enabling Next-Gen LLM Applications via Multi-Agent Conversation",
  18. "Description": "AutoGen enables diverse LLM-based applications",
  19. "ImageSize": "1615x1967",
  20. "DateTimeOriginal": "2024:03:14 22:10:00",
  21. }
  22. PDF_TEST_URL = "https://arxiv.org/pdf/2308.08155v2.pdf"
  23. PDF_TEST_STRINGS = ["While there is contemporaneous exploration of multi-agent approaches"]
  24. YOUTUBE_TEST_URL = "https://www.youtube.com/watch?v=V2qZ_lgxTzg"
  25. YOUTUBE_TEST_STRINGS = [
  26. "## AutoGen FULL Tutorial with Python (Step-By-Step)",
  27. "This is an intermediate tutorial for installing and using AutoGen locally",
  28. "PT15M4S",
  29. "the model we're going to be using today is GPT 3.5 turbo", # From the transcript
  30. ]
  31. XLSX_TEST_STRINGS = [
  32. "## 09060124-b5e7-4717-9d07-3c046eb",
  33. "6ff4173b-42a5-4784-9b19-f49caff4d93d",
  34. "affc7dad-52dc-4b98-9b5d-51e65d8a8ad0",
  35. ]
  36. DOCX_TEST_STRINGS = [
  37. "314b0a30-5b04-470b-b9f7-eed2c2bec74a",
  38. "49e168b7-d2ae-407f-a055-2167576f39a1",
  39. "## d666f1f7-46cb-42bd-9a39-9a39cf2a509f",
  40. "# Abstract",
  41. "# Introduction",
  42. "AutoGen: Enabling Next-Gen LLM Applications via Multi-Agent Conversation",
  43. ]
  44. PPTX_TEST_STRINGS = [
  45. "2cdda5c8-e50e-4db4-b5f0-9722a649f455",
  46. "04191ea8-5c73-4215-a1d3-1cfb43aaaf12",
  47. "44bf7d06-5e7a-4a40-a2e1-a2e42ef28c8a",
  48. "1b92870d-e3b5-4e65-8153-919f4ff45592",
  49. "AutoGen: Enabling Next-Gen LLM Applications via Multi-Agent Conversation",
  50. ]
  51. BLOG_TEST_URL = "https://microsoft.github.io/autogen/blog/2023/04/21/LLM-tuning-math"
  52. BLOG_TEST_STRINGS = [
  53. "Large language models (LLMs) are powerful tools that can generate natural language texts for various applications, such as chatbots, summarization, translation, and more. GPT-4 is currently the state of the art LLM in the world. Is model selection irrelevant? What about inference parameters?",
  54. "an example where high cost can easily prevent a generic complex",
  55. ]
  56. WIKIPEDIA_TEST_URL = "https://en.wikipedia.org/wiki/Microsoft"
  57. WIKIPEDIA_TEST_STRINGS = [
  58. "Microsoft entered the operating system (OS) business in 1980 with its own version of [Unix]",
  59. 'Microsoft was founded by [Bill Gates](/wiki/Bill_Gates "Bill Gates")',
  60. ]
  61. WIKIPEDIA_TEST_EXCLUDES = [
  62. "You are encouraged to create an account and log in",
  63. "154 languages",
  64. "move to sidebar",
  65. ]
  66. SERP_TEST_URL = "https://www.bing.com/search?q=microsoft+wikipedia"
  67. SERP_TEST_STRINGS = [
  68. "](https://en.wikipedia.org/wiki/Microsoft",
  69. "Microsoft Corporation is **an American multinational corporation and technology company headquartered** in Redmond",
  70. "1995–2007: Foray into the Web, Windows 95, Windows XP, and Xbox",
  71. ]
  72. SERP_TEST_EXCLUDES = [
  73. "https://www.bing.com/ck/a?!&&p=",
  74. "data:image/svg+xml,%3Csvg%20width%3D",
  75. ]
  76. @pytest.mark.skipif(
  77. skip_all,
  78. reason="do not run if dependency is not installed",
  79. )
  80. def test_mdconvert_remote():
  81. mdconvert = MarkdownConverter()
  82. # By URL
  83. result = mdconvert.convert(PDF_TEST_URL)
  84. for test_string in PDF_TEST_STRINGS:
  85. assert test_string in result.text_content
  86. # By stream
  87. response = requests.get(PDF_TEST_URL)
  88. result = mdconvert.convert_stream(io.BytesIO(response.content), file_extension=".pdf", url=PDF_TEST_URL)
  89. for test_string in PDF_TEST_STRINGS:
  90. assert test_string in result.text_content
  91. # Youtube
  92. result = mdconvert.convert(YOUTUBE_TEST_URL)
  93. for test_string in YOUTUBE_TEST_STRINGS:
  94. assert test_string in result.text_content
  95. @pytest.mark.skipif(
  96. skip_all,
  97. reason="do not run if dependency is not installed",
  98. )
  99. def test_mdconvert_local():
  100. mdconvert = MarkdownConverter()
  101. # Test XLSX processing
  102. result = mdconvert.convert(os.path.join(TEST_FILES_DIR, "test.xlsx"))
  103. for test_string in XLSX_TEST_STRINGS:
  104. assert test_string in result.text_content
  105. # Test DOCX processing
  106. result = mdconvert.convert(os.path.join(TEST_FILES_DIR, "test.docx"))
  107. for test_string in DOCX_TEST_STRINGS:
  108. assert test_string in result.text_content
  109. # Test PPTX processing
  110. result = mdconvert.convert(os.path.join(TEST_FILES_DIR, "test.pptx"))
  111. for test_string in PPTX_TEST_STRINGS:
  112. assert test_string in result.text_content
  113. # Test HTML processing
  114. result = mdconvert.convert(os.path.join(TEST_FILES_DIR, "test_blog.html"), url=BLOG_TEST_URL)
  115. for test_string in BLOG_TEST_STRINGS:
  116. assert test_string in result.text_content
  117. # Test Wikipedia processing
  118. result = mdconvert.convert(os.path.join(TEST_FILES_DIR, "test_wikipedia.html"), url=WIKIPEDIA_TEST_URL)
  119. for test_string in WIKIPEDIA_TEST_EXCLUDES:
  120. assert test_string not in result.text_content
  121. for test_string in WIKIPEDIA_TEST_STRINGS:
  122. assert test_string in result.text_content
  123. # Test Bing processing
  124. result = mdconvert.convert(os.path.join(TEST_FILES_DIR, "test_serp.html"), url=SERP_TEST_URL)
  125. for test_string in SERP_TEST_EXCLUDES:
  126. assert test_string not in result.text_content
  127. for test_string in SERP_TEST_STRINGS:
  128. assert test_string in result.text_content
  129. @pytest.mark.skipif(
  130. skip_exiftool,
  131. reason="do not run if exiftool is not installed",
  132. )
  133. def test_mdconvert_exiftool():
  134. mdconvert = MarkdownConverter()
  135. # Test JPG metadata processing
  136. result = mdconvert.convert(os.path.join(TEST_FILES_DIR, "test.jpg"))
  137. for key in JPG_TEST_EXIFTOOL:
  138. target = f"{key}: {JPG_TEST_EXIFTOOL[key]}"
  139. assert target in result.text_content
  140. if __name__ == "__main__":
  141. """Runs this file's tests from the command line."""
  142. # test_mdconvert_remote()
  143. test_mdconvert_local()
  144. # test_mdconvert_exiftool()