Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,7 @@ 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 = {
|
|
@@ -91,7 +92,7 @@ class TranslatorApp:
|
|
| 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-
|
| 95 |
messages=[
|
| 96 |
{"role": "system", "content": f"Translate from {source_lang} to {target_lang}:"},
|
| 97 |
{"role": "user", "content": text},
|
|
@@ -124,9 +125,9 @@ class TranslatorApp:
|
|
| 124 |
|
| 125 |
model = {
|
| 126 |
"DeepSeek": "deepseek-chat",
|
| 127 |
-
"ChatGPT": "gpt-
|
| 128 |
"Mistral": "mistral-tiny"
|
| 129 |
-
}.get(api_provider, "gpt-
|
| 130 |
|
| 131 |
response = client.chat.completions.create(
|
| 132 |
model=model,
|
|
@@ -143,12 +144,14 @@ class TranslatorApp:
|
|
| 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")
|
|
@@ -161,6 +164,10 @@ class TranslatorApp:
|
|
| 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)
|
|
@@ -179,32 +186,46 @@ class TranslatorApp:
|
|
| 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
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
gr.
|
| 201 |
-
gr.Dropdown(label="
|
| 202 |
-
gr.
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
return interface
|
| 209 |
|
| 210 |
if __name__ == "__main__":
|
|
|
|
| 5 |
from openai import OpenAI
|
| 6 |
import queue
|
| 7 |
from datetime import datetime
|
| 8 |
+
import time
|
| 9 |
|
| 10 |
# Configuration for different API providers
|
| 11 |
API_PROVIDERS = {
|
|
|
|
| 92 |
def translate_with_chatgpt(self, text, source_lang, target_lang, api_key):
|
| 93 |
client = OpenAI(api_key=api_key)
|
| 94 |
response = client.chat.completions.create(
|
| 95 |
+
model="gpt-3.5-turbo",
|
| 96 |
messages=[
|
| 97 |
{"role": "system", "content": f"Translate from {source_lang} to {target_lang}:"},
|
| 98 |
{"role": "user", "content": text},
|
|
|
|
| 125 |
|
| 126 |
model = {
|
| 127 |
"DeepSeek": "deepseek-chat",
|
| 128 |
+
"ChatGPT": "gpt-3.5-turbo",
|
| 129 |
"Mistral": "mistral-tiny"
|
| 130 |
+
}.get(api_provider, "gpt-3.5-turbo")
|
| 131 |
|
| 132 |
response = client.chat.completions.create(
|
| 133 |
model=model,
|
|
|
|
| 144 |
def log(self, message):
|
| 145 |
self.log_queue.put(message)
|
| 146 |
|
| 147 |
+
def process_pptx(self, file_path, source_lang, target_lang, api_provider, api_key, progress=gr.Progress()):
|
| 148 |
try:
|
| 149 |
prs = Presentation(file_path)
|
| 150 |
total_slides = len(prs.slides)
|
| 151 |
all_translated_text = ""
|
| 152 |
|
| 153 |
+
start_time = time.time()
|
| 154 |
+
|
| 155 |
for i, slide in enumerate(prs.slides):
|
| 156 |
if not self.processing:
|
| 157 |
self.log("Process interrupted by user")
|
|
|
|
| 164 |
shape.text = translated_text
|
| 165 |
all_translated_text += translated_text + "\n"
|
| 166 |
|
| 167 |
+
# Update progress
|
| 168 |
+
elapsed_time = time.time() - start_time
|
| 169 |
+
progress((i + 1) / total_slides, desc=f"Processing slide {i + 1}/{total_slides}")
|
| 170 |
+
|
| 171 |
if self.processing:
|
| 172 |
output_path = os.path.join(os.path.dirname(file_path), f"translated_{os.path.basename(file_path)}")
|
| 173 |
prs.save(output_path)
|
|
|
|
| 186 |
self.log(f"Error during processing: {str(e)}")
|
| 187 |
raise e
|
| 188 |
|
| 189 |
+
def run(self, file, api_provider, api_key, source_lang, target_lang, progress=gr.Progress()):
|
| 190 |
self.processing = True
|
| 191 |
self.save_config(api_provider, api_key, source_lang, target_lang)
|
| 192 |
try:
|
| 193 |
+
output_path, review_path = self.process_pptx(file.name, source_lang, target_lang, api_provider, api_key, progress)
|
| 194 |
+
return [
|
| 195 |
+
f"File has been translated and saved as:\n{output_path}\nReview saved as:\n{review_path}",
|
| 196 |
+
output_path,
|
| 197 |
+
review_path
|
| 198 |
+
]
|
| 199 |
except Exception as e:
|
| 200 |
+
return [f"An error occurred during processing: {str(e)}", None, None]
|
| 201 |
finally:
|
| 202 |
self.processing = False
|
| 203 |
|
| 204 |
def create_interface():
|
| 205 |
app = TranslatorApp()
|
| 206 |
+
with gr.Blocks() as interface:
|
| 207 |
+
gr.Markdown("# BabelSlide by Heuristica")
|
| 208 |
+
gr.Markdown("Translate PowerPoint presentations using various AI models.")
|
| 209 |
+
|
| 210 |
+
with gr.Row():
|
| 211 |
+
file_input = gr.File(label="Upload PowerPoint File")
|
| 212 |
+
api_provider = gr.Dropdown(label="API Provider", choices=list(API_PROVIDERS.keys()))
|
| 213 |
+
api_key = gr.Textbox(label="API Key", type="password")
|
| 214 |
+
source_lang = gr.Dropdown(label="Source Language", choices=list(LANGUAGES.keys()))
|
| 215 |
+
target_lang = gr.Dropdown(label="Target Language", choices=list(LANGUAGES.keys()))
|
| 216 |
+
|
| 217 |
+
progress_bar = gr.Progress()
|
| 218 |
+
output_text = gr.Textbox(label="Output", interactive=False)
|
| 219 |
+
output_file = gr.File(label="Download Translated PowerPoint")
|
| 220 |
+
output_review = gr.File(label="Download Review")
|
| 221 |
+
|
| 222 |
+
run_button = gr.Button("Translate")
|
| 223 |
+
run_button.click(
|
| 224 |
+
fn=app.run,
|
| 225 |
+
inputs=[file_input, api_provider, api_key, source_lang, target_lang, progress_bar],
|
| 226 |
+
outputs=[output_text, output_file, output_review]
|
| 227 |
+
)
|
| 228 |
+
|
| 229 |
return interface
|
| 230 |
|
| 231 |
if __name__ == "__main__":
|