translate_readme.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import sys
  2. from pathlib import Path
  3. import asyncio
  4. sys.path.append(str(Path(__file__).parent.parent.parent))
  5. import g4f
  6. g4f.debug.logging = True
  7. from g4f.debug import access_token
  8. provider = g4f.Provider.OpenaiChat
  9. iso = "GE"
  10. language = "german"
  11. translate_prompt = f"""
  12. Translate this markdown document to {language}.
  13. Don't translate or change inline code examples.
  14. ```md
  15. """
  16. keep_note = "Keep this: [!Note] as [!Note].\n"
  17. blocklist = [
  18. '## ©️ Copyright',
  19. '## 🚀 Providers and Models',
  20. '## 🔗 Related GPT4Free Projects'
  21. ]
  22. allowlist = [
  23. "### Other",
  24. "### Models"
  25. ]
  26. def read_text(text):
  27. start = end = 0
  28. new = text.strip().split('\n')
  29. for i, line in enumerate(new):
  30. if line.startswith('```'):
  31. if not start:
  32. start = i + 1
  33. end = i
  34. return '\n'.join(new[start:end]).strip()
  35. async def translate(text):
  36. prompt = translate_prompt + text.strip() + '\n```'
  37. if "[!Note]" in text:
  38. prompt = keep_note + prompt
  39. result = read_text(await provider.create_async(
  40. model="",
  41. messages=[{"role": "user", "content": prompt}],
  42. access_token=access_token
  43. ))
  44. if text.endswith("```") and not result.endswith("```"):
  45. result += "\n```"
  46. return result
  47. async def translate_part(part, i):
  48. blocklisted = False
  49. for headline in blocklist:
  50. if headline in part:
  51. blocklisted = True
  52. if blocklisted:
  53. lines = part.split('\n')
  54. lines[0] = await translate(lines[0])
  55. part = '\n'.join(lines)
  56. for trans in allowlist:
  57. if trans in part:
  58. part = part.replace(trans, await translate(trans))
  59. else:
  60. part = await translate(part)
  61. print(f"[{i}] translated")
  62. return part
  63. async def translate_readme(readme) -> str:
  64. parts = readme.split('\n## ')
  65. print(f"{len(parts)} parts...")
  66. parts = await asyncio.gather(
  67. *[translate_part("## " + part, i) for i, part in enumerate(parts)]
  68. )
  69. return "\n\n".join(parts)
  70. with open("README.md", "r") as fp:
  71. readme = fp.read()
  72. print("Translate readme...")
  73. readme = asyncio.run(translate_readme(readme))
  74. file = f"README-{iso}.md"
  75. with open(file, "w") as fp:
  76. fp.write(readme)
  77. print(f'"{file}" saved')