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
 
 
 
 
 
 
 
 
 
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
# import streamlit as st
# from transformers import AutoProcessor, Wav2Vec2ForCTC
# import torch
# import librosa
# import os
# from pydub import AudioSegment
# from moviepy.editor import VideoFileClip
# import google.generativeai as genai
# from google import genai
# from google.genai import types

# # ----------- Configuration -----------
# model_id = "facebook/mms-1b-l1107"
# lang_code = "urd-script_arabic"
# api_key = "AIzaSyBEWWn32PxVEaUsoe67GJOEpF4FQT87Kxo"  # ⚠️ Replace with st.secrets for production

# # ----------- Load Processor and Model -----------
# @st.cache_resource
# def load_model_and_processor():
#     processor = AutoProcessor.from_pretrained(model_id, target_lang=lang_code)
#     model = Wav2Vec2ForCTC.from_pretrained(
#         model_id,
#         target_lang=lang_code,
#         ignore_mismatched_sizes=True
#     )
#     model.load_adapter(lang_code)
#     return processor, model

# processor, model = load_model_and_processor()

# # ----------- Audio Conversion -----------
# def get_wav_from_input(file_path, output_path="converted.wav"):
#     ext = os.path.splitext(file_path)[-1].lower()
#     if ext in [".mp4", ".mkv", ".avi", ".mov"]:
#         video = VideoFileClip(file_path)
#         video.audio.write_audiofile(output_path, fps=16000)
#     elif ext in [".mp3", ".aac", ".flac", ".ogg", ".m4a"]:
#         audio = AudioSegment.from_file(file_path)
#         audio = audio.set_frame_rate(16000).set_channels(1)
#         audio.export(output_path, format="wav")
#     elif ext == ".wav":
#         audio = AudioSegment.from_wav(file_path)
#         audio.export(output_path, format="wav")
#     else:
#         raise ValueError("Unsupported file format.")
#     return output_path

# # ----------- Transcription -----------
# def transcribe(file_path):
#     wav_path = get_wav_from_input(file_path)
#     audio, sr = librosa.load(wav_path, sr=16000)
#     inputs = processor(audio, sampling_rate=sr, return_tensors="pt", padding=True)
#     with torch.no_grad():
#         logits = model(**inputs).logits
#     pred_ids = torch.argmax(logits, dim=-1)
#     return processor.batch_decode(pred_ids)[0]

# # ----------- Gemini Analysis -----------
# def analyze_transcript(transcript):
#     client = genai.Client(api_key=st.secrets["GEMINI_API_KEY"])

#     system_instr = """
# You are a speech analyst. The following transcription is in Urdu and contains no punctuation β€” your first task is to correct the transcript by segmenting it into grammatically correct sentences.

# Then:
# 1. Translate the corrected Urdu transcript into English.
# 2. Determine whether the transcript involves a single speaker or multiple speakers.
# 3. If multiple speakers are detected, perform diarization by segmenting the transcript with clear speaker labels.

# ⚠️ Format the segmented transcript *exactly* like this:

# **Segmented Transcript**

# **Urdu:**
# Person 01:
# [Urdu line here]

# Person 02:
# [Urdu line here]

# ...

# **English:**
# Person 01:
# [English line here]

# Person 02:
# [English line here]

# ...

# After that, provide your analysis in the following format:

# **Speaker-wise Analysis**
# [One or two sentences per speaker about tone, emotion, behavior]

# **Sentiment and Communication Style**
# [Concise overall tone: e.g., friendly, formal, tense, etc.]

# **Summary of Discussion**
# [A 2–3 line summary of what the speakers talked about, in English]
# """

#     response = client.models.generate_content(
#         model="gemini-2.5-flash",
#         contents=[transcript],
#         config=types.GenerateContentConfig(
#             system_instruction=system_instr,
#             temperature=0.0
#         )
#     )
#     return response.text
    
# # def analyze_transcript(transcript: str):
# #     client = genai.Client(api_key=st.secrets["GEMINI_API_KEY"])

# #      system_instr = """
# #         You are a speech analyst. The following transcription is in Urdu and contains no punctuation β€” your first task is to correct the transcript by segmenting it into grammatically correct sentences.
        
# #         Then:
# #         1. Translate the corrected Urdu transcript into English.
# #         2. Determine whether the transcript involves a single speaker or multiple speakers.
# #         3. If multiple speakers are detected, perform diarization by segmenting the transcript with clear speaker labels.
        
# #         ⚠️ Format the segmented transcript *exactly* like this:
        
# #         **Segmented Transcript**
        
# #         **Urdu:**
# #         Person 01:
# #         [Urdu line here]
        
# #         Person 02:
# #         [Urdu line here]
        
# #         ...
        
# #         **English:**
# #         Person 01:
# #         [English line here]
        
# #         Person 02:
# #         [English line here]
        
# #         ...
        
# #         After that, provide your analysis in the following format:
        
# #         **Speaker-wise Analysis**
# #         [One or two sentences per speaker about tone, emotion, behavior]
        
# #         **Sentiment and Communication Style**
# #         [Concise overall tone: e.g., friendly, formal, tense, etc.]
        
# #         **Summary of Discussion**
# #         [A 2–3 line summary of what the speakers talked about, in English]
# #         """
# #     resp = client.models.generate_content(
# #         model="gemini-2.5-flash",
# #         contents=[transcript],
# #         config=types.GenerateContentConfig(
# #             system_instruction=system_instr,
# #             temperature=0.0
# #         ),
# #     )
# #     return resp.text

# # ----------- Format Display Helper -----------
# def format_transcript_block(text: str) -> str:
#     lines = text.split("Person ")
#     formatted = ""
#     for line in lines:
#         line = line.strip()
#         if not line:
#             continue
#         if line.startswith("01:") or line.startswith("02:"):
#             formatted += f"\n**Person {line[:2]}**:\n{line[3:].strip()}\n\n"
#         else:
#             formatted += f"{line.strip()}\n\n"
#     return formatted

# # ----------- Streamlit UI -----------
# # Styled Header
# st.markdown("""
#     <div style="text-align: left; padding-bottom: 1rem;">
#         <h1 style='color:#1f77b4; font-size: 2.5em; font-weight: 800; margin-bottom: 0.2em;'>
#             πŸŽ™οΈ Urdu Audio & Video Speech Analyzer
#         </h1>
#         <p style='color: #CCCCCC; font-size: 1.05em; margin-top: 0;'>
#             Upload Urdu audio or video to get structured transcription, speaker diarization, and smart AI analysis.
#         </p>
#     </div>
# """, unsafe_allow_html=True)

# # File Upload
# st.markdown("### πŸ“‚ Upload an audio or video file")
# with st.container():
#     uploaded_file = st.file_uploader(
#         label="",
#         type=["mp3", "mp4", "wav", "mkv", "aac", "ogg", "m4a", "flac"],
#         label_visibility="collapsed"
#     )

# if uploaded_file is not None:
#     with st.spinner("⏳ Transcribing..."):
#         file_name = uploaded_file.name
#         temp_path = f"temp_input{os.path.splitext(file_name)[-1]}"
#         with open(temp_path, "wb") as f:
#             f.write(uploaded_file.read())
#         transcript = transcribe(temp_path)

#     st.markdown("### πŸ“ Raw Urdu Transcription")
#     st.text(transcript)

#     with st.spinner("πŸ” Analyzing with Gemini..."):
#         report = analyze_transcript(transcript)

#     # Extract Segmented Urdu and English
#     segmented_urdu = ""
#     segmented_english = ""
#     analysis_only = ""

#     if "Urdu:" in report and "English:" in report:
#         urdu_start = report.find("Urdu:")
#         english_start = report.find("English:")
#         segmented_urdu = report[urdu_start + len("Urdu:"):english_start].strip()

#         english_section = report[english_start + len("English:"):].strip()
#         if "**Speaker-wise Analysis**" in english_section:
#             parts = english_section.split("**Speaker-wise Analysis**")
#             segmented_english = parts[0].strip()
#             analysis_only = "**Speaker-wise Analysis**" + parts[1].strip()
#         else:
#             segmented_english = english_section.strip()
#             analysis_only = "⚠️ Could not extract structured analysis."

#     # Show Segmented Transcript
#     if segmented_urdu and segmented_english:
#         st.markdown("### πŸ—£οΈ Segmented Transcript")
#         col1, col2 = st.columns(2)

#         with col1:
#             st.markdown("####  Urdu")
#             st.markdown(format_transcript_block(segmented_urdu))

#         with col2:
#             st.markdown("####  English")
#             st.markdown(format_transcript_block(segmented_english))

#     # Show Gemini Analysis Only (No transcript repeat)
#     if analysis_only:
#         st.markdown("### 🧠 Gemini Analysis Summary")
#         st.markdown(analysis_only)


import io, os, numpy as np, streamlit as st, librosa, torch, soundfile as sf
from transformers import AutoProcessor, Wav2Vec2ForCTC
from pydub import AudioSegment
from moviepy.editor import VideoFileClip
from google import genai
from google.genai import types

# βœ… programmatic Start/Stop mic (no WebRTC)
from streamlit_mic_recorder import mic_recorder

# ---------------- Config ----------------
st.set_page_config(page_title="Urdu Speech Analyzer", page_icon="πŸŽ™οΈ", layout="wide")
PAGE_TITLE = "πŸŽ™οΈ Urdu Audio & Video Speech Analyzer"
model_id = "facebook/mms-1b-l1107"
lang_code = "urd-script_arabic"
api_key = "AIzaSyBEWWn32PxVEaUsoe67GJOEpF4FQT87Kxo"  # hard-coded as requested

# ---------------- Model ----------------
@st.cache_resource
def load_model_and_processor():
    processor = AutoProcessor.from_pretrained(model_id, target_lang=lang_code)
    model = Wav2Vec2ForCTC.from_pretrained(
        model_id, target_lang=lang_code, ignore_mismatched_sizes=True
    )
    model.load_adapter(lang_code)
    return processor, model

processor, model = load_model_and_processor()

# ---------------- Helpers ----------------
def get_wav_from_input(file_path, output_path="converted.wav"):
    ext = os.path.splitext(file_path)[-1].lower()
    if ext in [".mp4", ".mkv", ".avi", ".mov"]:
        video = VideoFileClip(file_path)
        video.audio.write_audiofile(output_path, fps=16000)
    elif ext in [".mp3", ".aac", ".flac", ".ogg", ".m4a"]:
        audio = AudioSegment.from_file(file_path)
        audio = audio.set_frame_rate(16000).set_channels(1)
        audio.export(output_path, format="wav")
    elif ext == ".wav":
        audio = AudioSegment.from_wav(file_path)
        audio = audio.set_frame_rate(16000).set_channels(1)
        audio.export(output_path, format="wav")
    else:
        raise ValueError("Unsupported file format.")
    return output_path

def save_wav_resampled(audio_f32: np.ndarray, sr_in: int, path: str):
    if sr_in != 16000:
        audio_f32 = librosa.resample(audio_f32, orig_sr=sr_in, target_sr=16000)
    audio_f32 = librosa.util.normalize(audio_f32)
    sf.write(path, audio_f32.astype(np.float32), 16000)

def transcribe(wav_path) -> str:
    audio, sr = librosa.load(wav_path, sr=16000, mono=True)
    inputs = processor(audio, sampling_rate=sr, return_tensors="pt", padding=True)
    with torch.no_grad():
        logits = model(**inputs).logits
    pred_ids = torch.argmax(logits, dim=-1)
    return processor.batch_decode(pred_ids)[0]

def analyze_transcript(transcript: str) -> str:
    client = genai.Client(api_key=api_key)
    system_instr = """

You are a speech analyst. The following transcription is in Urdu and contains no punctuation β€” your first task is to correct the transcript by segmenting it into grammatically correct sentences.



Then:

1. Translate the corrected Urdu transcript into English.

2. Determine whether the transcript involves a single speaker or multiple speakers.

3. If multiple speakers are detected, perform diarization by segmenting the transcript with clear speaker labels.



⚠️ Format the segmented transcript *exactly* like this:



**Segmented Transcript**



**Urdu:**

Person 01:

[Urdu line here]



Person 02:

[Urdu line here]



...



**English:**

Person 01:

[English line here]



Person 02:

[English line here]



...



After that, provide your analysis in the following format:



**Speaker-wise Analysis**

[One or two sentences per speaker about tone, emotion, behavior]



**Sentiment and Communication Style**

[Concise overall tone: e.g., friendly, formal, tense, etc.]



**Summary of Discussion**

[A 2–3 line summary of what the speakers talked about, in English]

"""
    resp = client.models.generate_content(
        model="gemini-2.5-flash",
        contents=[transcript],
        config=types.GenerateContentConfig(system_instruction=system_instr, temperature=0.0)
    )
    return resp.text

def format_transcript_block(text: str) -> str:
    lines = text.split("Person ")
    out = ""
    for line in lines:
        line = line.strip()
        if not line:
            continue
        if line.startswith("01:") or line.startswith("02:"):
            out += f"\n**Person {line[:2]}**:\n{line[3:].strip()}\n\n"
        else:
            out += f"{line}\n\n"
    return out

# ---------------- Header ----------------
st.markdown(f"""

    <div style="text-align: left; padding-bottom: 1rem;">

        <h1 style='color:#1f77b4; font-size: 2.5em; font-weight: 800; margin-bottom: 0.2em;'>

            {PAGE_TITLE}

        </h1>

        <p style='color: #7c8a98; font-size: 1.05em; margin-top: 0;'>

            Record or upload Urdu speech for structured transcription, diarization, and smart AI analysis.

        </p>

    </div>

""", unsafe_allow_html=True)

# ================= Mic: true Start/Stop + narrow Analyze =================
st.markdown("### 🎀 Live recording")

# The component renders **Start** and **Stop** buttons and keeps recording until you press Stop.
rec = mic_recorder(
    start_prompt="▢️ Start",
    stop_prompt="⏹️ Stop",
    just_once=False,       # allow multiple recordings in a session
    key="recorder",
    format="wav"           # returns WAV bytes
)

# `rec` returns after Stop. Different versions return bytes or a dict β€” handle both.
audio_bytes, sr_in = None, 44100
if rec is not None:
    if isinstance(rec, dict) and "bytes" in rec:
        audio_bytes = rec["bytes"]
        sr_in = int(rec.get("sample_rate", 44100))
    elif isinstance(rec, (bytes, bytearray)):
        audio_bytes = rec
        sr_in = 44100  # component default
    else:
        # fallback: try to extract .get("audio") etc if lib changes
        audio_bytes = rec.get("audio") if isinstance(rec, dict) else None

if audio_bytes:
    st.success("Audio captured.")
    # Convert to mono float32
    data, sr_read = sf.read(io.BytesIO(audio_bytes), dtype="float32", always_2d=False)
    if data.ndim > 1:
        data = data.mean(axis=1)
    if sr_read:  # prefer the rate embedded in the WAV
        sr_in = sr_read

    # Save as 16 kHz mono for the model
    tmp_wav = "mic_recording.wav"
    save_wav_resampled(data, sr_in, tmp_wav)

    # Minimal playback (no waveform)
    st.audio(audio_bytes, format="audio/wav")
    st.caption(f"Duration: {data.size / sr_in:.2f} s")

    # Slim Analyze button (not full width)
    if st.button("πŸ” Analyze", type="primary"):
            with st.spinner("⏳ Transcribing & analyzing..."):
                transcript = transcribe(tmp_wav)     # raw not displayed
                report = analyze_transcript(transcript)

            segmented_urdu = segmented_english = analysis_only = ""
            if "Urdu:" in report and "English:" in report:
                u0 = report.find("Urdu:")
                e0 = report.find("English:")
                segmented_urdu = report[u0 + len("Urdu:"):e0].strip()
                english_section = report[e0 + len("English:"):].strip()
                if "**Speaker-wise Analysis**" in english_section:
                    parts = english_section.split("**Speaker-wise Analysis**")
                    segmented_english = parts[0].strip()
                    analysis_only = "**Speaker-wise Analysis**" + parts[1].strip()
                else:
                    segmented_english = english_section.strip()
                    analysis_only = "⚠️ Could not extract structured analysis."

            if segmented_urdu or segmented_english:
                st.markdown("### πŸ—£οΈ Segmented Transcript")
                c1, c2 = st.columns(2)
                with c1:
                    st.markdown("#### Urdu")
                    st.markdown(format_transcript_block(segmented_urdu) if segmented_urdu else "_(none)_")
                with c2:
                    st.markdown("#### English")
                    st.markdown(format_transcript_block(segmented_english) if segmented_english else "_(none)_")
            if analysis_only:
                st.markdown("### 🧠 Gemini Analysis Summary")
                st.markdown(analysis_only)

st.markdown("---")

# ================= Upload (unchanged) =================
st.markdown("### πŸ“‚ Or upload an audio/video file")
uploaded_file = st.file_uploader(
    label="",
    type=["mp3", "mp4", "wav", "mkv", "aac", "ogg", "m4a", "flac"],
    label_visibility="collapsed"
)
if uploaded_file is not None:
    with st.spinner("⏳ Transcribing..."):
        file_name = uploaded_file.name
        temp_path = f"temp_input{os.path.splitext(file_name)[-1]}"
        with open(temp_path, "wb") as f:
            f.write(uploaded_file.read())
        wav_path = get_wav_from_input(temp_path)
        transcript = transcribe(wav_path)

    with st.spinner("πŸ” Analyzing with Gemini..."):
        report = analyze_transcript(transcript)

    segmented_urdu = segmented_english = analysis_only = ""
    if "Urdu:" in report and "English:" in report:
        u0 = report.find("Urdu:")
        e0 = report.find("English:")
        segmented_urdu = report[u0 + len("Urdu:"):e0].strip()
        english_section = report[e0 + len("English:"):].strip()
        if "**Speaker-wise Analysis**" in english_section:
            parts = english_section.split("**Speaker-wise Analysis**")
            segmented_english = parts[0].strip()
            analysis_only = "**Speaker-wise Analysis**" + parts[1].strip()
        else:
            segmented_english = english_section.strip()
            analysis_only = "⚠️ Could not extract structured analysis."

    if segmented_urdu or segmented_english:
        st.markdown("### πŸ—£οΈ Segmented Transcript")
        c1, c2 = st.columns(2)
        with c1:
            st.markdown("#### Urdu")
            st.markdown(format_transcript_block(segmented_urdu) if segmented_urdu else "_(none)_")
        with c2:
            st.markdown("#### English")
            st.markdown(format_transcript_block(segmented_english) if segmented_english else "_(none)_")
    if analysis_only:
        st.markdown("### 🧠 Gemini Analysis Summary")
        st.markdown(analysis_only)