vilarin commited on
Commit
5161bf5
·
verified ·
1 Parent(s): 874d68c

Update app/webui/process.py

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