Open jackllll1312 opened 5 months ago
import os from pptx import Presentation from pptx.util import Pt from openai import AzureOpenAI from tqdm import tqdm
class PPTTranslator: def init(self): self.client = AzureOpenAI( api_key="77a0ca4ac2524d5f97a937bf4b69e305", api_version="2023-12-01-preview", azure_endpoint="https://azure-openai-ais-poc.openai.azure.com/" ) self.system_message = {"role": "system", "content": "你擅長將英文翻譯成繁體中文"} self.reset_conversation()
def reset_conversation(self):
self.conversation = [self.system_message]
def translate_text(self, original_text):
# Similar implementation as before
pass
def apply_formatting(self, shape, translated_text):
# Similar implementation as before
pass
def calculate_total_tokens(self):
# Similar implementation as before
pass
def translate_ppt(self, ppt_filename, start_slide=0, save_interval=3):
ppt_translator = PPTTranslator()
presentation = Presentation(ppt_filename)
default_user_message = "幫我把以下內容全部翻成「繁體中文」"
print(f"\nDefault User Message: {default_user_message}")
ppt_translator.reset_conversation()
ppt_translator.conversation.append({"role": "user", "content": default_user_message})
slide_counter = 0
for slide_number, slide in tqdm(enumerate(presentation.slides), desc="Translation Progress", total=len(presentation.slides)):
if slide_number < start_slide:
continue
for shape_number, shape in enumerate(slide.shapes):
if shape.has_text_frame:
original_text = shape.text
if not original_text.strip():
continue
translated_text = ppt_translator.translate_text(original_text)
ppt_translator.apply_formatting(shape, translated_text)
slide_counter += 1
if slide_counter % save_interval == 0:
ppt_translator.reset_conversation()
ppt_translator.conversation.append({"role": "user", "content": default_user_message})
print(f"\nReset conversation after translating {save_interval} slides.")
print(f"Total Token Usage (reset after every {save_interval} slides): {ppt_translator.calculate_total_tokens()}")
ppt_translator.reset_conversation() # Reset the conversation to clear token count
# Save the final translated PPT after the loop completes
translated_filename = ppt_filename.replace('.', '_translated中文_final.')
translated_file_path = os.path.join(os.getcwd(), translated_filename)
presentation.save(translated_file_path)
print(f"\nFinal translation completed: {ppt_filename} -> {translated_filename}")
if name == 'main': ppt_filename = "Application Landscape Rationalisation in Post-Merger Integration Projects.pptx" start_slide = 0 save_interval = 3 PPTTranslator().translate_ppt(ppt_filename, start_slide, save_interval)
import os from pptx import Presentation from pptx.util import Pty from openai import AzureOpenAI from tqdm import tqdm
class PPTTranslator: def init(self): self.client = AzureOpenAI( api_key=("77a0ca4ac2524d5f97a937bf4b69e305"), api_version="2023-12-01-preview", azure_endpoint=("https://azure-openai-ais-poc.openai.azure.com/") ) self.conversation = [] self.system_message = {"role": "system", "content": "你擅長將英文翻譯成繁體中文"} self.reset_conversation()
def translate_ppt(ppt_filename, start_slide=0, save_interval=3): ppt_translator = PPTTranslator()
if name == 'main': ppt_filename = "Application Landscape Rationalisation in Post-Merger Integration Projects.pptx" start_slide = 0 # Change the start_slide value as needed save_interval = 3 # Number of slides to translate before saving translate_ppt(ppt_filename, start_slide, save_interval)