File size: 9,009 Bytes
5bd6e8a
4dee3b6
5bd6e8a
5267841
4dee3b6
5bd6e8a
 
 
272732e
5bd6e8a
4dee3b6
5bd6e8a
4dee3b6
5bd6e8a
 
 
 
4dee3b6
 
 
5bd6e8a
 
4dee3b6
5bd6e8a
 
 
 
4dee3b6
 
 
5bd6e8a
 
 
bffd18e
5bd6e8a
4dee3b6
5bd6e8a
 
4dee3b6
5bd6e8a
 
 
bffd18e
5bd6e8a
4dee3b6
5bd6e8a
 
4dee3b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5bd6e8a
 
 
 
 
4dee3b6
5bd6e8a
 
 
4dee3b6
 
 
 
bffd18e
5bd6e8a
 
4dee3b6
5bd6e8a
 
 
4dee3b6
5bd6e8a
 
 
 
 
 
 
4dee3b6
5bd6e8a
4dee3b6
 
5bd6e8a
 
4dee3b6
 
 
5bd6e8a
4dee3b6
5bd6e8a
4dee3b6
5bd6e8a
4dee3b6
 
5bd6e8a
4dee3b6
5bd6e8a
4dee3b6
 
 
 
 
 
 
 
 
 
 
 
 
5bd6e8a
 
 
 
 
bffd18e
 
4dee3b6
5bd6e8a
4dee3b6
 
5bd6e8a
4dee3b6
 
5bd6e8a
 
 
 
4dee3b6
5bd6e8a
 
 
 
 
 
 
 
4dee3b6
 
 
 
 
 
5bd6e8a
 
359c0b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations
import os, wave
from pathlib import Path
from datetime import datetime
from typing import List, Dict, Any, Optional

import numpy as np
from moviepy.editor import ImageClip, AudioFileClip, concatenate_videoclips
from PIL import Image, ImageDraw, ImageFont 

# offline TTS
try:
    import pyttsx3
    _HAS_PYTTSX3 = True
except Exception:
    _HAS_PYTTSX3 = False

# If you already have this helper in your file, keep using your version.
# It renders a PIL.Image for a single chat row.
# def _render_slide_image(row: Dict[str, Any]) -> PIL.Image.Image: ...

def _ensure_tts_engine() -> Optional["pyttsx3.Engine"]:
    """Initialize pyttsx3 (offline TTS). Returns engine or None on failure."""
    if not _HAS_PYTTSX3:
        return None
    try:
        eng = pyttsx3.init()
        # Optional: tweak voice/rate/volume
        # eng.setProperty("rate", 170)   # ~170 wpm
        # eng.setProperty("volume", 0.9)
        return eng
    except Exception:
        return None

def _tts_to_wav(text: str, out_wav: Path, engine: "pyttsx3.Engine") -> bool:
    """Synthesize `text` to WAV at `out_wav`. Returns True on success."""
    try:
        engine.save_to_file(text, str(out_wav))
        engine.runAndWait()  # blocks until the file is written
        return out_wav.exists() and out_wav.stat().st_size > 0
    except Exception:
        return False

def _wav_duration_seconds(path: Path) -> float:
    """Accurate duration of a WAV file in seconds."""
    with wave.open(str(path), "rb") as wf:
        frames = wf.getnframes()
        rate = wf.getframerate() or 1
        return frames / float(rate)

def _narration_for_row(row: Dict[str, Any]) -> str:
    """Compose concise narration text for a slide."""
    ts = (row.get("timestamp") or "").strip()
    user_msg = (row.get("user_message") or "").strip()
    bot_msg  = (row.get("bot_reply") or "").strip()
    intent   = (row.get("intent") or "").strip()

    # Keep it short and clear; you can tailor this
    parts = []
    if ts:
        parts.append(f"Conversation at {ts}.")
    if user_msg:
        parts.append(f"Customer said: {user_msg}")
    if bot_msg:
        parts.append(f"Assistant replied: {bot_msg}")
    if intent:
        parts.append(f"Intent: {intent}.")
    text = " ".join(parts)

    # Avoid overly long narration (pyttsx3 can be slow on huge text)
    if len(text) > 900:
        text = text[:900] + "…"
    return text or "Conversation."

def export_records_to_video(
    rows: List[Dict[str, Any]],
    out_path: Optional[str] = None,
    tts: bool = True,
    min_secs_per_slide: float = 4.0,
    fps: int = 30,
) -> str:
    """
    Create an MP4 slideshow from chat rows.
    - If `tts` is True and pyttsx3/eSpeak are available, generate per-slide narration WAVs
      and set each slide duration = max(min_secs_per_slide, narration_length).
    - If TTS unavailable or fails, falls back to silent slides with `min_secs_per_slide`.

    Returns absolute path to the generated MP4.
    """

    if not rows:
        raise ValueError("No rows provided for video export.")

    # Output path & folder
    out_path = out_path or str(Path("/mnt/data") / f"chat_export_{datetime.utcnow().strftime('%Y%m%d_%H%M%S')}.mp4")
    out_dir = Path(out_path).parent
    out_dir.mkdir(parents=True, exist_ok=True)

    # Prepare TTS (optional)
    engine = _ensure_tts_engine() if tts else None
    if tts and engine is None:
        print("[video_export] Offline TTS not available; exporting silent video.")

    # Build slide clips (+ audio if available)
    clips = []

    for idx, row in enumerate(rows, start=1):
        # 1) Render slide image
        pil_img = _render_slide_image(row)  # must return a PIL.Image
        frame = np.asarray(pil_img)

        # 2) Narration & duration
        audio_path = None
        duration = float(min_secs_per_slide)

        if engine is not None:
            narration = _narration_for_row(row)
            audio_path = out_dir / f"slide_{idx:03d}.wav"
            ok = _tts_to_wav(narration, audio_path, engine)
            if ok:
                try:
                    wav_len = _wav_duration_seconds(audio_path)
                    duration = max(min_secs_per_slide, wav_len)
                except Exception:
                    # keep default duration if length probing fails
                    pass
            else:
                audio_path = None  # fallback to silent

        # 3) Make a clip for this slide
        clip = ImageClip(frame).set_duration(duration)

        # 4) Attach audio if present
        if audio_path and audio_path.exists():
            try:
                aclip = AudioFileClip(str(audio_path))
                clip = clip.set_audio(aclip)
            except Exception:
                pass

        clips.append(clip)

    # 5) Concatenate & write MP4 with audio
    final = concatenate_videoclips(clips, method="compose")
    out_file = Path(out_path)

    # Using AAC for wide compatibility
    final.write_videofile(
        str(out_file),
        fps=fps,
        codec="libx264",
        audio_codec="aac",
        temp_audiofile=str(out_dir / "temp_mux_audio.m4a"),
        remove_temp=True,
        threads=2,
        preset="medium",
        verbose=False,
        logger=None,
    )

    # Close clips to free resources
    for c in clips:
        try:
            c.close()
        except Exception:
            pass

    return str(out_file.resolve())

def _font_try(paths, size):
    """Try multiple font paths, fallback to PIL default."""
    for p in paths:
        try:
            return ImageFont.truetype(p, size)
        except Exception:
            pass
    return ImageFont.load_default()

def _wrap(draw, text, font, max_width_px):
    """Word-wrap text to fit pixel width using PIL measurement."""
    if not text:
        return [""]
    words = text.split()
    lines, cur = [], []
    for w in words:
        test = " ".join(cur + [w])
        if draw.textlength(test, font=font) <= max_width_px:
            cur.append(w)
        else:
            if cur:
                lines.append(" ".join(cur))
            cur = [w]
    if cur:
        lines.append(" ".join(cur))
    return lines

def _draw_block(draw, x, y, text, font, fill, max_width_px, line_spacing=8):
    """Draw wrapped text block and return new y position."""
    for line in _wrap(draw, text, font, max_width_px):
        draw.text((x, y), line, font=font, fill=fill)
        y += font.size + line_spacing
    return y

def _render_slide_image(row, W=1280, H=720):
    """
    Render one slide as a PIL.Image from a chat row dict:
    expects keys like: id, timestamp, user_message, bot_reply, intent, sentiment, resolution, ftr_ms.
    """
    # Layout & theme
    margin = 60
    box_w = W - margin * 2

    bg          = (247, 249, 252)   # page background
    card        = (255, 255, 255)   # card background
    brand       = (38, 110, 241)    # brand blue
    text_primary= (23, 37, 61)      # main text
    text_muted  = (71, 85, 105)     # muted
    separator   = (229, 231, 235)   # border line

    # Fonts
    font_paths = [
        "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf",
        "/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf",
    ]
    font_title  = _font_try(font_paths, 40)
    font_label  = _font_try(font_paths, 24)
    font_body   = _font_try(font_paths, 28)
    font_footer = _font_try(font_paths, 22)

    # Canvas
    img = Image.new("RGB", (W, H), bg)
    draw = ImageDraw.Draw(img)

    # Card
    draw.rounded_rectangle([margin, margin, W - margin, H - margin],
                           radius=24, fill=card, outline=separator, width=1)

    x = margin + 40
    y = margin + 30

    # Title
    title = f"Ecomm Chat • {row.get('timestamp','')} • ID {row.get('id','')}"
    draw.text((x, y), title, font=font_title, fill=text_primary)
    y += font_title.size + 20
    draw.line([(x, y), (W - margin - 40, y)], fill=separator, width=2)
    y += 24

    # Customer message
    draw.text((x, y), "Customer", font=font_label, fill=brand)
    y += font_label.size + 8
    y = _draw_block(draw, x, y, row.get("user_message", ""), font_body, text_primary, box_w)
    y += 16

    # Assistant reply
    draw.text((x, y), "Assistant", font=font_label, fill=brand)
    y += font_label.size + 8
    y = _draw_block(draw, x, y, row.get("bot_reply", ""), font_body, text_primary, box_w)
    y += 16

    # Footer meta (optional)
    bits = []
    if row.get("intent"):     bits.append(f"Intent: {row['intent']}")
    if row.get("sentiment"):  bits.append(f"Sentiment: {row['sentiment']}")
    if row.get("resolution"): bits.append(f"Resolution: {row['resolution']}")
    if row.get("ftr_ms"):     bits.append(f"Latency: {row['ftr_ms']} ms")
    if bits:
        y2 = H - margin - 80
        draw.line([(x, y2), (W - margin - 40, y2)], fill=separator, width=1)
        draw.text((x, y2 + 20), "   •   ".join(bits), font=font_footer, fill=text_muted)

    return img