File size: 25,413 Bytes
c3c0d65
 
 
 
 
 
 
 
 
 
 
5911a81
c3c0d65
 
 
 
 
 
 
 
 
 
 
 
 
9faaac1
c3c0d65
 
 
 
 
 
 
 
 
 
 
 
9faaac1
 
 
 
 
 
 
 
 
 
c3c0d65
 
c162be1
c3c0d65
9faaac1
 
 
 
 
 
 
 
3b4c0b9
c162be1
3b4c0b9
9faaac1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fc4a5de
9faaac1
 
 
c3c0d65
9f9a52a
 
 
fc4d065
9f9a52a
 
 
9faaac1
 
 
4b62031
 
9f9a52a
 
 
 
 
 
 
 
 
 
 
652b643
9f9a52a
 
4b62031
 
 
 
 
9f9a52a
4b62031
 
 
 
 
c3c0d65
 
 
 
 
 
 
 
 
 
79cc3f4
c3c0d65
 
79cc3f4
 
 
 
 
 
 
 
 
c3c0d65
 
 
 
 
 
 
 
 
79cc3f4
c3c0d65
 
79cc3f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c3c0d65
79cc3f4
c3c0d65
 
 
 
79cc3f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22c3401
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c3c0d65
9f9a52a
79cc3f4
 
 
 
 
 
 
 
 
 
9f9a52a
22c3401
9f9a52a
 
 
 
 
22c3401
 
 
 
 
 
 
 
9f9a52a
79cc3f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22c3401
 
79cc3f4
22c3401
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79cc3f4
22c3401
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79cc3f4
9faaac1
9f9a52a
32ad752
9f9a52a
c3c0d65
 
 
 
 
 
 
9f9a52a
c162be1
 
35e5ec8
 
 
 
9f9a52a
 
 
c162be1
 
b1df6f1
 
 
 
 
 
 
 
9f9a52a
 
 
c162be1
35e5ec8
 
 
 
 
 
 
 
 
 
 
 
9f9a52a
 
c162be1
 
9faaac1
c162be1
79cc3f4
9faaac1
c162be1
 
 
35e5ec8
9faaac1
35e5ec8
c162be1
35e5ec8
c162be1
 
35e5ec8
c162be1
 
35e5ec8
c162be1
 
 
 
 
5911a81
c3c0d65
cd0d2d4
c162be1
c3c0d65
 
 
9faaac1
 
9f9a52a
8e783bb
 
 
 
 
 
c3c0d65
 
 
 
 
 
9faaac1
c3c0d65
9faaac1
8e783bb
9faaac1
c3c0d65
 
9faaac1
 
c3c0d65
 
 
 
9faaac1
c3c0d65
 
 
9faaac1
79cc3f4
 
 
 
 
 
 
 
 
 
 
9faaac1
c3c0d65
 
9faaac1
c3c0d65
 
9faaac1
c3c0d65
 
 
9faaac1
c3c0d65
 
 
 
 
 
cd0d2d4
 
c162be1
cd0d2d4
 
 
 
 
 
 
 
 
 
 
c162be1
 
 
cd0d2d4
c3c0d65
634bf12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c3c0d65
 
 
 
9f9a52a
c3c0d65
8e783bb
 
 
 
 
 
9f9a52a
c3c0d65
 
22c3401
 
 
 
 
 
 
 
 
 
 
 
634bf12
 
22c3401
634bf12
22c3401
 
 
 
 
 
c3c0d65
 
 
 
9f9a52a
c3c0d65
634bf12
 
 
8e783bb
634bf12
8e783bb
9f9a52a
c3c0d65
 
9faaac1
9f9a52a
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
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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
import os
import io
import tempfile
import subprocess
import requests
import torch
import numpy as np
import soundfile as sf
from fastapi import FastAPI, File, UploadFile, HTTPException, Form
from fastapi.responses import FileResponse
from fastapi.middleware.cors import CORSMiddleware
from transformers import pipeline, WhisperProcessor, WhisperForConditionalGeneration, AutoTokenizer, AutoModelForCausalLM
import imageio_ffmpeg
import logging
from contextlib import asynccontextmanager
import uvicorn
import nest_asyncio

nest_asyncio.apply()

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

@asynccontextmanager
async def lifespan(app: FastAPI):
    load_models()
    yield

app = FastAPI(title="Farmlingua AI Speech Interface", version="1.0.0", lifespan=lifespan)

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


ASK_URL = "https://remostart-milestone-one-farmlingua-ai.hf.space/ask"
tts_ha, tts_en, tts_yo, tts_ig = None, None, None, None

asr_models = {
    "ha": {"repo": "NCAIR1/Hausa-ASR", "model": None, "proc": None},
    "yo": {"repo": "NCAIR1/Yoruba-ASR", "model": None, "proc": None},
    "ig": {"repo": "NCAIR1/Igbo-ASR", "model": None, "proc": None},
    "en": {"repo": "NCAIR1/NigerianAccentedEnglish", "model": None, "proc": None},
}

def load_models():
    global tts_ha, tts_en, tts_yo, tts_ig
    device = 0 if torch.cuda.is_available() else -1
    hf_token = os.getenv("HF_TOKEN")
    if hf_token:
        hf_token = hf_token.strip()
    if not hf_token:
        logger.warning("HF_TOKEN not set! This may cause authentication failures for gated repositories.")
        logger.warning("Please set HF_TOKEN environment variable to access restricted models.")
    else:
        logger.info("HF_TOKEN is set and ready for authenticated model access.")
    
    logger.info("Using lightweight keyword-based language detection (no heavy models)")
    
    logger.info("Loading TTS models...")
    try:
        tts_ha = pipeline("text-to-speech", model="facebook/mms-tts-hau", device=device)
        logger.info("Loaded TTS (Hausa)")
    except Exception as e:
        logger.exception("Failed to load TTS (Hausa)")
        tts_ha = None
    try:
        tts_en = pipeline("text-to-speech", model="facebook/mms-tts-eng", device=device)
        logger.info("Loaded TTS (English)")
    except Exception:
        logger.exception("Failed to load TTS (English)")
        tts_en = None
    try:
        tts_yo = pipeline("text-to-speech", model="facebook/mms-tts-yor", device=device)
        logger.info("Loaded TTS (Yoruba)")
    except Exception:
        logger.exception("Failed to load TTS (Yoruba)")
        tts_yo = None
   
    tts_ig = None
    logger.info("Igbo TTS model disabled - will return text responses for Igbo language")

    logger.info("Deferred ASR model loads: will lazy-load per language on first use")

def _get_asr(lang_code: str):
    entry = asr_models.get(lang_code)
    if not entry:
        return None, None
    if entry["model"] is not None and entry["proc"] is not None:
        return entry["model"], entry["proc"]
    repo_id = entry["repo"]
    hf_token = os.getenv("HF_TOKEN")
    if hf_token:
        hf_token = hf_token.strip()
    try:
        device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
        logger.info(f"Lazy-loading ASR for {lang_code} from {repo_id}...")
        proc = WhisperProcessor.from_pretrained(repo_id, token=hf_token)
        model = WhisperForConditionalGeneration.from_pretrained(repo_id, token=hf_token)
        model.to(device)
        model.eval()
        entry["model"], entry["proc"] = model, proc
        logger.info(f"Loaded ASR for {lang_code}")
        return model, proc
    except Exception:
        logger.exception(f"Failed to load ASR for {lang_code} ({repo_id})")
        entry["model"], entry["proc"] = None, None
        return None, None

def _run_whisper(model: WhisperForConditionalGeneration, proc: WhisperProcessor, audio_array: np.ndarray) -> str:
    try:
        device = next(model.parameters()).device
        inputs = proc(audio_array, sampling_rate=16000, return_tensors="pt")
        input_features = inputs.input_features.to(device)
        with torch.no_grad():
            predicted_ids = model.generate(input_features)
        text_list = proc.batch_decode(predicted_ids, skip_special_tokens=True)
        return text_list[0] if text_list else ""
    except Exception:
        logging.exception("Whisper ASR inference failed")
        return ""

def preprocess_audio_ffmpeg(audio_data: bytes, target_sr: int = 16000) -> np.ndarray:
    try:
        with tempfile.NamedTemporaryFile(suffix='.input', delete=False) as in_file:
            in_file.write(audio_data)
            in_path = in_file.name
        with tempfile.NamedTemporaryFile(suffix='.wav', delete=False) as out_file:
            out_path = out_file.name

        ffmpeg_exe = imageio_ffmpeg.get_ffmpeg_exe()
        
        subprocess.run([
            ffmpeg_exe, '-y', '-i', in_path,
            '-ac', '1',
            '-ar', str(target_sr),
            '-af', 
            'highpass=f=80,' +
            'lowpass=f=8000,' +
            'dynaudnorm=p=0.95:m=10.0,' +
            'volume=1.0,' +
            'aresample=resampler=soxr',
            out_path
        ], check=True, capture_output=True)

        with open(out_path, 'rb') as f:
            wav_data = f.read()

        os.unlink(in_path)
        os.unlink(out_path)

        audio_array, sr = sf.read(io.BytesIO(wav_data))
        
        if len(audio_array.shape) > 1:
            audio_array = np.mean(audio_array, axis=1)
        
        if sr != target_sr:
            logger.warning(f"Audio sampling rate {sr} != target {target_sr}, applying additional resampling...")
            try:
                from scipy import signal
                ratio = target_sr / sr
                audio_array = signal.resample(audio_array, int(len(audio_array) * ratio))
                logger.info(f"Successfully resampled using scipy to {target_sr}Hz")
            except ImportError:
                logger.warning("scipy not available, using numpy interpolation")
                ratio = target_sr / sr
                new_length = int(len(audio_array) * ratio)
                audio_array = np.interp(
                    np.linspace(0, len(audio_array), new_length),
                    np.arange(len(audio_array)),
                    audio_array
                )
        
        audio_array = _validate_and_normalize_audio(audio_array)
        
        logger.info(f"Audio preprocessing complete: {len(audio_array)} samples at {target_sr}Hz")
        return audio_array.astype(np.float32)
        
    except Exception as e:
        logger.error(f"FFmpeg preprocessing failed: {e}")
        raise HTTPException(status_code=400, detail="Audio preprocessing failed. Ensure ffmpeg is installed.")

def _validate_and_normalize_audio(audio_array: np.ndarray) -> np.ndarray:
    rms = np.sqrt(np.mean(audio_array**2))
    if rms < 0.001:
        logger.warning("Audio appears to be very quiet or silent")
    
    max_val = np.max(np.abs(audio_array))
    if max_val > 0.95:
        logger.warning(f"Audio may be clipped (max: {max_val:.3f})")
    
    target_rms = 0.1
    current_rms = np.sqrt(np.mean(audio_array**2))
    
    if current_rms > 0:
        normalization_factor = min(target_rms / current_rms, 2.0)
        audio_array = audio_array * normalization_factor
        logger.info(f"Normalized audio RMS from {current_rms:.4f} to {np.sqrt(np.mean(audio_array**2)):.4f}")
    
    audio_array = np.clip(audio_array, -0.99, 0.99)
    
    audio_array = audio_array - np.mean(audio_array)
    
    return audio_array

def chunk_audio(audio_array: np.ndarray, chunk_length: float = 10.0, overlap: float = 1.0, sample_rate: int = 16000) -> list:
    chunk_samples = int(chunk_length * sample_rate)
    overlap_samples = int(overlap * sample_rate)
    step_samples = chunk_samples - overlap_samples
    
    chunks = []
    start = 0
    
    while start < len(audio_array):
        end = min(start + chunk_samples, len(audio_array))
        chunk = audio_array[start:end]
        
        fade_samples = int(0.05 * sample_rate)
        if len(chunk) > 2 * fade_samples:
            chunk[:fade_samples] *= np.linspace(0, 1, fade_samples)
            chunk[-fade_samples:] *= np.linspace(1, 0, fade_samples)
        
        if len(chunk) < chunk_samples:
            chunk = np.pad(chunk, (0, chunk_samples - len(chunk)), mode='constant')
        
        chunk_rms = np.sqrt(np.mean(chunk**2))
        if chunk_rms < 0.001:
            logger.warning(f"Chunk {len(chunks)+1} appears to be very quiet (RMS: {chunk_rms:.6f})")
        
        chunks.append(chunk)
        start += step_samples
        
        if end >= len(audio_array):
            break
    
    logger.info(f"Split audio into {len(chunks)} chunks of {chunk_length}s each with quality preservation")
    return chunks

def speech_to_text_with_language(audio_data: bytes, language: str) -> str:
    audio_array = preprocess_audio_ffmpeg(audio_data)
    
    audio_duration = len(audio_array) / 16000
    logger.info(f"Audio duration: {audio_duration:.2f} seconds, processing with {language} model")
    
    model, proc = _get_asr(language)
    if model is None or proc is None:
        logger.error(f"Failed to load {language} ASR model")
        return ""
    
    if audio_duration <= 15:
        return _process_single_chunk_with_language(audio_array, model, proc, language)
    else:
        return _process_chunked_audio_with_language(audio_array, model, proc, language)

def _process_single_chunk_with_language(audio_array: np.ndarray, model, proc, language: str) -> str:
    text = _run_whisper(model, proc, audio_array)
    if text and text.strip():
        logger.info(f"Transcription ({language}): {text[:100]}...")
        return text.strip()
    return ""

def _process_chunked_audio_with_language(audio_array: np.ndarray, model, proc, language: str) -> str:
    chunks = chunk_audio(audio_array, chunk_length=10.0, overlap=1.0)
    
    chunk_texts = []
    for i, chunk in enumerate(chunks):
        try:
            text = _run_whisper(model, proc, chunk)
            if text and text.strip():
                chunk_texts.append(text.strip())
                logger.info(f"Chunk {i+1}/{len(chunks)} ({language}): {text[:50]}...")
        except Exception as e:
            logger.warning(f"Failed to process chunk {i+1} with {language}: {e}")
            continue
    
    if chunk_texts:
        combined_text = " ".join(chunk_texts)
        logger.info(f"Combined {language} result: {combined_text[:100]}...")
        return combined_text
    
    return ""

def speech_to_text(audio_data: bytes) -> str:
    audio_array = preprocess_audio_ffmpeg(audio_data)
    
    audio_duration = len(audio_array) / 16000
    logger.info(f"Audio duration: {audio_duration:.2f} seconds")
    
    if audio_duration <= 15:
        return _process_single_chunk(audio_array)
    else:
        return _process_chunked_audio(audio_array)

def _process_single_chunk(audio_array: np.ndarray) -> str:
    candidates = []
    
    for code in ["yo", "ha", "ig", "en"]:
        model, proc = _get_asr(code)
        if model is None or proc is None:
            continue
        text = _run_whisper(model, proc, audio_array)
        if text and text.strip():
            candidates.append((code, text.strip()))
    
    if not candidates:
        return ""
    
    best_transcription = _select_best_transcription(candidates)
    return best_transcription

def _process_chunked_audio(audio_array: np.ndarray) -> str:
    chunks = chunk_audio(audio_array, chunk_length=10.0, overlap=1.0)
    
    language_results = {}
    
    for code in ["yo", "ha", "ig", "en"]:
        model, proc = _get_asr(code)
        if model is None or proc is None:
            continue
            
        chunk_texts = []
        for i, chunk in enumerate(chunks):
            try:
                text = _run_whisper(model, proc, chunk)
                if text and text.strip():
                    chunk_texts.append(text.strip())
                    logger.info(f"Chunk {i+1}/{len(chunks)} ({code}): {text[:50]}...")
            except Exception as e:
                logger.warning(f"Failed to process chunk {i+1} with {code}: {e}")
                continue
        
        if chunk_texts:
            combined_text = " ".join(chunk_texts)
            language_results[code] = combined_text
            logger.info(f"Combined {code} result: {combined_text[:100]}...")
    
    if not language_results:
        return ""
    
    best_transcription = _select_best_transcription(list(language_results.items()))
    return best_transcription

def _select_best_transcription(candidates: list) -> str:
    if not candidates:
        return ""
    
    if len(candidates) == 1:
        return candidates[0][1]
    
    scored_candidates = []
    
    for lang_code, text in candidates:
        score = _score_transcription_quality(text, lang_code)
        scored_candidates.append((score, lang_code, text))
        logger.info(f"Transcription quality score for {lang_code}: {score:.2f} - '{text[:50]}...'")
    
    scored_candidates.sort(key=lambda x: x[0], reverse=True)
    best_score, best_lang, best_text = scored_candidates[0]
    
    logger.info(f"Selected best transcription: {best_lang} (score: {best_score:.2f}) - '{best_text[:100]}...'")
    return best_text

def _score_transcription_quality(text: str, lang_code: str) -> float:
    if not text or not text.strip():
        return 0.0
    
    text = text.strip()
    score = 0.0
    
    word_count = len(text.split())
    if word_count == 0:
        return 0.0
    
    score += min(word_count * 0.5, 10.0)
    
    char_count = len(text)
    score += min(char_count * 0.1, 5.0)
    
    if lang_code == "yo":
        yoruba_chars = sum(1 for c in text if c in "ẹọṣgb")
        score += yoruba_chars * 2.0
    elif lang_code == "ig":
        igbo_chars = sum(1 for c in text if c in "ụọị")
        score += igbo_chars * 2.0
    elif lang_code == "ha":
        hausa_chars = sum(1 for c in text if c in "ts")
        score += hausa_chars * 1.5
    
    punctuation_penalty = text.count('?') + text.count('!') + text.count('.')
    score -= punctuation_penalty * 0.5
    
    repeated_chars = sum(1 for i in range(len(text)-2) if text[i] == text[i+1] == text[i+2])
    score -= repeated_chars * 1.0
    
    return max(score, 0.0)


def get_ai_response(text: str) -> str:
    try:
        response = requests.post(ASK_URL, json={"query": text}, timeout=30)
        response.raise_for_status()
        result = response.json()
        return result.get("answer", "Sorry, no answer returned.")
    except Exception as e:
        logger.error(f"AI request error: {e}")
        return f"I'm sorry, I couldn't connect to the AI service. You said: '{text}'."

HAUSA_WORDS = [
    "aikin", "manoma", "gona", "amfanin", "yanayi", "tsaba", "fasaha", "bisa", "noman", "shuka",
    "daji", "rani", "damina", "amfani", "bidi'a", "noma", "bashi", "manure", "tsiro", "gishiri",
    "gonaki", "gonar", "tsirrai", "kayan", "gonar", "tsirrai", "kayan", "gonar",
    "tsirrai", "kayan", "gonar", "tsirrai", "kayan", "gonar", "tsirrai", "kayan",
    "gonar", "tsirrai", "kayan", "gonar", "tsirrai", "kayan", "gonar", "tsirrai",
    "kayan", "gonar", "tsirrai", "kayan", "gonar", "tsirrai", "kayan", "gonar"
]

YORUBA_WORDS = [
    "ilé", "ọmọ", "òun", "awọn", "agbẹ", "oko", "ọgbà", "irugbin", "àkọsílẹ", "omi", "ojo", "àgbàlá", "irọlẹ",
    "ni", "ti", "si", "fun", "lati", "ninu", "lori", "labe", "pelu", "ati", "tabi", "sugbon",
    "o", "a", "e", "won", "mi", "re", "wa", "yin",
    "kan", "kankan", "die", "pupo", "gbogbo", "kookan",
    "nibi", "nibe", "igba", "akoko", "osu", "odun", "ise", "owo",
    "láàsìbà", "dára", "jùlẹ̀", "ìwẹ̀", "ṣe", "kú", "tún", "fi", "wo",
    "ẹ", "ọ", "ṣ", "gb", "gb", "gb", "gb", "gb", "gb", "gb", "gb",
    "jẹ", "wá", "lọ", "dúró", "sọ", "gbọ", "rí", "mọ", "fẹ", "ní",
    "pẹlu", "nitori", "tori", "nitori", "tori", "nitori", "tori", "nitori", "tori",
    "ṣugbọn", "ṣugbọn", "ṣugbọn", "ṣugbọn", "ṣugbọn", "ṣugbọn", "ṣugbọn", "ṣugbọn"
]

IGBO_WORDS = [
    "ugbo", "akụkọ", "mmiri", "ala", "ọrụ", "ncheta", "ọhụrụ", "ugwu", "nri", "ahụhụ",
    "ọkụkọ", "ewu", "atụrụ", "ehi", "azụ", "osisi", "mkpụrụ", "ubi", "ọka", "ji",
    "akwụkwọ", "ofe", "azu", "anụ", "nnu", "mmanụ", "ngwọ", "ọgwụ", "ahịhịa", "osisi",
    "n'", "maka", "n'ihi", "n'ime", "n'elu", "n'okpuru", "ya", "anyị", "unu", "ha",
    "otu", "ọtụtụ", "ebe", "oge", "ụ", "ọ", "ị", "bụ", "nọ", "ga", "dị", "ka", "ma"
]

ENGLISH_WORDS = [
    "farm", "farming", "agriculture", "crop", "crops", "plant", "plants", "seed", "seeds", "soil",
    "water", "rain", "weather", "harvest", "yield", "field", "fields", "farmer", "farmers", "grow",
    "growing", "fertilizer", "pesticide", "irrigation", "livestock", "cattle", "chicken", "goat", "sheep",
    "maize", "corn", "rice", "wheat", "vegetable", "vegetables", "fruit", "fruits", "tree", "trees",
    "cultivate", "cultivation", "plow", "plowing", "sow", "sowing", "reap", "reaping", "season", "seasons"
]

def detect_language_keywords(text: str) -> str:
    text_lower = text.lower().strip()
    
    if not text_lower:
        return "en"
    
    hausa_count = sum(1 for word in HAUSA_WORDS if word in text_lower)
    yoruba_count = sum(1 for word in YORUBA_WORDS if word in text_lower)
    igbo_count = sum(1 for word in IGBO_WORDS if word in text_lower)
    english_count = sum(1 for word in ENGLISH_WORDS if word in text_lower)
    
    logger.info(f"Language detection scores - Hausa: {hausa_count}, Yoruba: {yoruba_count}, Igbo: {igbo_count}, English: {english_count}")
    
    if hausa_count > yoruba_count and hausa_count > igbo_count and hausa_count > english_count:
        logger.info("Keyword detection: Hausa")
        return "ha"
    elif yoruba_count > hausa_count and yoruba_count > igbo_count and yoruba_count > english_count:
        logger.info("Keyword detection: Yoruba")
        return "yo"
    elif igbo_count > hausa_count and igbo_count > yoruba_count and igbo_count > english_count:
        logger.info("Keyword detection: Igbo")
        return "ig"
    else:
        logger.info("Keyword detection: English (default)")
        return "en"

def detect_language(text: str) -> str:
    logger.info(f"Detecting language for text: '{text[:50]}...'")
    return detect_language_keywords(text)

def text_to_speech_file(text: str) -> str:
    lang = detect_language(text)
    print(f"Detected language: {lang}")
    
    if lang == "ig":
        logger.info("Igbo language detected - returning text response instead of audio")
        fd, path = tempfile.mkstemp(suffix=".txt")
        os.close(fd)
        with open(path, 'w', encoding='utf-8') as f:
            f.write(text)
        return path
    if lang == "ha":
        tts_model = tts_ha
    elif lang == "yo":
        tts_model = tts_yo
    else:
        tts_model = tts_en
    
    if tts_model is None:
        logger.error(f"TTS model for {lang} is not available")
        raise HTTPException(status_code=500, detail=f"TTS model for {lang} is not available")
    
    speech_output = tts_model(text)
    audio_raw = speech_output["audio"]
    sampling_rate = int(speech_output["sampling_rate"]) 

    if isinstance(audio_raw, torch.Tensor):
        audio_np = audio_raw.detach().cpu().numpy()
    else:
        audio_np = np.asarray(audio_raw)

    if audio_np.ndim > 1:
        audio_np = audio_np.reshape(-1)
    audio_np = audio_np.astype(np.float32, copy=False)

    target_sr = 16000
    if sampling_rate != target_sr:
        logger.info(f"Resampling TTS audio from {sampling_rate}Hz to {target_sr}Hz")
        ratio = target_sr / sampling_rate
        new_length = int(len(audio_np) * ratio)
        audio_np = np.interp(
            np.linspace(0, len(audio_np), new_length),
            np.arange(len(audio_np)),
            audio_np
        )
        sampling_rate = target_sr

    audio_clipped = np.clip(audio_np, -1.0, 1.0)
    audio_int16 = (audio_clipped * 32767.0).astype(np.int16)

    fd, path = tempfile.mkstemp(suffix=".wav")
    os.close(fd)

    sf.write(path, audio_int16, sampling_rate, format='WAV', subtype='PCM_16')
    return path


@app.get("/")
async def root():
    return {"status": "ok", "message": "System ready"}

@app.get("/health")
async def health():
    return {
        "message": "Farmlingua AI Speech Interface is running!",
        "language_detection": "keyword-based (lightweight)",
        "tts_models": {
            "hausa": tts_ha is not None,
            "english": tts_en is not None,
            "yoruba": tts_yo is not None,
            "igbo": False
        }
    }

@app.get("/status")
async def status():
    return {
        "language_detection": "keyword-based (lightweight)",
        "status": "ready",
        "message": "Using lightweight keyword-based language detection - no heavy models required"
    }

@app.get("/languages")
async def get_languages():
    return {
        "supported_languages": {
            "yo": {
                "name": "Yoruba",
                "code": "yo",
                "tts_available": True,
                "asr_model": "NCAIR1/Yoruba-ASR"
            },
            "ha": {
                "name": "Hausa", 
                "code": "ha",
                "tts_available": True,
                "asr_model": "NCAIR1/Hausa-ASR"
            },
            "ig": {
                "name": "Igbo",
                "code": "ig", 
                "tts_available": False,
                "asr_model": "NCAIR1/Igbo-ASR",
                "note": "Text response only - TTS not available"
            },
            "en": {
                "name": "English",
                "code": "en",
                "tts_available": True,
                "asr_model": "NCAIR1/NigerianAccentedEnglish"
            }
        },
        "usage": {
            "specify_language": "Use /speak endpoint with language parameter for best accuracy",
            "auto_detection": "Use /speak-auto endpoint when language is unknown",
            "igbo_response": "Igbo responses return text only (no audio TTS available)"
        }
    }

@app.post("/chat")
async def chat(text: str = Form(...), speak: bool = False, raw: bool = False):
    if not text.strip():
        raise HTTPException(status_code=400, detail="Text cannot be empty")
    final_text = text if raw else get_ai_response(text)
    if speak:
        output_path = text_to_speech_file(final_text)
        lang = detect_language(final_text)
        if lang == "ig":
            return FileResponse(output_path, media_type="text/plain", filename="response.txt")
        else:
            return FileResponse(output_path, media_type="audio/wav", filename="response.wav")
    return {"question": text, "answer": final_text}

@app.post("/speak")
async def speak_to_ai(audio_file: UploadFile = File(...), language: str = Form(...), speak: bool = True):
    if not audio_file.content_type.startswith('audio/'):
        raise HTTPException(status_code=400, detail="File must be an audio file")
    
    if language not in ["yo", "ha", "ig", "en"]:
        raise HTTPException(status_code=400, detail="Language must be one of: yo (Yoruba), ha (Hausa), ig (Igbo), en (English)")
    
    audio_data = await audio_file.read()
    transcription = speech_to_text_with_language(audio_data, language)
    ai_response = get_ai_response(transcription)
    
    if speak:
        if language == "ig":
            return {"transcription": transcription, "ai_response": ai_response, "language": language, "response_type": "text", "note": "Igbo TTS not available - returning text response"}
        else:
            output_path = text_to_speech_file(ai_response)
            return FileResponse(output_path, media_type="audio/wav", filename="response.wav")
    
    return {"transcription": transcription, "ai_response": ai_response, "language": language}

@app.post("/speak-auto")
async def speak_to_ai_auto(audio_file: UploadFile = File(...), speak: bool = True):
    if not audio_file.content_type.startswith('audio/'):
        raise HTTPException(status_code=400, detail="File must be an audio file")
    audio_data = await audio_file.read()
    transcription = speech_to_text(audio_data)
    ai_response = get_ai_response(transcription)
    if speak:
        detected_lang = detect_language(ai_response)
        if detected_lang == "ig":
            return {"transcription": transcription, "ai_response": ai_response, "detected_language": detected_lang, "response_type": "text", "note": "Igbo TTS not available - returning text response"}
        else:
            output_path = text_to_speech_file(ai_response)
            return FileResponse(output_path, media_type="audio/wav", filename="response.wav")
    return {"transcription": transcription, "ai_response": ai_response}

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=int(os.getenv("PORT", "7860")))