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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# HF Spaces / Gradio app: Vochi CRM call logs + AI analysis
|
| 2 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 3 |
+
# How to deploy (short):
|
| 4 |
+
# 1) Create a new Space (Python + Gradio).
|
| 5 |
+
# 2) Add a file named `app.py` with THIS code.
|
| 6 |
+
# 3) Add a file named `requirements.txt` with the lines below.
|
| 7 |
+
# 4) In the Space β Settings β Repository secrets, add:
|
| 8 |
+
# - VOCHI_BASE_URL (e.g. https://crm.vochi.by/api)
|
| 9 |
+
# - VOCHI_CLIENT_ID (client id string)
|
| 10 |
+
# - GOOGLE_API_KEY (API key)
|
| 11 |
+
# - VOCHI_UI_PASSWORD (password to unlock the UI)
|
| 12 |
+
|
| 13 |
+
#
|
| 14 |
+
# UI language: English.
|
| 15 |
+
|
| 16 |
+
from __future__ import annotations
|
| 17 |
+
import os
|
| 18 |
+
import json
|
| 19 |
+
import datetime as _dt
|
| 20 |
+
from typing import List, Tuple, Optional
|
| 21 |
+
|
| 22 |
+
import requests
|
| 23 |
+
import pandas as pd
|
| 24 |
+
import numpy as np
|
| 25 |
+
import gradio as gr
|
| 26 |
+
|
| 27 |
+
try:
|
| 28 |
+
# New Google Gemini client library
|
| 29 |
+
from google import genai # type: ignore
|
| 30 |
+
_HAS_GENAI = True
|
| 31 |
+
except Exception:
|
| 32 |
+
genai = None
|
| 33 |
+
_HAS_GENAI = False
|
| 34 |
+
|
| 35 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 36 |
+
# Config
|
| 37 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 38 |
+
BASE_URL = os.environ.get("VOCHI_BASE_URL", "https://crm.vochi.by/api")
|
| 39 |
+
CLIENT_ID = os.environ.get("VOCHI_CLIENT_ID")
|
| 40 |
+
|
| 41 |
+
# If your API needs auth, fill it here (or via VOCHI_BEARER in Secrets)
|
| 42 |
+
_AUTH_TOKEN = os.environ.get("VOCHI_BEARER", "").strip()
|
| 43 |
+
AUTH_HEADERS = {
|
| 44 |
+
"Accept": "audio/*,application/json;q=0.9,*/*;q=0.8",
|
| 45 |
+
**({"Authorization": f"Bearer {_AUTH_TOKEN}"} if _AUTH_TOKEN else {}),
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
# π UI password from Space Secrets (set VOCHI_UI_PASSWORD there)
|
| 49 |
+
_UI_PASSWORD = os.environ.get("VOCHI_UI_PASSWORD", "")
|
| 50 |
+
|
| 51 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 52 |
+
# Vochi API helpers
|
| 53 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 54 |
+
def fetch_calllogs(date_str: str):
|
| 55 |
+
"""Get list of calls for a given date (YYYY-MM-DD)."""
|
| 56 |
+
r = requests.get(
|
| 57 |
+
f"{BASE_URL}/calllogs",
|
| 58 |
+
params={"start": date_str, "end": date_str, "clientId": CLIENT_ID},
|
| 59 |
+
headers=AUTH_HEADERS,
|
| 60 |
+
timeout=60,
|
| 61 |
+
)
|
| 62 |
+
r.raise_for_status()
|
| 63 |
+
data = r.json()
|
| 64 |
+
if isinstance(data, dict):
|
| 65 |
+
return data.get("data", data)
|
| 66 |
+
return data
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def fetch_mp3_by_unique_id(unique_id: str) -> Tuple[str, str]:
|
| 70 |
+
"""Fetch call recording by UniqueId and save to /tmp. Returns (filepath, url)."""
|
| 71 |
+
url = f"{BASE_URL}/calllogs/{CLIENT_ID}/{unique_id}"
|
| 72 |
+
r = requests.get(url, headers=AUTH_HEADERS, timeout=120)
|
| 73 |
+
r.raise_for_status()
|
| 74 |
+
path = f"/tmp/call_{unique_id}.mp3"
|
| 75 |
+
with open(path, "wb") as f:
|
| 76 |
+
f.write(r.content)
|
| 77 |
+
return path, url
|
| 78 |
+
|
| 79 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 80 |
+
# Prompt templates & model options
|
| 81 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 82 |
+
PROMPT_TEMPLATES = {
|
| 83 |
+
"simple": (
|
| 84 |
+
"You are a call-center conversation analyst for a medical clinic. From the call recording, provide a brief summary:\n"
|
| 85 |
+
"- Purpose of the call (appointment / results / complaint / billing / other).\n"
|
| 86 |
+
"- Patient intent and expectations.\n"
|
| 87 |
+
"- Outcome (booked / call-back / routed / unresolved).\n"
|
| 88 |
+
"- Next steps (owner and when).\n"
|
| 89 |
+
"- Patient emotion (1β5) and agent tone (1β5).\n"
|
| 90 |
+
"- Alerts: urgency/risks/privacy.\n\n"
|
| 91 |
+
"Keep it short (6β8 lines). End with a line: βService quality rating: X/5β and one sentence explaining the rating."
|
| 92 |
+
),
|
| 93 |
+
"medium": (
|
| 94 |
+
"Act as a senior service analyst. Analyze the call using this structure:\n"
|
| 95 |
+
"1) Quick overview: reason for the call, intent, key facts, urgency (low/medium/high).\n"
|
| 96 |
+
"2) Call flow (2β4 bullets): what was asked/answered, where friction occurred.\n"
|
| 97 |
+
"3) Outcomes & tasks: concrete next actions for clinic/patient with timeframes.\n"
|
| 98 |
+
"4) Emotions & empathy: patient mood; agent empathy (0β5).\n"
|
| 99 |
+
"5) Procedural compliance: identity verification, disclosure of recording (if stated), no off-protocol medical advice, data accuracy.\n"
|
| 100 |
+
"6) Quality rating (0β100) using rubric: greeting, verification, accuracy, empathy, issue resolution (each 0β20)."
|
| 101 |
+
),
|
| 102 |
+
"detailed": (
|
| 103 |
+
"You are a quality & operations analyst. Provide an in-depth analysis:\n"
|
| 104 |
+
"A) Segmentation: split the call into stages with approximate timestamps (if available) and roles (Patient/Agent).\n"
|
| 105 |
+
"B) Structured data for booking: full name (if stated), date of birth, phone, symptoms/complaints (list), onset/duration, possible pain level 0β10 (if mentioned), required specialist/service, preferred time windows, constraints.\n"
|
| 106 |
+
"C) Triage & risks: class (routine/urgent/emergency), red flags, whether immediate escalation is needed.\n"
|
| 107 |
+
"D) Compliance audit: identity/privacy checks, recording disclosure, consent to data processing, booking policies.\n"
|
| 108 |
+
"E) Conversation metrics: talk ratio (agent/patient), interruptions, long pauses, notable keywords.\n"
|
| 109 |
+
"F) Coaching for the agent: 3β5 concrete improvements with sample phrasing.\n\n"
|
| 110 |
+
"Deliver: (1) A short patient-chart summary (2β3 sentences). (2) A task table with columns: priority, owner, due."
|
| 111 |
+
),
|
| 112 |
+
}
|
| 113 |
+
|
| 114 |
+
TPL_OPTIONS = [
|
| 115 |
+
("Simple", "simple"),
|
| 116 |
+
("Medium", "medium"),
|
| 117 |
+
("Detailed", "detailed"),
|
| 118 |
+
("Custom", "custom"),
|
| 119 |
+
]
|
| 120 |
+
|
| 121 |
+
LANG_OPTIONS = [
|
| 122 |
+
("Russian", "ru"),
|
| 123 |
+
("Auto", "default"),
|
| 124 |
+
("Belarusian", "be"),
|
| 125 |
+
("English", "en"),
|
| 126 |
+
]
|
| 127 |
+
|
| 128 |
+
MODEL_OPTIONS = [
|
| 129 |
+
("flash", "models/gemini-2.5-flash"),
|
| 130 |
+
("pro", "models/gemini-2.5-pro"),
|
| 131 |
+
("flash-lite", "models/gemini-2.5-flash-lite"),
|
| 132 |
+
]
|
| 133 |
+
|
| 134 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 135 |
+
# Utilities
|
| 136 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 137 |
+
|
| 138 |
+
def label_row(row: dict) -> str:
|
| 139 |
+
start = row.get("Start", "")
|
| 140 |
+
src = row.get("CallerId", "")
|
| 141 |
+
dst = row.get("Destination", "")
|
| 142 |
+
dur = row.get("Duration", "")
|
| 143 |
+
return f"{start} | {src} β {dst} ({dur}s)"
|
| 144 |
+
|
| 145 |
+
|
| 146 |
+
def _resolve_model(client: "genai.Client", preferred: str) -> str:
|
| 147 |
+
name = preferred if preferred.startswith("models/") else f"models/{preferred}"
|
| 148 |
+
try:
|
| 149 |
+
models = list(client.models.list())
|
| 150 |
+
desired_short = name.split("/", 1)[1]
|
| 151 |
+
for m in models:
|
| 152 |
+
mname = getattr(m, "name", "")
|
| 153 |
+
short = mname.split("/", 1)[1] if mname.startswith("models/") else mname
|
| 154 |
+
methods = set(getattr(m, "supported_generation_methods", []) or [])
|
| 155 |
+
if short == desired_short and ("generateContent" in methods or not methods):
|
| 156 |
+
return f"models/{short}"
|
| 157 |
+
# Fallback to first available
|
| 158 |
+
for title, candidate in MODEL_OPTIONS:
|
| 159 |
+
try:
|
| 160 |
+
short = candidate.split("/", 1)[1]
|
| 161 |
+
for m in models:
|
| 162 |
+
mname = getattr(m, "name", "")
|
| 163 |
+
sm = mname.split("/", 1)[1] if mname.startswith("models/") else mname
|
| 164 |
+
methods = set(getattr(m, "supported_generation_methods", []) or [])
|
| 165 |
+
if sm == short and ("generateContent" in methods or not methods):
|
| 166 |
+
return candidate
|
| 167 |
+
except Exception:
|
| 168 |
+
pass
|
| 169 |
+
except Exception:
|
| 170 |
+
pass
|
| 171 |
+
return name
|
| 172 |
+
|
| 173 |
+
|
| 174 |
+
def _system_instruction(lang_code: str) -> str:
|
| 175 |
+
if lang_code == "be":
|
| 176 |
+
return "Reply in Belarusian."
|
| 177 |
+
if lang_code == "ru":
|
| 178 |
+
return "Reply in Russian."
|
| 179 |
+
if lang_code == "en":
|
| 180 |
+
return "Reply in English."
|
| 181 |
+
return "Reply in the caller's language; if unclear, use concise professional English."
|
| 182 |
+
|
| 183 |
+
|
| 184 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 185 |
+
# Gradio handlers
|
| 186 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 187 |
+
|
| 188 |
+
def ui_fetch_calls(date_str: str):
|
| 189 |
+
try:
|
| 190 |
+
items = fetch_calllogs(date_str.strip())
|
| 191 |
+
df = pd.DataFrame(items)
|
| 192 |
+
opts = [(label_row(r), i) for i, r in df.iterrows()]
|
| 193 |
+
msg = f"Calls found: {len(df)}"
|
| 194 |
+
# Update dropdown choices and default value
|
| 195 |
+
dd = gr.update(choices=[(lbl, idx) for lbl, idx in opts], value=(opts[0][1] if opts else None))
|
| 196 |
+
return df, dd, msg
|
| 197 |
+
except requests.HTTPError as e:
|
| 198 |
+
body = ""
|
| 199 |
+
try:
|
| 200 |
+
body = e.response.text[:800]
|
| 201 |
+
except Exception:
|
| 202 |
+
pass
|
| 203 |
+
return pd.DataFrame(), gr.update(choices=[], value=None), f"HTTP error: {e}\n{body}"
|
| 204 |
+
except Exception as e:
|
| 205 |
+
return pd.DataFrame(), gr.update(choices=[], value=None), f"Load error: {e}"
|
| 206 |
+
|
| 207 |
+
|
| 208 |
+
def ui_play_audio(selected_idx: Optional[int], df: pd.DataFrame):
|
| 209 |
+
if selected_idx is None or df is None or df.empty:
|
| 210 |
+
return "<em>First fetch the list and select a row.</em>", None, None, ""
|
| 211 |
+
try:
|
| 212 |
+
row = df.iloc[int(selected_idx)]
|
| 213 |
+
except Exception:
|
| 214 |
+
return "<em>Invalid row selection.</em>", None, None, ""
|
| 215 |
+
unique_id = str(row.get("UniqueId"))
|
| 216 |
+
try:
|
| 217 |
+
fpath = f"/tmp/call_{unique_id}.mp3"
|
| 218 |
+
url_used = f"{BASE_URL}/calllogs/{CLIENT_ID}/{unique_id}"
|
| 219 |
+
# Download only if not exists (avoid re-fetch)
|
| 220 |
+
if not os.path.exists(fpath) or os.path.getsize(fpath) == 0:
|
| 221 |
+
fpath, url_used = fetch_mp3_by_unique_id(unique_id)
|
| 222 |
+
html = f'URL: <a href="{url_used}" target="_blank">{url_used}</a>'
|
| 223 |
+
return html, fpath, fpath, "Ready β
"
|
| 224 |
+
except requests.HTTPError as e:
|
| 225 |
+
body = ""
|
| 226 |
+
try:
|
| 227 |
+
body = e.response.text[:800]
|
| 228 |
+
except Exception:
|
| 229 |
+
pass
|
| 230 |
+
return f"HTTP error: {e}<br><pre>{body}</pre>", None, None, ""
|
| 231 |
+
except Exception as e:
|
| 232 |
+
return f"Playback failed: {e}", None, None, ""
|
| 233 |
+
|
| 234 |
+
|
| 235 |
+
def ui_toggle_custom_prompt(template_key: str):
|
| 236 |
+
return gr.update(visible=(template_key == "custom"))
|
| 237 |
+
|
| 238 |
+
|
| 239 |
+
def ui_analyze(selected_idx: Optional[int], df: pd.DataFrame,
|
| 240 |
+
template_key: str, custom_prompt: str, lang_code: str, model_pref: str):
|
| 241 |
+
if df is None or df.empty or selected_idx is None:
|
| 242 |
+
return "First fetch the list, choose a call, and (optionally) click βπ§ Playβ."
|
| 243 |
+
if not _HAS_GENAI:
|
| 244 |
+
return "β google-genai library not found. Make sure it's in requirements.txt."
|
| 245 |
+
|
| 246 |
+
try:
|
| 247 |
+
row = df.iloc[int(selected_idx)]
|
| 248 |
+
except Exception:
|
| 249 |
+
return "Invalid row selection."
|
| 250 |
+
|
| 251 |
+
unique_id = str(row.get("UniqueId"))
|
| 252 |
+
mp3_path = f"/tmp/call_{unique_id}.mp3"
|
| 253 |
+
|
| 254 |
+
# Ensure audio file exists (download if needed)
|
| 255 |
+
try:
|
| 256 |
+
if not os.path.exists(mp3_path) or os.path.getsize(mp3_path) == 0:
|
| 257 |
+
mp3_path, _ = fetch_mp3_by_unique_id(unique_id)
|
| 258 |
+
except Exception as e:
|
| 259 |
+
return f"Failed to obtain audio for analysis: {e}"
|
| 260 |
+
|
| 261 |
+
api_key = os.environ.get("GOOGLE_API_KEY", "").strip()
|
| 262 |
+
if not api_key:
|
| 263 |
+
return "GOOGLE_API_KEY is not set in Space Secrets. Add it in Settings β Secrets and restart the Space."
|
| 264 |
+
|
| 265 |
+
try:
|
| 266 |
+
client = genai.Client(api_key=api_key)
|
| 267 |
+
except Exception as e:
|
| 268 |
+
return f"Failed to initialize the client: {e}"
|
| 269 |
+
|
| 270 |
+
# Upload file
|
| 271 |
+
try:
|
| 272 |
+
uploaded_file = client.files.upload(file=mp3_path)
|
| 273 |
+
except Exception as e:
|
| 274 |
+
return f"File upload error: {e}"
|
| 275 |
+
|
| 276 |
+
# Prepare prompt
|
| 277 |
+
if template_key == "custom":
|
| 278 |
+
prompt = (custom_prompt or "").strip() or PROMPT_TEMPLATES["simple"]
|
| 279 |
+
else:
|
| 280 |
+
prompt = PROMPT_TEMPLATES.get(template_key, PROMPT_TEMPLATES["simple"])
|
| 281 |
+
|
| 282 |
+
sys_inst = _system_instruction(lang_code)
|
| 283 |
+
model_name = _resolve_model(client, model_pref)
|
| 284 |
+
|
| 285 |
+
# Call model
|
| 286 |
+
try:
|
| 287 |
+
merged = f"""[SYSTEM INSTRUCTION: {sys_inst}]
|
| 288 |
+
|
| 289 |
+
{prompt}"""
|
| 290 |
+
resp = client.models.generate_content(model=model_name, contents=[uploaded_file, merged])
|
| 291 |
+
text = getattr(resp, "text", None)
|
| 292 |
+
if not text:
|
| 293 |
+
return "Analysis finished but returned no text. Check model settings and file format."
|
| 294 |
+
return f"### Analysis result\n\n{text}"
|
| 295 |
+
except Exception as e:
|
| 296 |
+
# Try to attach more error details
|
| 297 |
+
msg = str(e)
|
| 298 |
+
try:
|
| 299 |
+
if hasattr(e, "args") and e.args:
|
| 300 |
+
msg = msg + "\n\n" + str(e.args[0])
|
| 301 |
+
except Exception:
|
| 302 |
+
pass
|
| 303 |
+
return f"Error during model call: {msg}"
|
| 304 |
+
finally:
|
| 305 |
+
# Best-effort cleanup of remote file
|
| 306 |
+
try:
|
| 307 |
+
if 'uploaded_file' in locals() and hasattr(uploaded_file, 'name'):
|
| 308 |
+
client.files.delete(name=uploaded_file.name)
|
| 309 |
+
except Exception:
|
| 310 |
+
pass
|
| 311 |
+
|
| 312 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 313 |
+
# Password / gating helpers
|
| 314 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 315 |
+
|
| 316 |
+
def ui_check_password(pwd: str):
|
| 317 |
+
"""
|
| 318 |
+
Check password against VOCHI_UI_PASSWORD.
|
| 319 |
+
Returns: (authed_state, status_msg_md, pwd_group_visibility)
|
| 320 |
+
"""
|
| 321 |
+
if not _UI_PASSWORD:
|
| 322 |
+
# Admin hint if password not configured
|
| 323 |
+
return False, (
|
| 324 |
+
"β οΈ <b>VOCHI_UI_PASSWORD</b> Π½Π΅ Π½Π°Π»Π°Π΄ΠΆΠ°Π½Ρ Ρ Secrets. "
|
| 325 |
+
"ΠΠ°Π΄Π°ΠΉΡΠ΅ ΡΠ³ΠΎ Ρ Settings β Secrets Ρ ΠΏΠ΅ΡΠ°Π·Π°ΠΏΡΡΡΡΡΠ΅ Space."
|
| 326 |
+
), gr.update(visible=True)
|
| 327 |
+
|
| 328 |
+
if (pwd or "").strip() == _UI_PASSWORD:
|
| 329 |
+
return True, "β
ΠΠΎΡΡΡΠΏ Π°Π΄ΠΊΡΡΡΡ. Π¦ΡΠΏΠ΅Ρ ΠΌΠΎΠΆΠ½Π° Π½Π°ΡΡΡΠΊΠ°ΡΡ <b>Fetch list</b> Ρ ΠΏΡΠ°ΡΠ°Π²Π°ΡΡ.", gr.update(visible=False)
|
| 330 |
+
else:
|
| 331 |
+
return False, "β ΠΡΠΏΡΠ°Π²ΡΠ»ΡΠ½Ρ ΠΏΠ°ΡΠΎΠ»Ρ. ΠΠ°ΡΠΏΡΠ°Π±ΡΠΉΡΠ΅ ΡΡΡΡ ΡΠ°Π·.", gr.update(visible=True)
|
| 332 |
+
|
| 333 |
+
|
| 334 |
+
def ui_fetch_or_auth(date_str: str, authed: bool):
|
| 335 |
+
"""
|
| 336 |
+
If not authed, open password box instead of fetching.
|
| 337 |
+
Otherwise, fetch calls.
|
| 338 |
+
Returns: calls_df, row_dd, status_md, pwd_group_visibility
|
| 339 |
+
"""
|
| 340 |
+
if not authed:
|
| 341 |
+
return gr.update(), gr.update(), "π Π£Π²ΡΠ΄Π·ΡΡΠ΅ ΠΏΠ°ΡΠΎΠ»Ρ, ΠΊΠ°Π± Π°ΡΡΡΠΌΠ°ΡΡ Π·Π²Π°Π½ΠΊΡ.", gr.update(visible=True)
|
| 342 |
+
df, dd, msg = ui_fetch_calls(date_str)
|
| 343 |
+
return df, dd, msg, gr.update(visible=False)
|
| 344 |
+
|
| 345 |
+
|
| 346 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 347 |
+
# Build Gradio UI
|
| 348 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 349 |
+
|
| 350 |
+
def _today_str():
|
| 351 |
+
return _dt.date.today().strftime("%Y-%m-%d")
|
| 352 |
+
|
| 353 |
+
with gr.Blocks(title="Vochi CRM Call Logs (Gradio)") as demo:
|
| 354 |
+
gr.Markdown(
|
| 355 |
+
"""
|
| 356 |
+
# Vochi CRM β MP3 β AI analysis
|
| 357 |
+
*Fetch daily calls, play/download MP3, and analyze the call with an AI model.*
|
| 358 |
+
|
| 359 |
+
"""
|
| 360 |
+
)
|
| 361 |
+
|
| 362 |
+
# Auth state (False by default)
|
| 363 |
+
authed = gr.State(False)
|
| 364 |
+
|
| 365 |
+
# Password "modal" (group shown on demand)
|
| 366 |
+
with gr.Group(visible=False) as pwd_group:
|
| 367 |
+
gr.Markdown("### π Π£Π²ΡΠ΄Π·ΡΡΠ΅ ΠΏΠ°ΡΠΎΠ»Ρ")
|
| 368 |
+
pwd_tb = gr.Textbox(label="Password", type="password", placeholder="β’β’β’β’β’β’β’β’", lines=1)
|
| 369 |
+
pwd_btn = gr.Button("ΠΠ΄ΠΊΡΡΡΡ Π΄ΠΎΡΡΡΠΏ", variant="primary")
|
| 370 |
+
|
| 371 |
+
with gr.Tabs() as tabs:
|
| 372 |
+
with gr.Tab("Vochi CRM"):
|
| 373 |
+
with gr.Row():
|
| 374 |
+
date_inp = gr.Textbox(label="Date", value=_today_str(), scale=1)
|
| 375 |
+
fetch_btn = gr.Button("Fetch list", variant="primary", scale=0)
|
| 376 |
+
calls_df = gr.Dataframe(value=pd.DataFrame(), label="Call list", interactive=False)
|
| 377 |
+
row_dd = gr.Dropdown(choices=[], label="Call", info="Select a row for playback/analysis")
|
| 378 |
+
with gr.Row():
|
| 379 |
+
play_btn = gr.Button("π§ Play")
|
| 380 |
+
url_html = gr.HTML()
|
| 381 |
+
audio_out = gr.Audio(label="Audio", type="filepath")
|
| 382 |
+
file_out = gr.File(label="MP3 download")
|
| 383 |
+
status_fetch = gr.Markdown()
|
| 384 |
+
|
| 385 |
+
with gr.Tab("AI Analysis"):
|
| 386 |
+
with gr.Row():
|
| 387 |
+
tpl_dd = gr.Dropdown(choices=TPL_OPTIONS, value="simple", label="Template")
|
| 388 |
+
lang_dd = gr.Dropdown(choices=LANG_OPTIONS, value="default", label="Language")
|
| 389 |
+
model_dd = gr.Dropdown(choices=MODEL_OPTIONS, value="models/gemini-2.5-flash", label="Model")
|
| 390 |
+
custom_prompt_tb = gr.Textbox(label="Custom prompt", lines=8, visible=False)
|
| 391 |
+
analyze_btn = gr.Button("π§ Analyze", variant="primary")
|
| 392 |
+
analysis_md = gr.Markdown()
|
| 393 |
+
|
| 394 |
+
# Wire events
|
| 395 |
+
# 1) Fetch button: gate by password
|
| 396 |
+
fetch_btn.click(
|
| 397 |
+
ui_fetch_or_auth,
|
| 398 |
+
inputs=[date_inp, authed],
|
| 399 |
+
outputs=[calls_df, row_dd, status_fetch, pwd_group],
|
| 400 |
+
)
|
| 401 |
+
|
| 402 |
+
# 2) Password submit β set authed state, show message, hide group on success
|
| 403 |
+
pwd_btn.click(
|
| 404 |
+
ui_check_password,
|
| 405 |
+
inputs=[pwd_tb],
|
| 406 |
+
outputs=[authed, status_fetch, pwd_group],
|
| 407 |
+
)
|
| 408 |
+
|
| 409 |
+
# 3) Other interactions
|
| 410 |
+
play_btn.click(ui_play_audio, inputs=[row_dd, calls_df], outputs=[url_html, audio_out, file_out, status_fetch])
|
| 411 |
+
tpl_dd.change(ui_toggle_custom_prompt, inputs=[tpl_dd], outputs=[custom_prompt_tb])
|
| 412 |
+
analyze_btn.click(
|
| 413 |
+
ui_analyze,
|
| 414 |
+
inputs=[row_dd, calls_df, tpl_dd, custom_prompt_tb, lang_dd, model_dd],
|
| 415 |
+
outputs=[analysis_md],
|
| 416 |
+
)
|
| 417 |
+
|
| 418 |
+
|
| 419 |
+
if __name__ == "__main__":
|
| 420 |
+
# On HF Spaces, just running this file is enough; launch() is fine for local dev, too.
|
| 421 |
+
demo.launch()
|