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
| import requests import os import random import argparse from datetime import datetime import time
LOCAL_API_URL = "http://localhost:1188/translate" API_TOKEN = "your_access_token" current_path = os.path.dirname(os.path.realpath(__file__)) SERVERS_FILE = "https.txt" USE_LOCAL_ONLY = False MAX_RETRIES = 3 RETRY_DELAY = 5 TIMEOUT = 30
def load_servers(): with open(SERVERS_FILE, 'r') as f: return [line.strip() for line in f if line.strip().startswith('https://')]
def save_servers(servers): with open(SERVERS_FILE, 'w') as f: for server in servers: f.write(f"{server}\n")
def translate_text(text, source_lang, target_lang="ZH"): servers = [LOCAL_API_URL] if LOCAL_API_URL.startswith('https://') else [] if not USE_LOCAL_ONLY: servers += load_servers() random.shuffle(servers)
headers = { "Content-Type": "application/json", "Authorization": f"Bearer {API_TOKEN}" } data = { "text": text, "source_lang": source_lang, "target_lang": target_lang } for retry in range(MAX_RETRIES): for server in servers: try: response = requests.post(f"{server}/translate", json=data, headers=headers, timeout=TIMEOUT) response.raise_for_status() result = response.json() if "data" in result and result["data"]: print(f"Successfully translated using {server}") return result, server else: print(f"Server {server} returned empty result. Trying next server.") except requests.exceptions.RequestException as e: print(f"Failed to connect to {server}: {e}") if servers: print(f"All servers failed. Retrying in {RETRY_DELAY} seconds... (Attempt {retry + 1}/{MAX_RETRIES})") time.sleep(RETRY_DELAY) else: print("No servers available. Aborting.") break print("Max retries reached or no servers available. Translation failed.") return None, None
def remove_unavailable_server(unavailable_server): servers = load_servers() if unavailable_server in servers: servers.remove(unavailable_server) save_servers(servers) print(f"Removed unavailable server {unavailable_server} from {SERVERS_FILE}")
def save_translation(original, translated, server, output_file): timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") with open(output_file, "a", encoding="utf-8") as f: f.write(f"<p>{original}</p>\n") f.write(f"<p>{translated}</p>\n")
def split_into_paragraphs(text, max_length=1000): paragraphs = [] current_paragraph = "" for line in text.split('\n'): if len(current_paragraph) + len(line) > max_length and current_paragraph: paragraphs.append(current_paragraph.strip()) current_paragraph = "" current_paragraph += line + '\n' if current_paragraph: paragraphs.append(current_paragraph.strip()) return paragraphs
def main(source_lang, input_file, output_file): print(f"Translating from {source_lang} to Chinese.") print(f"Input file: {input_file}") print(f"Output file: {output_file}") print(f"Using local service only: {'Yes' if USE_LOCAL_ONLY else 'No'}") print()
with open(input_file, 'r', encoding='utf-8') as f: content = f.read()
paragraphs = split_into_paragraphs(content) total_paragraphs = len(paragraphs)
for i, paragraph in enumerate(paragraphs, 1): print(f"Translating paragraph {i}/{total_paragraphs}") result, server = translate_text(paragraph, source_lang) if result and "data" in result: translated = result["data"] save_translation(paragraph, translated, server, output_file) print(f"Paragraph {i} translated successfully using {server}") else: print(f"Translation failed for paragraph {i}. Skipping...")
print("Translation completed. Results saved to", output_file)
if __name__ == "__main__": parser = argparse.ArgumentParser(description="Book Translator") parser.add_argument("--lang", default="AUTO", help="Source language (default: AUTO)") parser.add_argument("--i", default="txt/book.txt", help="Input file path") parser.add_argument("--o", default="txt/translated_book.txt", help="Output file path") args = parser.parse_args() main(args.lang, args.i, args.o)
|