|
"""Module for gradio interfaces.""" |
|
|
|
import os |
|
from pathlib import Path |
|
import gradio as gr |
|
|
|
from translator.content import ( |
|
fill_scaffold, |
|
get_content, |
|
get_full_prompt, |
|
llm_translate, |
|
preprocess_content, |
|
) |
|
from translator.retriever import report, get_github_issue_open_pr |
|
|
|
|
|
try: |
|
from pr_generator.agent import GitHubPRAgent |
|
|
|
GITHUB_PR_AVAILABLE = True |
|
except ImportError as e: |
|
print(f"β οΈ GitHub PR Agent is not available: {e}") |
|
GITHUB_PR_AVAILABLE = False |
|
|
|
|
|
|
|
|
|
def report_translation_target_files( |
|
translate_lang: str, top_k: int = 1 |
|
) -> tuple[str, list[list[str]]]: |
|
"""Return the top-k files that need translation, excluding files already in progress. |
|
|
|
Args: |
|
translate_lang: Target language to translate |
|
top_k: Number of top-first files to return for translation. (Default 1) |
|
""" |
|
|
|
docs_in_progress, pr_info_list = get_github_issue_open_pr(translate_lang) |
|
|
|
|
|
all_status_report, all_filepath_list = report(translate_lang, top_k * 2) |
|
|
|
|
|
available_files = [f for f in all_filepath_list if f not in docs_in_progress] |
|
|
|
|
|
filepath_list = available_files[:top_k] |
|
|
|
|
|
status_report = all_status_report |
|
|
|
if docs_in_progress: |
|
status_report += f"\n\nπ€ Found {len(docs_in_progress)} files in progress for translation:" |
|
for i, file in enumerate(docs_in_progress): |
|
status_report += f"\n{i+1}. `{file}`: {pr_info_list[i]}" |
|
status_report += f"\n\nπ Showing {len(filepath_list)} available files (excluding in-progress):" |
|
|
|
return status_report, [[file] for file in filepath_list] |
|
|
|
|
|
def translate_docs(lang: str, file_path: str, additional_instruction: str = "") -> tuple[str, str]: |
|
"""Translate documentation.""" |
|
|
|
translation_file_path = ( |
|
Path(__file__).resolve().parent.parent |
|
/ f"translation_result/{file_path}" |
|
) |
|
|
|
if translation_file_path.exists(): |
|
print(f"π Found existing translation: {translation_file_path}") |
|
with open(translation_file_path, "r", encoding="utf-8") as f: |
|
existing_content = f.read() |
|
if existing_content.strip(): |
|
return "Existing translation loaded (no tokens used)", existing_content |
|
|
|
|
|
content = get_content(file_path) |
|
to_translate = preprocess_content(content) |
|
|
|
|
|
if lang == "ko": |
|
translation_lang = "Korean" |
|
to_translate_with_prompt = get_full_prompt(translation_lang, to_translate, additional_instruction) |
|
|
|
print("to_translate_with_prompt:\n", to_translate_with_prompt) |
|
|
|
|
|
|
|
callback_result, translated_content = llm_translate(to_translate_with_prompt) |
|
print("translated_content:\n") |
|
print(translated_content) |
|
|
|
translated_doc = fill_scaffold(content, to_translate, translated_content) |
|
print("translated_doc:\n") |
|
print(translated_doc) |
|
return callback_result, translated_doc |
|
|
|
|
|
def translate_docs_interactive( |
|
translate_lang: str, selected_files: list[list[str]], additional_instruction: str = "" |
|
) -> tuple[str, str]: |
|
"""Interactive translation function that processes files one by one. |
|
|
|
Args: |
|
translate_lang: Target language to translate |
|
selected_files: List of file paths to translate |
|
""" |
|
|
|
file_paths = [row[0] for row in selected_files if row and len(row) > 0] |
|
|
|
|
|
current_file = file_paths[0] |
|
|
|
status = f"β
Translation completed: `{current_file}` β `{translate_lang}`\n\n" |
|
callback_result, translated_content = translate_docs(translate_lang, current_file, additional_instruction) |
|
status += f"π° Used token and cost: \n```\n{callback_result}\n```" |
|
|
|
print(status) |
|
|
|
return translated_content |
|
|
|
|
|
def generate_github_pr( |
|
target_language: str, |
|
filepath: str, |
|
translated_content: str = None, |
|
github_config: dict = None, |
|
en_title: str = None, |
|
) -> str: |
|
"""Generate a GitHub PR for translated documentation. |
|
|
|
Args: |
|
target_language: Target language for translation (e.g., "ko") |
|
filepath: Original file path (e.g., "docs/source/en/accelerator_selection.md") |
|
translated_content: Translated content (if None, read from file) |
|
github_config: GitHub configuration dictionary |
|
en_title: English title for toctree mapping |
|
|
|
Returns: |
|
PR creation result message |
|
""" |
|
if not GITHUB_PR_AVAILABLE: |
|
return "β GitHub PR Agent is not available. Please install required libraries." |
|
|
|
if not github_config: |
|
return "β GitHub configuration not provided." |
|
|
|
|
|
required_fields = ["token", "owner", "repo_name", "reference_pr_url"] |
|
missing_fields = [ |
|
field for field in required_fields if not github_config.get(field) |
|
] |
|
|
|
if missing_fields: |
|
return f"β Missing required configuration: {', '.join(missing_fields)}. Please provide these values." |
|
|
|
|
|
os.environ["GITHUB_TOKEN"] = github_config["token"] |
|
|
|
try: |
|
|
|
if translated_content is None: |
|
translation_file_path = ( |
|
Path(__file__).resolve().parent.parent |
|
/ f"translation_result/{filepath}" |
|
) |
|
if not translation_file_path.exists(): |
|
return f"β Translation file not found: {translation_file_path}" |
|
|
|
with open(translation_file_path, "r", encoding="utf-8") as f: |
|
translated_content = f.read() |
|
|
|
if not translated_content or not translated_content.strip(): |
|
return "β Translated content is empty." |
|
|
|
|
|
print(f"π Starting GitHub PR creation...") |
|
print(f" π File: {filepath}") |
|
print(f" π Language: {target_language}") |
|
print(f" π Reference PR: {github_config['reference_pr_url']}") |
|
print(f" π Repository: {github_config['owner']}/{github_config['repo_name']}") |
|
|
|
agent = GitHubPRAgent() |
|
result = agent.run_translation_pr_workflow( |
|
reference_pr_url=github_config["reference_pr_url"], |
|
target_language=target_language, |
|
filepath=filepath, |
|
translated_doc=translated_content, |
|
owner=github_config["owner"], |
|
repo_name=github_config["repo_name"], |
|
base_branch=github_config.get("base_branch", "main"), |
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
toctree_result = None |
|
if en_title: |
|
from agent.toctree_handler import TocTreeHandler |
|
toctree_handler = TocTreeHandler() |
|
toctree_result = toctree_handler.update_toctree_after_translation( |
|
result, filepath, agent, github_config |
|
) |
|
|
|
|
|
|
|
toctree_status = "" |
|
if toctree_result: |
|
if toctree_result["status"] == "success": |
|
toctree_status = f"\nπ **Toctree Updated:** β
{toctree_result['message']}" |
|
else: |
|
toctree_status = f"\nπ **Toctree Update Failed:** β {toctree_result['message']}" |
|
|
|
if result["status"] == "success": |
|
|
|
import datetime |
|
pr_url = result.get('pr_url', 'NO_PR_URL') |
|
log_entry = f"[{datetime.datetime.now().isoformat()}] {result['file_path']} -> {pr_url} ({result['status']})\n" |
|
try: |
|
with open("pr_success.log", "a", encoding="utf-8") as f: |
|
f.write(log_entry) |
|
print(f"β
Logged PR result: {log_entry.strip()}") |
|
except Exception as e: |
|
print(f"β Failed to log PR result: {e}") |
|
|
|
return f"""β
**GitHub PR Creation Successful!** |
|
|
|
π **PR URL:** {result.get('pr_url', 'NO_PR_URL')} |
|
πΏ **Branch:** {result["branch"]} |
|
π **File:** {result["file_path"]}{toctree_status} |
|
|
|
{result["message"]}""" |
|
|
|
elif result["status"] == "partial_success": |
|
|
|
import datetime |
|
pr_url = result.get('pr_url', 'NO_PR_URL') |
|
log_entry = f"[{datetime.datetime.now().isoformat()}] {result['file_path']} -> {pr_url} ({result['status']})\n" |
|
try: |
|
with open("pr_success.log", "a", encoding="utf-8") as f: |
|
f.write(log_entry) |
|
print(f"β
Logged PR result: {log_entry.strip()}") |
|
except Exception as e: |
|
print(f"β Failed to log PR result: {e}") |
|
|
|
return f"""β οΈ **Partial Success** |
|
|
|
πΏ **Branch:** {result["branch"]} |
|
π **File:** {result["file_path"]}{toctree_status} |
|
|
|
{result["message"]} |
|
|
|
**Error Details:** |
|
{result.get("error_details", "Unknown error")}""" |
|
|
|
else: |
|
return f"""β **GitHub PR Creation Failed** |
|
|
|
**Error Message:** |
|
{result["message"]}""" |
|
|
|
except Exception as e: |
|
error_msg = f"β Unexpected error occurred during PR creation: {str(e)}" |
|
print(error_msg) |
|
return error_msg |
|
|
|
|
|
|
|
def mock_generate_PR(): |
|
"""Backward compatibility function - returns warning message only""" |
|
return ( |
|
"β οΈ mock_generate_PR() is deprecated. Please use generate_github_pr() instead." |
|
) |
|
|