zaktree commited on
Commit
7ef6f6c
·
verified ·
1 Parent(s): cc6ec55

Upload 3 files

Browse files
Files changed (3) hide show
  1. Dockerfile +39 -0
  2. app.py +209 -0
  3. index.html +453 -0
Dockerfile ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.10-slim
2
+
3
+ RUN apt-get update && apt-get install -y --no-install-recommends \
4
+ ffmpeg \
5
+ libsndfile1 \
6
+ git \
7
+ build-essential \
8
+ && rm -rf /var/lib/apt/lists/*
9
+
10
+ WORKDIR /app
11
+
12
+ # Clone LuxTTS's code once, at build time
13
+ RUN git clone https://github.com/ysharma3501/LuxTTS.git
14
+
15
+ # Install all dependencies once, at build time (not on every startup)
16
+ RUN pip install --no-cache-dir \
17
+ --find-links https://k2-fsa.github.io/icefall/piper_phonemize.html \
18
+ -r LuxTTS/requirements.txt
19
+
20
+ RUN pip install --no-cache-dir gradio==4.44.1 "pydantic<2.11" "starlette==0.37.2" "jinja2==3.1.4" uvicorn requests
21
+
22
+ ENV PYTHONPATH="/app/LuxTTS"
23
+ ENV OMP_NUM_THREADS=2
24
+ ENV MKL_NUM_THREADS=2
25
+
26
+ # Pre-download the model weights at build time too, so the container
27
+ # never has to fetch them again on startup (they get baked into the image)
28
+ RUN python -c "from zipvoice.luxvoice import LuxTTS; LuxTTS('YatharthS/LuxTTS', device='cpu', threads=2)"
29
+
30
+ COPY app.py /app/app.py
31
+ COPY index.html /app/index.html
32
+ COPY me.wav /app/me.wav
33
+ COPY me.jpg /app/me.jpg
34
+ COPY facts.json /app/facts.json
35
+
36
+ EXPOSE 7860
37
+ ENV GRADIO_SERVER_NAME="0.0.0.0"
38
+
39
+ CMD ["python", "app.py"]
app.py ADDED
@@ -0,0 +1,209 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import time
3
+ import numpy as np
4
+ import gradio as gr
5
+ import requests
6
+ import torch
7
+ from functools import lru_cache
8
+ from fastapi import FastAPI, HTTPException
9
+ from fastapi.responses import FileResponse, JSONResponse
10
+ from zipvoice.luxvoice import LuxTTS
11
+
12
+ from pydantic import BaseModel
13
+
14
+ GROQ_API_KEY = os.environ.get("GROQ_API_KEY", "")
15
+
16
+
17
+ class ChatBody(BaseModel):
18
+ messages: list
19
+
20
+
21
+ class SpeakBody(BaseModel):
22
+ text: str
23
+
24
+ # Init Model (weights already downloaded at build time)
25
+ device = "cuda" if torch.cuda.is_available() else "cpu"
26
+ lux_tts = LuxTTS("YatharthS/LuxTTS", device=device, threads=2)
27
+
28
+
29
+ @lru_cache(maxsize=8)
30
+ def get_encoded_prompt(audio_prompt, ref_duration, rms):
31
+ # encode_prompt() is deterministic for a given (file, duration, rms)
32
+ # combo, and me.wav never changes between calls from the chat bot -
33
+ # so we only need to pay this cost once instead of on every message.
34
+ return lux_tts.encode_prompt(audio_prompt, duration=ref_duration, rms=rms)
35
+
36
+
37
+ # Warm the cache at startup so the very first real request doesn't
38
+ # also have to pay the reference-encoding cost.
39
+ try:
40
+ get_encoded_prompt("me.wav", 5, 0.01)
41
+ print("Reference prompt pre-encoded and cached.")
42
+ except Exception as e:
43
+ print(f"Could not pre-warm reference prompt cache: {e}")
44
+
45
+
46
+ def infer(
47
+ text,
48
+ audio_prompt,
49
+ rms,
50
+ ref_duration,
51
+ t_shift,
52
+ num_steps,
53
+ speed,
54
+ return_smooth,
55
+ ):
56
+ if not text:
57
+ return None, "Please provide text."
58
+
59
+ # If no reference audio is given (e.g. calls from the website API),
60
+ # fall back to the baked-in me.wav
61
+ if not audio_prompt:
62
+ audio_prompt = "me.wav"
63
+
64
+ start_time = time.time()
65
+
66
+ encoded_prompt = get_encoded_prompt(audio_prompt, ref_duration, rms)
67
+
68
+ final_wav = lux_tts.generate_speech(
69
+ text,
70
+ encoded_prompt,
71
+ num_steps=int(num_steps),
72
+ t_shift=t_shift,
73
+ speed=speed,
74
+ return_smooth=return_smooth,
75
+ )
76
+
77
+ duration = round(time.time() - start_time, 2)
78
+
79
+ final_wav = final_wav.cpu().squeeze(0).numpy()
80
+ final_wav = (np.clip(final_wav, -1.0, 1.0) * 32767).astype(np.int16)
81
+
82
+ stats_msg = f"✨ Generation complete in **{duration}s**."
83
+ return (48000, final_wav), stats_msg
84
+
85
+
86
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
87
+ gr.Markdown("# 🎙️ Papa Jerry Voice (LuxTTS)")
88
+
89
+ gr.Markdown(
90
+ """
91
+ > **Note:** This demo runs on a **2-core CPU**, so expect slower inference.
92
+ > **Tip:** If words get cut off, lower **Speed** or increase **Ref Duration**.
93
+ """
94
+ )
95
+
96
+ with gr.Row():
97
+ with gr.Column():
98
+ input_text = gr.Textbox(
99
+ label="Text to Synthesize",
100
+ value="Hey, what's up? I'm feeling really great!",
101
+ )
102
+ input_audio = gr.Audio(
103
+ label="Reference Audio (.wav)",
104
+ type="filepath",
105
+ value="me.wav",
106
+ )
107
+
108
+ with gr.Row():
109
+ rms_val = gr.Number(value=0.01, label="RMS (Loudness)")
110
+ ref_duration_val = gr.Number(
111
+ value=5,
112
+ label="Reference Duration (sec)",
113
+ info="Lower = faster. Set ~1000 if you hear artifacts.",
114
+ )
115
+ t_shift_val = gr.Number(value=0.9, label="T-Shift")
116
+
117
+ with gr.Row():
118
+ steps_val = gr.Slider(1, 10, value=4, step=1, label="Num Steps")
119
+ speed_val = gr.Slider(
120
+ 0.5, 2.0, value=0.8, step=0.1,
121
+ label="Speed (Lower = Longer / Clearer)",
122
+ )
123
+ smooth_val = gr.Checkbox(label="Return Smooth", value=False)
124
+
125
+ btn = gr.Button("Generate Speech", variant="primary")
126
+
127
+ with gr.Column():
128
+ audio_out = gr.Audio(label="Result")
129
+ status_text = gr.Markdown("Ready to generate...")
130
+
131
+ btn.click(
132
+ fn=infer,
133
+ inputs=[
134
+ input_text, input_audio, rms_val, ref_duration_val,
135
+ t_shift_val, steps_val, speed_val, smooth_val,
136
+ ],
137
+ outputs=[audio_out, status_text],
138
+ api_name="predict",
139
+ )
140
+
141
+ app = FastAPI()
142
+
143
+ @app.get("/")
144
+ def read_index():
145
+ return FileResponse("index.html")
146
+
147
+ @app.get("/{filename}")
148
+ def read_static_file(filename: str):
149
+ # Serves any other flat file next to index.html/app.py in the repo -
150
+ # me.jpg, facts.json, etc. This only matches a single path segment
151
+ # (no slashes), so it won't intercept the /voice/... Gradio routes.
152
+ if os.path.isfile(filename):
153
+ return FileResponse(filename)
154
+ raise HTTPException(status_code=404, detail="Not found")
155
+
156
+
157
+ @app.post("/chat")
158
+ def chat(body: ChatBody):
159
+ # Proxies the Groq call server-side so GROQ_API_KEY never has to
160
+ # live in index.html or be visible via "View Page Source".
161
+ # Plain (non-async) def so FastAPI runs this blocking network call
162
+ # in a threadpool instead of freezing the whole server on it.
163
+ if not GROQ_API_KEY:
164
+ raise HTTPException(status_code=500, detail="GROQ_API_KEY not configured on server")
165
+
166
+ r = requests.post(
167
+ "https://api.groq.com/openai/v1/chat/completions",
168
+ headers={
169
+ "Content-Type": "application/json",
170
+ "Authorization": f"Bearer {GROQ_API_KEY}",
171
+ },
172
+ json={
173
+ "model": "llama-3.3-70b-versatile",
174
+ "max_tokens": 300,
175
+ "messages": body.messages,
176
+ },
177
+ timeout=30,
178
+ )
179
+ return JSONResponse(content=r.json(), status_code=r.status_code)
180
+
181
+
182
+ @app.post("/speak")
183
+ def speak_endpoint(body: SpeakBody):
184
+ # Triggers generation via the same internal Gradio call as before
185
+ # (this part isn't the change - it's proven reliable on its own).
186
+ # The difference: instead of ALSO fetching the finished file back
187
+ # over a second internal HTTP call and buffering it fully in
188
+ # memory, we serve it directly off disk with FileResponse, which
189
+ # streams the file in chunks rather than holding the whole thing
190
+ # in a Python bytes object. Same container/filesystem, so the path
191
+ # Gradio wrote to is already readable right here.
192
+ if not body.text:
193
+ raise HTTPException(status_code=400, detail="No text provided")
194
+
195
+ r = requests.post(
196
+ "http://127.0.0.1:7860/voice/api/predict",
197
+ json={"data": [body.text, None, 0.01, 5, 0.9, 4, 0.8, False]},
198
+ timeout=180,
199
+ )
200
+ r.raise_for_status()
201
+ file_path = r.json()["data"][0]["path"]
202
+
203
+ return FileResponse(file_path, media_type="audio/wav")
204
+
205
+ app = gr.mount_gradio_app(app, demo, path="/voice")
206
+
207
+ if __name__ == "__main__":
208
+ import uvicorn
209
+ uvicorn.run(app, host="0.0.0.0", port=7860)
index.html ADDED
@@ -0,0 +1,453 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>Chat with Me</title>
7
+ <link rel="preconnect" href="https://fonts.googleapis.com">
8
+ <link href="https://fonts.googleapis.com/css2?family=DM+Serif+Display:ital@0;1&family=DM+Sans:wght@300;400;500&display=swap" rel="stylesheet">
9
+ <style>
10
+ *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
11
+ :root {
12
+ --bg: #0d0d0f; --surface: #16161a; --border: rgba(255,255,255,0.08);
13
+ --accent: #c8a96e; --text: #e8e4dc; --muted: rgba(232,228,220,0.45);
14
+ --user-bg: #1e2533; --bot-bg: #1a1a1e; --radius: 18px;
15
+ --font-head: 'DM Serif Display', Georgia, serif;
16
+ --font-body: 'DM Sans', system-ui, sans-serif;
17
+ }
18
+ html, body { height: 100%; background: var(--bg); color: var(--text); font-family: var(--font-body); font-size: 16px; line-height: 1.6; }
19
+ .shell { display: grid; grid-template-columns: 320px 1fr; grid-template-rows: 100vh; max-width: 1100px; margin: 0 auto; }
20
+ .sidebar { border-right: 1px solid var(--border); display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 2.5rem 1.5rem; gap: 1.5rem; position: relative; overflow: hidden; }
21
+ .sidebar::before { content: ''; position: absolute; inset: 0; background: radial-gradient(ellipse at 50% 40%, rgba(200,169,110,0.07) 0%, transparent 70%); pointer-events: none; }
22
+
23
+ .avatar-wrap { position: relative; width: 220px; height: 290px; }
24
+
25
+ .avatar-ring { position: absolute; inset: -6px; border-radius: 16px; border: 1.5px solid var(--accent); opacity: 0.3; animation: pulse-ring 2.8s ease-in-out infinite; }
26
+ .avatar-ring.r2 { inset: -12px; opacity: 0.1; animation-delay: 0.6s; }
27
+ @keyframes pulse-ring { 0%, 100% { transform: scale(1); opacity: 0.3; } 50% { transform: scale(1.03); opacity: 0.1; } }
28
+
29
+ .avatar-img {
30
+ width: 220px;
31
+ height: 290px;
32
+ border-radius: 16px;
33
+ object-fit: cover;
34
+ object-position: center center;
35
+ border: 1.5px solid rgba(200, 169, 110, 0.4);
36
+ display: block;
37
+ background: var(--surface);
38
+ box-shadow: 0 8px 24px rgba(0, 0, 0, 0.5);
39
+ }
40
+
41
+ .speaking-badge { display: none; align-items: center; gap: 6px; font-size: 12px; color: var(--accent); letter-spacing: 0.08em; text-transform: uppercase; font-weight: 500; }
42
+ .speaking-badge.active { display: flex; }
43
+ .speaking-dots { display: flex; gap: 3px; }
44
+ .speaking-dots span { width: 4px; height: 4px; border-radius: 50%; background: var(--accent); animation: bounce-dot 1s ease-in-out infinite; }
45
+ .speaking-dots span:nth-child(2) { animation-delay: 0.15s; }
46
+ .speaking-dots span:nth-child(3) { animation-delay: 0.30s; }
47
+ @keyframes bounce-dot { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-5px); } }
48
+ .bot-name { font-family: var(--font-head); font-size: 1.6rem; color: var(--text); text-align: center; }
49
+ .bot-tagline { font-size: 13px; color: var(--muted); text-align: center; max-width: 220px; line-height: 1.5; }
50
+ .voice-toggle { display: flex; align-items: center; gap: 8px; font-size: 12px; color: var(--muted); cursor: pointer; user-select: none; padding: 6px 12px; border: 1px solid var(--border); border-radius: 99px; transition: border-color 0.2s, color 0.2s; }
51
+ .voice-toggle:hover { border-color: var(--accent); color: var(--accent); }
52
+ .voice-toggle input { display: none; }
53
+ .voice-toggle.on { border-color: var(--accent); color: var(--accent); }
54
+
55
+ .delay-note { font-size: 12px; color: var(--muted); text-align: center; max-width: 200px; line-height: 1.4; font-style: italic; margin-top: -4px; }
56
+
57
+ .chat-panel { display: flex; flex-direction: column; height: 100vh; }
58
+ .chat-header { padding: 1.25rem 1.75rem; border-bottom: 1px solid var(--border); font-size: 13px; color: var(--muted); letter-spacing: 0.06em; text-transform: uppercase; flex-shrink: 0; }
59
+ .messages { flex: 1; overflow-y: auto; padding: 1.75rem; display: flex; flex-direction: column; gap: 1rem; scroll-behavior: smooth; }
60
+ .messages::-webkit-scrollbar { width: 4px; }
61
+ .messages::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }
62
+ .msg { display: flex; flex-direction: column; max-width: 78%; gap: 4px; animation: msg-in 0.25s ease-out both; }
63
+ @keyframes msg-in { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: translateY(0); } }
64
+ .msg.user { align-self: flex-end; align-items: flex-end; }
65
+ .msg.bot { align-self: flex-start; align-items: flex-start; }
66
+ .bubble { padding: 0.75rem 1.1rem; border-radius: var(--radius); font-size: 15px; line-height: 1.65; }
67
+ .msg.user .bubble { background: var(--user-bg); border-bottom-right-radius: 4px; border: 1px solid rgba(126,159,199,0.2); }
68
+ .msg.bot .bubble { background: var(--bot-bg); border-bottom-left-radius: 4px; border: 1px solid var(--border); }
69
+ .msg-time { font-size: 11px; color: var(--muted); padding: 0 4px; }
70
+
71
+ .replay-btn { font-size: 11px; color: var(--accent); background: none; border: 1px solid rgba(200, 169, 110, 0.3); border-radius: 99px; padding: 3px 10px; cursor: pointer; margin-top: 6px; width: fit-content; transition: background 0.15s, border-color 0.15s; display: none; align-items: center; gap: 4px; }
72
+ .replay-btn.visible { display: flex; }
73
+ .replay-btn:hover { background: rgba(200, 169, 110, 0.1); border-color: var(--accent); }
74
+
75
+ .typing-bubble { background: var(--bot-bg); border: 1px solid var(--border); border-radius: var(--radius); border-bottom-left-radius: 4px; padding: 0.75rem 1.1rem; display: none; align-items: center; gap: 5px; width: fit-content; }
76
+ .typing-bubble.visible { display: flex; }
77
+ .typing-bubble span { width: 6px; height: 6px; border-radius: 50%; background: var(--muted); animation: bounce-dot 1s ease-in-out infinite; }
78
+ .typing-bubble span:nth-child(2) { animation-delay: 0.15s; }
79
+ .typing-bubble span:nth-child(3) { animation-delay: 0.30s; }
80
+ .gen-voice { font-size: 12px; color: var(--muted); padding: 4px 8px; display: none; }
81
+ .gen-voice.visible { display: block; }
82
+ .input-bar { padding: 1.25rem 1.75rem; border-top: 1px solid var(--border); display: flex; gap: 10px; align-items: flex-end; flex-shrink: 0; background: var(--bg); }
83
+ .input-wrap { flex: 1; background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 0.65rem 1rem; display: flex; align-items: flex-end; gap: 8px; transition: border-color 0.2s; }
84
+ .input-wrap:focus-within { border-color: rgba(200,169,110,0.4); }
85
+ #user-input { flex: 1; background: none; border: none; outline: none; color: var(--text); font-family: var(--font-body); font-size: 15px; resize: none; max-height: 120px; min-height: 24px; line-height: 1.5; }
86
+ #user-input::placeholder { color: var(--muted); }
87
+ .send-btn { width: 38px; height: 38px; border-radius: 10px; border: none; background: var(--accent); color: #0d0d0f; cursor: pointer; display: flex; align-items: center; justify-content: center; flex-shrink: 0; transition: opacity 0.15s, transform 0.1s; font-size: 18px; }
88
+ .send-btn:hover { opacity: 0.85; }
89
+ .send-btn:active { transform: scale(0.95); }
90
+ .send-btn:disabled { opacity: 0.35; cursor: not-allowed; }
91
+ @media (max-width: 660px) {
92
+ .shell { grid-template-columns: 1fr; grid-template-rows: auto 1fr; height: 100vh; }
93
+ .sidebar { flex-direction: row; border-right: none; border-bottom: 1px solid var(--border); padding: 1rem 1.25rem; gap: 1rem; justify-content: flex-start; }
94
+ .avatar-wrap { width: 56px; height: 56px; flex-shrink: 0; }
95
+ .avatar-img { width: 56px; height: 56px; border-radius: 8px; }
96
+ .avatar-ring, .avatar-ring.r2 { display: none; }
97
+ .bot-tagline, .voice-toggle, .speaking-badge, .delay-note { display: none !important; }
98
+ .bot-name { font-size: 1.1rem; }
99
+ .chat-panel { height: 0; min-height: 0; }
100
+ }
101
+ </style>
102
+ </head>
103
+ <body>
104
+ <div class="shell">
105
+ <aside class="sidebar">
106
+ <div class="avatar-wrap">
107
+ <div class="avatar-ring"></div>
108
+ <div class="avatar-ring r2"></div>
109
+ <img id="avatar-static" class="avatar-img" src="me.jpg"
110
+ onerror="this.style.background='#2a2a30';this.src=''" alt="Avatar">
111
+ </div>
112
+ <div class="speaking-badge" id="speaking-badge">
113
+ <div class="speaking-dots"><span></span><span></span><span></span></div>
114
+ Speaking
115
+ </div>
116
+ <div>
117
+ <h1 class="bot-name">Papa Jerry</h1>
118
+ <p class="bot-tagline">Ask me anything — I'm here to help.</p>
119
+ </div>
120
+ <label class="voice-toggle on" id="voice-toggle">
121
+ <input type="checkbox" id="voice-on" checked>
122
+ 🔊 Voice on
123
+ </label>
124
+ <div class="delay-note">
125
+ voice generation takes 15-45 seconds - please be patient
126
+ </div>
127
+ </aside>
128
+ <main class="chat-panel">
129
+ <div class="chat-header">Conversation</div>
130
+ <div class="messages" id="messages">
131
+ <div class="msg bot">
132
+ <div class="bubble">Hey there! 👋 I'm here and ready to chat. Ask me anything you'd like.</div>
133
+ <span class="msg-time" id="greeting-time"></span>
134
+ </div>
135
+ <div class="typing-bubble" id="typing">
136
+ <span></span><span></span><span></span>
137
+ </div>
138
+ <div class="gen-voice" id="gen-voice">🎙️ Generating voice...</div>
139
+ </div>
140
+ <div class="input-bar">
141
+ <div class="input-wrap">
142
+ <textarea id="user-input" rows="1" placeholder="Type a message…"></textarea>
143
+ </div>
144
+ <button class="send-btn" id="send-btn" title="Send">&#10148;</button>
145
+ </div>
146
+ </main>
147
+ </div>
148
+ <audio id="tts-audio" style="display:none"></audio>
149
+
150
+ <script>
151
+ // Silent debug logger - view via browser Console (F12), no on-page UI.
152
+ var debugStartTime = null;
153
+ function dlog(msg) {
154
+ if (debugStartTime === null) debugStartTime = performance.now();
155
+ var elapsed = ((performance.now() - debugStartTime) / 1000).toFixed(2);
156
+ console.log('[+' + elapsed + 's] ' + msg);
157
+ }
158
+ window.addEventListener('error', function(e) {
159
+ dlog('❌ JS ERROR: ' + e.message);
160
+ });
161
+ window.addEventListener('unhandledrejection', function(e) {
162
+ dlog('❌ UNHANDLED PROMISE REJECTION: ' + (e.reason && e.reason.message ? e.reason.message : e.reason));
163
+ });
164
+ </script>
165
+
166
+ <script>
167
+ // ============================================================
168
+ // CONFIGURE THESE
169
+ // ============================================================
170
+ var HF_SPACE_URL = 'https://zaktree-papa-jerry-v2.hf.space';
171
+ var BASE_SYSTEM_PROMPT = 'You are Papa Jerry, a warm and loving grandfather chatting with family. Speak with warmth, and gentle humor. Because your words are converted to spoken audio and family members are waiting to hear you, always keep replies short — 2 to 3 sentences — even when being thoughtful or wise.';
172
+ var SYSTEM_PROMPT = BASE_SYSTEM_PROMPT;
173
+ var POLL_INTERVAL = 4000;
174
+ // ============================================================
175
+
176
+ var chatHistory = [];
177
+ var busy = false;
178
+ var currentBotMsgId = null;
179
+
180
+ var msgsEl = document.getElementById('messages');
181
+ var inputEl = document.getElementById('user-input');
182
+ var sendBtn = document.getElementById('send-btn');
183
+ var typing = document.getElementById('typing');
184
+ var genVoiceEl = document.getElementById('gen-voice');
185
+ var voiceOn = document.getElementById('voice-on');
186
+ var voiceToggle = document.getElementById('voice-toggle');
187
+ var speakBadge = document.getElementById('speaking-badge');
188
+ var ttsAudio = document.getElementById('tts-audio');
189
+
190
+ document.getElementById('greeting-time').textContent = _time();
191
+
192
+ // Dynamic loader built to ingest 'family' and 'other' data fields safely
193
+ function loadFactsFromServer() {
194
+ fetch('facts.json')
195
+ .then(function(r) { return r.json(); })
196
+ .then(function(facts) {
197
+ var factsText = "\n\nHere are some facts you know about specific people:\n";
198
+ for (var person in facts) {
199
+ var location = facts[person].location || "unknown";
200
+ var job = facts[person].job || "unknown";
201
+ var hobbies = facts[person].hobbies || "unknown";
202
+
203
+ // Dynamic additions: Append context chunks string conditionally
204
+ var familyDetails = facts[person].family ? " Family connections: " + facts[person].family + "." : "";
205
+ var otherDetails = facts[person].other ? " Additional information: " + facts[person].other + "." : "";
206
+
207
+ factsText += "- " + person + " lives in " + location +
208
+ ", works as a " + job +
209
+ ", and their hobbies include " + hobbies + "." +
210
+ familyDetails + otherDetails + "\n";
211
+ }
212
+ SYSTEM_PROMPT = BASE_SYSTEM_PROMPT + factsText;
213
+ console.log("AI context initialized with multi-field profile definitions.");
214
+ })
215
+ .catch(function(err) {
216
+ console.log("No facts loaded or file empty, applying base prompt fallback.", err);
217
+ });
218
+ }
219
+
220
+ loadFactsFromServer();
221
+
222
+ voiceOn.addEventListener('change', function() {
223
+ voiceToggle.classList.toggle('on', voiceOn.checked);
224
+ voiceToggle.lastChild.textContent = voiceOn.checked ? ' Voice on' : ' Voice off';
225
+ });
226
+
227
+ inputEl.addEventListener('input', function() {
228
+ this.style.height = 'auto';
229
+ this.style.height = Math.min(this.scrollHeight, 120) + 'px';
230
+ });
231
+
232
+ inputEl.addEventListener('keydown', function(e) {
233
+ if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); send(); }
234
+ });
235
+
236
+ sendBtn.addEventListener('click', send);
237
+
238
+ function unlockAudio() {
239
+ dlog('unlockAudio() called (click gesture)');
240
+ try {
241
+ ttsAudio.muted = true;
242
+ var p = ttsAudio.play();
243
+ if (p && p.catch) p.catch(function(e) { dlog('unlock play() rejected: ' + e.message); });
244
+ ttsAudio.pause();
245
+ ttsAudio.currentTime = 0;
246
+ dlog('unlockAudio() completed OK');
247
+ } catch (e) { dlog('unlockAudio() threw: ' + e.message); }
248
+ }
249
+
250
+ function send() {
251
+ var text = inputEl.value.trim();
252
+ if (!text || busy) return;
253
+ debugStartTime = performance.now();
254
+ dlog('--- NEW MESSAGE: "' + text + '" ---');
255
+ if (voiceOn.checked) unlockAudio();
256
+ addMessage('user', text);
257
+ chatHistory.push({ role: 'user', content: text });
258
+ inputEl.value = '';
259
+ inputEl.style.height = 'auto';
260
+ setTyping(true);
261
+ busy = true;
262
+ sendBtn.disabled = true;
263
+ currentBotMsgId = 'msg-' + Date.now();
264
+
265
+ fetch('/chat', {
266
+ method: 'POST',
267
+ headers: {
268
+ 'Content-Type': 'application/json'
269
+ },
270
+ body: JSON.stringify({
271
+ messages: [{ role: 'system', content: SYSTEM_PROMPT }].concat(chatHistory)
272
+ })
273
+ })
274
+ .then(function(r) { return r.json(); })
275
+ .then(function(data) {
276
+ setTyping(false);
277
+ if (!data.choices || !data.choices[0]) {
278
+ addMessage('bot', 'Sorry, I could not get a response. Please try again.');
279
+ done(); return;
280
+ }
281
+ var reply = data.choices[0].message.content;
282
+ chatHistory.push({ role: 'assistant', content: reply });
283
+
284
+ addMessage('bot', reply, currentBotMsgId);
285
+
286
+ if (voiceOn.checked) {
287
+ speak(reply);
288
+ } else {
289
+ done();
290
+ }
291
+ })
292
+ .catch(function(err) {
293
+ setTyping(false);
294
+ addMessage('bot', 'Connection error: ' + err.message);
295
+ done();
296
+ });
297
+ }
298
+
299
+ var voiceStartTime = 0;
300
+ var UI_CounterInterval = null;
301
+
302
+ function startVoiceTimer() {
303
+ voiceStartTime = performance.now();
304
+ genVoiceEl.textContent = '🎙️ Generating voice... 0s';
305
+ genVoiceEl.classList.add('visible');
306
+ UI_CounterInterval = setInterval(function() {
307
+ var elapsed = Math.round((performance.now() - voiceStartTime) / 1000);
308
+ genVoiceEl.textContent = '🎙️ Generating voice... ' + elapsed + 's';
309
+ }, 1000);
310
+ }
311
+
312
+ function stopVoiceTimer() {
313
+ if (UI_CounterInterval) { clearInterval(UI_CounterInterval); UI_CounterInterval = null; }
314
+ genVoiceEl.classList.remove('visible');
315
+ }
316
+
317
+ function speak(text) {
318
+ startVoiceTimer();
319
+ dlog('speak(): sending POST /speak');
320
+ fetch('/speak', {
321
+ method: 'POST',
322
+ headers: { 'Content-Type': 'application/json' },
323
+ body: JSON.stringify({ text: text })
324
+ })
325
+ .then(function(r) {
326
+ dlog('speak response received, HTTP status ' + r.status);
327
+ if (!r.ok) throw new Error('HTTP ' + r.status);
328
+ return r.blob();
329
+ })
330
+ .then(function(blob) {
331
+ var accurateSeconds = ((performance.now() - voiceStartTime) / 1000).toFixed(1);
332
+ dlog('audio blob received after ' + accurateSeconds + 's, size ' + blob.size + ' bytes, type ' + blob.type);
333
+ stopVoiceTimer();
334
+ injectVoiceDuration(currentBotMsgId, accurateSeconds);
335
+
336
+ var typedBlob = blob.type === 'audio/wav' ? blob : new Blob([blob], { type: 'audio/wav' });
337
+ var finalUrl = URL.createObjectURL(typedBlob);
338
+ setupReplayButton(currentBotMsgId, finalUrl);
339
+ playAudio(finalUrl);
340
+ })
341
+ .catch(function(e) { dlog('❌ /speak failed: ' + e.message); stopVoiceTimer(); done(); });
342
+ }
343
+
344
+ function playAudio(src) {
345
+ dlog('playAudio() called, src=' + src.slice(0, 80));
346
+ if (busy && !ttsAudio.paused) {
347
+ ttsAudio.pause();
348
+ }
349
+ speakBadge.classList.add('active');
350
+ ttsAudio.muted = false;
351
+ ttsAudio.src = src;
352
+
353
+ ttsAudio.oncanplay = function() { dlog('audio event: canplay'); };
354
+ ttsAudio.onwaiting = function() { dlog('audio event: waiting (buffering)'); };
355
+ ttsAudio.onstalled = function() { dlog('audio event: stalled'); };
356
+ ttsAudio.onplaying = function() { dlog('🔊 audio event: PLAYING (actually audible now)'); };
357
+
358
+ var playPromise = ttsAudio.play();
359
+ if (playPromise && playPromise.then) {
360
+ playPromise.then(function() {
361
+ dlog('play() promise RESOLVED (browser allowed playback)');
362
+ }).catch(function(e) {
363
+ dlog('❌ play() promise REJECTED: ' + e.name + ' - ' + e.message);
364
+ });
365
+ }
366
+
367
+ ttsAudio.onended = function() { dlog('audio event: ended'); speakBadge.classList.remove('active'); done(); };
368
+ ttsAudio.onerror = function() { dlog('❌ audio event: error - ' + (ttsAudio.error ? ttsAudio.error.message : 'unknown')); speakBadge.classList.remove('active'); done(); };
369
+ }
370
+
371
+ function done() {
372
+ busy = false;
373
+ sendBtn.disabled = false;
374
+ inputEl.focus();
375
+ }
376
+
377
+ function addMessage(role, text, elementId) {
378
+ var msg = document.createElement('div');
379
+ msg.className = 'msg ' + role;
380
+ if (elementId) msg.id = elementId;
381
+
382
+ var bubble = document.createElement('div');
383
+ bubble.className = 'bubble';
384
+ bubble.textContent = text;
385
+ var time = document.createElement('span');
386
+ time.className = 'msg-time';
387
+ time.textContent = _time();
388
+ msg.appendChild(bubble);
389
+ msg.appendChild(time);
390
+
391
+ if (role === 'bot') {
392
+ var replayBtn = document.createElement('button');
393
+ replayBtn.className = 'replay-btn';
394
+ replayBtn.innerHTML = '▶️ Replay Voice';
395
+ msg.appendChild(replayBtn);
396
+ }
397
+
398
+ msgsEl.insertBefore(msg, typing);
399
+ msgsEl.scrollTop = msgsEl.scrollHeight;
400
+ }
401
+
402
+ function injectVoiceDuration(elementId, seconds) {
403
+ var targetMsg = document.getElementById(elementId);
404
+ if (!targetMsg) return;
405
+
406
+ var durationEl = document.createElement('div');
407
+ durationEl.style.fontSize = '11px';
408
+ durationEl.style.color = 'var(--muted)';
409
+ durationEl.style.marginTop = '4px';
410
+ durationEl.style.padding = '0 4px';
411
+ durationEl.textContent = '⏱️ Generated in ' + seconds + 's';
412
+
413
+ var replayBtn = targetMsg.querySelector('.replay-btn');
414
+ if (replayBtn) {
415
+ targetMsg.insertBefore(durationEl, replayBtn);
416
+ } else {
417
+ targetMsg.appendChild(durationEl);
418
+ }
419
+ msgsEl.scrollTop = msgsEl.scrollHeight;
420
+ }
421
+
422
+ function setupReplayButton(elementId, audioSrc) {
423
+ var targetMsg = document.getElementById(elementId);
424
+ if (!targetMsg) return;
425
+
426
+ var btn = targetMsg.querySelector('.replay-btn');
427
+ if (!btn) return;
428
+
429
+ btn.onclick = function() {
430
+ speakBadge.classList.add('active');
431
+ ttsAudio.src = audioSrc;
432
+ ttsAudio.play();
433
+ ttsAudio.onended = function() { speakBadge.classList.remove('active'); };
434
+ ttsAudio.onerror = function() { speakBadge.classList.remove('active'); };
435
+ };
436
+ btn.classList.add('visible');
437
+ }
438
+
439
+ function setTyping(show) {
440
+ typing.classList.toggle('visible', show);
441
+ msgsEl.scrollTop = msgsEl.scrollHeight;
442
+ }
443
+
444
+ // Global UI clock time builder string
445
+ function _time() {
446
+ var d = new Date(), h = d.getHours(), m = d.getMinutes();
447
+ var ampm = h >= 12 ? 'pm' : 'am';
448
+ h = h % 12 || 12;
449
+ return h + ':' + (m < 10 ? '0' : '') + m + ' ' + ampm;
450
+ }
451
+ </script>
452
+ </body>
453
+ </html>