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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
from pptx import Presentation
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from openai import OpenAI
|
| 6 |
+
import queue
|
| 7 |
+
from datetime import datetime
|
| 8 |
+
|
| 9 |
+
# Configuration for different API providers
|
| 10 |
+
API_PROVIDERS = {
|
| 11 |
+
"DeepSeek": "https://api.deepseek.com",
|
| 12 |
+
"ChatGPT": "https://api.openai.com/v1",
|
| 13 |
+
"Mistral": "https://api.mistral.ai/v1"
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
# List of supported languages
|
| 17 |
+
LANGUAGES = {
|
| 18 |
+
"Chinese (Simplified)": "zh",
|
| 19 |
+
"Chinese (Traditional)": "zh-TW",
|
| 20 |
+
"Korean": "ko",
|
| 21 |
+
"Japanese": "ja",
|
| 22 |
+
"Arabic": "ar",
|
| 23 |
+
"English": "en",
|
| 24 |
+
"Spanish": "es",
|
| 25 |
+
"French": "fr",
|
| 26 |
+
"German": "de",
|
| 27 |
+
"Italian": "it",
|
| 28 |
+
"Portuguese": "pt",
|
| 29 |
+
"Russian": "ru",
|
| 30 |
+
"Hindi": "hi",
|
| 31 |
+
"Turkish": "tr",
|
| 32 |
+
"Dutch": "nl",
|
| 33 |
+
"Polish": "pl",
|
| 34 |
+
"Vietnamese": "vi",
|
| 35 |
+
"Thai": "th",
|
| 36 |
+
"Indonesian": "id",
|
| 37 |
+
"Swedish": "sv",
|
| 38 |
+
"Greek": "el"
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
class TranslatorApp:
|
| 42 |
+
def __init__(self):
|
| 43 |
+
self.log_queue = queue.Queue()
|
| 44 |
+
self.processing = False
|
| 45 |
+
self.process_thread = None
|
| 46 |
+
|
| 47 |
+
def load_config(self):
|
| 48 |
+
try:
|
| 49 |
+
with open('config.json', 'r') as f:
|
| 50 |
+
return json.load(f)
|
| 51 |
+
except:
|
| 52 |
+
return {}
|
| 53 |
+
|
| 54 |
+
def save_config(self, api_provider, api_key, source_lang, target_lang):
|
| 55 |
+
config = {
|
| 56 |
+
'api_provider': api_provider,
|
| 57 |
+
'api_key': api_key,
|
| 58 |
+
'source_lang': source_lang,
|
| 59 |
+
'target_lang': target_lang
|
| 60 |
+
}
|
| 61 |
+
with open('config.json', 'w') as f:
|
| 62 |
+
json.dump(config, f)
|
| 63 |
+
|
| 64 |
+
def translate_text(self, text, source_lang, target_lang, api_provider, api_key):
|
| 65 |
+
if not api_key:
|
| 66 |
+
raise ValueError("API key is required")
|
| 67 |
+
|
| 68 |
+
translation_methods = {
|
| 69 |
+
"DeepSeek": self.translate_with_deepseek,
|
| 70 |
+
"ChatGPT": self.translate_with_chatgpt,
|
| 71 |
+
"Mistral": self.translate_with_mistral
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
translate_method = translation_methods.get(api_provider)
|
| 75 |
+
if not translate_method:
|
| 76 |
+
raise ValueError(f"Unsupported API provider: {api_provider}")
|
| 77 |
+
|
| 78 |
+
return translate_method(text, source_lang, target_lang, api_key)
|
| 79 |
+
|
| 80 |
+
def translate_with_deepseek(self, text, source_lang, target_lang, api_key):
|
| 81 |
+
client = OpenAI(api_key=api_key, base_url=API_PROVIDERS["DeepSeek"])
|
| 82 |
+
response = client.chat.completions.create(
|
| 83 |
+
model="deepseek-chat",
|
| 84 |
+
messages=[
|
| 85 |
+
{"role": "system", "content": f"Translate from {source_lang} to {target_lang}:"},
|
| 86 |
+
{"role": "user", "content": text},
|
| 87 |
+
]
|
| 88 |
+
)
|
| 89 |
+
return response.choices[0].message.content
|
| 90 |
+
|
| 91 |
+
def translate_with_chatgpt(self, text, source_lang, target_lang, api_key):
|
| 92 |
+
client = OpenAI(api_key=api_key)
|
| 93 |
+
response = client.chat.completions.create(
|
| 94 |
+
model="gpt-3.5-turbo",
|
| 95 |
+
messages=[
|
| 96 |
+
{"role": "system", "content": f"Translate from {source_lang} to {target_lang}:"},
|
| 97 |
+
{"role": "user", "content": text},
|
| 98 |
+
]
|
| 99 |
+
)
|
| 100 |
+
return response.choices[0].message.content
|
| 101 |
+
|
| 102 |
+
def translate_with_mistral(self, text, source_lang, target_lang, api_key):
|
| 103 |
+
client = OpenAI(api_key=api_key, base_url=API_PROVIDERS["Mistral"])
|
| 104 |
+
response = client.chat.completions.create(
|
| 105 |
+
model="mistral-tiny",
|
| 106 |
+
messages=[
|
| 107 |
+
{"role": "system", "content": f"Translate from {source_lang} to {target_lang}:"},
|
| 108 |
+
{"role": "user", "content": text},
|
| 109 |
+
]
|
| 110 |
+
)
|
| 111 |
+
return response.choices[0].message.content
|
| 112 |
+
|
| 113 |
+
def generate_review(self, translated_text, source_lang, api_provider, api_key):
|
| 114 |
+
try:
|
| 115 |
+
self.log("Generating review...")
|
| 116 |
+
client = OpenAI(api_key=api_key, base_url=API_PROVIDERS[api_provider])
|
| 117 |
+
|
| 118 |
+
system_prompt = f"""Generate a comprehensive review in {source_lang} covering:
|
| 119 |
+
1. Main themes and key points
|
| 120 |
+
2. Translation quality assessment
|
| 121 |
+
3. Coherence and flow
|
| 122 |
+
4. Technical terminology accuracy
|
| 123 |
+
5. Recommendations for improvement"""
|
| 124 |
+
|
| 125 |
+
model = {
|
| 126 |
+
"DeepSeek": "deepseek-chat",
|
| 127 |
+
"ChatGPT": "gpt-3.5-turbo",
|
| 128 |
+
"Mistral": "mistral-tiny"
|
| 129 |
+
}.get(api_provider, "gpt-3.5-turbo")
|
| 130 |
+
|
| 131 |
+
response = client.chat.completions.create(
|
| 132 |
+
model=model,
|
| 133 |
+
messages=[
|
| 134 |
+
{"role": "system", "content": system_prompt},
|
| 135 |
+
{"role": "user", "content": translated_text},
|
| 136 |
+
]
|
| 137 |
+
)
|
| 138 |
+
return response.choices[0].message.content
|
| 139 |
+
except Exception as e:
|
| 140 |
+
self.log(f"Error generating review: {str(e)}")
|
| 141 |
+
return "Failed to generate review."
|
| 142 |
+
|
| 143 |
+
def log(self, message):
|
| 144 |
+
self.log_queue.put(message)
|
| 145 |
+
|
| 146 |
+
def process_pptx(self, file_path, source_lang, target_lang, api_provider, api_key):
|
| 147 |
+
try:
|
| 148 |
+
prs = Presentation(file_path)
|
| 149 |
+
total_slides = len(prs.slides)
|
| 150 |
+
all_translated_text = ""
|
| 151 |
+
|
| 152 |
+
for i, slide in enumerate(prs.slides):
|
| 153 |
+
if not self.processing:
|
| 154 |
+
self.log("Process interrupted by user")
|
| 155 |
+
return
|
| 156 |
+
|
| 157 |
+
self.log(f"Processing slide {i+1}/{total_slides}")
|
| 158 |
+
for shape in slide.shapes:
|
| 159 |
+
if hasattr(shape, "text") and shape.text.strip():
|
| 160 |
+
translated_text = self.translate_text(shape.text, source_lang, target_lang, api_provider, api_key)
|
| 161 |
+
shape.text = translated_text
|
| 162 |
+
all_translated_text += translated_text + "\n"
|
| 163 |
+
|
| 164 |
+
if self.processing:
|
| 165 |
+
output_path = os.path.join(os.path.dirname(file_path), f"translated_{os.path.basename(file_path)}")
|
| 166 |
+
prs.save(output_path)
|
| 167 |
+
self.log(f"Saved translated file: {output_path}")
|
| 168 |
+
|
| 169 |
+
# Generate and save review
|
| 170 |
+
review = self.generate_review(all_translated_text, source_lang, api_provider, api_key)
|
| 171 |
+
review_path = output_path.replace(".pptx", "_review.txt")
|
| 172 |
+
with open(review_path, "w", encoding="utf-8") as f:
|
| 173 |
+
f.write(review)
|
| 174 |
+
self.log(f"Generated and saved review: {review_path}")
|
| 175 |
+
|
| 176 |
+
return output_path, review_path
|
| 177 |
+
|
| 178 |
+
except Exception as e:
|
| 179 |
+
self.log(f"Error during processing: {str(e)}")
|
| 180 |
+
raise e
|
| 181 |
+
|
| 182 |
+
def run(self, file, api_provider, api_key, source_lang, target_lang):
|
| 183 |
+
self.processing = True
|
| 184 |
+
self.save_config(api_provider, api_key, source_lang, target_lang)
|
| 185 |
+
try:
|
| 186 |
+
output_path, review_path = self.process_pptx(file.name, source_lang, target_lang, api_provider, api_key)
|
| 187 |
+
return f"File has been translated and saved as:\n{output_path}\nReview saved as:\n{review_path}"
|
| 188 |
+
except Exception as e:
|
| 189 |
+
return f"An error occurred during processing: {str(e)}"
|
| 190 |
+
finally:
|
| 191 |
+
self.processing = False
|
| 192 |
+
|
| 193 |
+
def create_interface():
|
| 194 |
+
app = TranslatorApp()
|
| 195 |
+
interface = gr.Interface(
|
| 196 |
+
fn=app.run,
|
| 197 |
+
inputs=[
|
| 198 |
+
gr.File(label="Upload PowerPoint File"),
|
| 199 |
+
gr.Dropdown(label="API Provider", choices=list(API_PROVIDERS.keys())),
|
| 200 |
+
gr.Textbox(label="API Key", type="password"),
|
| 201 |
+
gr.Dropdown(label="Source Language", choices=list(LANGUAGES.keys())),
|
| 202 |
+
gr.Dropdown(label="Target Language", choices=list(LANGUAGES.keys()))
|
| 203 |
+
],
|
| 204 |
+
outputs=gr.Textbox(label="Output"),
|
| 205 |
+
title="BabelSlide by Heuristica",
|
| 206 |
+
description="Translate PowerPoint presentations using various AI models."
|
| 207 |
+
)
|
| 208 |
+
return interface
|
| 209 |
+
|
| 210 |
+
if __name__ == "__main__":
|
| 211 |
+
interface = create_interface()
|
| 212 |
+
interface.launch()
|