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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
def render_admin_export_tab(conn):
|
| 6 |
-
"""Admin export
|
| 7 |
-
#
|
| 8 |
ensure_video_deps()
|
| 9 |
|
| 10 |
st.subheader("📥 Export Records")
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# src/admin_export.py
|
| 2 |
+
|
| 3 |
+
import io
|
| 4 |
+
import os
|
| 5 |
+
import tempfile
|
| 6 |
+
from datetime import datetime, timedelta
|
| 7 |
+
|
| 8 |
+
import pandas as pd
|
| 9 |
import streamlit as st
|
| 10 |
+
from PIL import Image, ImageDraw, ImageFont
|
| 11 |
+
|
| 12 |
+
# --- Try to use your project video helpers when available ---
|
| 13 |
+
try:
|
| 14 |
+
from src.video_export import ensure_video_deps # just deps check
|
| 15 |
+
except Exception: # pragma: no cover
|
| 16 |
+
def ensure_video_deps():
|
| 17 |
+
# If your project has a different setup, this no-op keeps the tab working.
|
| 18 |
+
pass
|
| 19 |
+
|
| 20 |
+
# Prefer a project-level export_video if it exists; if not, we’ll define a local fallback below.
|
| 21 |
+
try:
|
| 22 |
+
from src.video_export import export_video as project_export_video
|
| 23 |
+
except Exception:
|
| 24 |
+
project_export_video = None
|
| 25 |
+
|
| 26 |
+
# =========================
|
| 27 |
+
# Data helpers (SQLite)
|
| 28 |
+
# =========================
|
| 29 |
+
def search_users(conn, prefix: str):
|
| 30 |
+
"""Search users by email prefix (case insensitive)."""
|
| 31 |
+
rows = conn.execute(
|
| 32 |
+
"""
|
| 33 |
+
SELECT id, username, email
|
| 34 |
+
FROM users
|
| 35 |
+
WHERE role='user' AND lower(email) LIKE lower(?)
|
| 36 |
+
ORDER BY email
|
| 37 |
+
LIMIT 50
|
| 38 |
+
""",
|
| 39 |
+
(prefix + "%",),
|
| 40 |
+
).fetchall()
|
| 41 |
+
return [dict(r) for r in rows]
|
| 42 |
+
|
| 43 |
+
def fetch_filtered_chats(conn, from_date, to_date, user_id: int):
|
| 44 |
+
"""Chats for ONE user in date range."""
|
| 45 |
+
rows = conn.execute(
|
| 46 |
+
"""
|
| 47 |
+
SELECT id, timestamp, user_message, bot_reply, intent, sentiment, resolution, ftr_ms
|
| 48 |
+
FROM chat_history
|
| 49 |
+
WHERE user_id = ?
|
| 50 |
+
AND date(timestamp) BETWEEN ? AND ?
|
| 51 |
+
ORDER BY timestamp DESC
|
| 52 |
+
""",
|
| 53 |
+
(user_id, str(from_date), str(to_date)),
|
| 54 |
+
).fetchall()
|
| 55 |
+
return [dict(r) for r in rows]
|
| 56 |
+
|
| 57 |
+
# =========================
|
| 58 |
+
# TXT / CSV exports
|
| 59 |
+
# =========================
|
| 60 |
+
def export_txt(conn, ids):
|
| 61 |
+
if not ids:
|
| 62 |
+
st.warning("Select at least one record."); return
|
| 63 |
+
cur = conn.execute(
|
| 64 |
+
f"""
|
| 65 |
+
SELECT timestamp, user_message, bot_reply
|
| 66 |
+
FROM chat_history
|
| 67 |
+
WHERE id IN ({','.join('?' * len(ids))})
|
| 68 |
+
ORDER BY timestamp
|
| 69 |
+
""",
|
| 70 |
+
ids,
|
| 71 |
+
)
|
| 72 |
+
lines = []
|
| 73 |
+
for ts, u, b in cur.fetchall():
|
| 74 |
+
lines.append(f"[{ts}] USER: {u}\nBOT: {b}\n")
|
| 75 |
+
st.download_button("⬇️ Download TXT", "\n".join(lines), file_name="chats.txt")
|
| 76 |
+
|
| 77 |
+
def export_csv(conn, ids):
|
| 78 |
+
if not ids:
|
| 79 |
+
st.warning("Select at least one record."); return
|
| 80 |
+
cur = conn.execute(
|
| 81 |
+
f"""
|
| 82 |
+
SELECT timestamp, user_message, bot_reply, intent, sentiment, resolution, ftr_ms
|
| 83 |
+
FROM chat_history
|
| 84 |
+
WHERE id IN ({','.join('?' * len(ids))})
|
| 85 |
+
ORDER BY timestamp
|
| 86 |
+
""",
|
| 87 |
+
ids,
|
| 88 |
+
)
|
| 89 |
+
out = io.StringIO()
|
| 90 |
+
# header
|
| 91 |
+
cols = [d[0] for d in cur.description]
|
| 92 |
+
out.write(",".join(cols) + "\n")
|
| 93 |
+
# rows
|
| 94 |
+
for row in cur.fetchall():
|
| 95 |
+
# naive CSV (since fields are typically short); adapt to csv.writer if needed
|
| 96 |
+
out.write(",".join([str(x).replace("\n", " ").replace(",", " ") for x in row]) + "\n")
|
| 97 |
+
st.download_button("⬇️ Download CSV", out.getvalue(), file_name="chats.csv")
|
| 98 |
+
|
| 99 |
+
# =========================
|
| 100 |
+
# Video export fallback (PIL + gTTS + MoviePy)
|
| 101 |
+
# =========================
|
| 102 |
+
def _fallback_export_video(conn, ids):
|
| 103 |
+
"""Local implementation if your project doesn’t expose src.video_export.export_video."""
|
| 104 |
+
if not ids:
|
| 105 |
+
st.warning("Select at least one record."); return
|
| 106 |
+
|
| 107 |
+
# Lazy import heavy deps
|
| 108 |
+
try:
|
| 109 |
+
from gtts import gTTS
|
| 110 |
+
from moviepy.editor import ImageSequenceClip, AudioFileClip
|
| 111 |
+
except Exception as e: # pragma: no cover
|
| 112 |
+
st.error(f"Video deps missing: {e}")
|
| 113 |
+
return
|
| 114 |
+
|
| 115 |
+
# Fetch chats
|
| 116 |
+
cur = conn.execute(
|
| 117 |
+
f"""
|
| 118 |
+
SELECT timestamp, user_message, bot_reply
|
| 119 |
+
FROM chat_history
|
| 120 |
+
WHERE id IN ({','.join('?' * len(ids))})
|
| 121 |
+
ORDER BY timestamp
|
| 122 |
+
""",
|
| 123 |
+
ids,
|
| 124 |
+
)
|
| 125 |
+
chats = cur.fetchall()
|
| 126 |
+
if not chats:
|
| 127 |
+
st.error("No records to export as video."); return
|
| 128 |
+
|
| 129 |
+
# Build simple slides
|
| 130 |
+
frames = []
|
| 131 |
+
# Try nicer font if available, else default
|
| 132 |
+
try:
|
| 133 |
+
font = ImageFont.truetype("DejaVuSans.ttf", 22)
|
| 134 |
+
except Exception:
|
| 135 |
+
font = ImageFont.load_default()
|
| 136 |
+
|
| 137 |
+
for ts, user_msg, bot_msg in chats:
|
| 138 |
+
img = Image.new("RGB", (1024, 576), color=(255, 255, 255))
|
| 139 |
+
d = ImageDraw.Draw(img)
|
| 140 |
+
y = 30
|
| 141 |
+
d.text((40, y), f"Time: {ts}", fill=(0, 0, 0), font=font); y += 40
|
| 142 |
+
d.text((40, y), "User:", fill=(20, 20, 20), font=font); y += 30
|
| 143 |
+
_draw_wrapped_text(d, user_msg, (70, y), font, width=900); y += 120
|
| 144 |
+
d.text((40, y), "Bot:", fill=(20, 20, 20), font=font); y += 30
|
| 145 |
+
_draw_wrapped_text(d, bot_msg, (70, y), font, width=900)
|
| 146 |
+
frames.append(img)
|
| 147 |
+
|
| 148 |
+
tmpdir = tempfile.mkdtemp()
|
| 149 |
+
frame_files = []
|
| 150 |
+
for i, frame in enumerate(frames):
|
| 151 |
+
fpath = os.path.join(tmpdir, f"frame_{i:04d}.png")
|
| 152 |
+
frame.save(fpath)
|
| 153 |
+
frame_files.append(fpath)
|
| 154 |
+
|
| 155 |
+
# TTS voiceover (one narration)
|
| 156 |
+
narration = "\n".join([f"User said: {u}. Bot replied: {b}." for _, u, b in chats])
|
| 157 |
+
audio_path = os.path.join(tmpdir, "voice.mp3")
|
| 158 |
+
try:
|
| 159 |
+
gTTS(narration).save(audio_path)
|
| 160 |
+
except Exception as e: # pragma: no cover
|
| 161 |
+
st.error(f"Failed to synthesize voice: {e}")
|
| 162 |
+
return
|
| 163 |
+
|
| 164 |
+
# Compose video
|
| 165 |
+
try:
|
| 166 |
+
clip = ImageSequenceClip(frame_files, fps=1) # 1 slide / sec
|
| 167 |
+
audio = AudioFileClip(audio_path)
|
| 168 |
+
clip = clip.set_audio(audio)
|
| 169 |
+
video_path = os.path.join(tmpdir, "chats.mp4")
|
| 170 |
+
clip.write_videofile(video_path, codec="libx264", audio_codec="aac", verbose=False, logger=None)
|
| 171 |
+
except Exception as e: # pragma: no cover
|
| 172 |
+
st.error(f"Video composition failed: {e}")
|
| 173 |
+
return
|
| 174 |
|
| 175 |
+
with open(video_path, "rb") as f:
|
| 176 |
+
st.download_button("⬇️ Download Video", f, file_name="chats.mp4", mime="video/mp4")
|
| 177 |
+
|
| 178 |
+
def _draw_wrapped_text(draw: ImageDraw.ImageDraw, text: str, xy, font, width: int, fill=(0, 0, 0), leading: int = 6):
|
| 179 |
+
"""Basic word-wrapping for PIL text to fit a given width."""
|
| 180 |
+
x, y = xy
|
| 181 |
+
words = (text or "").split()
|
| 182 |
+
line = []
|
| 183 |
+
for w in words:
|
| 184 |
+
test = " ".join(line + [w])
|
| 185 |
+
w_px, _ = draw.textsize(test, font=font)
|
| 186 |
+
if w_px <= width:
|
| 187 |
+
line.append(w)
|
| 188 |
+
else:
|
| 189 |
+
draw.text((x, y), " ".join(line), font=font, fill=fill)
|
| 190 |
+
y += font.size + leading
|
| 191 |
+
line = [w]
|
| 192 |
+
if line:
|
| 193 |
+
draw.text((x, y), " ".join(line), font=font, fill=fill)
|
| 194 |
+
|
| 195 |
+
|
| 196 |
+
# =========================
|
| 197 |
+
# Main render
|
| 198 |
+
# =========================
|
| 199 |
def render_admin_export_tab(conn):
|
| 200 |
+
"""Admin export center with typeahead user, date filter, grid + pagination, selection and exports."""
|
| 201 |
+
# Only check ffmpeg/tts/etc when this tab is opened
|
| 202 |
ensure_video_deps()
|
| 203 |
|
| 204 |
st.subheader("📥 Export Records")
|
| 205 |
|
| 206 |
+
# --- Filters ---
|
| 207 |
+
with st.form("admin_export_filters"):
|
| 208 |
+
col1, col2 = st.columns(2)
|
| 209 |
+
with col1:
|
| 210 |
+
from_date = st.date_input("From Date", value=datetime.now().date() - timedelta(days=30))
|
| 211 |
+
with col2:
|
| 212 |
+
to_date = st.date_input("To Date", value=datetime.now().date())
|
| 213 |
+
|
| 214 |
+
prefix = st.text_input("Type first 3+ letters of email")
|
| 215 |
+
users = []
|
| 216 |
+
if prefix and len(prefix) >= 3:
|
| 217 |
+
users = search_users(conn, prefix)
|
| 218 |
+
|
| 219 |
+
selected_user = None
|
| 220 |
+
if users:
|
| 221 |
+
emails = [u["email"] for u in users]
|
| 222 |
+
selected_email = st.selectbox("Matching Users", emails)
|
| 223 |
+
selected_user = next(u for u in users if u["email"] == selected_email)
|
| 224 |
+
|
| 225 |
+
submitted = st.form_submit_button("Apply Filters")
|
| 226 |
+
|
| 227 |
+
if not submitted or not selected_user:
|
| 228 |
+
return
|
| 229 |
+
|
| 230 |
+
rows = fetch_filtered_chats(conn, from_date, to_date, selected_user["id"])
|
| 231 |
+
if not rows:
|
| 232 |
+
st.warning("No records found for selected filters.")
|
| 233 |
+
return
|
| 234 |
+
|
| 235 |
+
# --- Grid + pagination ---
|
| 236 |
+
df = pd.DataFrame(rows)
|
| 237 |
+
page_size = 20
|
| 238 |
+
total_pages = max(1, (len(df) - 1) // page_size + 1)
|
| 239 |
+
|
| 240 |
+
page = st.number_input("Page", min_value=1, max_value=total_pages, step=1, value=1)
|
| 241 |
+
start_idx = (page - 1) * page_size
|
| 242 |
+
end_idx = start_idx + page_size
|
| 243 |
+
df_page = df.iloc[start_idx:end_idx].copy()
|
| 244 |
+
|
| 245 |
+
st.dataframe(df_page, use_container_width=True, hide_index=True)
|
| 246 |
+
|
| 247 |
+
# --- Select all / multiselect for current page only ---
|
| 248 |
+
page_ids = df_page["id"].tolist()
|
| 249 |
+
select_all = st.checkbox("Select all rows on this page")
|
| 250 |
+
|
| 251 |
+
if select_all:
|
| 252 |
+
selected_ids = page_ids
|
| 253 |
+
else:
|
| 254 |
+
selected_ids = st.multiselect(
|
| 255 |
+
"Select chats to export (max 100 for TXT/CSV, max 20 for Video)",
|
| 256 |
+
page_ids,
|
| 257 |
+
)
|
| 258 |
+
|
| 259 |
+
st.caption(f"✅ Selected {len(selected_ids)} records (Page {page}/{total_pages})")
|
| 260 |
+
|
| 261 |
+
# --- Export actions with limits ---
|
| 262 |
+
st.markdown("### Export Options")
|
| 263 |
|
| 264 |
+
colA, colB, colC = st.columns(3)
|
| 265 |
+
with colA:
|
| 266 |
+
if st.button("⬇️ Export TXT"):
|
| 267 |
+
if len(selected_ids) > 100:
|
| 268 |
+
st.error("TXT export limited to 100 records.")
|
| 269 |
+
else:
|
| 270 |
+
export_txt(conn, selected_ids)
|
| 271 |
|
| 272 |
+
with colB:
|
| 273 |
+
if st.button("⬇️ Export CSV"):
|
| 274 |
+
if len(selected_ids) > 100:
|
| 275 |
+
st.error("CSV export limited to 100 records.")
|
| 276 |
+
else:
|
| 277 |
+
export_csv(conn, selected_ids)
|
| 278 |
|
| 279 |
+
with colC:
|
| 280 |
+
if st.button("🎬 Export Video"):
|
| 281 |
+
if len(selected_ids) > 20:
|
| 282 |
+
st.error("Video export limited to 20 records.")
|
| 283 |
+
else:
|
| 284 |
+
if project_export_video:
|
| 285 |
+
# Use project’s implementation if available
|
| 286 |
+
project_export_video(conn, selected_ids)
|
| 287 |
+
else:
|
| 288 |
+
_fallback_export_video(conn, selected_ids)
|