Update app/webui/process.py
Browse files- app/webui/process.py +243 -226
app/webui/process.py
CHANGED
|
@@ -1,227 +1,244 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from simplemma import simple_tokenizer
|
| 3 |
-
from difflib import Differ
|
| 4 |
-
from icecream import ic
|
| 5 |
-
from app.webui.patch import model_load,num_tokens_in_string,one_chunk_initial_translation, one_chunk_reflect_on_translation, one_chunk_improve_translation
|
| 6 |
-
from app.webui.patch import calculate_chunk_size, multichunk_initial_translation, multichunk_reflect_on_translation, multichunk_improve_translation
|
| 7 |
-
|
| 8 |
-
from llama_index.core.node_parser import SentenceSplitter
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
)
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
)
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
)
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
)
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
progress((
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
)
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
progress((
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
)
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
)
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
source_text_chunks,
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 227 |
return init_translation, reflection, final_translation
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from simplemma import simple_tokenizer
|
| 3 |
+
from difflib import Differ
|
| 4 |
+
from icecream import ic
|
| 5 |
+
from app.webui.patch import model_load,num_tokens_in_string,one_chunk_initial_translation, one_chunk_reflect_on_translation, one_chunk_improve_translation
|
| 6 |
+
from app.webui.patch import calculate_chunk_size, multichunk_initial_translation, multichunk_reflect_on_translation, multichunk_improve_translation
|
| 7 |
+
|
| 8 |
+
from llama_index.core.node_parser import SentenceSplitter
|
| 9 |
+
from translatepy.exceptions import UnknownLanguage
|
| 10 |
+
from translatepy.translators.google import GoogleTranslate
|
| 11 |
+
gtranslate = GoogleTranslate()
|
| 12 |
+
|
| 13 |
+
progress=gr.Progress()
|
| 14 |
+
|
| 15 |
+
def tokenize(text):
|
| 16 |
+
# Use nltk to tokenize the text
|
| 17 |
+
words = simple_tokenizer(text)
|
| 18 |
+
# Check if the text contains spaces
|
| 19 |
+
if ' ' in text:
|
| 20 |
+
# Create a list of words and spaces
|
| 21 |
+
tokens = []
|
| 22 |
+
for word in words:
|
| 23 |
+
tokens.append(word)
|
| 24 |
+
if not word.startswith("'") and not word.endswith("'"): # Avoid adding space after punctuation
|
| 25 |
+
tokens.append(' ') # Add space after each word
|
| 26 |
+
return tokens[:-1] # Remove the last space
|
| 27 |
+
else:
|
| 28 |
+
return words
|
| 29 |
+
|
| 30 |
+
def diff_texts(text1, text2):
|
| 31 |
+
tokens1 = tokenize(text1)
|
| 32 |
+
tokens2 = tokenize(text2)
|
| 33 |
+
|
| 34 |
+
d = Differ()
|
| 35 |
+
diff_result = list(d.compare(tokens1, tokens2))
|
| 36 |
+
|
| 37 |
+
highlighted_text = []
|
| 38 |
+
for token in diff_result:
|
| 39 |
+
word = token[2:]
|
| 40 |
+
category = None
|
| 41 |
+
if token[0] == '+':
|
| 42 |
+
category = 'added'
|
| 43 |
+
elif token[0] == '-':
|
| 44 |
+
category = 'removed'
|
| 45 |
+
elif token[0] == '?':
|
| 46 |
+
continue # Ignore the hints line
|
| 47 |
+
|
| 48 |
+
highlighted_text.append((word, category))
|
| 49 |
+
|
| 50 |
+
return highlighted_text
|
| 51 |
+
|
| 52 |
+
#modified from src.translaation-agent.utils.tranlsate
|
| 53 |
+
def translator(
|
| 54 |
+
source_lang: str,
|
| 55 |
+
target_lang: str,
|
| 56 |
+
source_text: str,
|
| 57 |
+
country: str,
|
| 58 |
+
max_tokens:int = 1000,
|
| 59 |
+
):
|
| 60 |
+
"""Translate the source_text from source_lang to target_lang."""
|
| 61 |
+
num_tokens_in_text = num_tokens_in_string(source_text)
|
| 62 |
+
|
| 63 |
+
ic(num_tokens_in_text)
|
| 64 |
+
|
| 65 |
+
if num_tokens_in_text < max_tokens:
|
| 66 |
+
ic("Translating text as single chunk")
|
| 67 |
+
|
| 68 |
+
progress((1,3), desc="First translation...")
|
| 69 |
+
init_translation = one_chunk_initial_translation(
|
| 70 |
+
source_lang, target_lang, source_text
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
progress((2,3), desc="Reflecton...")
|
| 74 |
+
reflection = one_chunk_reflect_on_translation(
|
| 75 |
+
source_lang, target_lang, source_text, init_translation, country
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
progress((3,3), desc="Second translation...")
|
| 79 |
+
final_translation = one_chunk_improve_translation(
|
| 80 |
+
source_lang, target_lang, source_text, init_translation, reflection
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
return init_translation, reflection, final_translation
|
| 84 |
+
|
| 85 |
+
else:
|
| 86 |
+
ic("Translating text as multiple chunks")
|
| 87 |
+
|
| 88 |
+
progress((1,5), desc="Calculate chunk size...")
|
| 89 |
+
token_size = calculate_chunk_size(
|
| 90 |
+
token_count=num_tokens_in_text, token_limit=max_tokens
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
ic(token_size)
|
| 94 |
+
|
| 95 |
+
#using sentence splitter
|
| 96 |
+
text_parser = SentenceSplitter(
|
| 97 |
+
chunk_size=token_size,
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
progress((2,5), desc="Spilt source text...")
|
| 101 |
+
source_text_chunks = text_parser.split_text(source_text)
|
| 102 |
+
|
| 103 |
+
progress((3,5), desc="First translation...")
|
| 104 |
+
translation_1_chunks = multichunk_initial_translation(
|
| 105 |
+
source_lang, target_lang, source_text_chunks
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
init_translation = "".join(translation_1_chunks)
|
| 109 |
+
|
| 110 |
+
progress((4,5), desc="Reflection...")
|
| 111 |
+
reflection_chunks = multichunk_reflect_on_translation(
|
| 112 |
+
source_lang,
|
| 113 |
+
target_lang,
|
| 114 |
+
source_text_chunks,
|
| 115 |
+
translation_1_chunks,
|
| 116 |
+
country,
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
reflection = "".join(reflection_chunks)
|
| 120 |
+
|
| 121 |
+
progress((5,5), desc="Second translation...")
|
| 122 |
+
translation_2_chunks = multichunk_improve_translation(
|
| 123 |
+
source_lang,
|
| 124 |
+
target_lang,
|
| 125 |
+
source_text_chunks,
|
| 126 |
+
translation_1_chunks,
|
| 127 |
+
reflection_chunks,
|
| 128 |
+
)
|
| 129 |
+
|
| 130 |
+
final_translation = "".join(translation_2_chunks)
|
| 131 |
+
|
| 132 |
+
return init_translation, reflection, final_translation
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
def translator_sec(
|
| 136 |
+
endpoint2: str,
|
| 137 |
+
model2: str,
|
| 138 |
+
api_key2: str,
|
| 139 |
+
context_window: int,
|
| 140 |
+
num_output: int,
|
| 141 |
+
source_lang: str,
|
| 142 |
+
target_lang: str,
|
| 143 |
+
source_text: str,
|
| 144 |
+
country: str,
|
| 145 |
+
max_tokens: int = 1000,
|
| 146 |
+
gt: bool = False,
|
| 147 |
+
):
|
| 148 |
+
|
| 149 |
+
"""Translate the source_text from source_lang to target_lang."""
|
| 150 |
+
num_tokens_in_text = num_tokens_in_string(source_text)
|
| 151 |
+
|
| 152 |
+
ic(num_tokens_in_text)
|
| 153 |
+
|
| 154 |
+
if num_tokens_in_text < max_tokens:
|
| 155 |
+
ic("Translating text as single chunk")
|
| 156 |
+
|
| 157 |
+
progress((1,3), desc="First translation...")
|
| 158 |
+
if gt:
|
| 159 |
+
try:
|
| 160 |
+
language = Language(target_lang)
|
| 161 |
+
except Exception as e:
|
| 162 |
+
raise gr.Error(f"An unexpected error occurred: {e}")
|
| 163 |
+
init_translation = gtranslate.translate(source_text, language)
|
| 164 |
+
else:
|
| 165 |
+
init_translation = one_chunk_initial_translation(
|
| 166 |
+
source_lang, target_lang, source_text
|
| 167 |
+
)
|
| 168 |
+
try:
|
| 169 |
+
model_load(endpoint2, model2, api_key2, context_window, num_output)
|
| 170 |
+
except Exception as e:
|
| 171 |
+
raise gr.Error(f"An unexpected error occurred: {e}")
|
| 172 |
+
|
| 173 |
+
progress((2,3), desc="Reflecton...")
|
| 174 |
+
reflection = one_chunk_reflect_on_translation(
|
| 175 |
+
source_lang, target_lang, source_text, init_translation, country
|
| 176 |
+
)
|
| 177 |
+
|
| 178 |
+
progress((3,3), desc="Second translation...")
|
| 179 |
+
final_translation = one_chunk_improve_translation(
|
| 180 |
+
source_lang, target_lang, source_text, init_translation, reflection
|
| 181 |
+
)
|
| 182 |
+
|
| 183 |
+
return init_translation, reflection, final_translation
|
| 184 |
+
|
| 185 |
+
else:
|
| 186 |
+
ic("Translating text as multiple chunks")
|
| 187 |
+
|
| 188 |
+
progress((1,5), desc="Calculate chunk size...")
|
| 189 |
+
token_size = calculate_chunk_size(
|
| 190 |
+
token_count=num_tokens_in_text, token_limit=max_tokens
|
| 191 |
+
)
|
| 192 |
+
|
| 193 |
+
ic(token_size)
|
| 194 |
+
|
| 195 |
+
#using sentence splitter
|
| 196 |
+
text_parser = SentenceSplitter(
|
| 197 |
+
chunk_size=token_size,
|
| 198 |
+
)
|
| 199 |
+
|
| 200 |
+
progress((2,5), desc="Spilt source text...")
|
| 201 |
+
source_text_chunks = text_parser.split_text(source_text)
|
| 202 |
+
|
| 203 |
+
progress((3,5), desc="First translation...")
|
| 204 |
+
if gt:
|
| 205 |
+
try:
|
| 206 |
+
language = Language(target_lang)
|
| 207 |
+
except Exception as e:
|
| 208 |
+
raise gr.Error(f"An unexpected error occurred: {e}")
|
| 209 |
+
translation_1_chunks = gtranslate.translate(source_text_chunks, language)
|
| 210 |
+
else:
|
| 211 |
+
translation_1_chunks = multichunk_initial_translation(
|
| 212 |
+
source_lang, target_lang, source_text_chunks
|
| 213 |
+
)
|
| 214 |
+
|
| 215 |
+
init_translation = "".join(translation_1_chunks)
|
| 216 |
+
|
| 217 |
+
try:
|
| 218 |
+
model_load(endpoint2, model2, api_key2, context_window, num_output)
|
| 219 |
+
except Exception as e:
|
| 220 |
+
raise gr.Error(f"An unexpected error occurred: {e}")
|
| 221 |
+
|
| 222 |
+
progress((4,5), desc="Reflection...")
|
| 223 |
+
reflection_chunks = multichunk_reflect_on_translation(
|
| 224 |
+
source_lang,
|
| 225 |
+
target_lang,
|
| 226 |
+
source_text_chunks,
|
| 227 |
+
translation_1_chunks,
|
| 228 |
+
country,
|
| 229 |
+
)
|
| 230 |
+
|
| 231 |
+
reflection = "".join(reflection_chunks)
|
| 232 |
+
|
| 233 |
+
progress((5,5), desc="Second translation...")
|
| 234 |
+
translation_2_chunks = multichunk_improve_translation(
|
| 235 |
+
source_lang,
|
| 236 |
+
target_lang,
|
| 237 |
+
source_text_chunks,
|
| 238 |
+
translation_1_chunks,
|
| 239 |
+
reflection_chunks,
|
| 240 |
+
)
|
| 241 |
+
|
| 242 |
+
final_translation = "".join(translation_2_chunks)
|
| 243 |
+
|
| 244 |
return init_translation, reflection, final_translation
|