File size: 6,790 Bytes
3bfe3dd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
"""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
# GitHub PR Agent import
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
# GitHub configuration - must be provided by user or environment variables
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.
Args:
translate_lang: Target language to translate
top_k: Number of top-first files to return for translation. (Default 1)
"""
status_report, filepath_list = report(translate_lang, top_k)
return status_report, [[file] for file in filepath_list]
def translate_docs(lang: str, file_path: str) -> tuple[str, str]:
"""Translate documentation."""
# step 1. Get content from file path
content = get_content(file_path)
to_translate = preprocess_content(content)
# step 2. Prepare prompt with docs content
if lang == "ko":
translation_lang = "Korean"
to_translate_with_prompt = get_full_prompt(translation_lang, to_translate)
# step 3. Translate with LLM
# TODO: MCP clilent λκΈΈ λΆλΆ
callback_result, translated_content = llm_translate(to_translate_with_prompt)
# step 4. Add scaffold to translation result
translated_doc = fill_scaffold(content, to_translate, translated_content)
return callback_result, translated_doc
def translate_docs_interactive(
translate_lang: str, selected_files: list[list[str]]
) -> tuple[str, 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
"""
# Extract file paths from the dataframe format
file_paths = [row[0] for row in selected_files if row and len(row) > 0]
if not file_paths:
return (
"No files selected for translation.",
gr.update(visible=False),
gr.update(visible=False),
gr.update(visible=False),
[],
0,
)
# Start with the first file
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)
status += f"π° Used token and cost: \n```\n{callback_result}\n```"
if len(file_paths) > 1:
status += f"\n### π Note: Currently, only the first file has been translated.\n> The remaining {len(file_paths) - 1} files have not been processed yet, as the system is in its beta version"
return status, translated_content
def generate_github_pr(
target_language: str,
filepath: str,
translated_content: str = None,
github_config: dict = 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
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."
# Validate required configuration
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."
# Set token in environment for the agent.
os.environ["GITHUB_TOKEN"] = github_config["token"]
try:
# Read translated content from file if not provided
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."
# Execute GitHub PR Agent
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"),
)
# Process result
if result["status"] == "success":
return f"""β
**GitHub PR Creation Successful!**
π **PR URL:** {result["pr_url"]}
πΏ **Branch:** {result["branch"]}
π **File:** {result["file_path"]}
{result["message"]}"""
elif result["status"] == "partial_success":
return f"""β οΈ **Partial Success**
πΏ **Branch:** {result["branch"]}
π **File:** {result["file_path"]}
{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
# Backward compatibility function (replaces old mock function)
def mock_generate_PR():
"""Backward compatibility function - returns warning message only"""
return (
"β οΈ mock_generate_PR() is deprecated. Please use generate_github_pr() instead."
)
|