archivartaunik commited on
Commit
e81c1a9
·
verified ·
1 Parent(s): 64bcc8d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +381 -0
app.py ADDED
@@ -0,0 +1,381 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # HF Spaces / Gradio app: Vochi CRM call logs + Gemini 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 (Gemini API key)
11
+ # 5) (Optional) If you prefer, you can also paste your Gemini key in the UI.
12
+ #
13
+ # requirements.txt (copy these lines into a separate file):
14
+ # -----------------------------------------------------------------------------
15
+ # gradio>=4.40.0
16
+ # requests>=2.31.0
17
+ # pandas>=2.2.2
18
+ # numpy>=1.26.4
19
+ # soundfile>=0.12.1
20
+ # google-genai>=0.6.0
21
+ # -----------------------------------------------------------------------------
22
+ #
23
+ # Notes:
24
+ # - Audio playback & download are handled inside the app; the MP3 is saved under /tmp.
25
+ # - Gemini accepts MP3 uploads directly, so WAV conversion is not strictly required.
26
+ # - If your Vochi instance needs auth, set an Authorization header below.
27
+ #
28
+ # UI language: Belarusian.
29
+
30
+ from __future__ import annotations
31
+ import os
32
+ import json
33
+ import datetime as _dt
34
+ from typing import List, Tuple, Optional
35
+
36
+ import requests
37
+ import pandas as pd
38
+ import numpy as np
39
+ import gradio as gr
40
+
41
+ try:
42
+ # New Google Gemini client library
43
+ from google import genai # type: ignore
44
+ _HAS_GENAI = True
45
+ except Exception:
46
+ genai = None
47
+ _HAS_GENAI = False
48
+
49
+ # ─────────────────────────────────────────────────────────────────────────────
50
+ # Config
51
+ # ─────────────────────────────────────────────────────────────────────────────
52
+ BASE_URL = os.environ.get("VOCHI_BASE_URL", "https://crm.vochi.by/api")
53
+ CLIENT_ID = os.environ.get("VOCHI_CLIENT_ID")
54
+
55
+ # If your API needs auth, fill it here (or via VOCHI_BEARER in Secrets)
56
+ _AUTH_TOKEN = os.environ.get("VOCHI_BEARER", "").strip()
57
+ AUTH_HEADERS = {
58
+ "Accept": "audio/*,application/json;q=0.9,*/*;q=0.8",
59
+ **({"Authorization": f"Bearer {_AUTH_TOKEN}"} if _AUTH_TOKEN else {}),
60
+ }
61
+
62
+ # ─────────────────────────────────────────────────────────────────────────────
63
+ # Vochi API helpers
64
+ # ─────────────────────────────────────────────────────────────────────────────
65
+ def fetch_calllogs(date_str: str):
66
+ """Get list of calls for a given date (YYYY-MM-DD)."""
67
+ r = requests.get(
68
+ f"{BASE_URL}/calllogs",
69
+ params={"start": date_str, "end": date_str, "clientId": CLIENT_ID},
70
+ headers=AUTH_HEADERS,
71
+ timeout=60,
72
+ )
73
+ r.raise_for_status()
74
+ data = r.json()
75
+ if isinstance(data, dict):
76
+ return data.get("data", data)
77
+ return data
78
+
79
+
80
+ def fetch_mp3_by_unique_id(unique_id: str) -> Tuple[str, str]:
81
+ """Fetch call recording by UniqueId and save to /tmp. Returns (filepath, url)."""
82
+ url = f"{BASE_URL}/calllogs/{CLIENT_ID}/{unique_id}"
83
+ r = requests.get(url, headers=AUTH_HEADERS, timeout=120)
84
+ r.raise_for_status()
85
+ path = f"/tmp/call_{unique_id}.mp3"
86
+ with open(path, "wb") as f:
87
+ f.write(r.content)
88
+ return path, url
89
+
90
+ # ─────────────────────────────────────────────────────────────────────────────
91
+ # Prompt templates & model options
92
+ # ─────────────────────────────────────────────────────────────────────────────
93
+ PROMPT_TEMPLATES = {
94
+ "simple": (
95
+ "You are a call-center conversation analyst for a medical clinic. From the call recording, provide a brief summary:\n"
96
+ "- Purpose of the call (appointment / results / complaint / billing / other).\n"
97
+ "- Patient intent and expectations.\n"
98
+ "- Outcome (booked / call-back / routed / unresolved).\n"
99
+ "- Next steps (owner and when).\n"
100
+ "- Patient emotion (1–5) and agent tone (1–5).\n"
101
+ "- Alerts: urgency/risks/privacy.\n\n"
102
+ "Keep it short (6–8 lines). End with a line: ‘Service quality rating: X/5’ and one sentence explaining the rating."
103
+ ),
104
+ "medium": (
105
+ "Act as a senior service analyst. Analyze the call using this structure:\n"
106
+ "1) Quick overview: reason for the call, intent, key facts, urgency (low/medium/high).\n"
107
+ "2) Call flow (2–4 bullets): what was asked/answered, where friction occurred.\n"
108
+ "3) Outcomes & tasks: concrete next actions for clinic/patient with timeframes.\n"
109
+ "4) Emotions & empathy: patient mood; agent empathy (0–5).\n"
110
+ "5) Procedural compliance: identity verification, disclosure of recording (if stated), no off-protocol medical advice, data accuracy.\n"
111
+ "6) Quality rating (0–100) using rubric: greeting, verification, accuracy, empathy, issue resolution (each 0–20)."
112
+ ),
113
+ "detailed": (
114
+ "You are a quality & operations analyst. Provide an in-depth analysis:\n"
115
+ "A) Segmentation: split the call into stages with approximate timestamps (if available) and roles (Patient/Agent).\n"
116
+ "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"
117
+ "C) Triage & risks: class (routine/urgent/emergency), red flags, whether immediate escalation is needed.\n"
118
+ "D) Compliance audit: identity/privacy checks, recording disclosure, consent to data processing, booking policies.\n"
119
+ "E) Conversation metrics: talk ratio (agent/patient), interruptions, long pauses, notable keywords.\n"
120
+ "F) Coaching for the agent: 3–5 concrete improvements with sample phrasing.\n\n"
121
+ "Deliver: (1) A short patient-chart summary (2–3 sentences). (2) A task table with columns: priority, owner, due."
122
+ ),
123
+ }
124
+
125
+ TPL_OPTIONS = [
126
+ ("Simple", "simple"),
127
+ ("Medium", "medium"),
128
+ ("Detailed", "detailed"),
129
+ ("Custom", "custom"),
130
+ ]
131
+
132
+ LANG_OPTIONS = [
133
+ ("Russian", "ru"),
134
+ ("Auto", "default"),
135
+ ("Belarusian", "be"),
136
+ ("English", "en"),
137
+ ]
138
+
139
+ MODEL_OPTIONS = [
140
+ ("flash", "models/gemini-2.5-flash"),
141
+ ("pro", "models/gemini-2.5-pro"),
142
+ ("flash-lite", "models/gemini-2.5-flash-lite"),
143
+ ]
144
+
145
+ # ─────────────────────────────────────────────────────────────────────────────
146
+ # Utilities
147
+ # ─────────────────────────────────────────────────────────────────────────────
148
+ def label_row(row: dict) -> str:
149
+ start = row.get("Start", "")
150
+ src = row.get("CallerId", "")
151
+ dst = row.get("Destination", "")
152
+ dur = row.get("Duration", "")
153
+ return f"{start} | {src} → {dst} ({dur}s)"
154
+
155
+
156
+ def _resolve_model(client: "genai.Client", preferred: str) -> str:
157
+ name = preferred if preferred.startswith("models/") else f"models/{preferred}"
158
+ try:
159
+ models = list(client.models.list())
160
+ desired_short = name.split("/", 1)[1]
161
+ for m in models:
162
+ mname = getattr(m, "name", "")
163
+ short = mname.split("/", 1)[1] if mname.startswith("models/") else mname
164
+ methods = set(getattr(m, "supported_generation_methods", []) or [])
165
+ if short == desired_short and ("generateContent" in methods or not methods):
166
+ return f"models/{short}"
167
+ # Fallback to first available
168
+ for title, candidate in MODEL_OPTIONS:
169
+ try:
170
+ short = candidate.split("/", 1)[1]
171
+ for m in models:
172
+ mname = getattr(m, "name", "")
173
+ sm = mname.split("/", 1)[1] if mname.startswith("models/") else mname
174
+ methods = set(getattr(m, "supported_generation_methods", []) or [])
175
+ if sm == short and ("generateContent" in methods or not methods):
176
+ return candidate
177
+ except Exception:
178
+ pass
179
+ except Exception:
180
+ pass
181
+ return name
182
+
183
+
184
+ def _system_instruction(lang_code: str) -> str:
185
+ if lang_code == "be":
186
+ return "Reply in Belarusian."
187
+ if lang_code == "ru":
188
+ return "Reply in Russian."
189
+ if lang_code == "en":
190
+ return "Reply in English."
191
+ return "Reply in the caller's language; if unclear, use concise professional English."
192
+
193
+
194
+ # ─────────────────────────────────────────────────────────────────────────────
195
+ # Gradio handlers
196
+ # ────────────────────────────────────────────────────────────────────────���────
197
+ def ui_fetch_calls(date_str: str):
198
+ try:
199
+ items = fetch_calllogs(date_str.strip())
200
+ df = pd.DataFrame(items)
201
+ opts = [(label_row(r), i) for i, r in df.iterrows()]
202
+ msg = f"Знойдзена званкоў: {len(df)}"
203
+ # Update dropdown choices and default value
204
+ dd = gr.update(choices=[(lbl, idx) for lbl, idx in opts], value=(opts[0][1] if opts else None))
205
+ return df, dd, msg
206
+ except requests.HTTPError as e:
207
+ body = ""
208
+ try:
209
+ body = e.response.text[:800]
210
+ except Exception:
211
+ pass
212
+ return pd.DataFrame(), gr.update(choices=[], value=None), f"HTTP памылка: {e}\n{body}"
213
+ except Exception as e:
214
+ return pd.DataFrame(), gr.update(choices=[], value=None), f"Памылка загрузкі: {e}"
215
+
216
+
217
+ def ui_play_audio(selected_idx: Optional[int], df: pd.DataFrame):
218
+ if selected_idx is None or df is None or df.empty:
219
+ return "<em>Спачатку атрымаць спіс і выбраць радок.</em>", None, None, ""
220
+ try:
221
+ row = df.iloc[int(selected_idx)]
222
+ except Exception:
223
+ return "<em>Некарэктны выбар радка.</em>", None, None, ""
224
+ unique_id = str(row.get("UniqueId"))
225
+ try:
226
+ fpath = f"/tmp/call_{unique_id}.mp3"
227
+ url_used = f"{BASE_URL}/calllogs/{CLIENT_ID}/{unique_id}"
228
+ # Download only if not exists (avoid re-fetch)
229
+ if not os.path.exists(fpath) or os.path.getsize(fpath) == 0:
230
+ fpath, url_used = fetch_mp3_by_unique_id(unique_id)
231
+ html = f'URL: <a href="{url_used}" target="_blank">{url_used}</a>'
232
+ return html, fpath, fpath, "Гатова ✅"
233
+ except requests.HTTPError as e:
234
+ body = ""
235
+ try:
236
+ body = e.response.text[:800]
237
+ except Exception:
238
+ pass
239
+ return f"HTTP памылка: {e}<br><pre>{body}</pre>", None, None, ""
240
+ except Exception as e:
241
+ return f"Не атрымалася прайграць: {e}", None, None, ""
242
+
243
+
244
+ def ui_toggle_custom_prompt(template_key: str):
245
+ return gr.update(visible=(template_key == "custom"))
246
+
247
+
248
+ def ui_analyze(selected_idx: Optional[int], df: pd.DataFrame, api_key_in: str,
249
+ template_key: str, custom_prompt: str, lang_code: str, model_pref: str):
250
+ if df is None or df.empty or selected_idx is None:
251
+ return "Спачатку атрымаць спіс, выбраць званок і (пры патрэбе) націснуць “🎧 Прайграць”."
252
+ if not _HAS_GENAI:
253
+ return "❌ Бібліятэка google-genai не знойдзена. Упэўніцеся, што яна ў requirements.txt."
254
+
255
+ try:
256
+ row = df.iloc[int(selected_idx)]
257
+ except Exception:
258
+ return "Некарэктны выбар радка."
259
+
260
+ unique_id = str(row.get("UniqueId"))
261
+ mp3_path = f"/tmp/call_{unique_id}.mp3"
262
+
263
+ # Ensure audio file exists (download if needed)
264
+ try:
265
+ if not os.path.exists(mp3_path) or os.path.getsize(mp3_path) == 0:
266
+ mp3_path, _ = fetch_mp3_by_unique_id(unique_id)
267
+ except Exception as e:
268
+ return f"Не атрымалася атрымаць аўдыё для аналізу: {e}"
269
+
270
+ api_key = (api_key_in or os.environ.get("GOOGLE_API_KEY", "")).strip()
271
+ if not api_key:
272
+ return "Увядзіце Gemini API key у полі або дадайце GOOGLE_API_KEY у Secrets Space."
273
+
274
+ try:
275
+ client = genai.Client(api_key=api_key)
276
+ except Exception as e:
277
+ return f"Не атрымалася ініцыялізаваць кліент Gemini: {e}"
278
+
279
+ # Upload file
280
+ try:
281
+ uploaded_file = client.files.upload(file=mp3_path)
282
+ except Exception as e:
283
+ return f"Памылка загрузкі файла ў Gemini: {e}"
284
+
285
+ # Prepare prompt
286
+ if template_key == "custom":
287
+ prompt = (custom_prompt or "").strip() or PROMPT_TEMPLATES["simple"]
288
+ else:
289
+ prompt = PROMPT_TEMPLATES.get(template_key, PROMPT_TEMPLATES["simple"])
290
+
291
+ sys_inst = _system_instruction(lang_code)
292
+ model_name = _resolve_model(client, model_pref)
293
+
294
+ # Call model
295
+ try:
296
+ resp = client.models.generate_content(
297
+ model=model_name,
298
+ contents=[uploaded_file, prompt],
299
+ system_instruction=sys_inst,
300
+ )
301
+ text = getattr(resp, "text", None)
302
+ if not text:
303
+ # Fallback: inline the system instruction
304
+ merged = f"[SYSTEM INSTRUCTION: {sys_inst}]\n\n" + prompt
305
+ resp = client.models.generate_content(model=model_name, contents=[uploaded_file, merged])
306
+ text = getattr(resp, "text", None)
307
+ if not text:
308
+ return "Аналіз завершаны без тэкставага адказу. Праверце налады мадэлі і фармат файла."
309
+ return f"### Вынік аналізу\n\n{text}"
310
+ except Exception as e:
311
+ # Try to attach more error details
312
+ msg = str(e)
313
+ try:
314
+ if hasattr(e, "args") and e.args:
315
+ msg = msg + "\n\n" + str(e.args[0])
316
+ except Exception:
317
+ pass
318
+ return f"Памылка падчас выкліку мадэлі: {msg}"
319
+ finally:
320
+ # Best-effort cleanup of remote file
321
+ try:
322
+ if 'uploaded_file' in locals() and hasattr(uploaded_file, 'name'):
323
+ client.files.delete(name=uploaded_file.name)
324
+ except Exception:
325
+ pass
326
+
327
+
328
+ # ─────────────────────────────────────────────────────────────────────────────
329
+ # Build Gradio UI
330
+ # ─────────────────────────────────────────────────────────────────────────────
331
+ def _today_str():
332
+ return _dt.date.today().strftime("%Y-%m-%d")
333
+
334
+ with gr.Blocks(title="Vochi CRM + Gemini (Gradio)") as demo:
335
+ gr.Markdown(
336
+ """
337
+ # Vochi CRM → MP3 → Gemini аналіз
338
+ *Атрымаць званкі за дзень, прайграць/скачаць MP3 і прааналізаваць званок мадэллю Google Gemini.*
339
+
340
+ **Парады бяспекі:** Захоўвайце ключ Gemini у Secrets Space (`GOOGLE_API_KEY`). Калі патрэбна, можна ўвесці яго ніжэй (ключ будзе выкарыстоўвацца толькі ў гэтай сесіі).
341
+ """
342
+ )
343
+
344
+ with gr.Tabs():
345
+ with gr.Tab("Vochi CRM"):
346
+ with gr.Row():
347
+ date_inp = gr.Textbox(label="Дата", value=_today_str(), scale=1)
348
+ fetch_btn = gr.Button("Атрымаць спіс", variant="primary", scale=0)
349
+ calls_df = gr.Dataframe(value=pd.DataFrame(), label="Спіс званкоў", interactive=False, wrap=True, pandas=True)
350
+ row_dd = gr.Dropdown(choices=[], label="Званок", info="Абярыце радок для прайгравання/аналізу")
351
+ with gr.Row():
352
+ play_btn = gr.Button("🎧 Прайграць")
353
+ url_html = gr.HTML()
354
+ audio_out = gr.Audio(label="Аўдыё", type="filepath")
355
+ file_out = gr.File(label="MP3 для сцягвання")
356
+ status_fetch = gr.Markdown()
357
+
358
+ with gr.Tab("AI Analysis (Gemini)"):
359
+ api_key_tb = gr.Textbox(label="Gemini API Key (неабавязкова, калі ў Secrets)", value=os.environ.get("GOOGLE_API_KEY", ""), type="password")
360
+ with gr.Row():
361
+ tpl_dd = gr.Dropdown(choices=TPL_OPTIONS, value="simple", label="Шаблон")
362
+ lang_dd = gr.Dropdown(choices=LANG_OPTIONS, value="default", label="Мова")
363
+ model_dd = gr.Dropdown(choices=MODEL_OPTIONS, value="models/gemini-2.5-flash", label="Мадэль")
364
+ custom_prompt_tb = gr.Textbox(label="Custom prompt", lines=8, visible=False)
365
+ analyze_btn = gr.Button("🧠 Аналіз", variant="primary")
366
+ analysis_md = gr.Markdown()
367
+
368
+ # Wire events
369
+ fetch_btn.click(ui_fetch_calls, inputs=[date_inp], outputs=[calls_df, row_dd, status_fetch])
370
+ play_btn.click(ui_play_audio, inputs=[row_dd, calls_df], outputs=[url_html, audio_out, file_out, status_fetch])
371
+ tpl_dd.change(ui_toggle_custom_prompt, inputs=[tpl_dd], outputs=[custom_prompt_tb])
372
+ analyze_btn.click(
373
+ ui_analyze,
374
+ inputs=[row_dd, calls_df, api_key_tb, tpl_dd, custom_prompt_tb, lang_dd, model_dd],
375
+ outputs=[analysis_md],
376
+ )
377
+
378
+
379
+ if __name__ == "__main__":
380
+ # On HF Spaces, just running this file is enough; launch() is fine for local dev, too.
381
+ demo.launch()