Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,26 @@ import streamlit as st
|
|
| 2 |
import fitz # pymupdf pour la manipulation PDF
|
| 3 |
from deep_translator import MyMemoryTranslator # GoogleTranslator
|
| 4 |
import time
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# Configuration de base pour l'interface Streamlit
|
| 7 |
st.set_page_config(
|
|
@@ -117,17 +137,17 @@ if doc_file and st.button("Lancer la traduction"):
|
|
| 117 |
progress_bar = st.progress(0)
|
| 118 |
start_time = time.time()
|
| 119 |
|
| 120 |
-
for page_num, page in enumerate(doc):
|
| 121 |
-
|
| 122 |
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
|
| 127 |
# Traduction du texte
|
| 128 |
#translated_text = translator.translate(english_text)
|
| 129 |
# Découper le texte en segments de 500 caractères max
|
| 130 |
-
|
| 131 |
# Remplacement du texte
|
| 132 |
#page.draw_rect(bbox, color=None, fill=blanc, oc=ocg_xref)
|
| 133 |
|
|
@@ -149,18 +169,29 @@ if doc_file and st.button("Lancer la traduction"):
|
|
| 149 |
# translated_text,
|
| 150 |
# oc=ocg_xref,
|
| 151 |
#)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
translated_text = ""
|
| 153 |
-
|
| 154 |
for chunk in chunks:
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
|
| 163 |
-
#
|
| 164 |
page.draw_rect(bbox, color=None, fill=blanc, oc=ocg_xref)
|
| 165 |
page.insert_htmlbox(bbox, translated_text.strip(), oc=ocg_xref)
|
| 166 |
|
|
|
|
| 2 |
import fitz # pymupdf pour la manipulation PDF
|
| 3 |
from deep_translator import MyMemoryTranslator # GoogleTranslator
|
| 4 |
import time
|
| 5 |
+
import re
|
| 6 |
+
|
| 7 |
+
def clean_text(text):
|
| 8 |
+
# Remplacer les séries de points par un espace unique
|
| 9 |
+
text = re.sub(r'\.{3,}', ' ', text)
|
| 10 |
+
# Supprimer les espaces multiples
|
| 11 |
+
text = re.sub(r'\s+', ' ', text).strip()
|
| 12 |
+
return text
|
| 13 |
+
|
| 14 |
+
def split_text(text, max_length=500):
|
| 15 |
+
chunks = []
|
| 16 |
+
while len(text) > max_length:
|
| 17 |
+
# Trouver le dernier espace avant max_length
|
| 18 |
+
split_pos = text.rfind(' ', 0, max_length)
|
| 19 |
+
if split_pos == -1: # Aucun espace trouvé (mot très long)
|
| 20 |
+
split_pos = max_length
|
| 21 |
+
chunks.append(text[:split_pos])
|
| 22 |
+
text = text[split_pos:].lstrip()
|
| 23 |
+
chunks.append(text)
|
| 24 |
+
return chunks
|
| 25 |
|
| 26 |
# Configuration de base pour l'interface Streamlit
|
| 27 |
st.set_page_config(
|
|
|
|
| 137 |
progress_bar = st.progress(0)
|
| 138 |
start_time = time.time()
|
| 139 |
|
| 140 |
+
#for page_num, page in enumerate(doc):
|
| 141 |
+
# blocks = page.get_text("blocks", flags=textflags)
|
| 142 |
|
| 143 |
+
# for block in blocks:
|
| 144 |
+
# bbox = block[:4]
|
| 145 |
+
# english_text = block[4]
|
| 146 |
|
| 147 |
# Traduction du texte
|
| 148 |
#translated_text = translator.translate(english_text)
|
| 149 |
# Découper le texte en segments de 500 caractères max
|
| 150 |
+
|
| 151 |
# Remplacement du texte
|
| 152 |
#page.draw_rect(bbox, color=None, fill=blanc, oc=ocg_xref)
|
| 153 |
|
|
|
|
| 169 |
# translated_text,
|
| 170 |
# oc=ocg_xref,
|
| 171 |
#)
|
| 172 |
+
for page_num, page in enumerate(doc):
|
| 173 |
+
blocks = page.get_text("blocks", flags=textflags)
|
| 174 |
+
|
| 175 |
+
for block in blocks:
|
| 176 |
+
bbox = block[:4]
|
| 177 |
+
raw_text = block[4]
|
| 178 |
+
|
| 179 |
+
# Nettoyage + découpage
|
| 180 |
+
clean = clean_text(raw_text)
|
| 181 |
+
chunks = split_text(clean)
|
| 182 |
+
|
| 183 |
+
# Traduction cumulative
|
| 184 |
translated_text = ""
|
|
|
|
| 185 |
for chunk in chunks:
|
| 186 |
+
try:
|
| 187 |
+
translated_chunk = translator.transrate(chunk)
|
| 188 |
+
translated_text += translated_chunk + " "
|
| 189 |
+
time.sleep(0.3) # Anti-rate-limiting
|
| 190 |
+
except Exception as e:
|
| 191 |
+
st.warning(f"Segment non traduit : {str(e)}")
|
| 192 |
+
translated_text += chunk + " " # Fallback
|
| 193 |
|
| 194 |
+
# Application au PDF
|
| 195 |
page.draw_rect(bbox, color=None, fill=blanc, oc=ocg_xref)
|
| 196 |
page.insert_htmlbox(bbox, translated_text.strip(), oc=ocg_xref)
|
| 197 |
|