wony617
commited on
Commit
Β·
4eb685d
1
Parent(s):
ef58ab4
Update toctree
Browse files- agent/handler.py +5 -0
- agent/workflow.py +40 -2
agent/handler.py
CHANGED
@@ -422,11 +422,16 @@ def approve_handler(history, owner, repo, reference_pr_url):
|
|
422 |
translated_content = state.current_file_content["translated"]
|
423 |
response += "\n\nπ **Generating GitHub PR...**"
|
424 |
|
|
|
|
|
|
|
|
|
425 |
pr_response = generate_github_pr(
|
426 |
target_language=state.target_language,
|
427 |
filepath=current_file,
|
428 |
translated_content=translated_content,
|
429 |
github_config=state.github_config,
|
|
|
430 |
)
|
431 |
response += f"\n{pr_response}"
|
432 |
else:
|
|
|
422 |
translated_content = state.current_file_content["translated"]
|
423 |
response += "\n\nπ **Generating GitHub PR...**"
|
424 |
|
425 |
+
# Extract title from file for toctree mapping
|
426 |
+
file_name = current_file.split("/")[-1].replace(".md", "").replace("_", " ").title()
|
427 |
+
print(file_name)
|
428 |
+
|
429 |
pr_response = generate_github_pr(
|
430 |
target_language=state.target_language,
|
431 |
filepath=current_file,
|
432 |
translated_content=translated_content,
|
433 |
github_config=state.github_config,
|
434 |
+
en_title=file_name,
|
435 |
)
|
436 |
response += f"\n{pr_response}"
|
437 |
else:
|
agent/workflow.py
CHANGED
@@ -53,6 +53,19 @@ def report_in_translation_status_files(translate_lang: str) -> tuple[str, list[s
|
|
53 |
|
54 |
def translate_docs(lang: str, file_path: str, additional_instruction: str = "") -> tuple[str, str]:
|
55 |
"""Translate documentation."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
# step 1. Get content from file path
|
57 |
content = get_content(file_path)
|
58 |
to_translate = preprocess_content(content)
|
@@ -105,6 +118,7 @@ def generate_github_pr(
|
|
105 |
filepath: str,
|
106 |
translated_content: str = None,
|
107 |
github_config: dict = None,
|
|
|
108 |
) -> str:
|
109 |
"""Generate a GitHub PR for translated documentation.
|
110 |
|
@@ -113,6 +127,7 @@ def generate_github_pr(
|
|
113 |
filepath: Original file path (e.g., "docs/source/en/accelerator_selection.md")
|
114 |
translated_content: Translated content (if None, read from file)
|
115 |
github_config: GitHub configuration dictionary
|
|
|
116 |
|
117 |
Returns:
|
118 |
PR creation result message
|
@@ -168,14 +183,37 @@ def generate_github_pr(
|
|
168 |
repo_name=github_config["repo_name"],
|
169 |
base_branch=github_config.get("base_branch", "main"),
|
170 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
171 |
|
172 |
# Process result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
173 |
if result["status"] == "success":
|
174 |
return f"""β
**GitHub PR Creation Successful!**
|
175 |
|
176 |
π **PR URL:** {result["pr_url"]}
|
177 |
πΏ **Branch:** {result["branch"]}
|
178 |
-
π **File:** {result["file_path"]}
|
179 |
|
180 |
{result["message"]}"""
|
181 |
|
@@ -183,7 +221,7 @@ def generate_github_pr(
|
|
183 |
return f"""β οΈ **Partial Success**
|
184 |
|
185 |
πΏ **Branch:** {result["branch"]}
|
186 |
-
π **File:** {result["file_path"]}
|
187 |
|
188 |
{result["message"]}
|
189 |
|
|
|
53 |
|
54 |
def translate_docs(lang: str, file_path: str, additional_instruction: str = "") -> tuple[str, str]:
|
55 |
"""Translate documentation."""
|
56 |
+
# Check if translation already exists
|
57 |
+
translation_file_path = (
|
58 |
+
Path(__file__).resolve().parent.parent
|
59 |
+
/ f"translation_result/{file_path}"
|
60 |
+
)
|
61 |
+
|
62 |
+
if translation_file_path.exists():
|
63 |
+
print(f"π Found existing translation: {translation_file_path}")
|
64 |
+
with open(translation_file_path, "r", encoding="utf-8") as f:
|
65 |
+
existing_content = f.read()
|
66 |
+
if existing_content.strip():
|
67 |
+
return "Existing translation loaded (no tokens used)", existing_content
|
68 |
+
|
69 |
# step 1. Get content from file path
|
70 |
content = get_content(file_path)
|
71 |
to_translate = preprocess_content(content)
|
|
|
118 |
filepath: str,
|
119 |
translated_content: str = None,
|
120 |
github_config: dict = None,
|
121 |
+
en_title: str = None,
|
122 |
) -> str:
|
123 |
"""Generate a GitHub PR for translated documentation.
|
124 |
|
|
|
127 |
filepath: Original file path (e.g., "docs/source/en/accelerator_selection.md")
|
128 |
translated_content: Translated content (if None, read from file)
|
129 |
github_config: GitHub configuration dictionary
|
130 |
+
en_title: English title for toctree mapping
|
131 |
|
132 |
Returns:
|
133 |
PR creation result message
|
|
|
183 |
repo_name=github_config["repo_name"],
|
184 |
base_branch=github_config.get("base_branch", "main"),
|
185 |
)
|
186 |
+
# result = {
|
187 |
+
# 'status': 'partial_success',
|
188 |
+
# 'branch': 'ko-attention_interface',
|
189 |
+
# 'file_path': 'docs/source/ko/attention_interface.md',
|
190 |
+
# 'message': 'File was saved and commit was successful.\nPR creation failed: ERROR: Existing PR found: https://github.com/Jwaminju/transformers/pull/1', 'error_details': 'ERROR: Existing PR found: https://github.com/Jwaminju/transformers/pull/1'
|
191 |
+
# }
|
192 |
+
# Process toctree update after successful translation PR
|
193 |
+
toctree_result = None
|
194 |
+
if en_title:
|
195 |
+
from agent.toctree_handler import TocTreeHandler
|
196 |
+
toctree_handler = TocTreeHandler()
|
197 |
+
toctree_result = toctree_handler.update_toctree_after_translation(
|
198 |
+
result, en_title, filepath, agent, github_config
|
199 |
+
)
|
200 |
+
print("toctree_result:", toctree_result)
|
201 |
|
202 |
# Process result
|
203 |
+
# Generate toctree status message (shared for both success and partial_success)
|
204 |
+
toctree_status = ""
|
205 |
+
if toctree_result:
|
206 |
+
if toctree_result["status"] == "success":
|
207 |
+
toctree_status = f"\nπ **Toctree Updated:** β
{toctree_result['message']}"
|
208 |
+
else:
|
209 |
+
toctree_status = f"\nπ **Toctree Update Failed:** β {toctree_result['message']}"
|
210 |
+
|
211 |
if result["status"] == "success":
|
212 |
return f"""β
**GitHub PR Creation Successful!**
|
213 |
|
214 |
π **PR URL:** {result["pr_url"]}
|
215 |
πΏ **Branch:** {result["branch"]}
|
216 |
+
π **File:** {result["file_path"]}{toctree_status}
|
217 |
|
218 |
{result["message"]}"""
|
219 |
|
|
|
221 |
return f"""β οΈ **Partial Success**
|
222 |
|
223 |
πΏ **Branch:** {result["branch"]}
|
224 |
+
π **File:** {result["file_path"]}{toctree_status}
|
225 |
|
226 |
{result["message"]}
|
227 |
|