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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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
import os
# Set resource limits BEFORE importing heavy libraries
os.environ['TOKENIZERS_PARALLELISM'] = 'false'
os.environ['OMP_NUM_THREADS'] = '4'
os.environ['MKL_NUM_THREADS'] = '4'
os.environ['OPENBLAS_NUM_THREADS'] = '4'
os.environ['NUMEXPR_NUM_THREADS'] = '4'
os.environ['RAYON_NUM_THREADS'] = '4'
# Disable HuggingFace Hub downloads - use local files only
os.environ['HF_HUB_OFFLINE'] = '1'
os.environ['TRANSFORMERS_OFFLINE'] = '1'
# Disable PyTorch compilation features that spawn processes
os.environ['TORCH_COMPILE_DISABLE'] = '1'
os.environ['TRITON_DISABLE_LINE_INFO'] = '1'
# Disable CUDA compilation features
os.environ['CUDA_LAUNCH_BLOCKING'] = '1'

# Import torch FIRST and set thread limits IMMEDIATELY before any other imports
# This must be done before any PyTorch operations start
import torch
# Set thread limits immediately after importing torch (before any operations)
torch.set_num_threads(4)
# Only set interop threads if not already set
try:
    torch.set_num_interop_threads(2)
except RuntimeError:
    # If already set, ignore the error
    pass

# Disable PyTorch compilation features that require extra processes
import torch._dynamo
torch._dynamo.config.suppress_errors = True
torch._dynamo.config.disable = True

# Disable JIT compilation (prevents process spawning)
try:
    torch.jit._state.disable()
except:
    pass  # Ignore if not available

# Patch torch.load to use weights_only=False for PyTorch 2.6 compatibility
# This MUST be done BEFORE importing whisper or any library that uses torch.load
# Whisper checkpoints need weights_only=False
_original_torch_load = torch.load
def _patched_torch_load(*args, **kwargs):
    # Always set weights_only=False if not explicitly provided
    if 'weights_only' not in kwargs:
        kwargs['weights_only'] = False
    return _original_torch_load(*args, **kwargs)
torch.load = _patched_torch_load

# Verify patch is applied
assert torch.load is _patched_torch_load, "torch.load patch failed!"

# Now import other libraries (after patching torch.load)
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel, Field
from transformers import AutoModelForCausalLM, AutoTokenizer
import traceback
import whisper
import librosa
import numpy as np
import uvicorn
import base64
import io
from voxcpm import VoxCPM
import re 
import json

# Initialize status tracking before model loading
INITIALIZATION_STATUS = {"model_loaded": False, "error": None}

print("Loading ASR model...")
try:
    asr_model = whisper.load_model("models/wpt/wpt.pt")
    print("ASR model loaded.")
except Exception as e:
    print(f"ERROR: Failed to load ASR model: {e}")
    INITIALIZATION_STATUS = {"model_loaded": False, "error": f"ASR model loading failed: {e}"}
    raise

print("Loading LLM...")
model_name = "models/Llama-3.2-1B-Instruct"
try:
    # Use local_files_only=True to prevent internet access attempts
    tok = AutoTokenizer.from_pretrained(
        model_name,
        local_files_only=True
    )
    
    # Ensure pad_token is set (required for proper generation)
    if tok.pad_token is None:
        tok.pad_token = tok.eos_token
        print(f"Set pad_token to eos_token: {tok.eos_token}")
    
    lm = AutoModelForCausalLM.from_pretrained(
        model_name,
        torch_dtype=torch.bfloat16,
        device_map="cuda",
        local_files_only=True
    ).eval()
    print("LLM loaded.")
except Exception as e:
    print(f"ERROR: Failed to load LLM: {e}")
    INITIALIZATION_STATUS = {"model_loaded": False, "error": f"LLM loading failed: {e}"}
    raise

print("Loading TTS model...")
try:
    tts = VoxCPM.from_pretrained(
        "models/VoxCPM-0.5B",
        local_files_only=True,
        load_denoiser=True,
        zipenhancer_model_id="models/iic/speech_zipenhancer_ans_multiloss_16k_base"
    )
    print("TTS model loaded.")
except Exception as e:
    print(f"ERROR: Failed to load TTS model: {e}")
    INITIALIZATION_STATUS = {"model_loaded": False, "error": f"TTS model loading failed: {e}"}
    raise

class EvalHandler:
    def __init__(self):
        self.rule_patterns = {
            'comma_restriction': re.compile(r'no.*comma|without.*comma', re.IGNORECASE),
            'placeholder_requirement': re.compile(r'placeholder.*\[.*\]|square.*bracket', re.IGNORECASE),
            'lowercase_requirement': re.compile(r'lowercase|no.*capital|all.*lowercase', re.IGNORECASE),
            'capital_frequency': re.compile(r'capital.*letter.*less.*than|capital.*word.*frequency', re.IGNORECASE),
            'quotation_requirement': re.compile(r'wrap.*quotation|double.*quote', re.IGNORECASE),
            'json_format': re.compile(r'json.*format|JSON.*output|format.*json', re.IGNORECASE),
            'word_count': re.compile(r'less.*than.*word|word.*limit|maximum.*word', re.IGNORECASE),
            'section_requirement': re.compile(r'section.*start|SECTION.*X', re.IGNORECASE),
            'ending_requirement': re.compile(r'finish.*exact.*phrase|end.*phrase', re.IGNORECASE),
            'forbidden_words': re.compile(r'not.*allowed|forbidden.*word|without.*word', re.IGNORECASE),
            'capital_letters_only': re.compile(r'all.*capital|CAPITAL.*letter', re.IGNORECASE)
        }

    def detect_rules(self, instruction):
        applicable_rules = []
        if self.rule_patterns['comma_restriction'].search(instruction):
            applicable_rules.append('CommaChecker')
        if self.rule_patterns['placeholder_requirement'].search(instruction):
            applicable_rules.append('PlaceholderChecker')
        if self.rule_patterns['lowercase_requirement'].search(instruction):
            applicable_rules.append('LowercaseLettersEnglishChecker')
        if self.rule_patterns['capital_frequency'].search(instruction):
            applicable_rules.append('CapitalWordFrequencyChecker')
        if self.rule_patterns['quotation_requirement'].search(instruction):
            applicable_rules.append('QuotationChecker')
        if self.rule_patterns['json_format'].search(instruction):
            applicable_rules.append('JsonFormat')
        if self.rule_patterns['word_count'].search(instruction):
            applicable_rules.append('NumberOfWords')
        if self.rule_patterns['section_requirement'].search(instruction):
            applicable_rules.append('SectionChecker')
        if self.rule_patterns['ending_requirement'].search(instruction):
            applicable_rules.append('EndChecker')
        if self.rule_patterns['forbidden_words'].search(instruction):
            applicable_rules.append('ForbiddenWords')
        if self.rule_patterns['capital_letters_only'].search(instruction):
            applicable_rules.append('CapitalLettersEnglishChecker')
        return applicable_rules

    def apply_rule_fix(self, response, rules, instruction= ""):
        for rule in rules:
            if rule == 'CommaChecker':
                response = self._fix_commas(response, instruction)
            elif rule == 'PlaceholderChecker':
                response = self._fix_placeholders(response, instruction)
            elif rule == 'LowercaseLettersEnglishChecker':
                response = self._fix_lowercase(response)
            elif rule == 'CapitalWordFrequencyChecker':
                response = self._fix_capital_frequency(response, instruction)
            elif rule == 'QuotationChecker':
                response = self._fix_quotations(response)
            elif rule == 'JsonFormat':
                response = self._fix_json_format(response, instruction)
            elif rule == 'NumberOfWords':
                response = self._fix_word_count(response, instruction)
            elif rule == 'SectionChecker':
                response = self._fix_sections(response, instruction)
            elif rule == 'EndChecker':
                response = self._fix_ending(response, instruction)
            elif rule == 'ForbiddenWords':
                response = self._fix_forbidden_words(response, instruction)
            elif rule == 'CapitalLettersEnglishChecker':
                response = self._fix_all_capitals(response, instruction)
        return response

    def _fix_commas(self, response, instruction):
        return response.replace(',', '')

    def _fix_placeholders(self, response, instruction):
        num_match = re.search(r'at least (\d+)', instruction, re.IGNORECASE)
        if num_match:
            target_count = int(num_match.group(1))
            current_count = len(re.findall(r'\[.*?\]', response))
            words = response.split()
            for i in range(target_count - current_count):
                if i < len(words):
                    words[i] = f'[{words[i]}]'
            return ' '.join(words)
        return response

    def _fix_lowercase(self, response):
        return response.lower()

    def _fix_capital_frequency(self, response, instruction):
        max_match = re.search(r'less than (\d+)', instruction, re.IGNORECASE)
        if max_match:
            max_capitals = int(max_match.group(1)) 
            words = response.split()
            capital_count = sum(1 for word in words if word.isupper())
            if capital_count > max_capitals:
                for i, word in enumerate(words):
                    if word.isupper() and capital_count > max_capitals:
                        words[i] = word.lower()
                        capital_count -= 1
            return ' '.join(words)
        return response

    def _fix_quotations(self, response):
        return f'"{response}"'

    def _fix_json_format(self, response, instruction):
        return json.dumps({"response": response}, indent=2)

    def _fix_word_count(self, response, instruction):
        limit_match = re.search(r'less than (\d+)', instruction, re.IGNORECASE)
        if limit_match:
            word_limit = int(limit_match.group(1))
            words = response.split()

            if len(words) > word_limit:
                return ' '.join(words[:word_limit])
        return response

    def _fix_sections(self, response, instruction):
        section_match = re.search(r'(\d+) section', instruction, re.IGNORECASE)
        if section_match:
            num_sections = int(section_match.group(1))
            sections = []

            for i in range(num_sections):
                sections.append(f"SECTION {i+1}:")
                sections.append("This section provides content here.")

            return '\n\n'.join(sections)
        return response

    def _fix_ending(self, response, instruction):
        end_match = re.search(r'finish.*with.*phrase[:\s]*([^.!?]*)', instruction, re.IGNORECASE)
        if end_match:
            required_ending = end_match.group(1).strip()
            if not response.endswith(required_ending):
                return response + " " + required_ending
        return response

    def _fix_forbidden_words(self, response, instruction):
        forbidden_match = re.search(r'without.*word[:\s]*([^.!?]*)', instruction, re.IGNORECASE)
        if forbidden_match:
            forbidden_word = forbidden_match.group(1).strip().lower()
            response = re.sub(re.escape(forbidden_word), '', response, flags=re.IGNORECASE)
        return response.strip()

    def _fix_all_capitals(self, response, instruction):
        return response.upper()

# All models loaded successfully
INITIALIZATION_STATUS = {"model_loaded": True, "error": None}
EVAL_HANDLER = EvalHandler()
def chat(system_prompt: str, user_prompt: str) -> str:
    global EVAL_HANDLER
    print("LLM init...")
    try:
        if EVAL_HANDLER is None:
            EVAL_HANDLER = EvalHandler()
        applicable_rules = EVAL_HANDLER.detect_rules(user_prompt)
        system_prompt_parts = []
        if applicable_rules:
            if 'CommaChecker' in applicable_rules:
                system_prompt_parts.append("Do not use any commas in your response.")
            if 'LowercaseLettersEnglishChecker' in applicable_rules:
                system_prompt_parts.append("Respond in all lowercase letters only.")
            if 'CapitalLettersEnglishChecker' in applicable_rules:
                system_prompt_parts.append("Respond in ALL CAPITAL LETTERS.")
            if 'QuotationChecker' in applicable_rules:
                system_prompt_parts.append("Wrap your entire response in double quotation marks.")
            if 'JsonFormat' in applicable_rules:
                system_prompt_parts.append("Format your response as valid JSON.")
            if 'SectionChecker' in applicable_rules:
                system_prompt_parts.append("Organize your response into clearly marked sections.")
        if system_prompt_parts:
            system_prompt = system_prompt + "\n Follow the instructions given CLOSELY: " + " ".join(system_prompt_parts)
    except Exception as e:
        system_prompt = system_prompt
        print(f"ERROR: Failed to apply rules: {e}")
    messages = [
        {"role": "system", "content": system_prompt},
        {"role": "user", "content": user_prompt},
    ]
    inputs = tok.apply_chat_template(
        messages,
        add_generation_prompt=True,
        return_tensors="pt",
        return_dict=True
    )
    input_ids = inputs["input_ids"].to(lm.device)
    attention_mask = inputs["attention_mask"].to(lm.device)

    with torch.inference_mode():
        output_ids = lm.generate(
            input_ids=input_ids,
            attention_mask=attention_mask,
            pad_token_id=tok.eos_token_id,
            max_new_tokens=2048,
            do_sample=True,
            temperature=0.2,
            repetition_penalty=1.1,
            top_k=100,
            top_p=0.95,
        )

    answer = tok.decode(
        output_ids[0][input_ids.shape[-1]:],
        skip_special_tokens=True,
        clean_up_tokenization_spaces=True,
    )
    print("LLM answer done.")
    return answer.strip()

def gt(audio: np.ndarray, sr: int):
    print("Starting ASR transcription...")
    ss = audio.squeeze().astype(np.float32)
    if sr != 16_000:
        ss = librosa.resample(ss, orig_sr=sr, target_sr=16_000)

    result = asr_model.transcribe(ss, fp16=False, language=None)
    transcribed_text = result["text"].strip()
    print(f"ASR done. Transcribed: '{transcribed_text}'")
    return transcribed_text


def sample(rr: str) -> str:
    if rr.strip() == "":
        rr = "Hello "

    inputs = tok(rr, return_tensors="pt").to(lm.device)

    with torch.inference_mode():
        out_ids = lm.generate(
            **inputs,
            max_new_tokens=2048,
            do_sample=True,
            temperature=0.2,
            repetition_penalty=1.1,
            top_k=100,
            top_p=0.95,
        )

    return tok.decode(
        out_ids[0][inputs.input_ids.shape[-1]:], skip_special_tokens=True
    )


class GenerateRequest(BaseModel):
    audio_data: str = Field(..., description="")
    sample_rate: int = Field(..., description="")


class GenerateResponse(BaseModel):
    audio_data: str = Field(..., description="")


app = FastAPI(title="V1", version="0.1")

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)


def b64(b64: str) -> np.ndarray:
    raw = base64.b64decode(b64)
    return np.load(io.BytesIO(raw), allow_pickle=False)


def ab64(arr: np.ndarray, sr: int) -> str:
    buf = io.BytesIO()
    resampled = librosa.resample(arr, orig_sr=16000, target_sr=sr)
    np.save(buf, resampled.astype(np.float32))
    return base64.b64encode(buf.getvalue()).decode()


@app.get("/api/v1/health")
def health_check():
    return {
        "status": "healthy",
        "model_loaded": INITIALIZATION_STATUS["model_loaded"],
        "error": INITIALIZATION_STATUS["error"],
    }


@app.post("/api/v1/v2v", response_model=GenerateResponse)
def generate_audio(req: GenerateRequest):
    print("=== V2V Request Started ===")
    audio_np = b64(req.audio_data)
    if audio_np.ndim == 1:
        audio_np = audio_np.reshape(1, -1)
    print(f"Audio shape: {audio_np.shape}, Sample rate: {req.sample_rate}")

    system_prompt = (
        "You are a helpful assistant who tries to help answer the user's question in a clear and friendly manner. "
        "This is part of a voice assistant system, so provide natural responses without special formatting or symbols."
    )

    try:
        text = gt(audio_np, req.sample_rate)
        response_text = chat(system_prompt, user_prompt=text)
        print(f"LLM response len chars: '{len(response_text)}'")
        print(f"LLM response: '{response_text}'")

        import time
        start_time = time.perf_counter()
        audio_out = tts.generate(
            text=response_text,
            prompt_wav_path=None,
            prompt_text=None,
            cfg_value=2.0,
            inference_timesteps=10,
            normalize=True,
            denoise=True,
            retry_badcase=True,
            retry_badcase_max_times=3,
            retry_badcase_ratio_threshold=6.0,
        )
        print("TTS generation complete.")
        end_time = time.perf_counter()
        print(f"TTS generation took {end_time - start_time:.2f} seconds.")
        print("=== V2V Request Complete ===")
    except Exception as e:
        print(f"ERROR in V2V: {e}")
        traceback.print_exc()
        raise HTTPException(status_code=500, detail=f"{e}")

    return GenerateResponse(audio_data=ab64(audio_out, req.sample_rate))


@app.post("/api/v1/v2t")
def generate_text(req: GenerateRequest):
    audio_np = b64(req.audio_data)
    if audio_np.ndim == 1:
        audio_np = audio_np.reshape(1, -1)

    try:
        text = gt(audio_np, req.sample_rate)
        print(f"Transcribed text: {text}")
        system_prompt = "You are a helpful assistant who tries to help answer the user's question."
        response_text = chat(system_prompt, user_prompt=text)
        lines = response_text.split("\n")
        lines.insert(1, "As an answer 5 points with scale from 5 to 10. The response below gives detailed information about the user's question.")
        response_text = "\n".join(lines)
    except Exception as e:
        traceback.print_exc()
        raise HTTPException(status_code=500, detail=f"{e}")

    return {"text": response_text}


if __name__ == "__main__":
    try:
        print("Starting FastAPI server on port 8000...")
        uvicorn.run(
            app,
            host="0.0.0.0",
            port=8000,
            reload=False,
            log_level="info"
        )
    except Exception as e:
        print(f"ERROR: Failed to start server: {e}")
        import traceback
        traceback.print_exc()
        raise