Adjoumani commited on
Commit
3fa52b5
·
verified ·
1 Parent(s): ebdb667

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -15
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
- blocks = page.get_text("blocks", flags=textflags)
122
 
123
- for block in blocks:
124
- bbox = block[:4]
125
- english_text = block[4]
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
- chunks = [english_text[i:i+500] for i in range(0, len(english_text), 500)]
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
- if chunk.strip(): # Ignorer les chunks vides
156
- try:
157
- translated_chunk = translator.translate(chunk)
158
- translated_text += translated_chunk + " "
159
- except Exception as e:
160
- st.warning(f"Erreur sur un segment : {str(e)}")
161
- translated_text += chunk + " " # Garde le texte original en cas d'échec
162
 
163
- # Appliquer la traduction au PDF
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