File size: 11,530 Bytes
f06b197
 
 
7f36089
f06b197
 
 
b27a850
7f36089
f06b197
 
 
 
 
 
b27a850
 
 
7f36089
b27a850
 
 
 
 
 
 
 
 
f06b197
 
 
 
 
 
 
b27a850
f06b197
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b27a850
f06b197
b27a850
f06b197
7f36089
f06b197
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7f36089
f06b197
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b27a850
f06b197
 
 
 
 
 
 
 
b27a850
f06b197
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c2b521a
f06b197
 
 
 
 
 
c2b521a
 
 
 
f06b197
c2b521a
f06b197
c2b521a
 
 
 
 
f06b197
 
c2b521a
 
f06b197
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c2b521a
f06b197
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7f36089
c2b521a
f06b197
 
 
 
 
b27a850
 
 
 
 
 
 
 
 
f06b197
b27a850
 
 
 
 
 
f06b197
 
 
 
 
 
 
 
 
 
b27a850
 
 
 
 
f06b197
b27a850
 
f06b197
 
b27a850
7f36089
f06b197
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
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
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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
import os
import time
import torch
import gradio as gr
from huggingface_hub import hf_hub_download
import threading
import queue
import multiprocessing

# First check if GPU is available for maximum speed
has_gpu = torch.cuda.is_available()
gpu_name = torch.cuda.get_device_name(0) if has_gpu else "No GPU"
print(f"GPU available: {has_gpu} - {gpu_name}")

# Download model files
def get_model_path(repo_id, filename):
    print(f"Obtaining {filename}...")
    return hf_hub_download(repo_id=repo_id, filename=filename)

base_model_path = get_model_path(
    "johnpaulbin/articulate-11-expspanish-base-merged-Q8_0-GGUF", 
    "articulate-11-expspanish-base-merged-q8_0.gguf"
)
adapter_path = get_model_path(
    "johnpaulbin/articulate-V1-Q8_0-GGUF", 
    "articulate-V1-q8_0.gguf"
)

# Set up optimized environment variables for llama-cpp-python
os.environ["LLAMA_CUBLAS"] = "1" if has_gpu else "0"
os.environ["LLAMA_CLBLAST"] = "0"  # Disable OpenCL
# For CPU: Use AVX2/AVX512/AVX-VNNI instruction sets if available
os.environ["LLAMA_AVX"] = "1" 
os.environ["LLAMA_AVX2"] = "1"
os.environ["LLAMA_F16"] = "1"  # Use FP16 where available

# Determine the most optimized backend
if has_gpu:
    try:
        from llama_cpp_python.llama_cpp.llama import Llama as GPULlama
        LlamaClass = GPULlama
        print("Using GPU-accelerated llama-cpp-python")
        n_gpu_layers = -1  # Use all layers on GPU
    except ImportError:
        from llama_cpp import Llama
        LlamaClass = Llama
        print("Using standard llama-cpp-python with GPU acceleration")
        n_gpu_layers = -1  # Use all layers on GPU
else:
    from llama_cpp import Llama
    LlamaClass = Llama
    print("Using CPU-only llama-cpp-python")
    n_gpu_layers = 0

# Cache for translations
translation_cache = {}
MAX_CACHE_SIZE = 1000

# Pre-compute common translations
COMMON_PHRASES = {
    "English to Spanish": [
        "Hello", "Thank you", "Good morning", "How are you?", "What's your name?",
        "I don't understand", "Please", "Sorry", "Yes", "No", "Where is"
    ],
    "Spanish to English": [
        "Hola", "Gracias", "Buenos dรญas", "ยฟCรณmo estรกs?", "ยฟCรณmo te llamas?",
        "No entiendo", "Por favor", "Lo siento", "Sรญ", "No", "Dรณnde estรก"
    ],
    "English to Korean": [
        "Hello", "Thank you", "Good morning", "How are you?", "What's your name?",
        "I don't understand", "Please", "Sorry", "Yes", "No", "Where is"
    ],
    "Korean to English": [
        "์•ˆ๋…•ํ•˜์„ธ์š”", "๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค", "์ข‹์€ ์•„์นจ์ž…๋‹ˆ๋‹ค", "์–ด๋–ป๊ฒŒ ์ง€๋‚ด์„ธ์š”?", "์ด๋ฆ„์ด ๋ญ์˜ˆ์š”?",
        "์ดํ•ด๊ฐ€ ์•ˆ ๋ผ์š”", "์ œ๋ฐœ", "์ฃ„์†กํ•ฉ๋‹ˆ๋‹ค", "๋„ค", "์•„๋‹ˆ์š”", "์–ด๋””์— ์žˆ์–ด์š”"
    ]
}

# Background worker for model loading and inference
class ModelWorker:
    def __init__(self):
        self.model = None
        self.request_queue = queue.Queue()
        self.response_queue = queue.Queue()
        self.worker_thread = threading.Thread(target=self._worker_loop, daemon=True)
        self.worker_thread.start()
    
    def _worker_loop(self):
        # Initialize model in the worker thread
        print("Initializing model in background thread...")
        
        # CPU optimization settings
        cpu_count = multiprocessing.cpu_count()
        optimal_threads = max(4, cpu_count - 2)  # Leave two cores free
        
        # Initialize with the most optimized settings
        start_time = time.time()
        self.model = LlamaClass(
            model_path=base_model_path,
            lora_path=adapter_path,
            n_ctx=512,               # Larger context for longer translations
            n_threads=optimal_threads, # Optimized thread count
            n_batch=1024,              # Large batch for parallel processing
            use_mmap=True,             # Efficient memory mapping
            n_gpu_layers=n_gpu_layers, # GPU acceleration if available
            seed=42,                   # Consistent results
            verbose=False,             # Reduce overhead
            main_gpu=0,                # Primary GPU
            tensor_split=None,         # Auto-distribute across GPUs if multiple
            rope_freq_base=10000,      # Optimized attention parameters
            rope_freq_scale=1.0,
        )
        print(f"Model loaded in {time.time() - start_time:.2f} seconds")
        
        # Pre-warm the model with common phrases
        self._prewarm_model()
        
        # Process requests
        while True:
            try:
                request = self.request_queue.get()
                if request is None:  # Shutdown signal
                    break
                    
                direction, text, callback_id = request
                result = self._process_translation(direction, text)
                self.response_queue.put((callback_id, result))
            except Exception as e:
                print(f"Error in worker thread: {e}")
                self.response_queue.put((callback_id, f"Error: {str(e)}"))
    
    def _prewarm_model(self):
        """Pre-compute common translations to warm up the model"""
        print("Pre-warming model with common phrases...")
        start = time.time()
        for direction, phrases in COMMON_PHRASES.items():
            for phrase in phrases[:3]:  # Just do a few to warm up
                self._process_translation(direction, phrase)
        print(f"Model pre-warming completed in {time.time() - start:.2f} seconds")
    
    def _process_translation(self, direction, text):
        # Skip empty inputs
        if not text or not text.strip():
            return ""
            
        # Check cache first for faster response
        cache_key = f"{direction}:{text}"
        if cache_key in translation_cache:
            return translation_cache[cache_key]
        
        # Start timing for performance tracking
        start_time = time.time()
        
        # Map language directions
        lang_map = {
            "English to Spanish": ("ENGLISH", "SPANISH"),
            "Spanish to English": ("SPANISH", "ENGLISH"),
            "Korean to English": ("KOREAN", "ENGLISH"),
            "English to Korean": ("ENGLISH", "KOREAN")
        }
        
        if direction not in lang_map:
            return "Invalid direction"
        
        source_lang, target_lang = lang_map[direction]
        
        # Efficient prompt format
        prompt = f"[{source_lang}]{text.strip()}[{target_lang}]"
        
        # Estimate appropriate token length based on input
        input_tokens = len(text.split())
        max_tokens = min(200, max(50, int(input_tokens * 1.5)))
        
        # Generate translation with optimized settings
        response = self.model.create_completion(
            prompt,
            max_tokens=max_tokens,
            temperature=0.0,      # Deterministic for faster inference
            top_k=1,              # Only consider most likely token
            top_p=1.0,            # No sampling
            repeat_penalty=1.0,   # No repeat penalty
            stream=False          # Get complete response at once
        )
        
        translation = response['choices'][0]['text'].strip()
        
        # Cache result
        if len(translation_cache) >= MAX_CACHE_SIZE:
            # Remove oldest entry (first key)
            translation_cache.pop(next(iter(translation_cache)))
        translation_cache[cache_key] = translation
        
        # Log performance
        inference_time = time.time() - start_time
        tokens_per_second = (input_tokens + len(translation.split())) / inference_time
        print(f"Translation: {inference_time:.3f}s ({tokens_per_second:.1f} tokens/sec)")
        
        return translation
    
    def request_translation(self, direction, text, callback_id):
        """Queue a translation request"""
        self.request_queue.put((direction, text, callback_id))

# Create worker instance
worker = ModelWorker()

# Counter for request IDs
next_request_id = 0

# Gradio interface functions
def translate(direction, text, progress=gr.Progress()):
    """Queue translation request and wait for result"""
    global next_request_id
    
    # Check cache first for immediate response
    cache_key = f"{direction}:{text}"
    if cache_key in translation_cache:
        return translation_cache[cache_key]
    
    # If input is very short, check if we have a similar cached phrase
    if len(text) < 20:
        for cached_key in translation_cache:
            cached_dir, cached_text = cached_key.split(":", 1)
            if cached_dir == direction and cached_text.lower().startswith(text.lower()):
                return translation_cache[cached_key]
    
    # Generate unique request ID
    request_id = next_request_id
    next_request_id += 1
    
    # Queue the request
    worker.request_translation(direction, text, request_id)
    
    # Wait for the response (with progress feedback)
    progress(0, desc="Translating...")
    max_wait = 30  # Maximum wait time in seconds
    start_time = time.time()
    
    while time.time() - start_time < max_wait:
        progress((time.time() - start_time) / max_wait)
        
        # Check for our response
        try:
            while not worker.response_queue.empty():
                resp_id, result = worker.response_queue.get_nowait()
                if resp_id == request_id:
                    progress(1.0)
                    return result
        except queue.Empty:
            pass
        
        # Small sleep to prevent CPU hogging
        time.sleep(0.05)
    
    progress(1.0)
    return "Translation timed out. Please try again."

# Create Gradio interface
with gr.Blocks(title="Ultra-Fast Translation App") as iface:
    gr.Markdown(f"""
    ## Ultra-Fast Translation App
    Running on: {'GPU: ' + gpu_name if has_gpu else 'CPU only'}
    """)
    
    with gr.Row():
        direction = gr.Dropdown(
            choices=["English to Spanish", "Spanish to English", "Korean to English", "English to Korean"],
            label="Translation Direction",
            value="English to Spanish"
        )
    
    with gr.Row():
        input_text = gr.Textbox(lines=5, label="Input Text", placeholder="Enter text to translate...")
        output_text = gr.Textbox(lines=5, label="Translation")
    
    # Add translate button
    translate_btn = gr.Button("Translate")
    translate_btn.click(fn=translate, inputs=[direction, input_text], outputs=output_text)
    
    # Optimization options
    with gr.Accordion("Advanced Options", open=False):
        gr.Markdown("""
        ### Performance Tips
        - Short sentences translate faster than long paragraphs
        - Common phrases may be cached for instant results
        - First translation might be slower as the model warms up
        """)
    
    # Add examples with preloaded common phrases
    gr.Examples(
        examples=[
            ["English to Spanish", "Hello, how are you today?"],
            ["Spanish to English", "Hola, ยฟcรณmo estรกs hoy?"],
            ["English to Korean", "The weather is nice today."],
            ["Korean to English", "์•ˆ๋…•ํ•˜์„ธ์š”, ๋งŒ๋‚˜์„œ ๋ฐ˜๊ฐ‘์Šต๋‹ˆ๋‹ค."]
        ],
        inputs=[direction, input_text],
        fn=translate,
        outputs=output_text
    )

# Launch with optimized settings
iface.launch(
    debug=False, 
    show_error=True,
    share=False,       # Don't share publicly by default
    quiet=True,        # Reduce console output
    server_name="0.0.0.0", 
    server_port=7860
)